From f0b2b015da4d8e7758bfb6243a61b68df7fca5be Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Sun, 15 Nov 2026 00:37:48 +0100 Subject: [PATCH 1/2] feat(settings): Add TMDb CSV import feature - Added "Import from TMDb" section in SettingsView below "Import Database". - Implemented importTmdbCsv to parse TMDb exported CSVs directly. - Fetches poster images directly from TMDB api during import. - Handles both movie ratings and watchlists based on "Your Rating". - Added proper foreign key constraints handling by correctly ordering DB insertions (Movies -> LoggedMovie). - Handled IO threaded background file processing with localized Toast feedback. Assisted-by: Gemini 3.1 Pro Preview Signed-off-by: Marcel Hibbe --- .../screen_navigation/Navigation.kt | 4 +- .../components/ImportTmdbSection.kt | 64 ++++++++++ .../screen_settings/SettingsView.kt | 7 +- .../presentation/utils/SettingsExtensions.kt | 109 +++++++++++++++++- .../logline/room_database/LoggedMovieDao.kt | 4 +- app/src/main/res/values/strings.xml | 3 + 6 files changed, 184 insertions(+), 7 deletions(-) create mode 100644 app/src/main/java/com/patch4code/logline/features/settings/presentation/components/ImportTmdbSection.kt diff --git a/app/src/main/java/com/patch4code/logline/features/navigation/presentation/screen_navigation/Navigation.kt b/app/src/main/java/com/patch4code/logline/features/navigation/presentation/screen_navigation/Navigation.kt index 156b5cd..d55b808 100644 --- a/app/src/main/java/com/patch4code/logline/features/navigation/presentation/screen_navigation/Navigation.kt +++ b/app/src/main/java/com/patch4code/logline/features/navigation/presentation/screen_navigation/Navigation.kt @@ -248,10 +248,10 @@ fun Navigation(db: LoglineDatabase){ } composable(route = Screen.SettingsScreen.route){ - SettingsView(navController = navController) + SettingsView(navController = navController, db = db) } } } } ) -} \ No newline at end of file +} diff --git a/app/src/main/java/com/patch4code/logline/features/settings/presentation/components/ImportTmdbSection.kt b/app/src/main/java/com/patch4code/logline/features/settings/presentation/components/ImportTmdbSection.kt new file mode 100644 index 0000000..2755e46 --- /dev/null +++ b/app/src/main/java/com/patch4code/logline/features/settings/presentation/components/ImportTmdbSection.kt @@ -0,0 +1,64 @@ +package com.patch4code.logline.features.settings.presentation.components + +import android.widget.Toast +import androidx.activity.compose.rememberLauncherForActivityResult +import androidx.activity.result.contract.ActivityResultContracts +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.Button +import androidx.compose.material3.HorizontalDivider +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.unit.dp +import com.patch4code.logline.R +import com.patch4code.logline.features.settings.presentation.utils.SettingsExtensions +import com.patch4code.logline.room_database.LoglineDatabase +import com.patch4code.logline.ui.theme.Beige +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch + +@Composable +fun ImportTmdbSection(db: LoglineDatabase) { + val context = LocalContext.current + + val singleFilePickerLauncher = rememberLauncherForActivityResult( + contract = ActivityResultContracts.GetContent(), + onResult = { localUri -> + if (localUri != null) { + CoroutineScope(Dispatchers.IO).launch { + SettingsExtensions.importTmdbCsv( + context = context, + importCsvFileUri = localUri, + db = db, + onImportSuccess = { + CoroutineScope(Dispatchers.Main).launch { + Toast.makeText(context, "TMDb Import Successful", Toast.LENGTH_LONG).show() + } + }, + onImportError = { errorMsg -> + CoroutineScope(Dispatchers.Main).launch { + Toast.makeText(context, errorMsg, Toast.LENGTH_LONG).show() + } + } + ) + } + } + } + ) + + HorizontalDivider() + Column(modifier = Modifier.padding(20.dp)) { + Text(text = stringResource(id = R.string.tmdb_import_title), style = MaterialTheme.typography.titleMedium, color = Beige) + Text(text = stringResource(id = R.string.tmdb_import_text), style = MaterialTheme.typography.bodyMedium) + Spacer(modifier = Modifier.padding(8.dp)) + Button(onClick = { singleFilePickerLauncher.launch("text/comma-separated-values") }) { + Text(text = stringResource(id = R.string.tmdb_import_button_text)) + } + } +} diff --git a/app/src/main/java/com/patch4code/logline/features/settings/presentation/screen_settings/SettingsView.kt b/app/src/main/java/com/patch4code/logline/features/settings/presentation/screen_settings/SettingsView.kt index 781f8e0..9c5ea07 100644 --- a/app/src/main/java/com/patch4code/logline/features/settings/presentation/screen_settings/SettingsView.kt +++ b/app/src/main/java/com/patch4code/logline/features/settings/presentation/screen_settings/SettingsView.kt @@ -12,7 +12,9 @@ import com.patch4code.logline.features.navigation.presentation.components.topbar import com.patch4code.logline.features.settings.presentation.components.EditProfileButton import com.patch4code.logline.features.settings.presentation.components.ExportDataSection import com.patch4code.logline.features.settings.presentation.components.ImportDataSection +import com.patch4code.logline.features.settings.presentation.components.ImportTmdbSection import com.patch4code.logline.features.settings.presentation.components.SelectCountrySection +import com.patch4code.logline.room_database.LoglineDatabase /** * GNU GENERAL PUBLIC LICENSE, VERSION 3.0 (https://www.gnu.org/licenses/gpl-3.0.html) @@ -25,7 +27,7 @@ import com.patch4code.logline.features.settings.presentation.components.SelectCo * @author Patch4Code */ @Composable -fun SettingsView(navController: NavController, settingsViewModel: SettingsViewModel = viewModel() +fun SettingsView(navController: NavController, db: LoglineDatabase, settingsViewModel: SettingsViewModel = viewModel() ){ val context = LocalContext.current @@ -44,6 +46,7 @@ fun SettingsView(navController: NavController, settingsViewModel: SettingsViewMo SelectCountrySection(settingsViewModel) ExportDataSection() ImportDataSection() + ImportTmdbSection(db) } } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/patch4code/logline/features/settings/presentation/utils/SettingsExtensions.kt b/app/src/main/java/com/patch4code/logline/features/settings/presentation/utils/SettingsExtensions.kt index e75d750..dfee08a 100644 --- a/app/src/main/java/com/patch4code/logline/features/settings/presentation/utils/SettingsExtensions.kt +++ b/app/src/main/java/com/patch4code/logline/features/settings/presentation/utils/SettingsExtensions.kt @@ -12,6 +12,15 @@ import java.time.format.DateTimeFormatter import java.util.zip.ZipEntry import java.util.zip.ZipInputStream import java.util.zip.ZipOutputStream +import java.io.BufferedReader +import java.io.InputStreamReader +import java.time.ZonedDateTime +import com.patch4code.logline.room_database.LoglineDatabase +import com.patch4code.logline.features.core.domain.model.Movie +import com.patch4code.logline.features.diary.domain.model.LoggedMovie +import com.patch4code.logline.api.RetrofitHelper +import com.patch4code.logline.api.TmdbApiService +import com.patch4code.logline.features.core.presentation.utils.TmdbCredentials /** * GNU GENERAL PUBLIC LICENSE, VERSION 3.0 (https://www.gnu.org/licenses/gpl-3.0.html) @@ -162,4 +171,102 @@ object SettingsExtensions { Log.e("SettingsHelper", "Import Error: ", e) } } -} \ No newline at end of file + + suspend fun importTmdbCsv( + context: Context, + importCsvFileUri: Uri, + db: LoglineDatabase, + onImportSuccess: () -> Unit, + onImportError: (errorMsg: String) -> Unit + ) { + try { + val contentResolver = context.contentResolver + val inputStream = contentResolver.openInputStream(importCsvFileUri) ?: throw Exception("Cannot open file") + val reader = BufferedReader(InputStreamReader(inputStream)) + + val tmdbApiService = RetrofitHelper.getInstance(TmdbCredentials.BASE_URL).create( + TmdbApiService::class.java + ) + + val header = reader.readLine() + if (header == null) { + onImportError("Empty file") + return + } + + var line: String? = reader.readLine() + while (line != null) { + val tokens = parseCsvLine(line) + if (tokens.size >= 10 && tokens[2] == "movie") { + val tmdbId = tokens[0].toIntOrNull() + val title = tokens[3] + val releaseDate = tokens[4] + val ratingStr = tokens[7] + val userRatingStr = tokens[8] + val dateRatedStr = tokens[9] + + if (tmdbId != null) { + val voteAverage = ratingStr.toDoubleOrNull() ?: 0.0 + var movie = Movie( + id = tmdbId, + title = title, + releaseDate = releaseDate.split("T").firstOrNull() ?: releaseDate, + voteAverage = voteAverage + ) + val userRating = userRatingStr.toDoubleOrNull()?.toInt() ?: -1 + + try { + val response = tmdbApiService.getMovieDetails(tmdbId) + if (response.isSuccessful) { + response.body()?.let { details -> + movie = movie.copy(posterUrl = details.posterPath) + } + } + } catch (e: Exception) { + Log.e("ImportTmdb", "Error fetching movie details for tmdbId $tmdbId", e) + } + + if (userRating != -1) { + db.movieUserDataDao.updateOrInsertRating(movie, userRating) + if (dateRatedStr.isNotBlank()) { + val dateRated = ZonedDateTime.parse(dateRatedStr).toLocalDateTime() + val loggedElement = LoggedMovie( + movieId = tmdbId, + date = dateRated, + rating = userRating, + review = "" + ) + db.loggedMovieDao.addNewMovieLog(loggedElement, movie) + } + } else { + db.movieUserDataDao.updateOrInsertOnWatchlist(movie, true) + } + } + } + line = reader.readLine() + } + reader.close() + onImportSuccess() + } catch (e: Exception) { + onImportError(e.message ?: "Unknown error") + } + } + + private fun parseCsvLine(line: String): List { + val tokens = mutableListOf() + val currentToken = StringBuilder() + var inQuotes = false + for (char in line) { + if (char == '\"') { + inQuotes = !inQuotes + } else if (char == ',' && !inQuotes) { + tokens.add(currentToken.toString().trim('\"')) + currentToken.clear() + } else { + currentToken.append(char) + } + } + tokens.add(currentToken.toString().trim('\"')) + return tokens + } +} diff --git a/app/src/main/java/com/patch4code/logline/room_database/LoggedMovieDao.kt b/app/src/main/java/com/patch4code/logline/room_database/LoggedMovieDao.kt index 99b2f1d..ddb44bc 100644 --- a/app/src/main/java/com/patch4code/logline/room_database/LoggedMovieDao.kt +++ b/app/src/main/java/com/patch4code/logline/room_database/LoggedMovieDao.kt @@ -36,8 +36,8 @@ interface LoggedMovieDao { @Transaction suspend fun addNewMovieLog(loggedElement: LoggedMovie, movie: Movie){ - upsertLoggedMovie(loggedElement) upsertMovie(movie) + upsertLoggedMovie(loggedElement) } @@ -342,4 +342,4 @@ interface LoggedMovieDao { """ ) suspend fun getReviewsOrderedByVoteAverageDesc(): List -} \ No newline at end of file +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6c3145a..8e612e9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -231,6 +231,9 @@ Warning Please note that the current app data will be permanently replaced after the import process is completed! Import Data + Import from TMDb + Import your movie ratings from a TMDb CSV export file. + Import CSV Country for Watch Providers Select your country to access streaming, rental, and purchase options customized specifically for this location on the movie page. From 13b43104d18eac9e289f55705222f62fdde8e4d3 Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Sun, 15 Nov 2026 00:53:16 +0100 Subject: [PATCH 2/2] feat(sync): Lazy load missing movie posters on list open - Removed synchronous TMDB API calls during CSV import to prevent long blocking times and rate limiting. - Added MovieSyncHelper to dynamically fetch missing poster URLs in the background. - Integrated MovieSyncHelper across all list view models (Watchlist, MyMovies, Diary, Reviews, Custom Lists). - Updates are persisted to Room DB and dynamically pushed to the UI via LiveData. Assisted-by: Gemini 3.1 Pro Preview Signed-off-by: Marcel Hibbe --- .../presentation/utils/MovieSyncHelper.kt | 50 +++++++++++++++++++ .../screen_diary/DiaryViewModel.kt | 17 ++++++- .../presentation/screen_list/ListViewModel.kt | 17 ++++++- .../screen_list/ListsTableViewModel.kt | 17 ++++++- .../screen_my_movies/MyMoviesViewModel.kt | 17 ++++++- .../screen_reviews/ReviewsViewModel.kt | 17 ++++++- .../presentation/utils/SettingsExtensions.kt | 20 +------- .../screen_watchlist/WatchlistViewModel.kt | 17 ++++++- 8 files changed, 147 insertions(+), 25 deletions(-) create mode 100644 app/src/main/java/com/patch4code/logline/features/core/presentation/utils/MovieSyncHelper.kt diff --git a/app/src/main/java/com/patch4code/logline/features/core/presentation/utils/MovieSyncHelper.kt b/app/src/main/java/com/patch4code/logline/features/core/presentation/utils/MovieSyncHelper.kt new file mode 100644 index 0000000..a3e7ea2 --- /dev/null +++ b/app/src/main/java/com/patch4code/logline/features/core/presentation/utils/MovieSyncHelper.kt @@ -0,0 +1,50 @@ +package com.patch4code.logline.features.core.presentation.utils + +import android.util.Log +import com.patch4code.logline.api.RetrofitHelper +import com.patch4code.logline.api.TmdbApiService +import com.patch4code.logline.features.core.domain.model.Movie +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext + +/** + * GNU GENERAL PUBLIC LICENSE, VERSION 3.0 (https://www.gnu.org/licenses/gpl-3.0.html) + * + * MovieSyncHelper - Utility to fetch missing details (like posters) from TMDB API for locally stored movies. + * + * @author Patch4Code + */ +object MovieSyncHelper { + + suspend fun syncMissingPosters( + movies: List, + upsertMovie: suspend (Movie) -> Unit, + onMovieUpdated: (Movie) -> Unit + ) { + val missingPosters = movies.filter { it.posterUrl.isNullOrBlank() } + if (missingPosters.isEmpty()) return + + val tmdbApiService = RetrofitHelper.getInstance(TmdbCredentials.BASE_URL).create(TmdbApiService::class.java) + + withContext(Dispatchers.IO) { + missingPosters.forEach { movie -> + try { + val response = tmdbApiService.getMovieDetails(movie.id) + if (response.isSuccessful) { + response.body()?.let { details -> + if (!details.posterPath.isNullOrBlank()) { + val updatedMovie = movie.copy(posterUrl = details.posterPath) + upsertMovie(updatedMovie) + withContext(Dispatchers.Main) { + onMovieUpdated(updatedMovie) + } + } + } + } + } catch (e: Exception) { + Log.e("MovieSyncHelper", "Error syncing poster for movie ${movie.id}", e) + } + } + } + } +} diff --git a/app/src/main/java/com/patch4code/logline/features/diary/presentation/screen_diary/DiaryViewModel.kt b/app/src/main/java/com/patch4code/logline/features/diary/presentation/screen_diary/DiaryViewModel.kt index f326850..f44f39e 100644 --- a/app/src/main/java/com/patch4code/logline/features/diary/presentation/screen_diary/DiaryViewModel.kt +++ b/app/src/main/java/com/patch4code/logline/features/diary/presentation/screen_diary/DiaryViewModel.kt @@ -9,6 +9,7 @@ import com.patch4code.logline.features.core.domain.model.FilterOptions import com.patch4code.logline.features.core.domain.model.SortOption import com.patch4code.logline.features.core.presentation.utils.FilterHelper import com.patch4code.logline.features.core.presentation.utils.MovieHelper +import com.patch4code.logline.features.core.presentation.utils.MovieSyncHelper import com.patch4code.logline.features.diary.domain.model.DiaryAndReviewSortOptions import com.patch4code.logline.features.diary.domain.model.MovieWithLoggedData import com.patch4code.logline.room_database.LoggedMovieDao @@ -52,6 +53,20 @@ class DiaryViewModel(private val loggedMovieDao: LoggedMovieDao): ViewModel() { } val filteredAndSortedDiaryLogs = filterDiaryLogs(sortedItems, filterOptions) _diaryLogs.value = filteredAndSortedDiaryLogs + + viewModelScope.launch { + MovieSyncHelper.syncMissingPosters( + movies = filteredAndSortedDiaryLogs.map { it.movie }, + upsertMovie = { loggedMovieDao.upsertMovie(it) } + ) { updatedMovie -> + val currentList = _diaryLogs.value?.toMutableList() ?: return@syncMissingPosters + val index = currentList.indexOfFirst { it.movie.id == updatedMovie.id } + if (index != -1) { + currentList[index] = currentList[index].copy(movie = updatedMovie) + _diaryLogs.value = currentList + } + } + } } } @@ -95,4 +110,4 @@ class DiaryViewModelFactory(private val dao: LoggedMovieDao) : ViewModelProvider } throw IllegalArgumentException("Unknown ViewModel class") } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/patch4code/logline/features/list/presentation/screen_list/ListViewModel.kt b/app/src/main/java/com/patch4code/logline/features/list/presentation/screen_list/ListViewModel.kt index fd39061..08d5658 100644 --- a/app/src/main/java/com/patch4code/logline/features/list/presentation/screen_list/ListViewModel.kt +++ b/app/src/main/java/com/patch4code/logline/features/list/presentation/screen_list/ListViewModel.kt @@ -10,6 +10,7 @@ import com.patch4code.logline.features.core.domain.model.Movie import com.patch4code.logline.features.core.domain.model.SortOption import com.patch4code.logline.features.core.presentation.utils.FilterHelper import com.patch4code.logline.features.core.presentation.utils.MovieHelper +import com.patch4code.logline.features.core.presentation.utils.MovieSyncHelper import com.patch4code.logline.features.list.domain.model.ListSortOptions import com.patch4code.logline.features.list.domain.model.MovieInList import com.patch4code.logline.features.list.domain.model.MovieList @@ -59,6 +60,20 @@ class ListViewModel(private val movieListDao: MovieListDao, private val movieInL } val filteredAndSortedListItems = filterList(sortedItems, filterOptions) _moviesInList.value = filteredAndSortedListItems + + viewModelScope.launch { + MovieSyncHelper.syncMissingPosters( + movies = filteredAndSortedListItems.map { it.movie }, + upsertMovie = { movieInListDao.upsertMovie(it) } + ) { updatedMovie -> + val currentList = _moviesInList.value?.toMutableList() ?: return@syncMissingPosters + val index = currentList.indexOfFirst { it.movie.id == updatedMovie.id } + if (index != -1) { + currentList[index] = currentList[index].copy(movie = updatedMovie) + _moviesInList.value = currentList + } + } + } } } @@ -157,4 +172,4 @@ class ListViewModelFactory(private val movieListDao: MovieListDao, private val m } throw IllegalArgumentException("Unknown ViewModel class") } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/patch4code/logline/features/list/presentation/screen_list/ListsTableViewModel.kt b/app/src/main/java/com/patch4code/logline/features/list/presentation/screen_list/ListsTableViewModel.kt index 8013cb6..5a0a026 100644 --- a/app/src/main/java/com/patch4code/logline/features/list/presentation/screen_list/ListsTableViewModel.kt +++ b/app/src/main/java/com/patch4code/logline/features/list/presentation/screen_list/ListsTableViewModel.kt @@ -6,6 +6,7 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.viewModelScope import com.patch4code.logline.features.core.domain.model.SortOption +import com.patch4code.logline.features.core.presentation.utils.MovieSyncHelper import com.patch4code.logline.features.list.domain.model.ListTableSortOptions import com.patch4code.logline.features.list.domain.model.MovieList import com.patch4code.logline.features.list.domain.model.MovieWithListItem @@ -51,6 +52,20 @@ class ListsTableViewModel(private val movieListDao: MovieListDao, private val mo fun getMoviesInLists(){ viewModelScope.launch { _moviesInLists.value = movieInListDao.getAllMovieWithListItems() + + viewModelScope.launch { + MovieSyncHelper.syncMissingPosters( + movies = _moviesInLists.value?.map { it.movie } ?: emptyList(), + upsertMovie = { movieInListDao.upsertMovie(it) } + ) { updatedMovie -> + val currentList = _moviesInLists.value?.toMutableList() ?: return@syncMissingPosters + val index = currentList.indexOfFirst { it.movie.id == updatedMovie.id } + if (index != -1) { + currentList[index] = currentList[index].copy(movie = updatedMovie) + _moviesInLists.value = currentList + } + } + } } } @@ -80,4 +95,4 @@ class ListsTableViewModelFactory(private val movieListDao: MovieListDao, private } throw IllegalArgumentException("Unknown ViewModel class") } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/patch4code/logline/features/my_movies/presentation/screen_my_movies/MyMoviesViewModel.kt b/app/src/main/java/com/patch4code/logline/features/my_movies/presentation/screen_my_movies/MyMoviesViewModel.kt index f464714..e77766f 100644 --- a/app/src/main/java/com/patch4code/logline/features/my_movies/presentation/screen_my_movies/MyMoviesViewModel.kt +++ b/app/src/main/java/com/patch4code/logline/features/my_movies/presentation/screen_my_movies/MyMoviesViewModel.kt @@ -10,6 +10,7 @@ import com.patch4code.logline.features.core.domain.model.MovieWithUserData import com.patch4code.logline.features.core.domain.model.SortOption import com.patch4code.logline.features.core.presentation.utils.FilterHelper import com.patch4code.logline.features.core.presentation.utils.MovieHelper +import com.patch4code.logline.features.core.presentation.utils.MovieSyncHelper import com.patch4code.logline.features.my_movies.domain.model.MyMoviesSortOptions import com.patch4code.logline.room_database.MovieUserDataDao import kotlinx.coroutines.launch @@ -51,6 +52,20 @@ class MyMoviesViewModel(private val dao: MovieUserDataDao): ViewModel() { } val filteredAndSortedMyMoviesItems = filterMyMoviesItems(sortedItems, filterOptions) _myMoviesItems.value = filteredAndSortedMyMoviesItems + + viewModelScope.launch { + MovieSyncHelper.syncMissingPosters( + movies = filteredAndSortedMyMoviesItems.map { it.movie }, + upsertMovie = { dao.upsertMovie(it) } + ) { updatedMovie -> + val currentList = _myMoviesItems.value?.toMutableList() ?: return@syncMissingPosters + val index = currentList.indexOfFirst { it.movie.id == updatedMovie.id } + if (index != -1) { + currentList[index] = currentList[index].copy(movie = updatedMovie) + _myMoviesItems.value = currentList + } + } + } } } @@ -94,4 +109,4 @@ class MyMoviesViewModelFactory(private val dao: MovieUserDataDao) : ViewModelPro } throw IllegalArgumentException("Unknown ViewModel class") } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/patch4code/logline/features/reviews/presentation/screen_reviews/ReviewsViewModel.kt b/app/src/main/java/com/patch4code/logline/features/reviews/presentation/screen_reviews/ReviewsViewModel.kt index de13abf..185fd5c 100644 --- a/app/src/main/java/com/patch4code/logline/features/reviews/presentation/screen_reviews/ReviewsViewModel.kt +++ b/app/src/main/java/com/patch4code/logline/features/reviews/presentation/screen_reviews/ReviewsViewModel.kt @@ -9,6 +9,7 @@ import com.patch4code.logline.features.core.domain.model.FilterOptions import com.patch4code.logline.features.core.domain.model.SortOption import com.patch4code.logline.features.core.presentation.utils.FilterHelper import com.patch4code.logline.features.core.presentation.utils.MovieHelper +import com.patch4code.logline.features.core.presentation.utils.MovieSyncHelper import com.patch4code.logline.features.diary.domain.model.DiaryAndReviewSortOptions import com.patch4code.logline.features.diary.domain.model.MovieWithLoggedData import com.patch4code.logline.room_database.LoggedMovieDao @@ -51,6 +52,20 @@ class ReviewsViewModel(private val loggedMovieDao: LoggedMovieDao): ViewModel(){ } val filteredAndSortedReviews = filterReviews(sortedItems, filterOptions) _reviewedLogs.value = filteredAndSortedReviews + + viewModelScope.launch { + MovieSyncHelper.syncMissingPosters( + movies = filteredAndSortedReviews.map { it.movie }, + upsertMovie = { loggedMovieDao.upsertMovie(it) } + ) { updatedMovie -> + val currentList = _reviewedLogs.value?.toMutableList() ?: return@syncMissingPosters + val index = currentList.indexOfFirst { it.movie.id == updatedMovie.id } + if (index != -1) { + currentList[index] = currentList[index].copy(movie = updatedMovie) + _reviewedLogs.value = currentList + } + } + } } } @@ -94,4 +109,4 @@ class ReviewsViewModelFactory(private val loggedMovieDao: LoggedMovieDao) : View } throw IllegalArgumentException("Unknown ViewModel class") } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/patch4code/logline/features/settings/presentation/utils/SettingsExtensions.kt b/app/src/main/java/com/patch4code/logline/features/settings/presentation/utils/SettingsExtensions.kt index dfee08a..dba0854 100644 --- a/app/src/main/java/com/patch4code/logline/features/settings/presentation/utils/SettingsExtensions.kt +++ b/app/src/main/java/com/patch4code/logline/features/settings/presentation/utils/SettingsExtensions.kt @@ -18,9 +18,6 @@ import java.time.ZonedDateTime import com.patch4code.logline.room_database.LoglineDatabase import com.patch4code.logline.features.core.domain.model.Movie import com.patch4code.logline.features.diary.domain.model.LoggedMovie -import com.patch4code.logline.api.RetrofitHelper -import com.patch4code.logline.api.TmdbApiService -import com.patch4code.logline.features.core.presentation.utils.TmdbCredentials /** * GNU GENERAL PUBLIC LICENSE, VERSION 3.0 (https://www.gnu.org/licenses/gpl-3.0.html) @@ -184,10 +181,6 @@ object SettingsExtensions { val inputStream = contentResolver.openInputStream(importCsvFileUri) ?: throw Exception("Cannot open file") val reader = BufferedReader(InputStreamReader(inputStream)) - val tmdbApiService = RetrofitHelper.getInstance(TmdbCredentials.BASE_URL).create( - TmdbApiService::class.java - ) - val header = reader.readLine() if (header == null) { onImportError("Empty file") @@ -207,7 +200,7 @@ object SettingsExtensions { if (tmdbId != null) { val voteAverage = ratingStr.toDoubleOrNull() ?: 0.0 - var movie = Movie( + val movie = Movie( id = tmdbId, title = title, releaseDate = releaseDate.split("T").firstOrNull() ?: releaseDate, @@ -215,17 +208,6 @@ object SettingsExtensions { ) val userRating = userRatingStr.toDoubleOrNull()?.toInt() ?: -1 - try { - val response = tmdbApiService.getMovieDetails(tmdbId) - if (response.isSuccessful) { - response.body()?.let { details -> - movie = movie.copy(posterUrl = details.posterPath) - } - } - } catch (e: Exception) { - Log.e("ImportTmdb", "Error fetching movie details for tmdbId $tmdbId", e) - } - if (userRating != -1) { db.movieUserDataDao.updateOrInsertRating(movie, userRating) if (dateRatedStr.isNotBlank()) { diff --git a/app/src/main/java/com/patch4code/logline/features/watchlist/presentation/screen_watchlist/WatchlistViewModel.kt b/app/src/main/java/com/patch4code/logline/features/watchlist/presentation/screen_watchlist/WatchlistViewModel.kt index b50863b..15b222e 100644 --- a/app/src/main/java/com/patch4code/logline/features/watchlist/presentation/screen_watchlist/WatchlistViewModel.kt +++ b/app/src/main/java/com/patch4code/logline/features/watchlist/presentation/screen_watchlist/WatchlistViewModel.kt @@ -10,6 +10,7 @@ import com.patch4code.logline.features.core.domain.model.MovieWithUserData import com.patch4code.logline.features.core.domain.model.SortOption import com.patch4code.logline.features.core.presentation.utils.FilterHelper import com.patch4code.logline.features.core.presentation.utils.MovieHelper +import com.patch4code.logline.features.core.presentation.utils.MovieSyncHelper import com.patch4code.logline.features.watchlist.domain.model.WatchlistSortOptions import com.patch4code.logline.room_database.MovieUserDataDao import kotlinx.coroutines.launch @@ -48,6 +49,20 @@ class WatchlistViewModel(private val dao: MovieUserDataDao): ViewModel() { } val filteredAndSortedWatchlistItems = filterWatchlistItems(sortedItems, filterOptions) _watchlistItems.value = filteredAndSortedWatchlistItems + + viewModelScope.launch { + MovieSyncHelper.syncMissingPosters( + movies = filteredAndSortedWatchlistItems.map { it.movie }, + upsertMovie = { dao.upsertMovie(it) } + ) { updatedMovie -> + val currentList = _watchlistItems.value?.toMutableList() ?: return@syncMissingPosters + val index = currentList.indexOfFirst { it.movie.id == updatedMovie.id } + if (index != -1) { + currentList[index] = currentList[index].copy(movie = updatedMovie) + _watchlistItems.value = currentList + } + } + } } } @@ -91,4 +106,4 @@ class WatchlistViewModelFactory(private val dao: MovieUserDataDao) : ViewModelPr } throw IllegalArgumentException("Unknown ViewModel class") } -} \ No newline at end of file +}