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
4 changes: 3 additions & 1 deletion app/src/main/java/com/patch4code/logline/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ class MainActivity : ComponentActivity() {
context = applicationContext,
klass = LoglineDatabase::class.java,
name = "loglineData.db"
).build()
)
.addMigrations(LoglineDatabase.MIGRATION_1_2)
.build()
}

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ sealed class SortOption(val label: UiText) {
data object ByAddedAsc : SortOption(UiText.StringResource(R.string.sort_by_added_asc))
data object ByTitleAsc : SortOption(UiText.StringResource(R.string.sort_by_title_asc))
data object ByTitleDesc : SortOption(UiText.StringResource(R.string.sort_by_title_desc))
data object ByNameAsc : SortOption(UiText.StringResource(R.string.sort_by_name_asc))
data object ByNameDesc : SortOption(UiText.StringResource(R.string.sort_by_name_desc))
data object ByReleaseDateDesc : SortOption(UiText.StringResource(R.string.sort_by_release_date_desc))
data object ByReleaseDateAsc : SortOption(UiText.StringResource(R.string.sort_by_release_date_asc))
data object ByRatingDesc : SortOption(UiText.StringResource(R.string.sort_by_rating_desc))
Expand All @@ -28,4 +30,4 @@ sealed class SortOption(val label: UiText) {
data object ByTimeUpdated : SortOption(UiText.StringResource(R.string.sort_by_time_updated))
data object ByListNameAsc : SortOption(UiText.StringResource(R.string.sort_by_list_name_asc))
data object ByListNameDesc : SortOption(UiText.StringResource(R.string.sort_by_list_name_desc))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import androidx.compose.material.icons.outlined.Reviews
import androidx.compose.material.icons.outlined.Search
import androidx.compose.material.icons.outlined.Settings
import androidx.compose.material.icons.outlined.WatchLater
import androidx.compose.material.icons.filled.Star
import androidx.compose.material.icons.outlined.Star
import androidx.compose.ui.graphics.vector.ImageVector
import com.patch4code.logline.R
import com.patch4code.logline.features.core.presentation.utils.UiText
Expand Down Expand Up @@ -72,6 +74,12 @@ data class DrawerNavigationItem(
unselectedIcon = Icons.Outlined.Movie,
route = Screen.MyMoviesScreen.route,
),
DrawerNavigationItem(
title = "Persons",
selectedIcon = Icons.Filled.Star,
unselectedIcon = Icons.Outlined.Star,
route = Screen.FavoritePersonsScreen.route,
),
DrawerNavigationItem(
title = UiText.StringResource(R.string.diary_text).asString(context),
selectedIcon = Icons.Filled.DateRange,
Expand Down Expand Up @@ -104,4 +112,4 @@ data class DrawerNavigationItem(
)
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ sealed class Screen(val route: String, var title: UiText) {
data object AboutScreen : Screen("about_screen", UiText.StringResource(R.string.about_text))
data object MovieScreen : Screen("movie_screen", UiText.StringResource(R.string.empty_text))
data object PersonDetailsScreen : Screen("person_details_screen",UiText.StringResource(R.string.empty_text))
data object FavoritePersonsScreen : Screen("favorite_persons_screen", UiText.DynamicString("Favorite Persons"))
data object MovieLogScreen: Screen("movie_log_screen", UiText.StringResource(R.string.movie_log_text))
data object MyMoviesScreen : Screen("my_movies_screen", UiText.StringResource(R.string.my_movies_text))
data object MoviePublicReviewsScreen: Screen("movie_public_reviews_screen", UiText.StringResource(R.string.empty_text))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,18 @@ fun Navigation(db: LoglineDatabase){
){
PersonDetailsView(
navController = navController,
db = db,
id = it.arguments?.getInt("person_id")
)
}

composable(route = Screen.FavoritePersonsScreen.route) {
com.patch4code.logline.features.person_details.presentation.screen_favorite_persons.FavoritePersonsView(
navController = navController,
db = db
)
}

composable(
route = Screen.MovieLogScreen.route + "/{movie}",
arguments = listOf(
Expand Down Expand Up @@ -254,4 +262,4 @@ fun Navigation(db: LoglineDatabase){
}
}
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.patch4code.logline.features.person_details.domain.model

import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "favorite_persons")
data class FavoritePerson(
@PrimaryKey val id: Int,
val name: String,
val profilePath: String?,
val knownForDepartment: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.patch4code.logline.features.person_details.domain.model

import com.patch4code.logline.features.core.domain.model.SortOption

object FavoritePersonsSortFilterOptions {
val sortOptions = listOf(
SortOption.ByNameAsc,
SortOption.ByNameDesc
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.patch4code.logline.features.person_details.presentation.components

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import com.patch4code.logline.R
import com.patch4code.logline.features.core.domain.model.SortOption
import com.patch4code.logline.features.core.presentation.components.base_elements.BaseDropdown
import com.patch4code.logline.features.core.presentation.components.filter_dialog.FilterApplyButton
import com.patch4code.logline.features.core.presentation.components.filter_dialog.FilterSortDropdown
import com.patch4code.logline.features.core.presentation.components.filter_dialog.FilterTopBarSection
import com.patch4code.logline.features.person_details.domain.model.FavoritePersonsSortFilterOptions

@Composable
fun FavoritePersonsSortFilterDialog(
showFilterDialog: MutableState<Boolean>,
selectedSortOption: SortOption,
selectedFilterDepartment: String?,
availableDepartments: List<String>,
onApply: (SortOption, String?) -> Unit
) {
if (!showFilterDialog.value) return

val tempSelectedSortOption = remember { mutableStateOf(selectedSortOption) }
val tempSelectedFilterDepartment = remember { mutableStateOf(selectedFilterDepartment) }

val sortOptions = FavoritePersonsSortFilterOptions.sortOptions

// Add "All" to the list of departments for the dropdown
val departmentOptions = listOf("All") + availableDepartments

Dialog(onDismissRequest = { showFilterDialog.value = false },
properties = DialogProperties(usePlatformDefaultWidth = false)
) {
Surface(
modifier = Modifier.fillMaxSize(),
shape = MaterialTheme.shapes.large,
color = MaterialTheme.colorScheme.background,
) {
Column {
FilterTopBarSection(
onCloseDialog = { showFilterDialog.value = false },
onResetClick = {
tempSelectedSortOption.value = sortOptions.first()
tempSelectedFilterDepartment.value = null
}
)

LazyColumn(modifier = Modifier.fillMaxSize().padding(16.dp).weight(1f)) {
item {
Text(stringResource(id = R.string.sort_by_text))
FilterSortDropdown(tempSelectedSortOption, sortOptions)

Spacer(modifier = Modifier.padding(8.dp))

if (availableDepartments.isNotEmpty()) {
Text("Filter by Department")
BaseDropdown(
selectedItem = tempSelectedFilterDepartment.value ?: "All",
items = departmentOptions,
labelProvider = { it },
onItemSelected = {
if (it == "All") {
tempSelectedFilterDepartment.value = null
} else {
tempSelectedFilterDepartment.value = it
}
}
)
}
}
}

FilterApplyButton {
onApply(tempSelectedSortOption.value, tempSelectedFilterDepartment.value)
showFilterDialog.value = false
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.patch4code.logline.features.person_details.presentation.screen_favorite_persons

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import coil.compose.AsyncImage
import com.patch4code.logline.R
import com.patch4code.logline.features.core.presentation.utils.MovieHelper
import com.patch4code.logline.features.navigation.domain.model.Screen
import com.patch4code.logline.features.navigation.presentation.components.topbar_providers.ProvideTopBarTitle
import com.patch4code.logline.features.person_details.domain.model.FavoritePerson
import com.patch4code.logline.room_database.LoglineDatabase

import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import com.patch4code.logline.features.navigation.presentation.components.topbar_providers.ProvideTopBarSortFilterActions
import com.patch4code.logline.features.person_details.presentation.components.FavoritePersonsSortFilterDialog

@Composable
fun FavoritePersonsView(
navController: NavController,
db: LoglineDatabase,
viewModel: FavoritePersonsViewModel = viewModel(
factory = FavoritePersonsViewModelFactory(db.favoritePersonDao)
)
) {
val showBottomSheet = remember { mutableStateOf(false) }

ProvideTopBarTitle(title = Screen.FavoritePersonsScreen.title.asString())
ProvideTopBarSortFilterActions(sortFilterOnClickAction = { showBottomSheet.value = true })

val favoritePersons by viewModel.favoritePersons.collectAsState(initial = emptyList())
val selectedSortOption by viewModel.sortOption.collectAsState()
val selectedFilterDepartment by viewModel.filterDepartment.collectAsState()
val availableDepartments by viewModel.availableDepartments.collectAsState()

LazyColumn(modifier = Modifier.fillMaxSize().padding(16.dp)) {
items(favoritePersons) { person ->
FavoritePersonItem(person) {
navController.navigate(Screen.PersonDetailsScreen.route + "/${person.id}")
}
Spacer(modifier = Modifier.height(8.dp))
}
}

FavoritePersonsSortFilterDialog(
showFilterDialog = showBottomSheet,
selectedSortOption = selectedSortOption,
selectedFilterDepartment = selectedFilterDepartment,
availableDepartments = availableDepartments,
onApply = { sortOption, filterDepartment ->
viewModel.setSortOption(sortOption)
viewModel.setFilterDepartment(filterDepartment)
}
)
}

@Composable
fun FavoritePersonItem(person: FavoritePerson, onClick: () -> Unit) {
Card(
modifier = Modifier
.fillMaxWidth()
.clickable { onClick() },
elevation = CardDefaults.cardElevation(defaultElevation = 2.dp)
) {
Row(
modifier = Modifier.padding(8.dp),
verticalAlignment = Alignment.CenterVertically
) {
Card(
modifier = Modifier
.height(100.dp)
.width(66.dp),
colors = CardDefaults.cardColors(containerColor = Color.Transparent)
) {
AsyncImage(
model = MovieHelper.processPosterUrl(person.profilePath),
contentDescription = person.name,
error = painterResource(id = R.drawable.movie_poster_placeholder)
)
}
Spacer(modifier = Modifier.width(16.dp))
Column {
Text(text = person.name, style = MaterialTheme.typography.titleLarge)
Text(text = person.knownForDepartment, style = MaterialTheme.typography.bodyMedium)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.patch4code.logline.features.person_details.presentation.screen_favorite_persons

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.person_details.domain.model.FavoritePerson
import com.patch4code.logline.room_database.FavoritePersonDao
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn

class FavoritePersonsViewModel(
private val favoritePersonDao: FavoritePersonDao
) : ViewModel() {

private val _sortOption = MutableStateFlow<SortOption>(SortOption.ByNameAsc)
val sortOption: StateFlow<SortOption> = _sortOption

private val _filterDepartment = MutableStateFlow<String?>(null)
val filterDepartment: StateFlow<String?> = _filterDepartment

val favoritePersons: StateFlow<List<FavoritePerson>> = combine(
favoritePersonDao.getAllFavoritePersons(),
_sortOption,
_filterDepartment
) { persons, sort, department ->
val filtered = if (department != null) {
persons.filter { it.knownForDepartment == department }
} else {
persons
}

when (sort) {
SortOption.ByNameAsc -> filtered.sortedBy { it.name }
SortOption.ByNameDesc -> filtered.sortedByDescending { it.name }
else -> filtered.sortedBy { it.name }
}
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList())

val availableDepartments: StateFlow<List<String>> = favoritePersonDao.getAllFavoritePersons()
.map { persons -> persons.map { it.knownForDepartment }.distinct().sorted() }
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList())

fun setSortOption(option: SortOption) {
_sortOption.value = option
}

fun setFilterDepartment(department: String?) {
_filterDepartment.value = department
}
}

class FavoritePersonsViewModelFactory(private val favoritePersonDao: FavoritePersonDao) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(FavoritePersonsViewModel::class.java)) {
@Suppress("UNCHECKED_CAST")
return FavoritePersonsViewModel(favoritePersonDao) as T
}
throw IllegalArgumentException("Unknown ViewModel class")
}
}
Loading