Skip to content

Commit

Permalink
Minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
gino-m committed Oct 6, 2023
1 parent f2fb9b7 commit b7f1ccc
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import com.google.android.ground.system.GeocodingManager
import com.google.android.ground.ui.map.Bounds
import com.google.android.ground.ui.map.gms.mog.*
import com.google.android.ground.ui.util.FileUtil
import com.google.android.ground.util.deleteIfEmpty
import com.google.android.ground.util.rangeOf
import io.reactivex.*
import java.io.File
import javax.inject.Inject
import javax.inject.Singleton
import kotlin.math.max
import kotlin.math.min
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.reactive.asFlow
import kotlinx.coroutines.reactive.awaitFirst
Expand Down Expand Up @@ -180,14 +180,3 @@ constructor(
}
}
}

private fun File?.isEmpty() = this?.listFiles().isNullOrEmpty()

private fun File?.deleteIfEmpty() {
if (isEmpty()) this?.delete()
}

private inline fun <T> Iterable<T>.rangeOf(selector: (T) -> Int): IntRange =
map(selector)
.map { IntRange(it, it) }
.reduce { out, el -> IntRange(min(out.first, el.first), max(out.last, el.last)) }
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ 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.MapType
import com.google.android.ground.util.toMb
import com.google.android.ground.util.toMbString
import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -139,7 +141,7 @@ internal constructor(
}
sizeOnDisk.postValue(offlineAreaSizeLoadingSymbol)
visibleBottomTextViewId.postValue(R.id.size_on_disk_text_view)
val sizeInMb = offlineAreaRepository.estimateSizeOnDisk(bounds) / (1024f * 1024f)
val sizeInMb = offlineAreaRepository.estimateSizeOnDisk(bounds).toMb()
if (sizeInMb > MAX_AREA_DOWNLOAD_SIZE_MB) {
onLargeAreaSelected()
} else {
Expand All @@ -153,8 +155,7 @@ internal constructor(
}

private fun onDownloadableAreaSelected(sizeInMb: Float) {
val sizeString = if (sizeInMb < 0.1f) "<0.1" else "%.1f".format(sizeInMb)
sizeOnDisk.postValue(sizeString)
sizeOnDisk.postValue(sizeInMb.toMbString())
visibleBottomTextViewId.postValue(R.id.size_on_disk_text_view)
downloadButtonEnabled.postValue(true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import com.google.android.ground.ui.common.BaseMapViewModel
import com.google.android.ground.ui.common.MapConfig
import com.google.android.ground.ui.common.Navigator
import com.google.android.ground.ui.map.MapType
import com.google.android.ground.util.toMbString
import com.google.android.ground.util.toMb
import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -68,7 +70,7 @@ constructor(
/** Returns the offline area associated with this view model. */
val area = MutableLiveData<OfflineArea>()
val areaName = area.map { it.name }
val areaSize = MutableLiveData<Float>()
val areaSize = MutableLiveData<String>()
val progressOverlayVisible = MutableLiveData<Boolean>()

private var offlineAreaId: String? = null
Expand All @@ -87,7 +89,7 @@ constructor(
viewModelScope.launch(ioDispatcher) {
val thisArea = offlineAreaRepository.getOfflineArea(offlineAreaId!!).await()
area.postValue(thisArea)
areaSize.postValue((offlineAreaRepository.sizeOnDevice(thisArea) / 1024f * 1024f))
areaSize.postValue((offlineAreaRepository.sizeOnDevice(thisArea).toMb().toMbString()))
}
}

Expand Down
41 changes: 41 additions & 0 deletions ground/src/main/java/com/google/android/ground/util/FileExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.
*/

package com.google.android.ground.util

import java.io.File
import kotlin.math.ceil

typealias ByteCount = Int

typealias MegabyteCount = Float

/** Return true iff the file is non-null and contains other files. */
fun File?.isEmpty() = this?.listFiles().isNullOrEmpty()

/** Deletes the file if itt is non-null and doesn't contain other files. */
fun File?.deleteIfEmpty() {
if (isEmpty()) this?.delete()
}

/** Returns the byte count as an equivalent megabyte count. */
fun ByteCount.toMb(): MegabyteCount = this / 1024f * 1024f

/**
* Returns the number of megabytes a string, replacing smaller sizes with "<1" and rounding up
* others.
*/
fun MegabyteCount.toMbString(): String = if (this < 1) "<1" else ceil(this).toString()
29 changes: 29 additions & 0 deletions ground/src/main/java/com/google/android/ground/util/RangeExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.
*/

package com.google.android.ground.util

import kotlin.math.max
import kotlin.math.min

/**
* Returns a range containing the smallest and largest among all values produced by the selector
* function applied to each element in the iterable collection.
*/
inline fun <T> Iterable<T>.rangeOf(selector: (T) -> Int): IntRange =
map(selector)
.map { IntRange(it, it) }
.reduce { out, el -> IntRange(min(out.first, el.first), max(out.last, el.last)) }
2 changes: 1 addition & 1 deletion ground/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -171,5 +171,5 @@
<string name="area">Area</string>
<string name="unnamed_point">Unnamed point</string>
<string name="unnamed_area">Unnamed area</string>
<string name="offline_area_size_on_disk_mb">%.1f\u00A0MB on disk</string>
<string name="offline_area_size_on_disk_mb">%s\u00A0MB on disk</string>
</resources>

0 comments on commit b7f1ccc

Please sign in to comment.