Skip to content

Commit

Permalink
Merge pull request #32 from AmeerAmjed/feature/sorting
Browse files Browse the repository at this point in the history
Feature/sorting
  • Loading branch information
SEAbdulbasit authored Feb 15, 2024
2 parents 692046c + 8081cf9 commit 50215c4
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 10 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,28 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import org.jetbrains.compose.resources.ExperimentalResourceApi

class ListScreenViewModel {
private val viewModelScope = CoroutineScope(Dispatchers.Main)
private val countriesApi = CountriesApiImpl()
val state = MutableStateFlow<ListScreenState>(ListScreenState.Loading)

init {
fetchCountries()
}

fun fetchCountries(sortOrder: SortOrder = SortOrder.ASCENDING) {
viewModelScope.launch(Dispatchers.Main) {
try {
val countriesList = countriesApi.getCountriesList()
val countries = getCountriesSorted(
countriesApi.getCountriesList(),
sortOrder
)
state.emit(
ListScreenState.Success(
countriesList = countriesList,
selectedCountry = countriesList.first(),
countriesList = countries,
selectedCountry = countries.first()
)
)
} catch (e: Exception) {
Expand All @@ -27,7 +35,23 @@ class ListScreenViewModel {
}
}
}
@OptIn(ExperimentalResourceApi::class)
private fun getCountriesSorted(
countries: List<Country>,
sortOrder: SortOrder
): List<Country> {
return if (sortOrder == SortOrder.ASCENDING) {
countries.sortedBy { it.name }
} else {
countries.sortedByDescending { it.name }
}.map { country ->
country.copy(
touristPlaces = country.touristPlaces.sortedBy { it.name }
)
}


}
fun onAction(actions: ListViewModelActions) {
viewModelScope.launch {
when (actions) {
Expand Down Expand Up @@ -73,6 +97,13 @@ class ListScreenViewModel {
is ListScreenState.Success -> state
}
}


}

enum class SortOrder {
ASCENDING,
DESCENDING
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
package com.example.travelapp_kmp.listing

import androidx.compose.foundation.*
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.aspectRatio
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.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Card
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.Divider
import androidx.compose.material.DropdownMenu
import androidx.compose.material.DropdownMenuItem
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.automirrored.filled.ArrowForward
Expand All @@ -17,7 +45,10 @@ import androidx.compose.runtime.MutableState
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.blur
Expand All @@ -44,8 +75,7 @@ import com.example.travelapp_kmp.style.TravelAppColors
import org.jetbrains.compose.resources.DrawableResource
import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.painterResource

//import org.jetbrains.compose.resources.resource
import travelappkmp.shared.generated.resources.Res


@OptIn(ExperimentalResourceApi::class)
Expand All @@ -61,6 +91,9 @@ internal fun MainScreen(
},
onCountrySelected = { viewMode.onAction(ListViewModelActions.OnCountrySelected(it)) },
moveToIndex = { viewMode.onAction(ListViewModelActions.MoveToIndex(it)) },
sortContent = { sortType ->
viewMode.fetchCountries(sortType)
}
)
}

Expand All @@ -70,6 +103,7 @@ internal fun MainScreenView(
onDetailsClicked: (TouristPlace) -> Unit,
onCountrySelected: (Country) -> Unit,
moveToIndex: (Int) -> Unit,
sortContent: (SortOrder) -> Unit
) {
when (val result = state.value) {
is ListScreenState.Error -> {
Expand All @@ -90,6 +124,7 @@ internal fun MainScreenView(
onDetailsClicked = onDetailsClicked,
onCountrySelected = onCountrySelected,
moveToIndex = moveToIndex,
sortContent = sortContent
)
}
}
Expand All @@ -102,6 +137,7 @@ internal fun RenderListingScreen(
onDetailsClicked: (TouristPlace) -> Unit,
onCountrySelected: (Country) -> Unit,
moveToIndex: (Int) -> Unit,
sortContent: (SortOrder) -> Unit,
) {

val listState = rememberLazyListState()
Expand Down Expand Up @@ -139,7 +175,17 @@ internal fun RenderListingScreen(

Column(modifier = Modifier.fillMaxSize().verticalScroll(rememberScrollState())) {
Column {
WeatherView(state.selectedCountry.touristPlaces[state.selectedItemIndex].images[0])
Row(
modifier = Modifier
.fillMaxWidth()
.padding(start = 16.dp, end = 16.dp, top = 64.dp),
) {
WeatherView(state.selectedCountry.touristPlaces[state.selectedItemIndex].images[0])
SortDropDownMenu(
sortContent = sortContent
)
}

ListCountryChips(
state.countriesList,
state.selectedCountry.name,
Expand Down Expand Up @@ -173,7 +219,6 @@ internal fun RenderListingScreen(
@Composable
internal fun WeatherView(drawableResource: DrawableResource) {
Row(
Modifier.fillMaxWidth().padding(start = 16.dp, end = 16.dp, top = 64.dp),
verticalAlignment = Alignment.CenterVertically
) {

Expand Down Expand Up @@ -449,4 +494,64 @@ private fun LazyListState.visibleItemsWithThreshold(percentThreshold: Float): Li
}
}
}.value
}

@OptIn(ExperimentalResourceApi::class)
@Composable
internal fun SortDropDownMenu(
sortContent: (SortOrder) -> Unit,
) {
var expanded by remember { mutableStateOf(false) }
var isSortByNameAsc by remember { mutableStateOf(true) }

Box(
modifier = Modifier.fillMaxWidth()
.wrapContentSize(Alignment.TopEnd)
) {
IconButton(onClick = { expanded = !expanded }) {
Icon(
painter = painterResource(Res.drawable.sort_icon),
contentDescription = "Sort",
modifier = Modifier.size(18.dp),
tint = Color(0xDEFFFFFF)
)
}

DropdownMenu(
modifier = Modifier.background(Color.White),
expanded = expanded,
onDismissRequest = { expanded = false }
) {
DropdownMenuItem(
modifier = Modifier.background(
if (isSortByNameAsc) Color.Gray.copy(alpha = 0.3F) else Color.Transparent
),
onClick = {
sortContent(SortOrder.ASCENDING)
expanded = false
isSortByNameAsc = true
}
) {
Text(
"A-Z",
color = Color.Black,
)
}
DropdownMenuItem(
modifier = Modifier.background(
if (!isSortByNameAsc) Color.Gray.copy(alpha = 0.3F) else Color.Transparent
),
onClick = {
sortContent(SortOrder.DESCENDING)
expanded = false
isSortByNameAsc = false
}
) {
Text(
"Z-A",
color = Color.Black,
)
}
}
}
}

0 comments on commit 50215c4

Please sign in to comment.