Commit 742d65b0 by Paktalin

Moved state change logic to vocabularyAdapter

parent 80ba4a9b
......@@ -136,11 +136,11 @@ class MainActivity : AppCompatActivity() {
private fun whenSelectionChanges(selection: Selection<Long>) {
if (!selection.isEmpty && vocabularyAdapter.state.notAddOrEdit()) {
vocabularyAdapter.state.set(State.TAG)
vocabularyAdapter.state = State.TAG
tagFragment.setSelection(selection.toList())
addFragment(supportFragmentManager, tagFragment, R.id.main_activity_container)
} else {
vocabularyAdapter.state.set(State.NONE)
vocabularyAdapter.state = State.NONE
removeFragment(supportFragmentManager, tagFragment)
}
}
......
......@@ -8,7 +8,7 @@ import kotlinx.android.synthetic.main.word_item.*
class SubmitAddedFragment : SubmitFragment() {
override fun init() {
mainActivity.vocabularyAdapter.state.set(State.ADD)
mainActivity.vocabularyAdapter.state = State.ADD
wordEt = mainActivity.word
translationEt = mainActivity.translation
}
......@@ -28,7 +28,7 @@ class SubmitAddedFragment : SubmitFragment() {
override fun finish() {
wordEt.text.clear()
translationEt.text.clear()
mainActivity.vocabularyAdapter.state.set(State.NONE)
mainActivity.vocabularyAdapter.state = State.NONE
removeFragment(mainActivity.supportFragmentManager, this)
return
}
......
......@@ -22,7 +22,7 @@ class SubmitEditedFragment : SubmitFragment() {
scrollToPositionWithOffset(recyclerViewPosition, 2)
isScrollEnabled = false
}
mainActivity.vocabularyAdapter.state.set(State.EDIT)
mainActivity.vocabularyAdapter.state = State.EDIT
gone(clickableView)
wordEt.requestFocus()
wordEt.setSelection(wordEt.text.length)
......@@ -47,7 +47,7 @@ class SubmitEditedFragment : SubmitFragment() {
translationEt.clearFocus()
visible(clickableView)
mainActivity.hideKeyboardNotFromActivity(mainActivity)
mainActivity.vocabularyAdapter.state.set(State.NONE)
mainActivity.vocabularyAdapter.state = 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, 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
}
......
......@@ -13,8 +13,11 @@ 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>() {
......@@ -22,20 +25,28 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
private lateinit var recyclerView: RecyclerView
lateinit var selectionTracker: MySelectionTracker
var state = State.NONE
init {
setHasStableIds(true)
state.setActivity(mainActivity)
set(state) {
when (state) {
State.EDIT -> mainActivity.clickable_view?.let { visible(it) }
State.TAG -> mainActivity.clickable_view?.let { visible(it) }
State.NONE -> mainActivity.clickable_view?.let { gone(it) }
State.ADD -> {
}
}
field = state
}
override fun getItemId(position: Int): Long = position.toLong()
var sort: Sort = Sort.BY_TIME
set(value) {
field = value
sort()
}
init {
setHasStableIds(true)
}
override fun getItemId(position: Int): Long = position.toLong()
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView)
this.recyclerView = recyclerView
......
package com.paktalin.vocabularynotebook.vocabulary
enum class Sort {
BY_TIME, BY_WORD, BY_TRANSLATION
BY_TIME, BY_WORD, BY_TRANSLATION;
}
\ No newline at end of file
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