Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download Progress Dialog Jetpack Compose Migration #2845

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
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
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.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 com.google.android.ground.R

@Composable
fun DownloadProgressDialog(progress: Float, onDismiss: () -> Unit) {
AlertDialog(
containerColor = MaterialTheme.colorScheme.surfaceContainer,
onDismissRequest = {},

Check warning on line 46 in ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialog.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialog.kt#L46

Added line #L46 was not covered by tests
title = {
Text(
stringResource(
R.string.offline_map_imagery_download_progress_dialog_title,
(progress * 100).toInt(),
),
color = MaterialTheme.colorScheme.onSurface,
fontFamily = FontFamily(Font(R.font.text_500)),
)

Check warning on line 55 in ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialog.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialog.kt#L55

Added line #L55 was not covered by tests
},
text = {
Column {
val animatedProgress by
animateFloatAsState(targetValue = progress, animationSpec = tween(durationMillis = 300))

LinearProgressIndicator(
modifier = Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp)).testTag("progressBar"),
progress = { animatedProgress },
color = MaterialTheme.colorScheme.primary,
trackColor = MaterialTheme.colorScheme.surfaceVariant,
)
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), fontFamily = FontFamily(Font(R.font.text_500)))
}
},
confirmButton = {},
)
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@
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
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

Expand All @@ -36,8 +41,6 @@
private lateinit var viewModel: OfflineAreaSelectorViewModel
private lateinit var mapContainerViewModel: HomeScreenMapContainerViewModel

private var downloadProgressDialogFragment: DownloadProgressDialogFragment? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mapContainerViewModel = getViewModel(HomeScreenMapContainerViewModel::class.java)
Expand All @@ -59,10 +62,7 @@
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.isDownloadProgressVisible.observe(viewLifecycleOwner) {
if (downloadProgressDialogFragment == null) {
downloadProgressDialogFragment = DownloadProgressDialogFragment()
}
downloadProgressDialogFragment?.setVisibility(childFragmentManager, it)
showDownloadProgressDialog(it)
}
}

Expand All @@ -75,4 +75,28 @@
}

override fun getMapViewModel(): BaseMapViewModel = viewModel

private fun showDownloadProgressDialog(isVisible: Boolean) {
val dialogComposeView =
ComposeView(requireContext()).apply {
setContent {
val openAlertDialog = remember { mutableStateOf(isVisible) }
val progress = viewModel.downloadProgress.observeAsState(0f)
when {
openAlertDialog.value -> {
AppTheme {
DownloadProgressDialog(
progress = progress.value,

Check warning on line 89 in ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorFragment.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorFragment.kt#L89

Added line #L89 was not covered by tests
// TODO - Add Download Cancel Feature
// https://github.com/google/ground-android/issues/2884
onDismiss = { openAlertDialog.value = false },
anandwana001 marked this conversation as resolved.
Show resolved Hide resolved
)
}

Check warning on line 94 in ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorFragment.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorFragment.kt#L92-L94

Added lines #L92 - L94 were not covered by tests
}
}

Check warning on line 96 in ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorFragment.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorFragment.kt#L96

Added line #L96 was not covered by tests
}
}

(view as ViewGroup).addView(dialogComposeView)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@
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<String?>(null)
val downloadButtonEnabled = MutableLiveData(false)

Expand All @@ -94,13 +93,16 @@
}

isDownloadProgressVisible.value = true
downloadProgress.value = 0
downloadProgress.value = 0f

Check warning on line 96 in ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt#L96

Added line #L96 was not covered by tests
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 =

Check warning on line 99 in ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt#L99

Added line #L99 was not covered by tests
if (totalBytes > 0) {
(bytesDownloaded.toFloat() / totalBytes.toFloat()).coerceIn(0f, 1f)

Check warning on line 101 in ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt#L101

Added line #L101 was not covered by tests
} else {
0f

Check warning on line 103 in ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt#L103

Added line #L103 was not covered by tests
}
downloadProgress.postValue(progressValue)

Check warning on line 105 in ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt#L105

Added line #L105 was not covered by tests
}
isDownloadProgressVisible.postValue(false)
navigator.navigate(OfflineAreaSelectorFragmentDirections.offlineAreaBackToHomescreen())
Expand Down
42 changes: 0 additions & 42 deletions ground/src/main/res/layout/download_progress_dialog_frag.xml

This file was deleted.

2 changes: 1 addition & 1 deletion ground/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
<string name="offline_map_imagery_pref_description">Mostrar u ocultar las imágenes descargadas</string>
<string name="layers">Capas</string>
<string name="base_map">Mapa base</string>
<string name="offline_map_imagery_download_progress_dialog_title">Progreso de descarga</string>
<string name="offline_map_imagery_download_progress_dialog_title">Progreso de descarga - %d%%</string>
<string name="offline_map_imagery_download_progress_dialog_message">Descargando imágenes del mapa sin conexión…</string>
<string name="multiple_regions">Múltiples regiones</string>
<string name="permission_denied">Permiso denegado</string>
Expand Down
2 changes: 1 addition & 1 deletion ground/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
<string name="offline_map_imagery_pref_description">Cacher ou afficher l&#8217;imagerie cartographique téléchargée</string>
<string name="layers">Couches</string>
<string name="base_map">Carte de base</string>
<string name="offline_map_imagery_download_progress_dialog_title">Progrès du téléchargement</string>
<string name="offline_map_imagery_download_progress_dialog_title">Progrès du téléchargement - %d%%</string>
<string name="offline_map_imagery_download_progress_dialog_message">Téléchargement d&#8217;imagerie cartographique hors ligne…</string>
<string name="multiple_regions">Plusieurs régions</string>
<string name="permission_denied">Autorisation refusée</string>
Expand Down
4 changes: 2 additions & 2 deletions ground/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@
<string name="offline_map_imagery_pref_description">Hide or show downloaded imagery</string>
<string name="layers">Layers</string>
<string name="base_map">Base map</string>
<string name="offline_map_imagery_download_progress_dialog_title">Download progress</string>
<string name="offline_map_imagery_download_progress_dialog_message">Downloading offline map imagery…</string>
<string name="offline_map_imagery_download_progress_dialog_title">Downloading - %d%%</string>
<string name="offline_map_imagery_download_progress_dialog_message">Download times may vary depending on the size of the selected area and your internet connection.</string>
<string name="multiple_regions">Multiple regions</string>
<string name="permission_denied">Permission denied</string>
<string name="close_app">Close app</string>
Expand Down
Loading