Skip to content

Commit

Permalink
Pharmacy information panel now can be rotated.
Browse files Browse the repository at this point in the history
Changed MeteredAsyncImage background to a LoadingSpinner.
  • Loading branch information
andre-j3sus committed May 10, 2024
1 parent 278bdb5 commit d4ce021
Show file tree
Hide file tree
Showing 11 changed files with 300 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ class PharmacistApplication : DependenciesContainer, Application() {
createNotificationsChannel()

val serviceIntent = Intent(this, MedicineNotificationsBackgroundService::class.java)

startService(serviceIntent)

serviceScope.launch {
Expand All @@ -85,7 +84,7 @@ class PharmacistApplication : DependenciesContainer, Application() {
companion object {
const val MEDICINE_NOTIFICATION_CHANNEL = "MedicineNotifications"

const val API_ENDPOINT_TYPE = "ngrok"
private const val API_ENDPOINT_TYPE = "render"
val API_ENDPOINT = when (API_ENDPOINT_TYPE) {
"localhost" -> "http://10.0.2.2:8080"
"ngrok" -> "https://2b02-2001-818-e871-b700-c937-8172-33bf-a88.ngrok-free.app"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ class MedicineNotificationsBackgroundService : Service() {
PharmacistApplication.MEDICINE_NOTIFICATION_CHANNEL
)
.setSmallIcon(R.drawable.pharmacy_logo)
.setContentTitle(getString(R.string.medicine_notification_title, notification.medicineStock.medicine.name))
.setContentTitle(
getString(
R.string.medicine_notification_title,
notification.medicineStock.medicine.name
)
)
.setStyle(NotificationCompat.BigTextStyle().bigText(message))
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package pt.ulisboa.ist.pharmacist.ui.screens.home

import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withTimeoutOrNull
import pt.ulisboa.ist.pharmacist.service.http.PharmacistService
import pt.ulisboa.ist.pharmacist.service.http.connection.isSuccess
import pt.ulisboa.ist.pharmacist.session.SessionManager
Expand Down Expand Up @@ -35,13 +37,24 @@ class HomeViewModel(
* Logs out the user.
*/
fun logout() = viewModelScope.launch {
check(sessionManager.isLoggedIn()) { "The user is not logged in." }
if (!sessionManager.isLoggedIn()) {
Log.d("LOGOUT", "User is not logged in")

pharmacistService.usersService.logout()
sessionManager.clearSession()
isLoggedIn = false
isGuest = false
return@launch
}

Log.d("LOGOUT", "Logging out user")
withTimeoutOrNull(5000) {
pharmacistService.usersService.logout()
}

sessionManager.clearSession()
isLoggedIn = false
isGuest = false
Log.d("LOGOUT", "User logged out")
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,28 @@
package pt.ulisboa.ist.pharmacist.ui.screens.pharmacy

import androidx.compose.foundation.isSystemInDarkTheme
import android.content.res.Configuration
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
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.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Favorite
import androidx.compose.material.icons.rounded.FavoriteBorder
import androidx.compose.material.icons.rounded.Flag
import androidx.compose.material.icons.rounded.Medication
import androidx.compose.material.icons.rounded.OutlinedFlag
import androidx.compose.material.icons.rounded.Share
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.snapshots.SnapshotStateMap
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.HorizontalPagerIndicator
import com.google.accompanist.pager.rememberPagerState
import com.google.android.gms.maps.model.CameraPosition
import com.google.maps.android.compose.CameraPositionState
import com.google.maps.android.compose.GoogleMap
import com.google.maps.android.compose.MapUiSettings
import com.google.maps.android.compose.Marker
import com.google.maps.android.compose.MarkerState
import pt.ulisboa.ist.pharmacist.R
import pt.ulisboa.ist.pharmacist.service.http.services.pharmacies.models.getPharmacyById.PharmacyWithUserDataModel
import pt.ulisboa.ist.pharmacist.service.http.services.pharmacies.models.listAvailableMedicines.MedicineStockModel
import pt.ulisboa.ist.pharmacist.ui.screens.PharmacistScreen
import pt.ulisboa.ist.pharmacist.ui.screens.pharmacy.components.PharmacyMedicineEntry
import pt.ulisboa.ist.pharmacist.ui.screens.pharmacy.components.PharmacyHeader
import pt.ulisboa.ist.pharmacist.ui.screens.pharmacy.components.PharmacyMedicineList
import pt.ulisboa.ist.pharmacist.ui.screens.pharmacy.components.PharmacyRating
import pt.ulisboa.ist.pharmacist.ui.screens.shared.components.IconTextButton
import pt.ulisboa.ist.pharmacist.ui.screens.shared.components.LoadingSpinner
import pt.ulisboa.ist.pharmacist.ui.screens.shared.components.MeteredAsyncImage
import pt.ulisboa.ist.pharmacist.ui.theme.Favorite

/**
* Screen that displays the pharmacy information.
Expand All @@ -70,131 +46,72 @@ fun PharmacyScreen(
onRatingChanged: (Int) -> Unit
) {
val medicinesStock = medicinesList.values.toList()

val isLandscape =
LocalConfiguration.current.orientation == Configuration.ORIENTATION_LANDSCAPE
val pagerState = rememberPagerState(initialPage = 0)

PharmacistScreen {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier.fillMaxSize()
) {
if (loadingState == PharmacyViewModel.PharmacyLoadingState.LOADED && pharmacy != null) {
HorizontalPager(
count = 2,
state = pagerState,
modifier = Modifier
.fillMaxWidth(0.6f)
.padding(top = 16.dp, bottom = 8.dp)
.height(200.dp),
) { page ->
if (page == 0)
MeteredAsyncImage(
url = pharmacy.pharmacy.pictureUrl,
contentDescription = stringResource(R.string.pharmacyMap_pharmacyPicture_description),
modifier = Modifier.fillMaxSize()
)
else
GoogleMap(
modifier = Modifier.fillMaxSize(),
cameraPositionState = CameraPositionState(
position = CameraPosition(
pharmacy.pharmacy.location.toLatLng(),
15f,
0f,
0f
)
),
uiSettings = MapUiSettings(
myLocationButtonEnabled = false,
rotationGesturesEnabled = false,
scrollGesturesEnabled = false,
tiltGesturesEnabled = false
)
) {
Marker(
state = MarkerState(position = pharmacy.pharmacy.location.toLatLng()),
title = pharmacy.pharmacy.name
)
}
}
HorizontalPagerIndicator(
pagerState = pagerState,
activeColor = if (isSystemInDarkTheme()) Color.White else Color.Black,
)

Text(
text = pharmacy.pharmacy.name,
style = MaterialTheme.typography.titleLarge
)
Row {
IconButton(onClick = onFavoriteClick) {
Icon(
if (pharmacy.userMarkedAsFavorite) Icons.Rounded.Favorite else Icons.Rounded.FavoriteBorder,
contentDescription = stringResource(R.string.medicine_addToNotifications_button_description),
tint = if (pharmacy.userMarkedAsFavorite) Favorite else MaterialTheme.colorScheme.primary
)
}
IconButton(onClick = onReportClick) { // TODO: After report, show a dialog to confirm the report and then return to the pharmacy map
Icon(
if (pharmacy.userFlagged) Icons.Rounded.Flag else Icons.Rounded.OutlinedFlag,
contentDescription = stringResource(R.string.report_pharmacy),
tint = Color.Red
)
}
IconButton(onClick = onShareClick) {
Icon(
Icons.Rounded.Share,
contentDescription = stringResource(R.string.share),
tint = MaterialTheme.colorScheme.primary
)
}
}

PharmacyRating(
pharmacy = pharmacy,
onRatingChanged = onRatingChanged,
)

Text(
text = "${medicinesStock.size} " +
stringResource(
if (medicinesStock.size != 1)
R.string.medicines_available
else R.string.medicine_available
),
style = MaterialTheme.typography.bodyMedium,
modifier = Modifier
.padding(8.dp)
)

IconTextButton(
imageVector = Icons.Rounded.Medication,
text = stringResource(R.string.add_medicine),
contentDescription = stringResource(R.string.add_medicine),
onClick = onAddMedicineClick
)

LazyColumn(
modifier = Modifier
.padding(20.dp)
if (loadingState == PharmacyViewModel.PharmacyLoadingState.LOADED && pharmacy != null)
if (isLandscape)
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.fillMaxSize()
) {
items(medicinesStock.size) { index ->
val (medicine, stock) = medicinesStock[index]
PharmacyMedicineEntry(
medicine,
stock,
onMedicineClick = onMedicineClick,
onAddStockClick = onAddStockClick,
onRemoveStockClick = onRemoveStockClick,
modifier = Modifier.fillMaxWidth()
)
}
PharmacyHeader(
pagerState,
pharmacy,
onFavoriteClick,
onReportClick,
onShareClick,
modifier = Modifier
.fillMaxWidth(0.3f)
.fillMaxHeight()
)
PharmacyRating(
pharmacy = pharmacy,
onRatingChanged = onRatingChanged,
modifier = Modifier.fillMaxWidth(0.3f)
)
PharmacyMedicineList(
medicinesStock,
onAddMedicineClick,
onMedicineClick,
onAddStockClick,
onRemoveStockClick
)
}
} else
Box {
LoadingSpinner()
else
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier.fillMaxSize()
) {
PharmacyHeader(
pagerState,
pharmacy,
onFavoriteClick,
onReportClick,
onShareClick,
modifier = Modifier.fillMaxWidth(1f)
)
PharmacyRating(
pharmacy = pharmacy,
onRatingChanged = onRatingChanged,
modifier = Modifier.fillMaxWidth(0.8f)
)
PharmacyMedicineList(
medicinesStock,
onAddMedicineClick,
onMedicineClick,
onAddStockClick,
onRemoveStockClick
)
}
}
else
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
LoadingSpinner(text = stringResource(R.string.loading_pharmacy))
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import androidx.compose.runtime.mutableStateMapOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import pt.ulisboa.ist.pharmacist.service.http.PharmacistService
import pt.ulisboa.ist.pharmacist.service.http.connection.isSuccess
Expand Down
Loading

0 comments on commit d4ce021

Please sign in to comment.