From 6c63450d2f68ea6b8d705f7e5f12e7d2f1ec9663 Mon Sep 17 00:00:00 2001 From: Kailash Sharma Date: Sun, 24 Sep 2023 03:57:16 +0530 Subject: [PATCH] Initiate Tags in CollectWaste --- .../com/collectwaste/CollectWasteInfo.kt | 4 +- .../com/collectwaste/ListOfCollectWaste.kt | 274 ++++++++++-------- .../com/location/LocationViewModel.kt | 2 + .../com/navigation/NavigationController.kt | 1 - .../com/reportwaste/ReportWaste.kt | 8 +- .../java/app/waste2wealth/com/tags/Tag.kt | 5 + .../app/waste2wealth/com/tags/TagsScreen.kt | 9 +- 7 files changed, 177 insertions(+), 126 deletions(-) diff --git a/app/src/main/java/app/waste2wealth/com/collectwaste/CollectWasteInfo.kt b/app/src/main/java/app/waste2wealth/com/collectwaste/CollectWasteInfo.kt index 0060125..ac39264 100644 --- a/app/src/main/java/app/waste2wealth/com/collectwaste/CollectWasteInfo.kt +++ b/app/src/main/java/app/waste2wealth/com/collectwaste/CollectWasteInfo.kt @@ -88,6 +88,7 @@ fun CollectWasteInfo( viewModel.wastePhoto.value = "" viewModel.theirLongitude.value = 0.0 viewModel.theirLatitude.value = 0.0 + viewModel.tags.value = listOf() navController.popBackStack() } LaunchedEffect(key1 = Unit) { @@ -139,6 +140,7 @@ fun CollectWasteInfo( time = viewModel.time.value, isCollectedInfo = true, isEllipsis = false, + tags = viewModel.tags.value, onCollected = { val gmmIntentUri = Uri.parse( @@ -149,7 +151,7 @@ fun CollectWasteInfo( mapIntent.setPackage("com.google.android.apps.maps") context.startActivity(mapIntent) - } + }, ) Spacer(modifier = Modifier.height(30.dp)) var imageUrlState by remember { diff --git a/app/src/main/java/app/waste2wealth/com/collectwaste/ListOfCollectWaste.kt b/app/src/main/java/app/waste2wealth/com/collectwaste/ListOfCollectWaste.kt index 2707134..b4b248f 100644 --- a/app/src/main/java/app/waste2wealth/com/collectwaste/ListOfCollectWaste.kt +++ b/app/src/main/java/app/waste2wealth/com/collectwaste/ListOfCollectWaste.kt @@ -3,6 +3,7 @@ package app.waste2wealth.com.collectwaste import android.Manifest import androidx.compose.foundation.background import androidx.compose.foundation.clickable +import androidx.compose.foundation.interaction.MutableInteractionSource import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.PaddingValues @@ -15,6 +16,8 @@ import androidx.compose.foundation.layout.offset import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.LazyRow +import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.itemsIndexed import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.BottomDrawerValue @@ -47,10 +50,14 @@ import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.navigation.NavHostController +import app.waste2wealth.com.bottombar.items +import app.waste2wealth.com.firebase.firestore.ProfileInfo import app.waste2wealth.com.firebase.firestore.WasteItem import app.waste2wealth.com.location.LocationViewModel import app.waste2wealth.com.maps.MapScreen import app.waste2wealth.com.navigation.Screens +import app.waste2wealth.com.tags.Tag +import app.waste2wealth.com.tags.TagItem import app.waste2wealth.com.ui.theme.CardColor import app.waste2wealth.com.ui.theme.CardTextColor import app.waste2wealth.com.ui.theme.appBackground @@ -77,15 +84,8 @@ fun CollectWaste( navController: NavHostController, viewModel: LocationViewModel ) { - val permissionState = rememberMultiplePermissionsState( - permissions = listOf( - Manifest.permission.ACCESS_FINE_LOCATION, - Manifest.permission.ACCESS_COARSE_LOCATION, - ) - ) - val permissionDrawerState = rememberBottomDrawerState(BottomDrawerValue.Closed) - val gesturesEnabled by remember { derivedStateOf { permissionDrawerState.isOpen } } var allWastes by remember { mutableStateOf?>(null) } + var profileInfo by remember { mutableStateOf?>(null) } LaunchedEffect(key1 = Unit) { viewModel.getPlaces() } @@ -96,140 +96,152 @@ fun CollectWaste( allWastes = values?.getListOfObjects() }) { - Column( - modifier = Modifier - .fillMaxSize() - .background(appBackground) - ) { - Row( + JetFirestore(path = { + collection("ProfileInfo") + }, onRealtimeCollectionFetch = { values, _ -> + profileInfo = values?.getListOfObjects() + }) { + Column( modifier = Modifier - .fillMaxWidth() - .padding(top = 30.dp, start = 0.dp), - horizontalArrangement = Arrangement.Start + .fillMaxSize() + .background(appBackground) ) { - Icon( - imageVector = Icons.Filled.ArrowBackIos, - contentDescription = "", - tint = textColor, - modifier = Modifier - .padding(start = 15.dp) - .size(25.dp) - .clickable { - navController.popBackStack() - } - ) Row( modifier = Modifier .fillMaxWidth() - .offset(x = (-10).dp), - horizontalArrangement = Arrangement.Center + .padding(top = 30.dp, start = 0.dp), + horizontalArrangement = Arrangement.Start ) { - AutoResizedText( - text = "Collect Waste", - color = textColor, - fontFamily = monteBold, - fontSize = 25.sp + Icon( + imageVector = Icons.Filled.ArrowBackIos, + contentDescription = "", + tint = textColor, + modifier = Modifier + .padding(start = 15.dp) + .size(25.dp) + .clickable { + navController.popBackStack() + } ) + Row( + modifier = Modifier + .fillMaxWidth() + .offset(x = (-10).dp), + horizontalArrangement = Arrangement.Center + ) { + AutoResizedText( + text = "Collect Waste", + color = textColor, + fontFamily = monteBold, + fontSize = 25.sp + ) + } } - } - val cList = listOf("List View", "Map View (Beta)") - var tabIndex by remember { mutableStateOf(0) } - Column( - modifier = Modifier - .fillMaxWidth() - .padding(start = 35.dp, end = 35.dp) - ) { - TabRow( - selectedTabIndex = tabIndex, - backgroundColor = appBackground, - contentColor = textColor, - divider = { - TabRowDefaults.Divider( - color = Color(0xFFF37952), - thickness = 1.dp - ) - }, + val cList = listOf("List View", "Map View (Beta)") + var tabIndex by remember { mutableStateOf(0) } + Column( + modifier = Modifier + .fillMaxWidth() + .padding(start = 35.dp, end = 35.dp) ) { - cList.forEachIndexed { index, title -> - Tab(text = { - AutoResizedText( - title, - softWrap = false, - fontSize = 13.sp, + TabRow( + selectedTabIndex = tabIndex, + backgroundColor = appBackground, + contentColor = textColor, + divider = { + TabRowDefaults.Divider( + color = Color(0xFFF37952), + thickness = 1.dp ) }, - selected = tabIndex == index, - onClick = { tabIndex = index } - ) + ) { + cList.forEachIndexed { index, title -> + Tab(text = { + AutoResizedText( + title, + softWrap = false, + fontSize = 13.sp, + ) + }, + selected = tabIndex == index, + onClick = { tabIndex = index } + ) + } + } } - - } - if (tabIndex == 0) { - Spacer(modifier = Modifier.height(30.dp)) - if (allWastes != null) { - LazyColumn( - contentPadding = PaddingValues( - bottom = 150.dp, - top = 40.dp - ) - ) { - allWastes = allWastes?.sortedBy { - distance( - viewModel.latitude, - viewModel.longitude, - it.latitude, - it.longitude + if (tabIndex == 0) { + Spacer(modifier = Modifier.height(30.dp)) + if (allWastes != null) { + LazyColumn( + contentPadding = PaddingValues( + bottom = 150.dp, + top = 40.dp ) - } - itemsIndexed(allWastes ?: emptyList()) { index, wasteItem -> - WasteItemCard( - locationNo = "Location ${index + 1}", - address = wasteItem.address, - distance = "${ - convertDistance( - distance( - viewModel.latitude, - viewModel.longitude, - wasteItem.latitude, - wasteItem.longitude + ) { + allWastes = allWastes?.sortedBy { + distance( + viewModel.latitude, + viewModel.longitude, + it.latitude, + it.longitude + ) + } + itemsIndexed(allWastes ?: emptyList()) { index, wasteItem -> + WasteItemCard( + locationNo = "Location ${index + 1}", + address = wasteItem.address, + distance = "${ + convertDistance( + distance( + viewModel.latitude, + viewModel.longitude, + wasteItem.latitude, + wasteItem.longitude + ) ) - ) - } away", - time = getTimeAgo(wasteItem.timeStamp), - ) { - viewModel.locationNo.value = "Location ${index + 1}" - viewModel.address.value = wasteItem.address - viewModel.distance.value = "${ - convertDistance( - distance( - viewModel.latitude, - viewModel.longitude, - wasteItem.latitude, - wasteItem.longitude + } away", + time = getTimeAgo(wasteItem.timeStamp), + tags = wasteItem.tag.map { + it.mapWithTips() + }, + ) { + viewModel.locationNo.value = "Location ${index + 1}" + viewModel.address.value = wasteItem.address + viewModel.distance.value = "${ + convertDistance( + distance( + viewModel.latitude, + viewModel.longitude, + wasteItem.latitude, + wasteItem.longitude + ) ) - ) - } away" - viewModel.time.value = getTimeAgo(wasteItem.timeStamp) - viewModel.wastePhoto.value = wasteItem.imagePath - viewModel.theirLatitude.value = wasteItem.latitude - viewModel.theirLongitude.value = wasteItem.longitude - println("Collected time ${viewModel.time.value}") - navController.navigate(Screens.CollectWasteInfo.route) - } + } away" + viewModel.time.value = getTimeAgo(wasteItem.timeStamp) + viewModel.wastePhoto.value = wasteItem.imagePath + viewModel.theirLatitude.value = wasteItem.latitude + viewModel.theirLongitude.value = wasteItem.longitude + viewModel.tags.value = wasteItem.tag.map { + it.mapWithTips() + } + println("Collected time ${viewModel.time.value}") + navController.navigate(Screens.CollectWasteInfo.route) + } + } } } + } else { + MapScreen( + paddingValues = paddingValues, + viewModel = viewModel, + ) } - } else { - MapScreen( - paddingValues = paddingValues, - viewModel = viewModel, - ) - } + } } @@ -239,6 +251,7 @@ fun CollectWaste( @Composable fun WasteItemCard( + tags: List = emptyList(), locationNo: String, address: String, distance: String, @@ -300,6 +313,29 @@ fun WasteItemCard( overflow = if (isEllipsis) TextOverflow.Ellipsis else TextOverflow.Visible ) } + if (tags.isNotEmpty()) { + AutoResizedText( + text = "Tags", + color = textColor, + fontSize = 15.sp, + fontFamily = monteSB, + modifier = Modifier.padding(start = 10.dp) + ) + Spacer(modifier = Modifier.height(10.dp)) + LazyRow( + contentPadding = PaddingValues(10.dp), + modifier = Modifier.fillMaxWidth() + ) { + items(tags) { tag -> + TagItem( + item = tag, + modifier = Modifier, + isSelected = false + ) + } + } + } + Row( modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.Bottom diff --git a/app/src/main/java/app/waste2wealth/com/location/LocationViewModel.kt b/app/src/main/java/app/waste2wealth/com/location/LocationViewModel.kt index 710552d..36cf7dc 100644 --- a/app/src/main/java/app/waste2wealth/com/location/LocationViewModel.kt +++ b/app/src/main/java/app/waste2wealth/com/location/LocationViewModel.kt @@ -11,6 +11,7 @@ import androidx.lifecycle.viewModelScope import app.waste2wealth.com.ktorClient.Resource import app.waste2wealth.com.ktorClient.repository.PlacesRepository import app.waste2wealth.com.rewards.Level +import app.waste2wealth.com.tags.Tag import com.google.android.gms.location.LocationServices import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.MutableStateFlow @@ -42,6 +43,7 @@ class LocationViewModel @Inject constructor( var address = mutableStateOf("") var distance = mutableStateOf("") var time = mutableStateOf("") + var tags = mutableStateOf(listOf()) var wastePhoto = mutableStateOf("") var rewardImage = mutableStateOf("") var rewardTitle = mutableStateOf("") diff --git a/app/src/main/java/app/waste2wealth/com/navigation/NavigationController.kt b/app/src/main/java/app/waste2wealth/com/navigation/NavigationController.kt index 14a12ea..fa80aa7 100644 --- a/app/src/main/java/app/waste2wealth/com/navigation/NavigationController.kt +++ b/app/src/main/java/app/waste2wealth/com/navigation/NavigationController.kt @@ -154,7 +154,6 @@ fun NavigationController( email = email.value, name = name.value, pfp = profile.value, - reportWasteViewModel = reportWasteViewModel ) } composable(Screens.CollectWasteLists.route) { diff --git a/app/src/main/java/app/waste2wealth/com/reportwaste/ReportWaste.kt b/app/src/main/java/app/waste2wealth/com/reportwaste/ReportWaste.kt index 714ba2b..f4acf35 100644 --- a/app/src/main/java/app/waste2wealth/com/reportwaste/ReportWaste.kt +++ b/app/src/main/java/app/waste2wealth/com/reportwaste/ReportWaste.kt @@ -70,6 +70,7 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateListOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope @@ -100,6 +101,7 @@ import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.compose.ui.window.Dialog import androidx.core.app.ActivityOptionsCompat +import androidx.hilt.navigation.compose.hiltViewModel import androidx.navigation.NavHostController import app.waste2wealth.com.R import app.waste2wealth.com.bottombar.BottomBar @@ -114,6 +116,7 @@ import app.waste2wealth.com.rewards.levels import app.waste2wealth.com.tags.TagItem import app.waste2wealth.com.tags.TagWithoutTips import app.waste2wealth.com.tags.TagsScreen +import app.waste2wealth.com.tags.wasteGroups import app.waste2wealth.com.ui.theme.CardColor import app.waste2wealth.com.ui.theme.CardTextColor import app.waste2wealth.com.ui.theme.appBackground @@ -132,6 +135,7 @@ import com.google.firebase.storage.FirebaseStorage import com.jet.firestore.JetFirestore import com.jet.firestore.getListOfObjects import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.replay import kotlinx.coroutines.launch import java.io.ByteArrayOutputStream import kotlin.math.roundToInt @@ -148,7 +152,7 @@ fun ReportWaste( email: String, name: String, pfp: String, - reportWasteViewModel: ReportWasteViewModel + reportWasteViewModel: ReportWasteViewModel = hiltViewModel() ) { val permissionState = rememberMultiplePermissionsState( permissions = listOf( @@ -237,7 +241,7 @@ fun ReportWaste( modalSheetState.hide() } else { // Show the bottom sheet - modalSheetState.show() + navController.popBackStack() } } } diff --git a/app/src/main/java/app/waste2wealth/com/tags/Tag.kt b/app/src/main/java/app/waste2wealth/com/tags/Tag.kt index f38aa6a..058054c 100644 --- a/app/src/main/java/app/waste2wealth/com/tags/Tag.kt +++ b/app/src/main/java/app/waste2wealth/com/tags/Tag.kt @@ -20,6 +20,11 @@ data class TagWithoutTips( val image: Int, ) { constructor() : this("", 0) + + fun mapWithTips() = Tag( + name = name, + image = image + ) } data class Groups( diff --git a/app/src/main/java/app/waste2wealth/com/tags/TagsScreen.kt b/app/src/main/java/app/waste2wealth/com/tags/TagsScreen.kt index a625452..b9bbcd0 100644 --- a/app/src/main/java/app/waste2wealth/com/tags/TagsScreen.kt +++ b/app/src/main/java/app/waste2wealth/com/tags/TagsScreen.kt @@ -43,6 +43,7 @@ import androidx.compose.material.TextFieldDefaults import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Search import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.remember @@ -64,6 +65,8 @@ import app.waste2wealth.com.ui.theme.appBackground import app.waste2wealth.com.ui.theme.monteBold import app.waste2wealth.com.ui.theme.textColor import app.waste2wealth.com.utils.AutoResizedText +import kotlinx.coroutines.flow.onCompletion +import kotlinx.coroutines.flow.retry @OptIn(ExperimentalFoundationApi::class, ExperimentalAnimationApi::class) @Composable @@ -71,7 +74,7 @@ fun TagsScreen(reportWasteViewModel: ReportWasteViewModel) { val searchText by reportWasteViewModel.searchText.collectAsState() val tags by reportWasteViewModel.tags.collectAsState() val isSearching by reportWasteViewModel.isSearching.collectAsState() - val seconds by reportWasteViewModel.tagsSearch.collectAsState(initial = "00") + val seconds by reportWasteViewModel.tagsSearch.collectAsState(initial = "") Column( modifier = Modifier @@ -124,7 +127,7 @@ fun TagsScreen(reportWasteViewModel: ReportWasteViewModel) { animationSpec = tween(durationMillis = 500) ) + fadeOut() ) { - if (reportWasteViewModel.tagsList.isNotEmpty()) { + if (reportWasteViewModel.tagsList.size > 0) { Row(verticalAlignment = Alignment.CenterVertically) { Image( painter = painterResource(id = reportWasteViewModel.tagsList[0].image), @@ -182,7 +185,7 @@ fun TagsScreen(reportWasteViewModel: ReportWasteViewModel) { }, label = "" ) { targetCount -> AutoResizedText( - text = "Search '$targetCount'", + text = "Search $targetCount", color = textColor, fontSize = 15.sp, modifier = Modifier