Commit f0081656 by Paktalin

Made vocabularyAdapter nullable

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