Skip to content

Commit

Permalink
Use coroutine instead of Rx when fetching offlineArea from local db (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
shobhitagarwal1612 authored Oct 26, 2023
1 parent f138c3e commit 0ebb3ab
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ import androidx.room.Dao
import androidx.room.Query
import com.google.android.ground.persistence.local.room.entity.OfflineAreaEntity
import io.reactivex.Flowable
import io.reactivex.Maybe

/** Provides read/write operations for writing [OfflineAreaEntity] to the local db. */
@Dao
interface OfflineAreaDao : BaseDao<OfflineAreaEntity> {
@Query("SELECT * FROM offline_area") fun findAllOnceAndStream(): Flowable<List<OfflineAreaEntity>>

@Query("SELECT * FROM offline_area WHERE id = :id")
fun findById(id: String): Maybe<OfflineAreaEntity>
suspend fun findById(id: String): OfflineAreaEntity?

@Query("DELETE FROM offline_area WHERE id = :id") suspend fun deleteById(id: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import com.google.android.ground.persistence.local.room.entity.OfflineAreaEntity
import com.google.android.ground.persistence.local.stores.LocalOfflineAreaStore
import com.google.android.ground.rx.Schedulers
import io.reactivex.Flowable
import io.reactivex.Single
import javax.inject.Inject
import javax.inject.Singleton

Expand All @@ -42,8 +41,8 @@ class RoomOfflineAreaStore @Inject internal constructor() : LocalOfflineAreaStor
.map { areas: List<OfflineAreaEntity> -> areas.map { it.toModelObject() } }
.subscribeOn(schedulers.io())

override fun getOfflineAreaById(id: String): Single<OfflineArea> =
offlineAreaDao.findById(id).map { it.toModelObject() }.toSingle().subscribeOn(schedulers.io())
override suspend fun getOfflineAreaById(id: String): OfflineArea? =
offlineAreaDao.findById(id)?.toModelObject()

override suspend fun deleteOfflineArea(offlineAreaId: String) =
offlineAreaDao.deleteById(offlineAreaId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package com.google.android.ground.persistence.local.stores
import com.google.android.ground.model.imagery.OfflineArea
import com.google.android.ground.rx.annotations.Cold
import io.reactivex.Flowable
import io.reactivex.Single

interface LocalOfflineAreaStore {
/**
Expand All @@ -34,5 +33,5 @@ interface LocalOfflineAreaStore {
suspend fun deleteOfflineArea(offlineAreaId: String)

/** Returns the offline area with the specified id. */
fun getOfflineAreaById(id: String): Single<OfflineArea>
suspend fun getOfflineAreaById(id: String): OfflineArea?
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,23 @@ import com.google.android.ground.persistence.uuid.OfflineUuidGenerator
import com.google.android.ground.rx.annotations.Cold
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.map.gms.mog.MogClient
import com.google.android.ground.ui.map.gms.mog.MogCollection
import com.google.android.ground.ui.map.gms.mog.MogSource
import com.google.android.ground.ui.map.gms.mog.MogTileDownloader
import com.google.android.ground.ui.map.gms.mog.getTilePath
import com.google.android.ground.ui.map.gms.mog.maxZoom
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 io.reactivex.*
import io.reactivex.Flowable
import java.io.File
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.reactive.asFlow
import kotlinx.coroutines.reactive.awaitFirst
import timber.log.Timber
Expand Down Expand Up @@ -74,11 +81,8 @@ constructor(
fun offlineAreasOnceAndStream(): @Cold(terminates = false) Flowable<List<OfflineArea>> =
localOfflineAreaStore.offlineAreasOnceAndStream()

/**
* Fetches a single offline area by ID. Triggers `onError` when the area is not found. Triggers
* `onSuccess` when the area is found.
*/
fun getOfflineArea(offlineAreaId: String): @Cold Single<OfflineArea> =
/** Fetches a single offline area by ID. */
suspend fun getOfflineArea(offlineAreaId: String): OfflineArea? =
localOfflineAreaStore.getOfflineAreaById(offlineAreaId)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import com.google.android.ground.util.toMbString
import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.launch
import kotlinx.coroutines.rx2.await
import timber.log.Timber

/**
Expand Down Expand Up @@ -87,7 +86,7 @@ constructor(
fun initialize(args: OfflineAreaViewerFragmentArgs) {
offlineAreaId = args.offlineAreaId
viewModelScope.launch(ioDispatcher) {
val thisArea = offlineAreaRepository.getOfflineArea(offlineAreaId).await()
val thisArea = offlineAreaRepository.getOfflineArea(offlineAreaId)!!
area.postValue(thisArea)
areaSize.postValue(offlineAreaRepository.sizeOnDevice(thisArea).toMb().toMbString())
}
Expand Down

0 comments on commit 0ebb3ab

Please sign in to comment.