Commit 781ae10e by Paktalin

Removed inEditMode property + refactoring

parent 307bfc47
......@@ -8,16 +8,17 @@ import com.paktalin.vocabularynotebook.utils.FirestoreManager.Companion.vocabula
class Vocabulary() {
private var words: MutableList<WordItem> = mutableListOf()
/* constructor(words: MutableList<WordItem>) : this() {
this.words = words
}*/
constructor(vocabulary: Vocabulary) : this() {
copyWordsFrom(vocabulary)
}
constructor(documents: MutableList<DocumentSnapshot>) : this() {
for (ref in documents) {
val word = ref["word"].toString()
val translation = ref["translation"].toString()
val time = ref["time"] as Timestamp
words.add(WordItem(word, translation, time.toDate(), ref.id, vocabularyId!!))
documents.forEach { ref ->
words.add(WordItem(
ref["word"].toString(),
ref["translation"].toString(),
(ref["time"] as Timestamp).toDate(),
ref.id, vocabularyId!!))
}
}
......@@ -46,24 +47,12 @@ class Vocabulary() {
words.add(0, newWord)
}
fun addWords(newWords:MutableList<WordItem>) {
words.addAll(newWords)
fun copyWordsFrom(vocabulary: Vocabulary) {
words.addAll(vocabulary.get())
}
fun addWordsAsDocuments(documents: MutableList<DocumentSnapshot>) {
for (ref in documents) {
val word = ref["word"].toString()
val translation = ref["translation"].toString()
val time = ref["time"] as Timestamp
words.add(WordItem(word, translation, time.toDate(), ref.id, vocabularyId!!))
}
}
fun addWordsFittingQuery(newWords:MutableList<WordItem>, query:String) {
for (newWord in newWords) {
if (newWord.contains(query))
this.addWord(newWord)
}
fun addWordsFittingQuery(vocabulary: Vocabulary, query:String) {
vocabulary.get().filter { wordItem -> wordItem.contains(query) }.toCollection(words)
}
fun updateWord(updatedWord: WordItem) {
......
......@@ -14,9 +14,9 @@ import com.paktalin.vocabularynotebook.ui.activities.MainActivity
import com.paktalin.vocabularynotebook.utils.addFragment
import kotlinx.android.synthetic.main.word_item.view.*
class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val mainActivity: MainActivity) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
class VocabularyAdapter(private val fullVocabulary: Vocabulary, private val mainActivity: MainActivity) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
private var fullVocabulary: MutableList<WordItem> = mutableListOf() // stores all the words loaded from the db
private var displayedVocabulary = Vocabulary(fullVocabulary)
private lateinit var recyclerView: RecyclerView
var sortOrder: Int = 0
......@@ -24,10 +24,6 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val
field = value; sort()
}
init {
fullVocabulary.addAll(displayedVocabulary.get())
}
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView)
this.recyclerView = recyclerView
......@@ -58,10 +54,11 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val
inflater.inflate(R.menu.word_item_menu, popup.menu)
popup.setOnMenuItemClickListener {
if (it.itemId == R.id.option_delete) { deleteWord(position) }
if (it.itemId == R.id.option_edit) { editWord(v, displayedVocabulary.getAt(position)) }
if (it.itemId == R.id.option_edit) { startEditFragment(v, displayedVocabulary.getAt(position)) }
true
}
if (!mainActivity.inEditMode) popup.show()
if (mainActivity.supportFragmentManager.findFragmentByTag("edit_fragment") == null)
popup.show()
}
private fun deleteWord(position: Int) {
......@@ -96,18 +93,16 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val
}
@SuppressLint("ResourceType")
private fun editWord(container: View, wordItem: WordItem) {
if (!mainActivity.inEditMode) {
//set container id
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
container.id = View.generateViewId()
} else container.id = 18071999
// start EditWordFragment
val arguments = Bundle()
arguments.putSerializable("wordItem", wordItem)
addFragment(mainActivity.supportFragmentManager, EditWordFragment(), container.id, arguments)
}
private fun startEditFragment(container: View, wordItem: WordItem) {
//set container id
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
container.id = View.generateViewId()
} else container.id = 18071999
// start EditWordFragment
val arguments = Bundle()
arguments.putSerializable("wordItem", wordItem)
addFragment(mainActivity.supportFragmentManager, EditWordFragment(), container.id, arguments, "edit_fragment")
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
......@@ -119,7 +114,7 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val
fun filter(query: String) {
displayedVocabulary.clear()
if (query.isEmpty())
displayedVocabulary.addWords(fullVocabulary)
displayedVocabulary.copyWordsFrom(fullVocabulary)
else
displayedVocabulary.addWordsFittingQuery(fullVocabulary, query.toLowerCase())
notifyDataSetChanged()
......
......@@ -24,7 +24,6 @@ class MainActivity : AppCompatActivity() {
lateinit var vocabularyFragment: VocabularyFragment
lateinit var searchView: SearchView
var inEditMode = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
......
......@@ -28,7 +28,6 @@ class EditWordFragment : WordFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
mainActivity = activity as MainActivity
mainActivity.inEditMode = true
this.container = container
hidePreviousViews()
wordItem = arguments!!["wordItem"] as WordItem
......@@ -87,8 +86,8 @@ class EditWordFragment : WordFragment() {
}
private fun onFailure() {
shortToast(mainActivity, getString(R.string.toast_update_word_failed))
stop()
/*shortToast(mainActivity, getString(R.string.toast_update_word_failed))
stop()*/
}
private fun stop() {
......@@ -97,7 +96,6 @@ class EditWordFragment : WordFragment() {
removeFragment(mainActivity.supportFragmentManager, this)
mainActivity.hideKeyboardNotFromActivity(mainActivity)
gone(mainActivity.btnSubmitLayout)
mainActivity.inEditMode = false
}
companion object {
......
......@@ -13,12 +13,12 @@ import org.apache.commons.lang3.StringUtils
val progressFragment: Fragment = ProgressFragment()
fun addFragment(fragmentManager: FragmentManager, fragment: Fragment, containerId: Int, arguments: Bundle?) {
fun addFragment(fragmentManager: FragmentManager, fragment: Fragment, containerId: Int, arguments: Bundle?, tag: String? = null) {
fragment.arguments = arguments
// remove progressFragment if it exists
if(fragmentManager.findFragmentById(fragment.id) != null)
removeFragment(fragmentManager, fragment)
fragmentManager.beginTransaction().add(containerId, fragment).commitAllowingStateLoss()
fragmentManager.beginTransaction().add(containerId, fragment, tag).commitAllowingStateLoss()
}
fun removeFragment(fragmentManager: FragmentManager, fragment: Fragment) {
......
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