From d9f37d6d4be2eccfd9ef904892834293dec53e8b Mon Sep 17 00:00:00 2001 From: Akshay Nandwana Date: Mon, 18 Nov 2024 07:16:40 +0530 Subject: [PATCH 1/6] compose migration --- .../DownloadProgressDialogFragment.kt | 70 +++++++++++-------- .../selector/OfflineAreaSelectorFragment.kt | 37 ++++++++-- ground/src/main/res/values/strings.xml | 4 +- 3 files changed, 73 insertions(+), 38 deletions(-) diff --git a/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialogFragment.kt b/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialogFragment.kt index 9c4a6575a8..32790e5700 100644 --- a/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialogFragment.kt +++ b/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialogFragment.kt @@ -16,38 +16,46 @@ package com.google.android.ground.ui.offlineareas.selector -import android.app.AlertDialog -import android.app.Dialog -import android.os.Bundle -import androidx.fragment.app.FragmentManager +import androidx.compose.foundation.layout.Column +import androidx.compose.material3.AlertDialog +import androidx.compose.material3.LinearProgressIndicator +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.material3.TextButton +import androidx.compose.runtime.Composable +import androidx.compose.runtime.livedata.observeAsState +import androidx.compose.ui.res.stringResource import com.google.android.ground.R -import com.google.android.ground.databinding.DownloadProgressDialogFragBinding -import com.google.android.ground.ui.common.AbstractDialogFragment -import dagger.hilt.android.AndroidEntryPoint -@AndroidEntryPoint -class DownloadProgressDialogFragment : AbstractDialogFragment() { - override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { - val inflater = requireActivity().layoutInflater - val binding = DownloadProgressDialogFragBinding.inflate(inflater) - binding.lifecycleOwner = this - binding.viewModel = getViewModel(OfflineAreaSelectorViewModel::class.java) - val dialog = - AlertDialog.Builder(requireActivity()) - .setTitle(getString(R.string.offline_map_imagery_download_progress_dialog_title)) - .setMessage(getString(R.string.offline_map_imagery_download_progress_dialog_message)) - .setView(binding.root) - .setCancelable(false) - .create() - dialog.setCanceledOnTouchOutside(false) - return dialog - } +@Composable +fun DownloadProgressDialog(viewModel: OfflineAreaSelectorViewModel, onDismiss: () -> Unit) { + val progressData = viewModel.downloadProgress.observeAsState() - fun setVisibility(childFragmentManager: FragmentManager, newVisibility: Boolean) { - if (newVisibility && !isVisible) { - show(childFragmentManager, this::class.simpleName) - } else if (!newVisibility && isVisible) { - dismiss() - } - } + AlertDialog( + containerColor = MaterialTheme.colorScheme.surface, + onDismissRequest = {}, + title = { + Text( + stringResource(R.string.offline_map_imagery_download_progress_dialog_title), + color = MaterialTheme.colorScheme.onSurface, + ) + }, + text = { + Column { + LinearProgressIndicator( + progress = { progressData.value?.toFloat() ?: 0f }, + color = MaterialTheme.colorScheme.primary, + trackColor = MaterialTheme.colorScheme.surfaceVariant, + ) + Text( + stringResource(R.string.offline_map_imagery_download_progress_dialog_message), + color = MaterialTheme.colorScheme.onSurfaceVariant, + ) + } + }, + dismissButton = { + TextButton(onClick = { onDismiss() }) { Text(text = stringResource(R.string.cancel)) } + }, + confirmButton = {}, + ) } diff --git a/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorFragment.kt b/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorFragment.kt index 30c7138eb4..de45733245 100644 --- a/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorFragment.kt +++ b/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorFragment.kt @@ -20,12 +20,16 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.ui.platform.ComposeView import androidx.lifecycle.lifecycleScope import com.google.android.ground.databinding.OfflineAreaSelectorFragBinding import com.google.android.ground.ui.common.AbstractMapContainerFragment import com.google.android.ground.ui.common.BaseMapViewModel import com.google.android.ground.ui.home.mapcontainer.HomeScreenMapContainerViewModel import com.google.android.ground.ui.map.MapFragment +import com.google.android.ground.ui.theme.AppTheme import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.launch @@ -36,15 +40,10 @@ class OfflineAreaSelectorFragment : AbstractMapContainerFragment() { private lateinit var viewModel: OfflineAreaSelectorViewModel private lateinit var mapContainerViewModel: HomeScreenMapContainerViewModel - private var downloadProgressDialogFragment = DownloadProgressDialogFragment() - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) mapContainerViewModel = getViewModel(HomeScreenMapContainerViewModel::class.java) viewModel = getViewModel(OfflineAreaSelectorViewModel::class.java) - viewModel.isDownloadProgressVisible.observe(this) { - downloadProgressDialogFragment.setVisibility(childFragmentManager, it) - } } override fun onCreateView( @@ -59,6 +58,13 @@ class OfflineAreaSelectorFragment : AbstractMapContainerFragment() { return binding.root } + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + viewModel.isDownloadProgressVisible.observe(viewLifecycleOwner) { + showDownloadProgressDialog(it) + } + } + override fun onMapReady(map: MapFragment) { // Observe events emitted by the ViewModel. viewLifecycleOwner.lifecycleScope.launch { @@ -68,4 +74,25 @@ class OfflineAreaSelectorFragment : AbstractMapContainerFragment() { } override fun getMapViewModel(): BaseMapViewModel = viewModel + + private fun showDownloadProgressDialog(isVisible: Boolean) { + val dialogComposeView = + ComposeView(requireContext()).apply { + setContent { + val openAlertDialog = remember { mutableStateOf(isVisible) } + when { + openAlertDialog.value -> { + AppTheme { + DownloadProgressDialog( + viewModel = viewModel, + onDismiss = { openAlertDialog.value = false }, + ) + } + } + } + } + } + + (view as ViewGroup).addView(dialogComposeView) + } } diff --git a/ground/src/main/res/values/strings.xml b/ground/src/main/res/values/strings.xml index 3c38b35dfb..3184306371 100644 --- a/ground/src/main/res/values/strings.xml +++ b/ground/src/main/res/values/strings.xml @@ -95,8 +95,8 @@ Hide or show downloaded imagery Layers Base map - Download progress - Downloading offline map imagery… + Downloading - %d% + Download times may vary depending on the size of the selected area and your internet connection. Multiple regions Permission denied Close app From ebcb687dec53e727e8f0c3d61c4ae61c6ae7d3b5 Mon Sep 17 00:00:00 2001 From: Akshay Nandwana Date: Wed, 27 Nov 2024 21:06:33 +0530 Subject: [PATCH 2/6] update ui --- .../DownloadProgressDialogFragment.kt | 28 +++++++++++-- .../layout/download_progress_dialog_frag.xml | 42 ------------------- ground/src/main/res/values-es/strings.xml | 2 +- ground/src/main/res/values-fr/strings.xml | 2 +- ground/src/main/res/values/strings.xml | 2 +- 5 files changed, 28 insertions(+), 48 deletions(-) delete mode 100644 ground/src/main/res/layout/download_progress_dialog_frag.xml diff --git a/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialogFragment.kt b/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialogFragment.kt index 32790e5700..151adff0d4 100644 --- a/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialogFragment.kt +++ b/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialogFragment.kt @@ -16,37 +16,59 @@ package com.google.android.ground.ui.offlineareas.selector +import androidx.compose.animation.core.animateFloatAsState +import androidx.compose.animation.core.tween +import androidx.compose.foundation.background import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.AlertDialog import androidx.compose.material3.LinearProgressIndicator import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.material3.TextButton import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue import androidx.compose.runtime.livedata.observeAsState +import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource +import androidx.compose.ui.unit.dp import com.google.android.ground.R @Composable fun DownloadProgressDialog(viewModel: OfflineAreaSelectorViewModel, onDismiss: () -> Unit) { - val progressData = viewModel.downloadProgress.observeAsState() + val progress by viewModel.downloadProgress.observeAsState(0) + val maxProgress by viewModel.downloadProgressMax.observeAsState(100) AlertDialog( containerColor = MaterialTheme.colorScheme.surface, onDismissRequest = {}, title = { Text( - stringResource(R.string.offline_map_imagery_download_progress_dialog_title), + stringResource(R.string.offline_map_imagery_download_progress_dialog_title, progress), color = MaterialTheme.colorScheme.onSurface, ) }, text = { Column { + val animatedProgress by + animateFloatAsState( + targetValue = progress.toFloat() / maxProgress, + animationSpec = tween(durationMillis = 300), + ) + LinearProgressIndicator( - progress = { progressData.value?.toFloat() ?: 0f }, + modifier = + Modifier.background( + shape = RoundedCornerShape(4.dp), + color = MaterialTheme.colorScheme.primary, + ), + progress = { animatedProgress }, color = MaterialTheme.colorScheme.primary, trackColor = MaterialTheme.colorScheme.surfaceVariant, ) + Spacer(Modifier.height(8.dp)) Text( stringResource(R.string.offline_map_imagery_download_progress_dialog_message), color = MaterialTheme.colorScheme.onSurfaceVariant, diff --git a/ground/src/main/res/layout/download_progress_dialog_frag.xml b/ground/src/main/res/layout/download_progress_dialog_frag.xml deleted file mode 100644 index 7932a8ceab..0000000000 --- a/ground/src/main/res/layout/download_progress_dialog_frag.xml +++ /dev/null @@ -1,42 +0,0 @@ - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/ground/src/main/res/values-es/strings.xml b/ground/src/main/res/values-es/strings.xml index 452d3647f4..f1d455edd3 100644 --- a/ground/src/main/res/values-es/strings.xml +++ b/ground/src/main/res/values-es/strings.xml @@ -94,7 +94,7 @@ Mostrar u ocultar las imágenes descargadas Capas Mapa base - Progreso de descarga + Progreso de descarga - %d%% Descargando imágenes del mapa sin conexión… Múltiples regiones Permiso denegado diff --git a/ground/src/main/res/values-fr/strings.xml b/ground/src/main/res/values-fr/strings.xml index ea904158b7..6a20cff412 100644 --- a/ground/src/main/res/values-fr/strings.xml +++ b/ground/src/main/res/values-fr/strings.xml @@ -94,7 +94,7 @@ Cacher ou afficher l’imagerie cartographique téléchargée Couches Carte de base - Progrès du téléchargement + Progrès du téléchargement - %d%% Téléchargement d’imagerie cartographique hors ligne… Plusieurs régions Autorisation refusée diff --git a/ground/src/main/res/values/strings.xml b/ground/src/main/res/values/strings.xml index 3184306371..598c8a3d1c 100644 --- a/ground/src/main/res/values/strings.xml +++ b/ground/src/main/res/values/strings.xml @@ -95,7 +95,7 @@ Hide or show downloaded imagery Layers Base map - Downloading - %d% + Downloading: %d%% Download times may vary depending on the size of the selected area and your internet connection. Multiple regions Permission denied From 1d03cde273358752b4f6426aa483ecd2c7dd9386 Mon Sep 17 00:00:00 2001 From: Akshay Nandwana Date: Thu, 28 Nov 2024 09:44:15 +0530 Subject: [PATCH 3/6] fixed ui added tests --- ...gFragment.kt => DownloadProgressDialog.kt} | 23 ++-- .../selector/OfflineAreaSelectorViewModel.kt | 16 +-- .../selector/DownloadProgressDialogTest.kt | 108 ++++++++++++++++++ 3 files changed, 127 insertions(+), 20 deletions(-) rename ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/{DownloadProgressDialogFragment.kt => DownloadProgressDialog.kt} (80%) create mode 100644 ground/src/test/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialogTest.kt diff --git a/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialogFragment.kt b/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialog.kt similarity index 80% rename from ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialogFragment.kt rename to ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialog.kt index 151adff0d4..08bf43cde4 100644 --- a/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialogFragment.kt +++ b/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialog.kt @@ -18,9 +18,9 @@ package com.google.android.ground.ui.offlineareas.selector import androidx.compose.animation.core.animateFloatAsState import androidx.compose.animation.core.tween -import androidx.compose.foundation.background import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.AlertDialog @@ -32,38 +32,35 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.livedata.observeAsState import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.platform.testTag import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import com.google.android.ground.R @Composable fun DownloadProgressDialog(viewModel: OfflineAreaSelectorViewModel, onDismiss: () -> Unit) { - val progress by viewModel.downloadProgress.observeAsState(0) - val maxProgress by viewModel.downloadProgressMax.observeAsState(100) + val progress by viewModel.downloadProgress.observeAsState(0f) AlertDialog( containerColor = MaterialTheme.colorScheme.surface, onDismissRequest = {}, title = { Text( - stringResource(R.string.offline_map_imagery_download_progress_dialog_title, progress), + stringResource( + R.string.offline_map_imagery_download_progress_dialog_title, + (progress * 100).toInt(), + ), color = MaterialTheme.colorScheme.onSurface, ) }, text = { Column { val animatedProgress by - animateFloatAsState( - targetValue = progress.toFloat() / maxProgress, - animationSpec = tween(durationMillis = 300), - ) + animateFloatAsState(targetValue = progress, animationSpec = tween(durationMillis = 300)) LinearProgressIndicator( - modifier = - Modifier.background( - shape = RoundedCornerShape(4.dp), - color = MaterialTheme.colorScheme.primary, - ), + modifier = Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp)).testTag("progressBar"), progress = { animatedProgress }, color = MaterialTheme.colorScheme.primary, trackColor = MaterialTheme.colorScheme.surfaceVariant, diff --git a/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt b/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt index 8765d285c8..75b652fcad 100644 --- a/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt +++ b/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt @@ -74,8 +74,7 @@ internal constructor( private val offlineAreaSizeLoadingSymbol = resources.getString(R.string.offline_area_size_loading_symbol) val isDownloadProgressVisible = MutableLiveData(false) - val downloadProgressMax = MutableLiveData(0) - val downloadProgress = MutableLiveData(0) + val downloadProgress = MutableLiveData(0f) val bottomText = MutableLiveData(null) val downloadButtonEnabled = MutableLiveData(false) @@ -94,13 +93,16 @@ internal constructor( } isDownloadProgressVisible.value = true - downloadProgress.value = 0 + downloadProgress.value = 0f viewModelScope.launch(ioDispatcher) { offlineAreaRepository.downloadTiles(viewport!!).collect { (bytesDownloaded, totalBytes) -> - // Set total bytes / max value on first iteration. - if (downloadProgressMax.value != totalBytes) downloadProgressMax.postValue(totalBytes) - // Add number of bytes downloaded to progress. - downloadProgress.postValue(bytesDownloaded) + val progressValue = + if (totalBytes > 0) { + (bytesDownloaded.toFloat() / totalBytes.toFloat()).coerceIn(0f, 1f) + } else { + 0f + } + downloadProgress.postValue(progressValue) } isDownloadProgressVisible.postValue(false) navigator.navigate(OfflineAreaSelectorFragmentDirections.offlineAreaBackToHomescreen()) diff --git a/ground/src/test/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialogTest.kt b/ground/src/test/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialogTest.kt new file mode 100644 index 0000000000..399727480f --- /dev/null +++ b/ground/src/test/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialogTest.kt @@ -0,0 +1,108 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.android.ground.ui.offlineareas.selector + +import androidx.activity.ComponentActivity +import androidx.compose.ui.test.assertIsDisplayed +import androidx.compose.ui.test.junit4.createAndroidComposeRule +import androidx.compose.ui.test.onNodeWithText +import androidx.compose.ui.test.performClick +import com.google.android.ground.BaseHiltTest +import com.google.android.ground.R +import dagger.hilt.android.testing.HiltAndroidTest +import javax.inject.Inject +import kotlin.test.Test +import org.junit.Assert.assertTrue +import org.junit.Rule +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner + +@HiltAndroidTest +@RunWith(RobolectricTestRunner::class) +class DownloadProgressDialogTest : BaseHiltTest() { + + @get:Rule override val composeTestRule = createAndroidComposeRule() + + @Inject lateinit var viewModel: OfflineAreaSelectorViewModel + + @Test + fun downloadProgressDialog_DisplaysTitleCorrectly() { + composeTestRule.setContent { DownloadProgressDialog(viewModel) {} } + + composeTestRule + .onNodeWithText( + composeTestRule.activity.getString( + R.string.offline_map_imagery_download_progress_dialog_title, + 0, + ) + ) + .assertIsDisplayed() + } + + @Test + fun downloadProgressDialog_DisplaysCorrectMessage() { + composeTestRule.setContent { DownloadProgressDialog(viewModel) {} } + + composeTestRule + .onNodeWithText( + composeTestRule.activity.getString( + R.string.offline_map_imagery_download_progress_dialog_message + ) + ) + .assertIsDisplayed() + } + + @Test + fun downloadProgressDialog_CallsOnDismissOnDismissButtonClick() { + var isDismissed = false + + composeTestRule.setContent { DownloadProgressDialog(viewModel) { isDismissed = true } } + + composeTestRule + .onNodeWithText(composeTestRule.activity.getString(R.string.cancel)) + .performClick() + + assertTrue(isDismissed) + } + + @Test + fun downloadProgressDialog_DisplaysCorrectTitleForProgress() { + viewModel.downloadProgress.value = 0.5f + + composeTestRule.setContent { DownloadProgressDialog(viewModel) {} } + + composeTestRule + .onNodeWithText( + composeTestRule.activity.getString( + R.string.offline_map_imagery_download_progress_dialog_title, + 50, + ) + ) + .assertIsDisplayed() + + viewModel.downloadProgress.value = 1f + + composeTestRule + .onNodeWithText( + composeTestRule.activity.getString( + R.string.offline_map_imagery_download_progress_dialog_title, + 100, + ) + ) + .assertIsDisplayed() + } +} From 8d3bd6e6a2d34e9580a3e24d3fd791bab6197952 Mon Sep 17 00:00:00 2001 From: Akshay Nandwana Date: Thu, 28 Nov 2024 14:56:30 +0530 Subject: [PATCH 4/6] suggested fix --- .../selector/DownloadProgressDialog.kt | 19 +++++++++++++------ .../selector/OfflineAreaSelectorFragment.kt | 2 +- ground/src/main/res/values/strings.xml | 2 +- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialog.kt b/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialog.kt index 08bf43cde4..468829097d 100644 --- a/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialog.kt +++ b/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialog.kt @@ -1,5 +1,5 @@ /* - * Copyright 2023 Google LLC + * Copyright 2024 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,15 +35,18 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.platform.testTag import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.font.Font +import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.unit.dp +import androidx.lifecycle.MutableLiveData import com.google.android.ground.R @Composable -fun DownloadProgressDialog(viewModel: OfflineAreaSelectorViewModel, onDismiss: () -> Unit) { - val progress by viewModel.downloadProgress.observeAsState(0f) +fun DownloadProgressDialog(downloadProgress: MutableLiveData, onDismiss: () -> Unit) { + val progress by downloadProgress.observeAsState(0f) AlertDialog( - containerColor = MaterialTheme.colorScheme.surface, + containerColor = MaterialTheme.colorScheme.surfaceContainer, onDismissRequest = {}, title = { Text( @@ -52,6 +55,7 @@ fun DownloadProgressDialog(viewModel: OfflineAreaSelectorViewModel, onDismiss: ( (progress * 100).toInt(), ), color = MaterialTheme.colorScheme.onSurface, + fontFamily = FontFamily(Font(R.font.text_500)), ) }, text = { @@ -65,15 +69,18 @@ fun DownloadProgressDialog(viewModel: OfflineAreaSelectorViewModel, onDismiss: ( color = MaterialTheme.colorScheme.primary, trackColor = MaterialTheme.colorScheme.surfaceVariant, ) - Spacer(Modifier.height(8.dp)) + Spacer(Modifier.height(16.dp)) Text( stringResource(R.string.offline_map_imagery_download_progress_dialog_message), color = MaterialTheme.colorScheme.onSurfaceVariant, + fontFamily = FontFamily(Font(R.font.text_500)), ) } }, dismissButton = { - TextButton(onClick = { onDismiss() }) { Text(text = stringResource(R.string.cancel)) } + TextButton(onClick = { onDismiss() }) { + Text(text = stringResource(R.string.cancel), fontFamily = FontFamily(Font(R.font.text_500))) + } }, confirmButton = {}, ) diff --git a/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorFragment.kt b/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorFragment.kt index de45733245..3bc63dd644 100644 --- a/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorFragment.kt +++ b/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorFragment.kt @@ -84,7 +84,7 @@ class OfflineAreaSelectorFragment : AbstractMapContainerFragment() { openAlertDialog.value -> { AppTheme { DownloadProgressDialog( - viewModel = viewModel, + downloadProgress = viewModel.downloadProgress, onDismiss = { openAlertDialog.value = false }, ) } diff --git a/ground/src/main/res/values/strings.xml b/ground/src/main/res/values/strings.xml index 598c8a3d1c..81343f21d4 100644 --- a/ground/src/main/res/values/strings.xml +++ b/ground/src/main/res/values/strings.xml @@ -95,7 +95,7 @@ Hide or show downloaded imagery Layers Base map - Downloading: %d%% + Downloading - %d%% Download times may vary depending on the size of the selected area and your internet connection. Multiple regions Permission denied From ab0fa5a418301cb55176a94a17837f45cc4182a8 Mon Sep 17 00:00:00 2001 From: Akshay Nandwana Date: Thu, 28 Nov 2024 15:12:15 +0530 Subject: [PATCH 5/6] test fix --- .../selector/DownloadProgressDialog.kt | 6 +----- .../selector/OfflineAreaSelectorFragment.kt | 4 +++- .../selector/DownloadProgressDialogTest.kt | 21 ++++++------------- 3 files changed, 10 insertions(+), 21 deletions(-) diff --git a/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialog.kt b/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialog.kt index 468829097d..e3d7ee002c 100644 --- a/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialog.kt +++ b/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialog.kt @@ -30,7 +30,6 @@ import androidx.compose.material3.Text import androidx.compose.material3.TextButton import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue -import androidx.compose.runtime.livedata.observeAsState import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.platform.testTag @@ -38,13 +37,10 @@ import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.font.Font import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.unit.dp -import androidx.lifecycle.MutableLiveData import com.google.android.ground.R @Composable -fun DownloadProgressDialog(downloadProgress: MutableLiveData, onDismiss: () -> Unit) { - val progress by downloadProgress.observeAsState(0f) - +fun DownloadProgressDialog(progress: Float, onDismiss: () -> Unit) { AlertDialog( containerColor = MaterialTheme.colorScheme.surfaceContainer, onDismissRequest = {}, diff --git a/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorFragment.kt b/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorFragment.kt index 3bc63dd644..fa4613d716 100644 --- a/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorFragment.kt +++ b/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorFragment.kt @@ -20,6 +20,7 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import androidx.compose.runtime.livedata.observeAsState import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.platform.ComposeView @@ -80,11 +81,12 @@ class OfflineAreaSelectorFragment : AbstractMapContainerFragment() { ComposeView(requireContext()).apply { setContent { val openAlertDialog = remember { mutableStateOf(isVisible) } + val progress = viewModel.downloadProgress.observeAsState(0f) when { openAlertDialog.value -> { AppTheme { DownloadProgressDialog( - downloadProgress = viewModel.downloadProgress, + progress = progress.value, onDismiss = { openAlertDialog.value = false }, ) } diff --git a/ground/src/test/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialogTest.kt b/ground/src/test/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialogTest.kt index 399727480f..c4c212495a 100644 --- a/ground/src/test/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialogTest.kt +++ b/ground/src/test/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialogTest.kt @@ -41,7 +41,7 @@ class DownloadProgressDialogTest : BaseHiltTest() { @Test fun downloadProgressDialog_DisplaysTitleCorrectly() { - composeTestRule.setContent { DownloadProgressDialog(viewModel) {} } + composeTestRule.setContent { DownloadProgressDialog(viewModel.downloadProgress.value!!, {}) } composeTestRule .onNodeWithText( @@ -55,7 +55,7 @@ class DownloadProgressDialogTest : BaseHiltTest() { @Test fun downloadProgressDialog_DisplaysCorrectMessage() { - composeTestRule.setContent { DownloadProgressDialog(viewModel) {} } + composeTestRule.setContent { DownloadProgressDialog(viewModel.downloadProgress.value!!, {}) } composeTestRule .onNodeWithText( @@ -70,7 +70,9 @@ class DownloadProgressDialogTest : BaseHiltTest() { fun downloadProgressDialog_CallsOnDismissOnDismissButtonClick() { var isDismissed = false - composeTestRule.setContent { DownloadProgressDialog(viewModel) { isDismissed = true } } + composeTestRule.setContent { + DownloadProgressDialog(viewModel.downloadProgress.value!!, { isDismissed = true }) + } composeTestRule .onNodeWithText(composeTestRule.activity.getString(R.string.cancel)) @@ -83,7 +85,7 @@ class DownloadProgressDialogTest : BaseHiltTest() { fun downloadProgressDialog_DisplaysCorrectTitleForProgress() { viewModel.downloadProgress.value = 0.5f - composeTestRule.setContent { DownloadProgressDialog(viewModel) {} } + composeTestRule.setContent { DownloadProgressDialog(viewModel.downloadProgress.value!!, {}) } composeTestRule .onNodeWithText( @@ -93,16 +95,5 @@ class DownloadProgressDialogTest : BaseHiltTest() { ) ) .assertIsDisplayed() - - viewModel.downloadProgress.value = 1f - - composeTestRule - .onNodeWithText( - composeTestRule.activity.getString( - R.string.offline_map_imagery_download_progress_dialog_title, - 100, - ) - ) - .assertIsDisplayed() } } From dae91b2cbf8aa8b6c1792fec2930d4abda182b41 Mon Sep 17 00:00:00 2001 From: Akshay Nandwana Date: Thu, 28 Nov 2024 21:32:01 +0530 Subject: [PATCH 6/6] added todo --- .../ui/offlineareas/selector/OfflineAreaSelectorFragment.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorFragment.kt b/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorFragment.kt index fa4613d716..8389024988 100644 --- a/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorFragment.kt +++ b/ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorFragment.kt @@ -87,6 +87,8 @@ class OfflineAreaSelectorFragment : AbstractMapContainerFragment() { AppTheme { DownloadProgressDialog( progress = progress.value, + // TODO - Add Download Cancel Feature + // https://github.com/google/ground-android/issues/2884 onDismiss = { openAlertDialog.value = false }, ) }