-
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.
- Loading branch information
1 parent
97d5c58
commit b1be39e
Showing
2 changed files
with
279 additions
and
142 deletions.
There are no files selected for viewing
242 changes: 242 additions & 0 deletions
242
app/src/main/java/app/waste2wealth/com/collectioncentres/CollectionCenters.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,242 @@ | ||
package app.waste2wealth.com.collectioncentres | ||
|
||
import android.content.Intent | ||
import android.net.Uri | ||
import androidx.activity.compose.BackHandler | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.layout.Arrangement | ||
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.offset | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.foundation.shape.RoundedCornerShape | ||
import androidx.compose.material.Button | ||
import androidx.compose.material.ButtonDefaults | ||
import androidx.compose.material.Icon | ||
import androidx.compose.material.Text | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.ArrowBackIos | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.clip | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.unit.sp | ||
import androidx.navigation.NavHostController | ||
import app.waste2wealth.com.collectwaste.WasteItemCard | ||
import app.waste2wealth.com.location.LocationViewModel | ||
import app.waste2wealth.com.navigation.Screens | ||
import app.waste2wealth.com.reportwaste.DialogBox | ||
import app.waste2wealth.com.ui.theme.CardColor | ||
import app.waste2wealth.com.ui.theme.CardTextColor | ||
import app.waste2wealth.com.ui.theme.appBackground | ||
import app.waste2wealth.com.ui.theme.monteSB | ||
import app.waste2wealth.com.ui.theme.textColor | ||
import coil.compose.AsyncImage | ||
import com.google.firebase.storage.FirebaseStorage | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.tasks.await | ||
import kotlinx.coroutines.withContext | ||
import kotlin.math.atan2 | ||
import kotlin.math.cos | ||
import kotlin.math.sin | ||
import kotlin.math.sqrt | ||
|
||
@Composable | ||
fun CollectionCenters( | ||
navController: NavHostController, | ||
viewModel: LocationViewModel | ||
) { | ||
val context = LocalContext.current | ||
var isDialogVisible by remember { mutableStateOf(false) } | ||
val isWithin = isWithinRadius( | ||
viewModel.latitude, | ||
viewModel.longitude, | ||
viewModel.theirLatitude.value, | ||
viewModel.theirLongitude.value | ||
) | ||
BackHandler { | ||
viewModel.locationNo.value = "" | ||
viewModel.address.value = "" | ||
viewModel.distance.value = "" | ||
viewModel.time.value = "" | ||
viewModel.wastePhoto.value = "" | ||
viewModel.theirLongitude.value = 0.0 | ||
viewModel.theirLatitude.value = 0.0 | ||
viewModel.tags.value = listOf() | ||
navController.popBackStack() | ||
} | ||
LaunchedEffect(key1 = Unit) { | ||
viewModel.getPlaces() | ||
} | ||
|
||
Column( | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.background(appBackground) | ||
) { | ||
Row( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(top = 30.dp, start = 0.dp), | ||
horizontalArrangement = Arrangement.Start | ||
) { | ||
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 | ||
) { | ||
Text( | ||
text = "Collect Waste", | ||
color = textColor, | ||
fontFamily = monteSB, | ||
fontSize = 25.sp | ||
) | ||
} | ||
} | ||
|
||
Spacer(modifier = Modifier.height(30.dp)) | ||
WasteItemCard( | ||
locationNo = viewModel.locationNo.value, | ||
address = viewModel.address.value, | ||
distance = viewModel.distance.value, | ||
time = viewModel.time.value, | ||
isCollectedInfo = true, | ||
isEllipsis = false, | ||
tags = viewModel.tags.value, | ||
onCollected = { | ||
val gmmIntentUri = | ||
Uri.parse( | ||
"google.navigation:q=${viewModel.theirLatitude.value}," + | ||
"${viewModel.theirLongitude.value}" | ||
) | ||
val mapIntent = Intent(Intent.ACTION_VIEW, gmmIntentUri) | ||
mapIntent.setPackage("com.google.android.apps.maps") | ||
context.startActivity(mapIntent) | ||
|
||
}, | ||
) | ||
Spacer(modifier = Modifier.height(30.dp)) | ||
var imageUrlState by remember { | ||
mutableStateOf("") | ||
} | ||
LaunchedEffect(key1 = Unit) { | ||
val imageUrl = withContext(Dispatchers.IO) { | ||
try { | ||
getDownloadUrlFromPath(viewModel.wastePhoto.value) | ||
} catch (e: Exception) { | ||
"" | ||
} | ||
} | ||
imageUrlState = imageUrl | ||
} | ||
if (imageUrlState != "") { | ||
AsyncImage( | ||
model = imageUrlState, | ||
contentDescription = "", | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.height(300.dp) | ||
.padding(bottom = 30.dp) | ||
.clip(RoundedCornerShape(30.dp)), | ||
) | ||
} | ||
|
||
Row( | ||
modifier = Modifier.fillMaxWidth(), | ||
horizontalArrangement = Arrangement.Center | ||
) { | ||
Button( | ||
onClick = { | ||
if (!isWithin) { | ||
isDialogVisible = true | ||
} else { | ||
navController.navigate(Screens.CollectedWasteSuccess.route) | ||
} | ||
}, | ||
colors = ButtonDefaults.buttonColors( | ||
backgroundColor = CardColor, | ||
contentColor = CardTextColor | ||
), | ||
shape = RoundedCornerShape(35.dp), | ||
modifier = Modifier.padding(start = 10.dp) | ||
) { | ||
Text( | ||
text = "Collect Waste", | ||
color = CardTextColor, | ||
fontSize = 12.sp, | ||
fontFamily = monteSB, | ||
modifier = Modifier.padding(bottom = 4.dp), | ||
maxLines = 1, | ||
softWrap = true | ||
) | ||
} | ||
} | ||
if (!isWithin) { | ||
DialogBox( | ||
isVisible = isDialogVisible, | ||
title = "Are You Sure you reached the location?", | ||
description = "It feels like you aren't at the location yet. " + | ||
"Please make sure you are at the location before you collect the waste.", | ||
successRequest = { | ||
isDialogVisible = false | ||
navController.navigate(Screens.CollectedWasteSuccess.route) | ||
}, | ||
dismissRequest = { | ||
isDialogVisible = false | ||
} | ||
) | ||
} | ||
} | ||
|
||
} | ||
|
||
suspend fun getDownloadUrlFromPath(path: String): String { | ||
val storageRef = FirebaseStorage.getInstance().reference | ||
val fileRef = storageRef.child(path) | ||
return fileRef.downloadUrl.await().toString() | ||
} | ||
|
||
fun isWithinRadius( | ||
sourceLat: Double, | ||
sourceLon: Double, | ||
destLat: Double, | ||
destLon: Double | ||
): Boolean { | ||
val earthRadius = 6371 // Radius of the Earth in kilometers | ||
|
||
val dLat = Math.toRadians(destLat - sourceLat) | ||
val dLon = Math.toRadians(destLon - sourceLon) | ||
|
||
val a = sin(dLat / 2) * sin(dLat / 2) + | ||
cos(Math.toRadians(sourceLat)) * cos(Math.toRadians(destLat)) * | ||
sin(dLon / 2) * sin(dLon / 2) | ||
val c = 2 * atan2(sqrt(a), sqrt(1 - a)) | ||
|
||
val distance = earthRadius * c | ||
|
||
return distance <= 1.2 // Check if distance is within x km radius | ||
} |
Oops, something went wrong.