Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<Movie>,
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)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
}
}
}

Expand Down Expand Up @@ -95,4 +110,4 @@ class DiaryViewModelFactory(private val dao: LoggedMovieDao) : ViewModelProvider
}
throw IllegalArgumentException("Unknown ViewModel class")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
}
}
}

Expand Down Expand Up @@ -157,4 +172,4 @@ class ListViewModelFactory(private val movieListDao: MovieListDao, private val m
}
throw IllegalArgumentException("Unknown ViewModel class")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
}
}
}

Expand Down Expand Up @@ -80,4 +95,4 @@ class ListsTableViewModelFactory(private val movieListDao: MovieListDao, private
}
throw IllegalArgumentException("Unknown ViewModel class")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
}
}
}

Expand Down Expand Up @@ -94,4 +109,4 @@ class MyMoviesViewModelFactory(private val dao: MovieUserDataDao) : ViewModelPro
}
throw IllegalArgumentException("Unknown ViewModel class")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,10 @@ fun Navigation(db: LoglineDatabase){
}

composable(route = Screen.SettingsScreen.route){
SettingsView(navController = navController)
SettingsView(navController = navController, db = db)
}
}
}
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
}
}
}

Expand Down Expand Up @@ -94,4 +109,4 @@ class ReviewsViewModelFactory(private val loggedMovieDao: LoggedMovieDao) : View
}
throw IllegalArgumentException("Unknown ViewModel class")
}
}
}
Original file line number Diff line number Diff line change
@@ -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))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -44,6 +46,7 @@ fun SettingsView(navController: NavController, settingsViewModel: SettingsViewMo
SelectCountrySection(settingsViewModel)
ExportDataSection()
ImportDataSection()
ImportTmdbSection(db)
}
}
}
}
Loading