Commit 742d65b0 by Paktalin

Moved state change logic to vocabularyAdapter

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