Commit 0c67e41d by Paktalin

Refactoring. Generalized Vocabularies with Vocabulary interface

parent 977460e3
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
</value> </value>
</option> </option>
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" 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">
......
...@@ -82,7 +82,7 @@ class MainActivity : AppCompatActivity() { ...@@ -82,7 +82,7 @@ class MainActivity : AppCompatActivity() {
private fun setUpVocabularyAdapter() { private fun setUpVocabularyAdapter() {
addProgressBar(supportFragmentManager, R.id.container_main) addProgressBar(supportFragmentManager, R.id.container_main)
FirestoreManager().extractVocabularyId({ FirestoreManager().extractVocabularyId({
// recyclerView.adapter = VocabularyAdapter(Vocabulary(), this@MainActivity) // recyclerView.adapter = VocabularyAdapter(BasicVocabulary(), this@MainActivity)
recyclerView.layoutManager = LockableLayoutManager(this@MainActivity) recyclerView.layoutManager = LockableLayoutManager(this@MainActivity)
FirestoreManager().extractVocabulary { querySnapshot -> FirestoreManager().extractVocabulary { querySnapshot ->
run { run {
......
package com.paktalin.vocabularynotebook.vocabulary
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
open class BasicVocabulary(var wordList: MutableList<WordItem>): Vocabulary {
override fun addAll(words: MutableList<WordItem>) {
wordList.addAll(words)
}
override fun deleteWord(wordItem: WordItem) {
wordList.remove(wordItem)
}
override fun addWord(newWord: WordItem) {
wordList.add(0, newWord)
}
override fun updateWord(updatedWord: WordItem) {
wordList[wordList.indexOf(updatedWord)] = updatedWord
}
}
\ No newline at end of file
...@@ -2,7 +2,7 @@ package com.paktalin.vocabularynotebook.vocabulary ...@@ -2,7 +2,7 @@ package com.paktalin.vocabularynotebook.vocabulary
import com.paktalin.vocabularynotebook.firestoreitems.WordItem import com.paktalin.vocabularynotebook.firestoreitems.WordItem
class DisplayedVocabulary(override var wordList: MutableList<WordItem>): Vocabulary(wordList) { class DisplayedVocabulary(wordList: MutableList<WordItem>): BasicVocabulary(wordList) {
var sort: Sort = Sort.BY_TIME var sort: Sort = Sort.BY_TIME
fun clear() { fun clear() {
......
...@@ -4,8 +4,8 @@ import com.paktalin.vocabularynotebook.firestoreitems.ModifiedLabel ...@@ -4,8 +4,8 @@ import com.paktalin.vocabularynotebook.firestoreitems.ModifiedLabel
import com.paktalin.vocabularynotebook.firestoreitems.WordItem import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.utils.FirestoreManager import com.paktalin.vocabularynotebook.utils.FirestoreManager
class ModifiedVocabulary : Vocabulary() { class ModifiedVocabulary : Vocabulary {
override var wordList = mutableListOf<WordItem>() var wordList = mutableListOf<WordItem>()
private val maxPermitted = 9 private val maxPermitted = 9
override fun addWord(newWord: WordItem) { override fun addWord(newWord: WordItem) {
...@@ -22,7 +22,6 @@ class ModifiedVocabulary : Vocabulary() { ...@@ -22,7 +22,6 @@ class ModifiedVocabulary : Vocabulary() {
override fun addAll(words: MutableList<WordItem>) { } override fun addAll(words: MutableList<WordItem>) { }
fun add(wordItem: WordItem, modifiedLabel: ModifiedLabel) { fun add(wordItem: WordItem, modifiedLabel: ModifiedLabel) {
wordItem.modifiedLabel = modifiedLabel wordItem.modifiedLabel = modifiedLabel
wordList.add(wordItem) wordList.add(wordItem)
......
...@@ -5,12 +5,11 @@ import com.google.firebase.firestore.QueryDocumentSnapshot ...@@ -5,12 +5,11 @@ import com.google.firebase.firestore.QueryDocumentSnapshot
import com.google.firebase.firestore.QuerySnapshot import com.google.firebase.firestore.QuerySnapshot
import com.paktalin.vocabularynotebook.firestoreitems.WordItem import com.paktalin.vocabularynotebook.firestoreitems.WordItem
class VocabSet(override var wordList: MutableList<WordItem>) : Vocabulary() { class VocabSet(var wordList: MutableList<WordItem>) : Vocabulary {
var fullVocabulary = Vocabulary(wordList) var fullVocabulary = BasicVocabulary(wordList)
private var displayedVocabulary = DisplayedVocabulary(wordList) private var displayedVocabulary = DisplayedVocabulary(wordList)
var modifiedVocabulary = ModifiedVocabulary() var modifiedVocabulary = ModifiedVocabulary()
private val vocabs = setOf(fullVocabulary, displayedVocabulary, modifiedVocabulary)
companion object { companion object {
fun createFromSnapshot(querySnapshot: QuerySnapshot): VocabSet { fun createFromSnapshot(querySnapshot: QuerySnapshot): VocabSet {
...@@ -29,19 +28,23 @@ class VocabSet(override var wordList: MutableList<WordItem>) : Vocabulary() { ...@@ -29,19 +28,23 @@ class VocabSet(override var wordList: MutableList<WordItem>) : Vocabulary() {
} }
override fun addAll(words: MutableList<WordItem>) { override fun addAll(words: MutableList<WordItem>) {
vocabs.forEach { v -> v.addAll(words) } listOf(fullVocabulary, displayedVocabulary, modifiedVocabulary)
.forEach { v -> v.addAll(words) }
} }
override fun updateWord(updatedWord: WordItem) { override fun updateWord(updatedWord: WordItem) {
vocabs.forEach { v -> v.updateWord(updatedWord) } listOf(fullVocabulary, displayedVocabulary, modifiedVocabulary)
.forEach { v -> v.updateWord(updatedWord) }
} }
override fun deleteWord(wordItem: WordItem) { override fun deleteWord(wordItem: WordItem) {
vocabs.forEach { v -> v.deleteWord(wordItem) } listOf(fullVocabulary, displayedVocabulary, modifiedVocabulary)
.forEach { v -> v.deleteWord(wordItem) }
} }
override fun addWord(newWord: WordItem) { override fun addWord(newWord: WordItem) {
vocabs.forEach { v -> v.addWord(newWord) } listOf(fullVocabulary, displayedVocabulary, modifiedVocabulary)
.forEach { v -> v.addWord(newWord) }
} }
fun sort(sort: Sort) { fun sort(sort: Sort) {
......
...@@ -2,26 +2,13 @@ package com.paktalin.vocabularynotebook.vocabulary ...@@ -2,26 +2,13 @@ package com.paktalin.vocabularynotebook.vocabulary
import com.paktalin.vocabularynotebook.firestoreitems.WordItem import com.paktalin.vocabularynotebook.firestoreitems.WordItem
open class Vocabulary() { interface Vocabulary {
open lateinit var wordList: MutableList<WordItem>
constructor(wordList: MutableList<WordItem>): this() { fun addAll(words: MutableList<WordItem>)
this.wordList = wordList
}
open fun addAll(words: MutableList<WordItem>) { fun deleteWord(wordItem: WordItem)
wordList.addAll(words)
}
open fun deleteWord(wordItem: WordItem) { fun addWord(newWord: WordItem)
wordList.remove(wordItem)
}
open fun addWord(newWord: WordItem) { fun updateWord(updatedWord: WordItem)
wordList.add(0, newWord)
}
open fun updateWord(updatedWord: WordItem) {
wordList[wordList.indexOf(updatedWord)] = updatedWord
}
} }
\ 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