Commit 80ba4a9b by Paktalin

Added state behavior

parent 148396ff
......@@ -12,8 +12,8 @@ import androidx.appcompat.widget.SearchView
import androidx.recyclerview.selection.Selection
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.ui.fragments.SubmitAddedFragment
import com.paktalin.vocabularynotebook.ui.fragments.SubmitFragment
import com.paktalin.vocabularynotebook.ui.fragments.TagFragment
import com.paktalin.vocabularynotebook.ui.recycler_view.State
import com.paktalin.vocabularynotebook.ui.recycler_view.VocabularyAdapter
import com.paktalin.vocabularynotebook.ui.recycler_view.selection_tracker.MySelectionTracker
import com.paktalin.vocabularynotebook.ui.views.LockableLayoutManager
......@@ -135,12 +135,12 @@ class MainActivity : AppCompatActivity() {
}
private fun whenSelectionChanges(selection: Selection<Long>) {
if (!selection.isEmpty && SubmitFragment.zeroInstances()) {
if (!selection.isEmpty && vocabularyAdapter.state.notAddOrEdit()) {
vocabularyAdapter.state.set(State.TAG)
tagFragment.setSelection(selection.toList())
addFragment(supportFragmentManager, tagFragment, R.id.main_activity_container)
} else if (selection.isEmpty) {
vocabularyAdapter.editAllowed = true
} else {
vocabularyAdapter.state.set(State.NONE)
removeFragment(supportFragmentManager, tagFragment)
}
}
......
package com.paktalin.vocabularynotebook.ui.fragments
import com.paktalin.vocabularynotebook.firestoreitems.WordPojo
import com.paktalin.vocabularynotebook.ui.recycler_view.State
import com.paktalin.vocabularynotebook.utils.removeFragment
import kotlinx.android.synthetic.main.word_item.*
class SubmitAddedFragment : SubmitFragment() {
override fun init() {
mainActivity.vocabularyAdapter.editAllowed = false
mainActivity.vocabularyAdapter.state.set(State.ADD)
wordEt = mainActivity.word
translationEt = mainActivity.translation
}
......@@ -27,7 +28,7 @@ class SubmitAddedFragment : SubmitFragment() {
override fun finish() {
wordEt.text.clear()
translationEt.text.clear()
mainActivity.vocabularyAdapter.editAllowed = true
mainActivity.vocabularyAdapter.state.set(State.NONE)
removeFragment(mainActivity.supportFragmentManager, this)
return
}
......
......@@ -3,6 +3,7 @@ package com.paktalin.vocabularynotebook.ui.fragments
import android.view.View
import android.widget.EditText
import com.paktalin.vocabularynotebook.firestoreitems.WordPojo
import com.paktalin.vocabularynotebook.ui.recycler_view.State
import com.paktalin.vocabularynotebook.ui.views.LockableLayoutManager
import com.paktalin.vocabularynotebook.utils.gone
import com.paktalin.vocabularynotebook.utils.removeFragment
......@@ -21,7 +22,7 @@ class SubmitEditedFragment : SubmitFragment() {
scrollToPositionWithOffset(recyclerViewPosition, 2)
isScrollEnabled = false
}
mainActivity.vocabularyAdapter.editAllowed = false
mainActivity.vocabularyAdapter.state.set(State.EDIT)
gone(clickableView)
wordEt.requestFocus()
wordEt.setSelection(wordEt.text.length)
......@@ -46,7 +47,7 @@ class SubmitEditedFragment : SubmitFragment() {
translationEt.clearFocus()
visible(clickableView)
mainActivity.hideKeyboardNotFromActivity(mainActivity)
mainActivity.vocabularyAdapter.editAllowed = true
mainActivity.vocabularyAdapter.state.set(State.NONE)
(mainActivity.recyclerView.layoutManager as LockableLayoutManager).isScrollEnabled = true
removeFragment(mainActivity.supportFragmentManager, this)
return
......
package com.paktalin.vocabularynotebook.ui.recycler_view
import com.paktalin.vocabularynotebook.ui.activities.MainActivity
import com.paktalin.vocabularynotebook.utils.gone
import com.paktalin.vocabularynotebook.utils.visible
import kotlinx.android.synthetic.main.word_item.*
enum class State {
EDIT,
ADD,
TAG,
NOTHING
EDIT, ADD, TAG, NONE;
var mainActivity: MainActivity? = null
fun setActivity(mainActivity: MainActivity) {
this.mainActivity = mainActivity
set(this)
}
fun set(state: State) {
when (state) {
EDIT -> mainActivity?.clickable_view?.let { visible(it) }
TAG -> mainActivity?.clickable_view?.let { visible(it) }
NONE -> mainActivity?.clickable_view?.let { gone(it) }
ADD -> {
}
}
}
fun notAddOrEdit(): Boolean {
return this != ADD && this != EDIT
}
}
\ No newline at end of file
......@@ -13,27 +13,19 @@ import com.paktalin.vocabularynotebook.ui.fragments.SubmitEditedFragment
import com.paktalin.vocabularynotebook.ui.recycler_view.selection_tracker.MySelectionTracker
import com.paktalin.vocabularynotebook.utils.Log
import com.paktalin.vocabularynotebook.utils.addSubmitFragment
import com.paktalin.vocabularynotebook.utils.gone
import com.paktalin.vocabularynotebook.utils.visible
import com.paktalin.vocabularynotebook.vocabulary.Sort
import com.paktalin.vocabularynotebook.vocabulary.VocabSet
import kotlinx.android.synthetic.main.word_item.*
import kotlinx.android.synthetic.main.word_item.view.*
class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivity: MainActivity) : RecyclerView.Adapter<ViewHolder>() {
private lateinit var recyclerView: RecyclerView
lateinit var selectionTracker: MySelectionTracker
var editAllowed = true
set(value) {
if (!value) visible(mainActivity.clickable_view)
else gone(mainActivity.clickable_view)
Log.d(TAG, "editAllowed $editAllowed")
field = value
}
var state = State.NONE
init {
setHasStableIds(true)
state.setActivity(mainActivity)
}
override fun getItemId(position: Int): Long = position.toLong()
......@@ -63,12 +55,13 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
viewHolder.bind(
vocabulary.displayedAt(position),
position,
selectionTracker.selected(position.toLong())) { view, p -> showPopupMenu(view, p) }
(selectionTracker.selected(position.toLong())) && state.notAddOrEdit())
{ view, p -> showPopupMenu(view, p) }
// TODO not allow to edit when tag is being selected
}
private fun showPopupMenu(v: View, position: Int) {
if (editAllowed) {
if (state == State.NONE) {
val popup = PopupMenu(mainActivity, v)
popup.menuInflater.inflate(R.menu.word_item_menu, popup.menu)
popup.setOnMenuItemClickListener {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment