Commit d6a372f9 by Paktalin

Removed vocabularyFragment

parent 781ae10e
package com.paktalin.vocabularynotebook.ui.activities
import android.app.Activity
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import android.app.Activity
import android.support.v7.widget.SearchView
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.inputmethod.InputMethodManager
import kotlinx.android.synthetic.main.fragment_vocabulary.*
import android.support.v7.widget.SearchView
import android.view.WindowManager
import com.paktalin.vocabularynotebook.*
import android.view.inputmethod.InputMethodManager
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.Vocabulary
import com.paktalin.vocabularynotebook.Vocabulary.Companion.SORT_BY_TIME
import com.paktalin.vocabularynotebook.Vocabulary.Companion.SORT_BY_TRANSLATION
import com.paktalin.vocabularynotebook.Vocabulary.Companion.SORT_BY_WORD
import com.paktalin.vocabularynotebook.ui.fragments.VocabularyFragment
import com.paktalin.vocabularynotebook.VocabularyAdapter
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.ui.views.LockableLayoutManager
import com.paktalin.vocabularynotebook.utils.*
import kotlinx.android.synthetic.main.content_main.swipeRefresh
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.content_main.*
class MainActivity : AppCompatActivity() {
lateinit var vocabularyFragment: VocabularyFragment
lateinit var searchView: SearchView
lateinit var vocabularyAdapter: VocabularyAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
......@@ -39,12 +40,11 @@ class MainActivity : AppCompatActivity() {
searchView = menu!!.findItem(R.id.search).actionView as SearchView
// extract vocabulary data only after searchView is initialized,
// since it needs to be called in the VocabularyFragment initialization
setUpVocabularyFragment()
setUpVocabularyAdapter()
return true
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
val vocabularyAdapter = recyclerView.adapter as VocabularyAdapter
if (item!!.itemId == R.id.sortByTime)
vocabularyAdapter.sortOrder = SORT_BY_TIME
if (item.itemId == R.id.sortByWord)
......@@ -61,7 +61,7 @@ class MainActivity : AppCompatActivity() {
}
private fun refreshVocabulary() {
(recyclerView.adapter as VocabularyAdapter).refresh()
vocabularyAdapter.refresh()
swipeRefresh.isRefreshing = false
}
......@@ -81,13 +81,20 @@ class MainActivity : AppCompatActivity() {
swipeRefresh.setColorSchemeResources(R.color.colorAccent)
}
private fun setUpVocabularyFragment() {
private fun setUpVocabularyAdapter() {
addProgressBar()
FirestoreManager().extractVocabularyId(
{ vocabularyFragment = VocabularyFragment()
val arguments = Bundle()
arguments.putString("vocabularyId", FirestoreManager.vocabularyId!!)
addFragment(supportFragmentManager, vocabularyFragment, R.id.swipeRefresh, arguments)
FirestoreManager().extractVocabularyId({
// recyclerView.adapter = VocabularyAdapter(Vocabulary(), this@MainActivity)
recyclerView.layoutManager = LockableLayoutManager(this@MainActivity)
FirestoreManager().extractVocabulary { documents ->
run {
if (documents.isNotEmpty()) {
vocabularyAdapter = VocabularyAdapter(Vocabulary(documents), this@MainActivity)
recyclerView.adapter = vocabularyAdapter
}
else showToastNoWords()
}
}
},
{ showToastNoWords() },
{ removeProgressBar() }, this)
......@@ -114,10 +121,18 @@ class MainActivity : AppCompatActivity() {
com.paktalin.vocabularynotebook.utils.removeProgressBar(supportFragmentManager)
}
fun showToastNoWords() {
private fun showToastNoWords() {
shortToast(this, getString(R.string.toast_empty_vocabulary))
}
fun addWord(newWord: WordItem) {
vocabularyAdapter.addWord(newWord)
}
fun updateWord(updatedWord: WordItem) {
vocabularyAdapter.updateWord(updatedWord)
}
override fun onPause() {
super.onPause()
// TODO save changes
......
......@@ -46,7 +46,7 @@ class AddWordFragment : WordFragment() {
}
override fun updateRecycleView(wordItem: WordItem) {
mainActivity.vocabularyFragment.addWord(wordItem)
mainActivity.addWord(wordItem)
}
private fun textWatcher(setEmpty: () -> Unit): TextWatcher {
......
......@@ -2,19 +2,18 @@ package com.paktalin.vocabularynotebook.ui.fragments
import android.content.Context
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import com.paktalin.vocabularynotebook.*
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.ui.activities.MainActivity
import com.paktalin.vocabularynotebook.ui.views.LockableLayoutManager
import com.paktalin.vocabularynotebook.utils.*
import kotlinx.android.synthetic.main.fragment_editable_word.*
import com.paktalin.vocabularynotebook.utils.FirestoreManager
import com.paktalin.vocabularynotebook.utils.gone
import com.paktalin.vocabularynotebook.utils.removeFragment
import com.paktalin.vocabularynotebook.utils.visible
import kotlinx.android.synthetic.main.content_main.*
import kotlinx.android.synthetic.main.fragment_vocabulary.*
import kotlinx.android.synthetic.main.fragment_editable_word.*
import kotlinx.android.synthetic.main.word_item.view.*
class EditWordFragment : WordFragment() {
......@@ -43,7 +42,7 @@ class EditWordFragment : WordFragment() {
}
override fun updateRecycleView(wordItem: WordItem) {
mainActivity.vocabularyFragment.updateWord(wordItem)
mainActivity.updateWord(wordItem)
}
private fun setWordItemData() {
......
package com.paktalin.vocabularynotebook.ui.fragments
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.firebase.firestore.DocumentSnapshot
import com.paktalin.vocabularynotebook.*
import com.paktalin.vocabularynotebook.Vocabulary
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.ui.activities.MainActivity
import com.paktalin.vocabularynotebook.ui.views.LockableLayoutManager
import com.paktalin.vocabularynotebook.utils.FirestoreManager
import kotlinx.android.synthetic.main.fragment_vocabulary.*
class VocabularyFragment : Fragment() {
companion object {
private val TAG = "VN/" + VocabularyFragment::class.simpleName
}
private lateinit var mainActivity: MainActivity
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_vocabulary, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
mainActivity = activity as MainActivity
setEmptyVocabularyAdapter()
extractVocabularyById()
}
private fun extractVocabularyById() {
FirestoreManager().extractVocabulary { documents ->
run {
if (documents.isNotEmpty()) setVocabularyAdapter(documents)
else mainActivity.showToastNoWords()
}
}
}
private fun setEmptyVocabularyAdapter() {
recyclerView.adapter = VocabularyAdapter(Vocabulary(), mainActivity)
recyclerView.layoutManager = LockableLayoutManager(mainActivity)
}
private fun setVocabularyAdapter(documents: MutableList<DocumentSnapshot>) {
recyclerView.adapter = VocabularyAdapter(Vocabulary(documents), mainActivity)
}
fun addWord(newWord: WordItem) { (recyclerView.adapter as VocabularyAdapter).addWord(newWord) }
fun updateWord(updatedWord: WordItem) {
(recyclerView.adapter as VocabularyAdapter).updateWord(updatedWord) }
}
\ No newline at end of file
......@@ -2,7 +2,6 @@ package com.paktalin.vocabularynotebook.ui.views
import android.content.Context
import android.support.v7.widget.LinearLayoutManager
import com.paktalin.vocabularynotebook.ui.fragments.VocabularyFragment
class LockableLayoutManager(context: Context) : LinearLayoutManager(context) {
private var isScrollEnabled = true
......
......@@ -13,7 +13,7 @@ import org.apache.commons.lang3.StringUtils
val progressFragment: Fragment = ProgressFragment()
fun addFragment(fragmentManager: FragmentManager, fragment: Fragment, containerId: Int, arguments: Bundle?, tag: String? = null) {
fun addFragment(fragmentManager: FragmentManager, fragment: Fragment, containerId: Int, arguments: Bundle? = null, tag: String? = null) {
fragment.arguments = arguments
// remove progressFragment if it exists
if(fragmentManager.findFragmentById(fragment.id) != null)
......
......@@ -32,7 +32,24 @@
android:layout_marginEnd="@dimen/small_margin"
android:layout_marginLeft="@dimen/small_margin"
android:layout_marginRight="@dimen/small_margin"
android:layout_marginStart="@dimen/small_margin" />
android:layout_marginStart="@dimen/small_margin" >
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
<LinearLayout
......
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.paktalin.vocabularynotebook.ui.fragments.VocabularyFragment">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
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