Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
gino-m committed Sep 7, 2023
1 parent b6f72cd commit 6e71857
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 4 deletions.
2 changes: 1 addition & 1 deletion ground/src/main/java/com/google/android/ground/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ object Config {

// Local db settings.
// TODO(#128): Reset version to 1 before releasing.
const val DB_VERSION = 103
const val DB_VERSION = 104
const val DB_NAME = "ground.db"

// Firebase Cloud Firestore settings.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,10 @@ constructor(
private fun getFirstTileSourceUrl() =
surveyRepository.activeSurvey?.tileSources?.firstOrNull()?.url
?: error("Survey has no tile sources")

suspend fun estimateSizeOnDisk(bounds: Bounds): Int {
val client = getMogClient()
val requests = client.buildTilesRequests(bounds.toGoogleMapsObject())
return requests.sumOf { it.totalBytes }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package com.google.android.ground.ui.offlinebasemap.selector

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.google.android.ground.R
import com.google.android.ground.coroutines.IoDispatcher
import com.google.android.ground.model.imagery.TileSource
import com.google.android.ground.repository.LocationOfInterestRepository
Expand All @@ -30,12 +31,17 @@ import com.google.android.ground.ui.common.BaseMapViewModel
import com.google.android.ground.ui.common.Navigator
import com.google.android.ground.ui.common.SharedViewModel
import com.google.android.ground.ui.map.Bounds
import com.google.android.ground.ui.map.CameraPosition
import com.google.android.ground.ui.map.Map
import com.google.android.ground.ui.map.MapType
import javax.inject.Inject
import kotlin.math.round
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.launch

private const val MIN_DOWNLOAD_ZOOM_LEVEL = 10
private const val MAX_AREA_DOWNLOAD_SIZE_MB = 50

/** States and behaviors of Map UI used to select areas for download and viewing offline. */
@SharedViewModel
class OfflineAreaSelectorViewModel
Expand Down Expand Up @@ -67,6 +73,9 @@ internal constructor(
val isDownloadProgressVisible = MutableLiveData(false)
val downloadProgressMax = MutableLiveData(0)
val downloadProgress = MutableLiveData(0)
val sizeOnDisk = MutableLiveData<String>(null)
val visibleBottomTextView = MutableLiveData<Int>(null)
val downloadButtonEnabled = MutableLiveData(false)

init {
tileSources = surveyRepository.activeSurvey!!.tileSources
Expand All @@ -81,11 +90,11 @@ internal constructor(
isDownloadProgressVisible.value = true
downloadProgress.value = 0
viewModelScope.launch(ioDispatcher) {
offlineAreaRepository.downloadTiles(viewport!!).collect { (byteDownloaded, totalBytes) ->
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(byteDownloaded)
downloadProgress.postValue(bytesDownloaded)
}
isDownloadProgressVisible.postValue(false)
navigator.navigateUp()
Expand All @@ -97,4 +106,31 @@ internal constructor(
tileSources.forEach { map.addTileOverlay(it) }
disposeOnClear(cameraBoundUpdates.subscribe { viewport = it })
}

override fun onMapCameraMoved(newCameraPosition: CameraPosition) {
super.onMapCameraMoved(newCameraPosition)
val (_, zoomLevel, _, bounds) = newCameraPosition
if (bounds == null || zoomLevel == null) return
if (zoomLevel < MIN_DOWNLOAD_ZOOM_LEVEL) {
visibleBottomTextView.value = R.id.area_too_large_text_view
downloadButtonEnabled.value = false
} else {
sizeOnDisk.value = ""
visibleBottomTextView.value = R.id.size_on_disk_text_view
viewModelScope.launch(ioDispatcher) {
val size = offlineAreaRepository.estimateSizeOnDisk(bounds) / (1024f * 1024f)
if (size > MAX_AREA_DOWNLOAD_SIZE_MB) {
visibleBottomTextView.postValue(R.id.area_too_large_text_view)
downloadButtonEnabled.postValue(false)
} else {
if (size < 1f) {
sizeOnDisk.postValue("<1")
} else {
sizeOnDisk.postValue(round(size).toInt().toString())
}
downloadButtonEnabled.postValue(true)
}
}
}
}
}
21 changes: 21 additions & 0 deletions ground/src/main/res/color/color_states_chip_button.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>

<!--
~ Copyright 2023 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.
-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="@color/md_theme_primary" />
<item android:color="@color/md_theme_on_surface_disabled" />
</selector>
35 changes: 35 additions & 0 deletions ground/src/main/res/layout/offline_base_map_selector_frag.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>
<import type="com.google.android.ground.R" />
<import type="android.view.View" />
<variable
name="viewModel"
type="com.google.android.ground.ui.offlinebasemap.selector.OfflineAreaSelectorViewModel" />
Expand Down Expand Up @@ -70,10 +72,43 @@
app:backgroundTint="@color/colorMapAccent"
app:chipIcon="@drawable/ic_area_download"
app:icon="@drawable/ic_area_download"
android:enabled="@{viewModel.downloadButtonEnabled}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:useCompatPadding="true" />
</com.google.android.ground.ui.common.TwoLineToolbar>
<TextView
android:id="@+id/size_on_disk_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:textAlignment="center"
android:textSize="20sp"
android:textColor="@color/textOverMap"
android:fontFamily="Google Sans Text"
android:layout_marginLeft="16sp"
android:layout_marginRight="16sp"
android:layout_marginBottom="24sp"
android:visibility="@{viewModel.visibleBottomTextView == R.id.size_on_disk_text_view ? View.VISIBLE : View.GONE}"
android:text="@{@string/selected_area_size_on_disk(viewModel.sizeOnDisk)}"/>
<TextView
android:id="@+id/area_too_large_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:textAlignment="center"
android:textSize="20sp"
android:textColor="@color/textOverMap"
android:fontFamily="Google Sans Text"
android:layout_marginLeft="16sp"
android:layout_marginRight="16sp"
android:layout_marginBottom="24sp"
android:visibility="@{viewModel.visibleBottomTextView == R.id.area_too_large_text_view ? View.VISIBLE : View.GONE}"
android:text="@string/selected_area_too_large"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
</layout>
1 change: 1 addition & 0 deletions ground/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<color name="colorMapAccent">#ff9131</color>
<color name="clusterColor">#6DDD81</color>
<color name="polyLineColor">#55ffffff</color>
<color name="textOverMap">#FCFDF7</color>

<!-- Updated Color palette -->
<color name="md_theme_primary">#006E2C</color>
Expand Down
2 changes: 2 additions & 0 deletions ground/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,6 @@
<string name="contact_survey_organizer_to_obtain_access">Contact your system administrator to request access</string>
<string name="close_app">Close app</string>
<string name="warning_sign_out">Warning: If you sign out, all unsaved data will be lost </string>
<string name="selected_area_size_on_disk">The selected area may take up to %s\u00A0MB of space on your device</string>
<string name="selected_area_too_large">Zoom in and select a smaller area to download to your device</string>
</resources>
3 changes: 2 additions & 1 deletion ground/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@
</style>

<style name="ChipButton" parent="Widget.MaterialComponents.Chip.Action">
<item name="chipBackgroundColor">?attr/colorPrimary</item>

<item name="backgroundTint">@color/color_states_chip_button</item>
<item name="chipCornerRadius">@dimen/chip_button_corner_radius</item>
<item name="chipStartPadding">@dimen/chip_button_padding</item>
<item name="chipEndPadding">@dimen/chip_button_padding</item>
Expand Down

0 comments on commit 6e71857

Please sign in to comment.