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)) | |
} | |
} | |
} |
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
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
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
@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 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
<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
fun collection(path: String) = Firebase.firestore.collection(path) | |
override fun getLeaders(): Flow<List<User>> { | |
return collection("users") | |
.whereGreaterThan("points", 0) | |
.orderBy("points", Query.Direction.DESCENDING) | |
.limit(50) | |
.asFlow().map { snapshots -> | |
snapshots.map { it.toUserDto().toUser() } | |
} |
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 Query.asFlow(): Flow<QuerySnapshot> { | |
return callbackFlow { | |
val callback = addSnapshotListener { querySnapshot, ex -> | |
if (ex != null) { | |
close(ex) | |
} else { | |
offer(querySnapshot!!) | |
} | |
} | |
awaitClose { |
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
override fun getLeaders(lastVisibleItem: Flow<Int>): Flow<List<User>> = | |
flow { | |
val users = mutableListOf<DocumentSnapshot>() | |
users.addAll( | |
suspendCoroutine<List<DocumentSnapshot>> { c -> | |
collection("users") | |
.whereGreaterThan("points", 0) | |
.orderBy("points", Query.Direction.DESCENDING) | |
.limit(25) | |
.get().addOnSuccessListener { |
OlderNewer