Skip to content

Commit

Permalink
Remove dependency on survey.tileSources, instead use default Firebase…
Browse files Browse the repository at this point in the history
… storage path: /offline-imagery/default.
  • Loading branch information
sufyanAbbasi committed Feb 28, 2024
1 parent 3a628cf commit 08443c6
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
5 changes: 3 additions & 2 deletions ground/src/main/java/com/google/android/ground/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ object Config {
const val MAX_MEDIA_UPLOAD_RETRY_COUNT = 5

// TODO(#1730): Make sub-paths configurable and stop hardcoding here.
const val DEFAULT_MOG_TILE_URL = "/offline-imagery/default/"
const val DEFAULT_MOG_MIN_ZOOM = 8
const val DEFAULT_MOG_MAX_ZOOM = 14

fun getMogSources(path: String = "/offline-imagery/default/") =
fun getMogSources(path: String) =
listOf(
MogSource(
0 ..< DEFAULT_MOG_MIN_ZOOM,
0..<DEFAULT_MOG_MIN_ZOOM,
"$path/$DEFAULT_MOG_MIN_ZOOM/overview.tif",
),
MogSource(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ data class Survey(
val title: String,
val description: String,
val jobMap: Map<String, Job>,
// TODO(#1730): Allow tile source configuration from a non-survey accessible source.
val tileSources: List<TileSource> = listOf(),
val acl: Map<String, String> = mapOf()
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ import com.google.android.ground.ui.util.FileUtil
import com.google.android.ground.util.ByteCount
import com.google.android.ground.util.deleteIfEmpty
import com.google.android.ground.util.rangeOf
import java.io.File
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import timber.log.Timber
import java.io.File
import javax.inject.Inject
import javax.inject.Singleton

/**
* Corners of the viewport are scaled by this value when determining the name of downloaded areas.
Expand All @@ -51,8 +51,7 @@ const val AREA_NAME_SENSITIVITY = 0.5

@Singleton
class OfflineAreaRepository
@Inject
constructor(
@Inject constructor(
private val localOfflineAreaStore: LocalOfflineAreaStore,
private val surveyRepository: SurveyRepository,
private val fileUtil: FileUtil,
Expand Down Expand Up @@ -109,18 +108,16 @@ constructor(

fun getOfflineTileSourcesFlow() =
surveyRepository.activeSurveyFlow.combine(getOfflineAreaBounds()) { survey, bounds ->
applyBounds(survey?.tileSources, bounds)
applyBounds(getDefaultTileSources(), bounds)
}

private suspend fun applyBounds(
tileSources: List<TileSource>?,
bounds: List<Bounds>
tileSources: List<TileSource>?, bounds: List<Bounds>
): List<TileSource> =
tileSources?.mapNotNull { tileSource -> toOfflineTileSource(tileSource, bounds) } ?: listOf()

private suspend fun toOfflineTileSource(
tileSource: TileSource,
clipBounds: List<Bounds>
tileSource: TileSource, clipBounds: List<Bounds>
): TileSource? {
if (tileSource.type != TileSource.Type.MOG_COLLECTION) return null
return TileSource(
Expand All @@ -143,15 +140,15 @@ constructor(
return MogClient(mogCollection, remoteStorageManager)
}

private fun getMogSources(): List<MogSource> = Config.getMogSources(getFirstTileSourceUrl())
private fun getMogSources(): List<MogSource> =
Config.getMogSources(getDefaultTileSources().first().url)

/**
* Returns the URL of the first tile source in the current survey, or throws an error if no survey
* is active or if no tile sources are defined.
* Returns the default configured tile sources.
*
*/
private fun getFirstTileSourceUrl() =
surveyRepository.activeSurvey?.tileSources?.firstOrNull()?.url
?: error("Survey has no tile sources")
fun getDefaultTileSources(): List<TileSource> =
listOf(TileSource(url = Config.DEFAULT_MOG_TILE_URL, type = TileSource.Type.MOG_COLLECTION))

suspend fun hasHiResImagery(bounds: Bounds): Boolean {
val client = getMogClient()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import com.google.android.ground.repository.SurveyRepository
import com.google.android.ground.ui.common.AbstractViewModel
import com.google.android.ground.ui.common.Navigator
import com.google.android.ground.ui.common.SharedViewModel
import javax.inject.Inject
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject

@SharedViewModel
class HomeScreenViewModel
Expand All @@ -40,8 +40,9 @@ internal constructor(
private val _openDrawerRequests: MutableSharedFlow<Unit> = MutableSharedFlow()
val openDrawerRequestsFlow: SharedFlow<Unit> = _openDrawerRequests.asSharedFlow()

// TODO(#1730): Allow tile source configuration from a non-survey accessible source.
val showOfflineAreaMenuItem: LiveData<Boolean> =
surveyRepository.activeSurveyFlow.map { it?.tileSources?.isNotEmpty() ?: false }.asLiveData()
surveyRepository.activeSurveyFlow.map { true }.asLiveData()

fun openNavDrawer() {
viewModelScope.launch { _openDrawerRequests.emit(Unit) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ 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
import javax.inject.Inject

private const val MIN_DOWNLOAD_ZOOM_LEVEL = 9
private const val MAX_AREA_DOWNLOAD_SIZE_MB = 50
Expand Down Expand Up @@ -90,7 +90,7 @@ internal constructor(
)

init {
remoteTileSources = surveyRepository.activeSurvey!!.tileSources
remoteTileSources = offlineAreaRepository.getDefaultTileSources()
}

fun onDownloadClick() {
Expand Down

0 comments on commit 08443c6

Please sign in to comment.