Skip to content

Commit

Permalink
Replace dialog fragment with compose UI for downloading offline areas (
Browse files Browse the repository at this point in the history
  • Loading branch information
anandwana001 authored Nov 28, 2024
1 parent f2441dd commit 20d1540
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 112 deletions.
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 = {},
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)),
)
},
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.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
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 @@ class OfflineAreaSelectorFragment : AbstractMapContainerFragment() {
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 @@ class OfflineAreaSelectorFragment : AbstractMapContainerFragment() {
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 @@ class OfflineAreaSelectorFragment : AbstractMapContainerFragment() {
}

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,
// TODO - Add Download Cancel Feature
// https://github.com/google/ground-android/issues/2884
onDismiss = { openAlertDialog.value = false },
)
}
}
}
}
}

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

Expand All @@ -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())
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

0 comments on commit 20d1540

Please sign in to comment.