-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #125 from CrisisCleanup/onsite-fixes
Onsite fixes
- Loading branch information
Showing
31 changed files
with
596 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
core/data/src/main/java/com/crisiscleanup/core/data/repository/ShareLocationRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.crisiscleanup.core.data.repository | ||
|
||
import com.crisiscleanup.core.common.LocationProvider | ||
import com.crisiscleanup.core.common.log.AppLogger | ||
import com.crisiscleanup.core.common.log.CrisisCleanupLoggers | ||
import com.crisiscleanup.core.common.log.Logger | ||
import com.crisiscleanup.core.network.CrisisCleanupWriteApi | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.datetime.Clock | ||
import kotlinx.datetime.Instant | ||
import java.util.concurrent.atomic.AtomicReference | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
import kotlin.time.Duration.Companion.minutes | ||
|
||
interface ShareLocationRepository { | ||
suspend fun shareLocation() | ||
} | ||
|
||
@Singleton | ||
class CrisisCleanupShareLocationRepository @Inject | ||
constructor( | ||
private val accountDataRepository: AccountDataRepository, | ||
private val appPreferencesRepository: LocalAppPreferencesRepository, | ||
private val locationProvider: LocationProvider, | ||
private val writeApiClient: CrisisCleanupWriteApi, | ||
@Logger(CrisisCleanupLoggers.App) private val logger: AppLogger, | ||
) : ShareLocationRepository { | ||
|
||
private var shareTimestamp = AtomicReference(Instant.fromEpochSeconds(0)) | ||
private val shareInterval = 1.minutes | ||
|
||
override suspend fun shareLocation() { | ||
val shareLocationWithOrg = | ||
appPreferencesRepository.userPreferences.first().shareLocationWithOrg | ||
val areTokensValid = accountDataRepository.accountData.first().areTokensValid | ||
if (shareLocationWithOrg && | ||
areTokensValid | ||
) { | ||
locationProvider.getLocation()?.let { location -> | ||
synchronized(shareTimestamp) { | ||
val now = Clock.System.now() | ||
if (shareTimestamp.get() + shareInterval > now) { | ||
return | ||
} | ||
shareTimestamp.set(now) | ||
} | ||
|
||
try { | ||
writeApiClient.shareLocation(location.first, location.second) | ||
} catch (e: Exception) { | ||
logger.logException(e) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.