Commit f0081656 by Paktalin

Made vocabularyAdapter nullable

parent 742d65b0
...@@ -35,7 +35,7 @@ ...@@ -35,7 +35,7 @@
</value> </value>
</option> </option>
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" /> <output url="file://$PROJECT_DIR$/build/classes" />
</component> </component>
<component name="ProjectType"> <component name="ProjectType">
......
...@@ -27,7 +27,7 @@ import kotlinx.android.synthetic.main.word_item.* ...@@ -27,7 +27,7 @@ import kotlinx.android.synthetic.main.word_item.*
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
var searchView: SearchView? = null var searchView: SearchView? = null
lateinit var vocabularyAdapter: VocabularyAdapter var vocabularyAdapter: VocabularyAdapter? = null
private var tagFragment = TagFragment() private var tagFragment = TagFragment()
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
...@@ -52,7 +52,7 @@ class MainActivity : AppCompatActivity() { ...@@ -52,7 +52,7 @@ class MainActivity : AppCompatActivity() {
} }
override fun onOptionsItemSelected(item: MenuItem?): Boolean { override fun onOptionsItemSelected(item: MenuItem?): Boolean {
vocabularyAdapter.sort = when (item!!.itemId) { vocabularyAdapter?.sort = when (item!!.itemId) {
R.id.sortByWord -> Sort.BY_WORD R.id.sortByWord -> Sort.BY_WORD
R.id.sortByTranslation -> Sort.BY_TRANSLATION R.id.sortByTranslation -> Sort.BY_TRANSLATION
else -> Sort.BY_TIME else -> Sort.BY_TIME
...@@ -66,7 +66,7 @@ class MainActivity : AppCompatActivity() { ...@@ -66,7 +66,7 @@ class MainActivity : AppCompatActivity() {
} }
private fun refreshVocabulary() { private fun refreshVocabulary() {
vocabularyAdapter.refresh() vocabularyAdapter?.refresh()
swipeRefresh.isRefreshing = false swipeRefresh.isRefreshing = false
} }
...@@ -128,20 +128,22 @@ class MainActivity : AppCompatActivity() { ...@@ -128,20 +128,22 @@ class MainActivity : AppCompatActivity() {
} }
private fun setUpSelectionTracker() { private fun setUpSelectionTracker() {
vocabularyAdapter.selectionTracker = MySelectionTracker().apply { vocabularyAdapter?.selectionTracker = MySelectionTracker().apply {
init(recyclerView) init(recyclerView)
addObserver { selection -> whenSelectionChanges(selection) } addObserver { selection -> whenSelectionChanges(selection) }
} }
} }
private fun whenSelectionChanges(selection: Selection<Long>) { private fun whenSelectionChanges(selection: Selection<Long>) {
if (!selection.isEmpty && vocabularyAdapter.state.notAddOrEdit()) { vocabularyAdapter?.let {
vocabularyAdapter.state = State.TAG if (!selection.isEmpty && it.state.notAddOrEdit()) {
tagFragment.setSelection(selection.toList()) vocabularyAdapter?.state = State.TAG
addFragment(supportFragmentManager, tagFragment, R.id.main_activity_container) tagFragment.setSelection(selection.toList())
} else { addFragment(supportFragmentManager, tagFragment, R.id.main_activity_container)
vocabularyAdapter.state = State.NONE } else {
removeFragment(supportFragmentManager, tagFragment) vocabularyAdapter?.state = State.NONE
removeFragment(supportFragmentManager, tagFragment)
}
} }
} }
...@@ -149,7 +151,7 @@ class MainActivity : AppCompatActivity() { ...@@ -149,7 +151,7 @@ class MainActivity : AppCompatActivity() {
super.onPause() super.onPause()
hideKeyboard() hideKeyboard()
try { try {
FirestoreManager().saveWords(vocabularyAdapter.getModifiedWords()) vocabularyAdapter?.getModifiedWords()?.let { FirestoreManager().saveWords(it) }
} catch (ignored: Exception) { } catch (ignored: Exception) {
} }
} }
......
...@@ -8,13 +8,13 @@ import kotlinx.android.synthetic.main.word_item.* ...@@ -8,13 +8,13 @@ import kotlinx.android.synthetic.main.word_item.*
class SubmitAddedFragment : SubmitFragment() { class SubmitAddedFragment : SubmitFragment() {
override fun init() { override fun init() {
mainActivity.vocabularyAdapter.state = State.ADD mainActivity.vocabularyAdapter?.state = State.ADD
wordEt = mainActivity.word wordEt = mainActivity.word
translationEt = mainActivity.translation translationEt = mainActivity.translation
} }
override fun submit() { override fun submit() {
mainActivity.vocabularyAdapter.addWord(WordPojo(wordEt.text.toString(), translationEt.text.toString())) mainActivity.vocabularyAdapter?.addWord(WordPojo(wordEt.text.toString(), translationEt.text.toString()))
wordEt.requestFocus() wordEt.requestFocus()
finish() finish()
return return
...@@ -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 = 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 = 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)
...@@ -32,7 +32,7 @@ class SubmitEditedFragment : SubmitFragment() { ...@@ -32,7 +32,7 @@ class SubmitEditedFragment : SubmitFragment() {
override fun submit() { override fun submit() {
wordPojo.word = wordEt.text.toString() wordPojo.word = wordEt.text.toString()
wordPojo.translation = translationEt.text.toString() wordPojo.translation = translationEt.text.toString()
mainActivity.vocabularyAdapter.updateWord(wordPojo) mainActivity.vocabularyAdapter?.updateWord(wordPojo)
finish() finish()
} }
...@@ -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 = 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
......
...@@ -36,12 +36,12 @@ class TagFragment : Fragment() { ...@@ -36,12 +36,12 @@ class TagFragment : Fragment() {
if (it.isBlank()) if (it.isBlank())
shortToast(mainActivity, mainActivity.getString(R.string.toast_tag_cannot_be_empty)) shortToast(mainActivity, mainActivity.getString(R.string.toast_tag_cannot_be_empty))
else else
mainActivity.vocabularyAdapter.addTagToSelected(it) mainActivity.vocabularyAdapter?.addTagToSelected(it)
} }
} }
private fun cancel() { private fun cancel() {
mainActivity.vocabularyAdapter.selectionTracker.clearSelection() mainActivity.vocabularyAdapter?.selectionTracker?.clearSelection()
removeFragment(mainActivity.supportFragmentManager, this) removeFragment(mainActivity.supportFragmentManager, this)
} }
......
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