-
Notifications
You must be signed in to change notification settings - Fork 46
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 #140 from muun/51.6-release-branch
Apollo: Release source code for 51.6
- Loading branch information
Showing
20 changed files
with
312 additions
and
73 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
95 changes: 95 additions & 0 deletions
95
android/apollo/src/main/java/io/muun/apollo/data/preferences/BackgroundTimesRepository.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,95 @@ | ||
package io.muun.apollo.data.preferences | ||
|
||
import android.content.Context | ||
import io.muun.apollo.data.preferences.adapter.JsonListPreferenceAdapter | ||
import io.muun.apollo.data.preferences.rx.Preference | ||
import io.muun.apollo.domain.model.BackgroundEvent | ||
import javax.inject.Inject | ||
|
||
class BackgroundTimesRepository @Inject constructor( | ||
context: Context, | ||
repositoryRegistry: RepositoryRegistry, | ||
) : BaseRepository(context, repositoryRegistry) { | ||
|
||
companion object { | ||
private const val BACKGROUND_TIMES_KEY = "background_times_key" | ||
private const val LAST_BACKGROUND_BEGIN_TIME_KEY = "last_background_begin_time_key" | ||
} | ||
|
||
private class StoredBackgroundEvent { | ||
var beginTimeInMillis: Long = 0 | ||
var durationInMillis: Long = 0 | ||
|
||
/** | ||
* Constructor from the model. | ||
*/ | ||
constructor(bkgEvent: BackgroundEvent) { | ||
beginTimeInMillis = bkgEvent.beginTimeInMillis | ||
durationInMillis = bkgEvent.durationInMillis | ||
|
||
} | ||
|
||
/** | ||
* JSON constructor. | ||
*/ | ||
@Suppress("unused") | ||
constructor() | ||
|
||
fun toModel(): BackgroundEvent { | ||
return BackgroundEvent( | ||
beginTimeInMillis, | ||
durationInMillis | ||
) | ||
} | ||
} | ||
|
||
override fun getFileName(): String = | ||
"background_times" | ||
|
||
private val lastBackgroundBeginTimePreference: Preference<Long?> = | ||
rxSharedPreferences.getLong(LAST_BACKGROUND_BEGIN_TIME_KEY, null) | ||
|
||
private val backgroundTimesPreferences: Preference<List<StoredBackgroundEvent>> = | ||
rxSharedPreferences.getObject( | ||
BACKGROUND_TIMES_KEY, | ||
emptyList(), | ||
JsonListPreferenceAdapter(StoredBackgroundEvent::class.java) | ||
) | ||
|
||
fun recordEnterBackground() { | ||
lastBackgroundBeginTimePreference.set(System.currentTimeMillis()) | ||
} | ||
|
||
fun getLastBackgroundBeginTime(): Long? { | ||
return lastBackgroundBeginTimePreference.get() | ||
} | ||
|
||
fun recordBackgroundEvent(bkgBeginTime: Long, duration: Long) { | ||
val storedBkgTimes = getBackgroundTimes() | ||
val bkgTimes = storedBkgTimes.toMutableList() | ||
|
||
bkgTimes.add(BackgroundEvent(bkgBeginTime, duration)) | ||
|
||
storeBkgTimes(bkgTimes) | ||
lastBackgroundBeginTimePreference.set(null) | ||
} | ||
|
||
fun getBackgroundTimes(): List<BackgroundEvent> { | ||
return backgroundTimesPreferences.get()!!.map { it.toModel() } | ||
} | ||
|
||
fun pruneIfGreaterThan(maxBkgTimesArraySize: Int) { | ||
val storedBkgTimes = getBackgroundTimes() | ||
val bkgTimes = storedBkgTimes.takeLast(maxBkgTimesArraySize) | ||
|
||
storeBkgTimes(bkgTimes) | ||
} | ||
|
||
private fun storeBkgTimes(bkgTimes: List<BackgroundEvent>) { | ||
val storedBkgTimes = bkgTimes.map { it.toJson() } | ||
backgroundTimesPreferences.set(storedBkgTimes) | ||
} | ||
|
||
private fun BackgroundEvent.toJson(): StoredBackgroundEvent = | ||
StoredBackgroundEvent(this) | ||
} |
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
28 changes: 28 additions & 0 deletions
28
android/apollo/src/main/java/io/muun/apollo/domain/BackgroundTimesService.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,28 @@ | ||
package io.muun.apollo.domain | ||
|
||
import io.muun.apollo.data.preferences.BackgroundTimesRepository | ||
import javax.inject.Inject | ||
|
||
class BackgroundTimesService @Inject constructor( | ||
private val backgroundTimesRepository: BackgroundTimesRepository, | ||
) { | ||
|
||
private val MAX_BKG_TIMES_ARRAY_SIZE: Int = 100 | ||
|
||
fun enterBackground() { | ||
backgroundTimesRepository.recordEnterBackground() | ||
} | ||
|
||
fun enterForeground() { | ||
backgroundTimesRepository.pruneIfGreaterThan(MAX_BKG_TIMES_ARRAY_SIZE) | ||
|
||
val backgroundBeginTime = backgroundTimesRepository.getLastBackgroundBeginTime() | ||
@Suppress("FoldInitializerAndIfToElvis") | ||
if (backgroundBeginTime == null) { | ||
return | ||
} | ||
|
||
val duration = System.currentTimeMillis() - backgroundBeginTime | ||
backgroundTimesRepository.recordBackgroundEvent(backgroundBeginTime, duration) | ||
} | ||
} |
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.