Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add drag to delete feature & handle the success and duplicated favorites scenarios. #11

Merged
merged 5 commits into from
Oct 21, 2024
Merged
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
72 changes: 58 additions & 14 deletions app/src/main/java/com/nabilbdev/searchflight/SearchFlightApp.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
package com.nabilbdev.searchflight

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import com.nabilbdev.searchflight.data.local.entity.Favorite
Expand All @@ -22,6 +33,7 @@ import com.nabilbdev.searchflight.ui.screens.search.MySearchBar
import com.nabilbdev.searchflight.ui.screens.search.SearchUiState
import com.nabilbdev.searchflight.ui.screens.search.SearchViewModel
import com.nabilbdev.searchflight.ui.screens.search.SelectFiltersUiState
import com.nabilbdev.searchflight.utils.DraggableScreen

@Composable
fun SearchFlightApp() {
Expand All @@ -43,7 +55,19 @@ fun SearchFlightApp() {
val favoriteUiState: FavoriteUiState =
favoriteVM.favoriteUiState.collectAsStateWithLifecycle().value

val snackBarHostState = remember { SnackbarHostState() }

Scaffold(
snackbarHost = {
Box(
modifier = Modifier
.fillMaxSize()
.wrapContentSize(Alignment.BottomEnd)
.padding(4.dp)
) {
SnackbarHost(hostState = snackBarHostState)
}
},
topBar = {
MySearchBar(
query = searchUiState.searchQuery,
Expand All @@ -63,13 +87,23 @@ fun SearchFlightApp() {
when (routeUiState.airportResultSelected) {
true -> {
RouteScreen(
favoriteStatus = favoriteUiState
.favoriteStatus,
fromAirport = routeUiState.fromAirport,
toAirport = routeUiState.toAirport,
otherAirports = routeUiState.otherAirports,
onHideBottomSheet = routeVM::hideSelectionBottomSheet,
onArrivalAirportSelected = routeVM::selectArrivalAirport,
saveToFavoriteSelected = routeFavButtonUiState.saveToFavoriteSelected,
isFavButtonDisabled = routeFavButtonUiState.isFavoriteDisabled,

clearMessageAndButtonSelection = {
favoriteVM.clearFavoriteStatusAndMessage()
},
onSaveToFavoriteClicked = { favorite ->
favoriteVM.saveToFavorites(favorite)
routeVM.onSaveToFavoriteClicked()
},
onHideBottomSheet = routeVM::hideSelectionBottomSheet,
onArrivalAirportSelected = routeVM::selectArrivalAirport

onSaveToFavoriteClicked = { favorite ->
favoriteVM.saveToFavorites(favorite)
routeVM.onSaveToFavoriteClicked()
Expand All @@ -78,17 +112,27 @@ fun SearchFlightApp() {
}

false -> {
/*HomeScreen(
modifier = Modifier.padding(innerPadding),
popularCitiesAirports = homeUiState.popularCityAirports,
isLoadingAirports = homeUiState.isLoadingPopularCityAirports
)*/
FavoriteScreen(
modifier = Modifier.padding(innerPadding),
statusMessage = favoriteUiState.statusMessage,
favoriteRoutes = favoriteUiState.favoriteAirports,
viewModel = favoriteVM
)
Column(
modifier = Modifier
.padding(innerPadding)
) {
HomeScreen(
popularCitiesAirports = homeUiState.popularCityAirports,
isLoadingAirports = homeUiState.isLoadingPopularCityAirports,
)
Spacer(modifier = Modifier.height(8.dp))
DraggableScreen(
modifier = Modifier.fillMaxSize()
) {
FavoriteScreen(
deletedOrNoFavoriteStatusMessage = favoriteUiState
.deletedOrNoFavoriteStatusMessage,
favoriteRoutes = favoriteUiState.favoriteAirports,
viewModel = favoriteVM,
snackBarHostState = snackBarHostState
)
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ interface FavoriteDAO {

@Query("SELECT * FROM favorite")
fun getAllFavoriteAirports(): Flow<List<Favorite>>

@Query("SELECT * FROM favorite WHERE departure_code = :departureCode AND destination_code = :destinationCode")
fun getFavoriteByDepartureCodeAndDestinationCode(
departureCode: String, destinationCode: String
): Flow<Favorite>
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ interface SearchFlightRepository {

fun getAllFavoriteAirportsStream(): Flow<List<Favorite>>

fun getFavoriteByDepartureCodeAndDestinationCodeStream(
departureCode: String,
destinationCode: String
): Flow<Favorite?>

suspend fun insertFavoriteAirport(favorite: Favorite)

suspend fun deleteFavoriteAirport(favorite: Favorite)
Expand Down Expand Up @@ -53,6 +58,14 @@ class OfflineSearchFlightRepository(
override fun getAllFavoriteAirportsStream(): Flow<List<Favorite>> =
favoriteDAO.getAllFavoriteAirports()

override fun getFavoriteByDepartureCodeAndDestinationCodeStream(
departureCode: String,
destinationCode: String
): Flow<Favorite?> = favoriteDAO.getFavoriteByDepartureCodeAndDestinationCode(
departureCode = departureCode,
destinationCode = destinationCode
)

override suspend fun insertFavoriteAirport(favorite: Favorite) =
favoriteDAO.insert(favorite)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@ import androidx.compose.animation.fadeOut
import androidx.compose.foundation.layout.Box
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.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand All @@ -23,7 +19,6 @@ import com.nabilbdev.searchflight.data.local.entity.Airport

@Composable
fun CommonAirportVerticalGrid(
titleContent: String,
airportList: List<Airport>,
imageVector: ImageVector,
modifier: Modifier = Modifier,
Expand All @@ -36,7 +31,6 @@ fun CommonAirportVerticalGrid(
}
AnimatedVisibility(visible = !isLoadingAirports, enter = fadeIn()) {
CustomVerticalGrid(
titleContent = titleContent,
airportList = airportList,
imageVector = imageVector
)
Expand All @@ -48,14 +42,11 @@ fun CommonAirportVerticalGrid(
*/
@Composable
fun CustomVerticalGrid(
titleContent: String,
airportList: List<Airport>,
imageVector: ImageVector,
modifier: Modifier = Modifier
) {
Column {
Text(text = titleContent, style = MaterialTheme.typography.titleLarge)
Spacer(modifier = modifier.height(8.dp))
airportList.chunked(2).forEach { halfList ->
Row(modifier = modifier.fillMaxWidth()) {
halfList.forEach { airport ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,161 @@
package com.nabilbdev.searchflight.ui.screens.favorite

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.slideInVertically
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.twotone.Star
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.nabilbdev.searchflight.data.local.entity.Airport
import com.nabilbdev.searchflight.ui.components.FavoriteRouteTicket
import com.nabilbdev.searchflight.utils.DragTarget
import com.nabilbdev.searchflight.utils.DropItem
import kotlinx.coroutines.delay

@Composable
fun FavoriteScreen(
favoriteRoutes: Map<Airport, Airport>,
statusMessage: String?,
favoriteRoutes: List<Pair<Airport, Airport>>,
deletedOrNoFavoriteStatusMessage: String?,
viewModel: FavoriteViewModel,
snackBarHostState: SnackbarHostState,
modifier: Modifier = Modifier
) {

when {
favoriteRoutes.isEmpty() -> {
Box(
modifier.fillMaxSize(),
contentAlignment = Alignment.Center
Column(
modifier
.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(text = statusMessage ?: "Loading...")
Icon(
imageVector = Icons.TwoTone.Star,
contentDescription = null,
modifier = Modifier.fillMaxSize(0.5f)
)
deletedOrNoFavoriteStatusMessage?.let {
Text(text = it)
}
}
}

else -> {
LazyColumn(
modifier = modifier.padding(top = 8.dp),
verticalArrangement = Arrangement.SpaceEvenly

Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = modifier
.fillMaxSize()
) {
items(favoriteRoutes.entries.toList()) { entry ->
FavoriteRouteTicket(
fromAirport = entry.key,
toAirport = entry.value
Text(
text = "Your Favorite Routes",
style = MaterialTheme.typography.titleLarge,
modifier = Modifier.padding(
start = 8.dp,
top = 8.dp,
bottom = 4.dp
)
)
LazyRow {
items(favoriteRoutes) { favoriteRouteItem ->
DragTarget(
dataToDrop = favoriteRouteItem,
viewModel = viewModel
) {
FavoriteRouteTicket(
fromAirport = favoriteRouteItem.first,
toAirport = favoriteRouteItem.second,
modifier = Modifier
.width(325.dp)
)
}
}
}
Spacer(modifier = Modifier.height(16.dp))
BinDropItem(
viewModel = viewModel,
statusMessage = deletedOrNoFavoriteStatusMessage,
snackBarHostState = snackBarHostState
)
}
}
}
}

@Composable
fun BinDropItem(
viewModel: FavoriteViewModel,
statusMessage: String?,
snackBarHostState: SnackbarHostState
) {

LaunchedEffect(key1 = statusMessage) {
statusMessage?.let {
snackBarHostState.showSnackbar(statusMessage)
delay(3000)
viewModel.clearFavoriteStatusAndMessage()
}
}

AnimatedVisibility(
visible = viewModel.isCurrentlyDragging,
enter = slideInVertically(initialOffsetY = { it })
) {
DropItem<Pair<Airport, Airport>>(
modifier = Modifier.size(50.dp)
) { isInBound, favoriteRouteItem ->
if (isInBound && favoriteRouteItem != null) {
LaunchedEffect(key1 = favoriteRouteItem) {
viewModel.deleteFromFavorites(favoriteRouteItem)
}
}
if (isInBound) {
BinIconDropItem(iconColor = Color.Red)
} else {
BinIconDropItem(iconColor = Color.DarkGray)
}
}
}
}

@Composable
fun BinIconDropItem(
iconColor: Color,
modifier: Modifier = Modifier
) {
Box(
modifier = modifier
.fillMaxSize()
.background(
iconColor.copy(0.3f), MaterialTheme.shapes.medium
),
contentAlignment = Alignment.Center
) {
Icon(
imageVector = Icons.Filled.Delete,
contentDescription = "Delete a favorite route!",
tint = iconColor,
)
}
}
Loading
Loading