-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from manununhez/epic/45-favorites-or-saved-ui-…
…optional epic/45-favorites-saved-UI
- Loading branch information
Showing
93 changed files
with
1,886 additions
and
284 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package com.manuelnunez.apps | ||
|
||
import android.content.res.Configuration | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.WindowInsets | ||
import androidx.compose.foundation.layout.WindowInsetsSides | ||
import androidx.compose.foundation.layout.consumeWindowInsets | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.only | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.safeDrawing | ||
import androidx.compose.foundation.layout.windowInsetsPadding | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.runtime.Stable | ||
import androidx.compose.runtime.State | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalConfiguration | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.navigation.NavController | ||
import androidx.navigation.NavDestination.Companion.hierarchy | ||
import androidx.navigation.compose.rememberNavController | ||
import com.manuelnunez.apps.core.ui.component.MainGradientBackground | ||
import com.manuelnunez.apps.core.ui.component.MainNavigationBar | ||
import com.manuelnunez.apps.core.ui.component.MainNavigationBarItem | ||
import com.manuelnunez.apps.core.ui.component.MainNavigationRail | ||
import com.manuelnunez.apps.core.ui.component.MainNavigationRailItem | ||
import com.manuelnunez.apps.navigation.MainNavigation | ||
import com.manuelnunez.apps.navigation.RootScreen | ||
import com.manuelnunez.apps.navigation.mainDestinations | ||
import com.manuelnunez.apps.navigation.navigateToRootScreen | ||
|
||
@Composable | ||
fun MainApp() { | ||
val navController = rememberNavController() | ||
val currentSelectedScreen by navController.currentScreenAsState() | ||
val shouldShowBottomBar = | ||
LocalConfiguration.current.orientation == Configuration.ORIENTATION_PORTRAIT | ||
|
||
Scaffold( | ||
bottomBar = { | ||
if (shouldShowBottomBar) { | ||
MainBottomNavBar(navController, currentSelectedScreen) | ||
} | ||
}) { paddingValues -> | ||
Row( | ||
Modifier.fillMaxSize() | ||
.padding(paddingValues) | ||
.consumeWindowInsets(paddingValues) | ||
.windowInsetsPadding( | ||
WindowInsets.safeDrawing.only( | ||
WindowInsetsSides.Horizontal, | ||
), | ||
), | ||
) { | ||
if (!shouldShowBottomBar) { | ||
MainNavRail(navController, currentSelectedScreen) | ||
} | ||
MainGradientBackground { MainNavigation(navController) } | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
private fun MainBottomNavBar(navController: NavController, currentSelectedScreen: RootScreen) { | ||
MainNavigationBar { | ||
mainDestinations().forEach { rootScreen -> | ||
MainNavigationBarItem( | ||
selected = currentSelectedScreen == rootScreen, | ||
onClick = { navController.navigateToRootScreen(rootScreen) }, | ||
icon = { | ||
Icon( | ||
imageVector = rootScreen.unselectedIcon, | ||
contentDescription = null, | ||
) | ||
}, | ||
selectedIcon = { | ||
Icon( | ||
imageVector = rootScreen.selectedIcon, | ||
contentDescription = null, | ||
) | ||
}, | ||
label = { Text(stringResource(rootScreen.iconTextId)) }) | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
private fun MainNavRail(navController: NavController, currentSelectedScreen: RootScreen) { | ||
MainNavigationRail { | ||
mainDestinations().forEach { rootScreen -> | ||
MainNavigationRailItem( | ||
selected = currentSelectedScreen == rootScreen, | ||
onClick = { navController.navigateToRootScreen(rootScreen) }, | ||
icon = { | ||
Icon( | ||
imageVector = rootScreen.unselectedIcon, | ||
contentDescription = null, | ||
) | ||
}, | ||
selectedIcon = { | ||
Icon( | ||
imageVector = rootScreen.selectedIcon, | ||
contentDescription = null, | ||
) | ||
}, | ||
label = { Text(stringResource(rootScreen.iconTextId)) }) | ||
} | ||
} | ||
} | ||
|
||
@Stable | ||
@Composable | ||
private fun NavController.currentScreenAsState(): State<RootScreen> { | ||
val selectedItem = remember { mutableStateOf(RootScreen.HOME) } | ||
DisposableEffect(key1 = this) { | ||
val listener = | ||
NavController.OnDestinationChangedListener { _, destination, _ -> | ||
when { | ||
destination.hierarchy.any { it.route == RootScreen.HOME.route } -> { | ||
selectedItem.value = RootScreen.HOME | ||
} | ||
destination.hierarchy.any { it.route == RootScreen.FAVORITES.route } -> { | ||
selectedItem.value = RootScreen.FAVORITES | ||
} | ||
} | ||
} | ||
|
||
addOnDestinationChangedListener(listener) | ||
|
||
onDispose { removeOnDestinationChangedListener(listener) } | ||
} | ||
return selectedItem | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
<resources> | ||
<string name="app_name">Purrfect Pics</string> | ||
<string name="home_destination_title">Home</string> | ||
<string name="favorites_destination_title">Favorites</string> | ||
</resources> |
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
49 changes: 49 additions & 0 deletions
49
...ta/src/main/kotlin/com/manuelnunez/apps/core/data/datasource/local/FavoritesDataSource.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,49 @@ | ||
package com.manuelnunez.apps.core.data.datasource.local | ||
|
||
import androidx.datastore.core.DataStore | ||
import com.manuelnunez.apps.core.datastore.proto.Item | ||
import com.manuelnunez.apps.core.datastore.proto.ItemList | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
import javax.inject.Inject | ||
import com.manuelnunez.apps.core.domain.model.Item as ItemModel | ||
|
||
class FavoritesDataSource @Inject constructor(private val itemDataStore: DataStore<ItemList>) { | ||
val favorites: Flow<List<ItemModel>> = | ||
itemDataStore.data.map { | ||
it.itemsList.map { item -> | ||
ItemModel(item.photoId, item.imageUrl, item.thumbnailUrl, item.description) | ||
} | ||
} | ||
|
||
suspend fun addItemToFavorites(newItem: ItemModel) { | ||
itemDataStore.updateData { itemList -> | ||
val updatedItems = | ||
itemList | ||
.toBuilder() | ||
.addItems( | ||
Item.newBuilder() | ||
.setPhotoId(newItem.photoId) | ||
.setImageUrl(newItem.imageUrl) | ||
.setThumbnailUrl(newItem.thumbnailUrl) | ||
.setDescription(newItem.description) | ||
.build()) | ||
.build() | ||
updatedItems | ||
} | ||
} | ||
|
||
suspend fun removeItemFromFavorites(itemPhotoId: String) { | ||
itemDataStore.updateData { itemList -> | ||
val updatedItems = | ||
itemList | ||
.toBuilder() | ||
.removeItems(itemList.itemsList.indexOfFirst { it.photoId == itemPhotoId }) | ||
.build() | ||
updatedItems | ||
} | ||
} | ||
|
||
fun isItemFavorite(itemPhotoId: String): Flow<Boolean> = | ||
favorites.map { it.any { item -> item.photoId == itemPhotoId } } | ||
} |
Oops, something went wrong.