-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: Migrate data operations to FavoritesRepository
This commit refactors data operations related to favorites and watched chapters by migrating them to a new `FavoritesRepository` class. - Created a new `FavoritesRepository` to centralize data operations for favorites and watched chapters. - Moved favorite and watched chapter-related functions from ViewModels and Fragments to the `FavoritesRepository`. - Updated ViewModels and Fragments to use the `FavoritesRepository` for data operations. - Updated the Koin module to include the `FavoritesRepository`. - Removed direct database and FirebaseDb calls from ViewModels and Fragments. - Updated related dependencies and imports. - Improved code organization and maintainability by centralizing data operations.
- Loading branch information
jacobrein
committed
Jan 23, 2025
1 parent
f84d5da
commit 3b4a1b1
Showing
8 changed files
with
124 additions
and
62 deletions.
There are no files selected for viewing
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
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
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
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
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
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
70 changes: 70 additions & 0 deletions
70
UIViews/src/main/java/com/programmersbox/uiviews/repository/FavoritesRepository.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.programmersbox.uiviews.repository | ||
|
||
import androidx.compose.ui.util.fastMaxBy | ||
import com.programmersbox.favoritesdatabase.ChapterWatched | ||
import com.programmersbox.favoritesdatabase.DbModel | ||
import com.programmersbox.favoritesdatabase.ItemDao | ||
import com.programmersbox.sharedutils.FirebaseDb | ||
import com.programmersbox.uiviews.utils.FireListenerClosable | ||
import kotlinx.coroutines.flow.collect | ||
import kotlinx.coroutines.flow.combine | ||
|
||
class FavoritesRepository( | ||
private val dao: ItemDao, | ||
) { | ||
|
||
suspend fun addFavorite(db: DbModel) { | ||
dao.insertFavorite(db) | ||
FirebaseDb.insertShowFlow(db).collect() | ||
} | ||
|
||
suspend fun removeFavorite(db: DbModel) { | ||
dao.deleteFavorite(db) | ||
FirebaseDb.removeShowFlow(db).collect() | ||
} | ||
|
||
suspend fun addWatched(chapterWatched: ChapterWatched) { | ||
dao.insertChapter(chapterWatched) | ||
FirebaseDb.insertEpisodeWatchedFlow(chapterWatched).collect() | ||
} | ||
|
||
suspend fun removeWatched(chapterWatched: ChapterWatched) { | ||
dao.deleteChapter(chapterWatched) | ||
FirebaseDb.removeEpisodeWatchedFlow(chapterWatched).collect() | ||
} | ||
|
||
suspend fun toggleNotify(db: DbModel) { | ||
dao.updateFavoriteItem(db) | ||
FirebaseDb.toggleUpdateCheckShowFlow(db).collect() | ||
} | ||
|
||
fun isFavorite( | ||
url: String, | ||
fireListenerClosable: FireListenerClosable, | ||
) = combine( | ||
fireListenerClosable.findItemByUrlFlow(url), | ||
dao.containsItem(url) | ||
) { f, d -> f || d } | ||
|
||
fun getChapters( | ||
url: String, | ||
fireListenerClosable: FireListenerClosable, | ||
) = combine( | ||
fireListenerClosable.getAllEpisodesByShowFlow(url), | ||
dao.getAllChapters(url) | ||
) { f, d -> (f + d).distinctBy { it.url } } | ||
|
||
fun getModel( | ||
url: String, | ||
fireListenerClosable: FireListenerClosable, | ||
) = combine( | ||
fireListenerClosable.getShowFlow(url), | ||
dao.getDbModel(url), | ||
) { d, f -> d ?: f } | ||
|
||
fun getAllFavorites(fireListenerClosable: FireListenerClosable) = combine( | ||
fireListenerClosable.getAllShowsFlow(), | ||
dao.getAllFavorites() | ||
) { f, d -> (f + d).groupBy(DbModel::url).map { it.value.fastMaxBy(DbModel::numChapters)!! } } | ||
|
||
} |
Oops, something went wrong.