This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Button | |
android:onClick="@{() -> viewModel.addPhrase(viewModel.phrase, viewModel.translation, viewModel.groupId)}" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val phraseModule = module { | |
viewModel { PhraseViewModel(get()) } | |
single<PhraseDatabase> { | |
Room.databaseBuilder( | |
get(), | |
PhraseDatabase::class.java, "database-name" | |
).build() | |
} | |
single { PreferenceService(get()) } | |
fun provideDao(db: PhraseDatabase): PhraseDao = db.phraseDao() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Database(entities = [Phrase::class, Group::class]) | |
abstract class PhraseDatabase : RoomDatabase() { | |
abstract fun phraseDao(): PhraseDao | |
} | |
@Dao | |
interface PhraseDao { | |
@Query("SELECT * FROM phrases WHERE `group` = :groupId") | |
fun getAll(groupId: String): Flow<List<Phrase>> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val activeGroup = prefs.activeGroup.asLiveData(viewModelScope.coroutineContext) | |
val phrases: LiveData<List<Phrase>> = activeGroup.switchMap { group -> | |
db.getAll(group) | |
.asLiveData(viewModelScope.coroutineContext) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val canAddNewPhrase: LiveData<Boolean> = Lives.combineLatest(phrase, translation){ | |
phrase, translation -> phrase.isNotEmpty() && translation.isNotEmpty() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Button | |
android:onClick="@{() -> viewModel.addWord(viewModel.phrase, viewModel.translation, viewModel.groupId)}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun addPhrase(phrase: String, translation: String, groupId: String){ | |
if(phrase.isNotEmpty() && translation.isNotEmpty()){ | |
viewmodelScope.launch(Dispatchers.IO){ | |
db.addAll(Phrase(newPhrase, newTranslation)) | |
} | |
} | |
} |
NewerOlder