-
Notifications
You must be signed in to change notification settings - Fork 1
fix: unify timed sheets behavior #556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
77c3848
fix: remove check on balance change
jvsena42 84a9085
chore: log context tag
jvsena42 9c558a2
refactor: move timed sheet logic to specific classes
jvsena42 0ed3260
test: timed sheet tests
jvsena42 b7879fd
refactor: replace timed sheet logic with manager
jvsena42 6e99ee1
fix: reimplement critical update logic
jvsena42 4e13983
fix: add two seconds delay
jvsena42 a72fc22
refactor: Migrate navigation to nav3
ovitrif 06d5091
chore: Update deps
ovitrif 4fe4ecc
Merge branch 'feat/nav3' into fix/uniffy-timed-sheet-behavior
jvsena42 a110582
Merge remote-tracking branch 'origin/fix/uniffy-timed-sheet-behavior'…
jvsena42 920aa1d
refactor: Migrate navigation to nav3
ovitrif 5b5fe6e
chore: Update deps
ovitrif 6059092
Merge remote-tracking branch 'origin/feat/nav3' into feat/nav3
jvsena42 eabe2c0
Merge branch 'feat/nav3' into fix/uniffy-timed-sheet-behavior
jvsena42 697bf90
refactor: Migrate navigation to nav3
ovitrif e73fbef
chore: Update deps
ovitrif a66ffe6
Merge remote-tracking branch 'origin/feat/nav3' into feat/nav3
jvsena42 b25be02
Merge branch 'feat/nav3' into fix/uniffy-timed-sheet-behavior
jvsena42 63897cf
fix: sheet navigation
jvsena42 989602d
fix: detect when the sheet is dismissed by swipe down
jvsena42 9bd5cc6
fix: return early if sheet is null
jvsena42 b37efac
fix: text log
jvsena42 2f1b45d
chore: lint
jvsena42 89dd6e7
refactor: replace try catch with runCatching
jvsena42 2f0b00c
fix: add transition delay
jvsena42 ba10eb3
fix: clear backstack on critical update navigation
jvsena42 ad97ef0
chore: remove redundant drop
jvsena42 8fe29b9
Merge branch 'feat/nav3' into fix/uniffy-timed-sheet-behavior
jvsena42 fe8b2d9
fix: detect dismiss by swipe down
jvsena42 b2e8b70
fix: clear backstack on critical update navigation
jvsena42 e72d4ce
Merge branch 'feat/nav3' into fix/uniffy-timed-sheet-behavior
jvsena42 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,18 @@ | ||
| package to.bitkit.di | ||
|
|
||
| import dagger.Module | ||
| import dagger.Provides | ||
| import dagger.hilt.InstallIn | ||
| import dagger.hilt.components.SingletonComponent | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import to.bitkit.utils.timedsheets.TimedSheetManager | ||
|
|
||
| @Module | ||
| @InstallIn(SingletonComponent::class) | ||
| object TimedSheetModule { | ||
|
|
||
| @Provides | ||
| fun provideTimedSheetManagerProvider(): (CoroutineScope) -> TimedSheetManager { | ||
| return ::TimedSheetManager | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
12 changes: 12 additions & 0 deletions
12
app/src/main/java/to/bitkit/utils/timedsheets/TimedSheetItem.kt
This file contains hidden or 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,12 @@ | ||
| package to.bitkit.utils.timedsheets | ||
|
|
||
| import to.bitkit.ui.components.TimedSheetType | ||
|
|
||
| interface TimedSheetItem { | ||
| val type: TimedSheetType | ||
| val priority: Int | ||
|
|
||
| suspend fun shouldShow(): Boolean | ||
| suspend fun onShown() | ||
| suspend fun onDismissed() | ||
| } |
87 changes: 87 additions & 0 deletions
87
app/src/main/java/to/bitkit/utils/timedsheets/TimedSheetManager.kt
This file contains hidden or 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,87 @@ | ||
| package to.bitkit.utils.timedsheets | ||
|
|
||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.Job | ||
| import kotlinx.coroutines.delay | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import kotlinx.coroutines.flow.asStateFlow | ||
| import kotlinx.coroutines.launch | ||
| import to.bitkit.ui.components.TimedSheetType | ||
| import to.bitkit.utils.Logger | ||
|
|
||
| class TimedSheetManager(private val scope: CoroutineScope) { | ||
| private val _currentSheet = MutableStateFlow<TimedSheetType?>(null) | ||
| val currentSheet: StateFlow<TimedSheetType?> = _currentSheet.asStateFlow() | ||
|
|
||
| private val registeredSheets = mutableListOf<TimedSheetItem>() | ||
| private var currentTimedSheet: TimedSheetItem? = null | ||
| private var checkJob: Job? = null | ||
|
|
||
| fun registerSheet(sheet: TimedSheetItem) { | ||
| registeredSheets.add(sheet) | ||
| registeredSheets.sortByDescending { it.priority } | ||
| Logger.debug( | ||
| "Registered timed sheet: ${sheet.type.name} with priority: ${sheet.priority}", | ||
| context = TAG | ||
| ) | ||
| } | ||
|
|
||
| fun onHomeScreenEntered() { | ||
| Logger.debug("User entered home screen, starting timer", context = TAG) | ||
| checkJob?.cancel() | ||
| checkJob = scope.launch { | ||
| delay(CHECK_DELAY_MILLIS) | ||
| checkAndShowNextSheet() | ||
| } | ||
| } | ||
|
|
||
| fun onHomeScreenExited() { | ||
| Logger.debug("User exited home screen, cancelling timer", context = TAG) | ||
| checkJob?.cancel() | ||
| checkJob = null | ||
| } | ||
|
|
||
| fun dismissCurrentSheet(skipQueue: Boolean = false) { | ||
| if (currentTimedSheet == null) return | ||
|
|
||
| scope.launch { | ||
| currentTimedSheet?.onDismissed() | ||
| _currentSheet.value = null | ||
| currentTimedSheet = null | ||
|
|
||
| if (skipQueue) { | ||
| Logger.debug("Clearing timed sheet queue", context = TAG) | ||
| } else { | ||
| delay(CHECK_DELAY_MILLIS) | ||
| checkAndShowNextSheet() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private suspend fun checkAndShowNextSheet() { | ||
| Logger.debug("Registered sheets: ${registeredSheets.map { it.type.name }}") | ||
| for (sheet in registeredSheets.toList()) { | ||
| if (sheet.shouldShow()) { | ||
| Logger.debug( | ||
| "Showing timed sheet: ${sheet.type.name} with priority: ${sheet.priority}", | ||
| context = TAG | ||
| ) | ||
| currentTimedSheet = sheet | ||
| _currentSheet.value = sheet.type | ||
| sheet.onShown() | ||
| registeredSheets.remove(sheet) | ||
ovitrif marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return | ||
| } | ||
| } | ||
|
|
||
| Logger.debug("No timed sheets need to be shown", context = TAG) | ||
| _currentSheet.value = null | ||
| currentTimedSheet = null | ||
| } | ||
|
|
||
| companion object { | ||
| private const val TAG = "TimedSheetManager" | ||
| private const val CHECK_DELAY_MILLIS = 2000L | ||
| } | ||
| } | ||
21 changes: 21 additions & 0 deletions
21
app/src/main/java/to/bitkit/utils/timedsheets/TimedSheetUtils.kt
This file contains hidden or 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,21 @@ | ||
| package to.bitkit.utils.timedsheets | ||
|
|
||
| import kotlin.time.Clock | ||
| import kotlin.time.ExperimentalTime | ||
|
|
||
| @OptIn(ExperimentalTime::class) | ||
| fun checkTimeout( | ||
| lastIgnoredMillis: Long, | ||
| intervalMillis: Long, | ||
| additionalCondition: Boolean = true, | ||
| ): Boolean { | ||
| if (!additionalCondition) return false | ||
|
|
||
| val currentTime = Clock.System.now().toEpochMilliseconds() | ||
| val isTimeOutOver = lastIgnoredMillis == 0L || | ||
| (currentTime - lastIgnoredMillis > intervalMillis) | ||
| return isTimeOutOver | ||
| } | ||
|
|
||
| const val ONE_DAY_ASK_INTERVAL_MILLIS = 1000 * 60 * 60 * 24L | ||
| const val ONE_WEEK_ASK_INTERVAL_MILLIS = ONE_DAY_ASK_INTERVAL_MILLIS * 7L |
47 changes: 47 additions & 0 deletions
47
app/src/main/java/to/bitkit/utils/timedsheets/sheets/AppUpdateTimedSheet.kt
This file contains hidden or 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,47 @@ | ||
| package to.bitkit.utils.timedsheets.sheets | ||
|
|
||
| import kotlinx.coroutines.CoroutineDispatcher | ||
| import kotlinx.coroutines.withContext | ||
| import to.bitkit.BuildConfig | ||
| import to.bitkit.di.BgDispatcher | ||
| import to.bitkit.services.AppUpdaterService | ||
| import to.bitkit.ui.components.TimedSheetType | ||
| import to.bitkit.utils.Logger | ||
| import to.bitkit.utils.timedsheets.TimedSheetItem | ||
| import javax.inject.Inject | ||
|
|
||
| class AppUpdateTimedSheet @Inject constructor( | ||
| private val appUpdaterService: AppUpdaterService, | ||
| @BgDispatcher private val bgDispatcher: CoroutineDispatcher, | ||
| ) : TimedSheetItem { | ||
| override val type = TimedSheetType.APP_UPDATE | ||
| override val priority = 5 | ||
|
|
||
| override suspend fun shouldShow(): Boolean = withContext(bgDispatcher) { | ||
| return@withContext runCatching { | ||
| val androidReleaseInfo = appUpdaterService.getReleaseInfo().platforms.android | ||
| val currentBuildNumber = BuildConfig.VERSION_CODE | ||
|
|
||
| if (androidReleaseInfo.buildNumber <= currentBuildNumber) return@withContext false | ||
|
|
||
| if (androidReleaseInfo.isCritical) { | ||
| return@runCatching false | ||
| } | ||
| return@runCatching true | ||
| }.onFailure { e -> | ||
| Logger.warn("Failure fetching new releases", e = e, context = TAG) | ||
| }.getOrDefault(false) | ||
| } | ||
|
|
||
| override suspend fun onShown() { | ||
| Logger.debug("App update sheet shown", context = TAG) | ||
| } | ||
|
|
||
| override suspend fun onDismissed() { | ||
| Logger.debug("App update sheet dismissed", context = TAG) | ||
| } | ||
|
|
||
| companion object { | ||
| private const val TAG = "AppUpdateTimedSheet" | ||
| } | ||
| } |
51 changes: 51 additions & 0 deletions
51
app/src/main/java/to/bitkit/utils/timedsheets/sheets/BackupTimedSheet.kt
This file contains hidden or 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,51 @@ | ||
| package to.bitkit.utils.timedsheets.sheets | ||
|
|
||
| import kotlinx.coroutines.flow.first | ||
| import to.bitkit.data.SettingsStore | ||
| import to.bitkit.ext.nowMillis | ||
| import to.bitkit.repositories.WalletRepo | ||
| import to.bitkit.ui.components.TimedSheetType | ||
| import to.bitkit.utils.Logger | ||
| import to.bitkit.utils.timedsheets.ONE_DAY_ASK_INTERVAL_MILLIS | ||
| import to.bitkit.utils.timedsheets.TimedSheetItem | ||
| import to.bitkit.utils.timedsheets.checkTimeout | ||
| import javax.inject.Inject | ||
| import kotlin.time.ExperimentalTime | ||
|
|
||
| class BackupTimedSheet @Inject constructor( | ||
| private val settingsStore: SettingsStore, | ||
| private val walletRepo: WalletRepo, | ||
| ) : TimedSheetItem { | ||
| override val type = TimedSheetType.BACKUP | ||
| override val priority = 4 | ||
|
|
||
| override suspend fun shouldShow(): Boolean { | ||
| val settings = settingsStore.data.first() | ||
| if (settings.backupVerified) return false | ||
|
|
||
| val hasBalance = walletRepo.balanceState.value.totalSats > 0U | ||
| if (!hasBalance) return false | ||
|
|
||
| return checkTimeout( | ||
| lastIgnoredMillis = settings.backupWarningIgnoredMillis, | ||
| intervalMillis = ONE_DAY_ASK_INTERVAL_MILLIS | ||
| ) | ||
| } | ||
|
|
||
| override suspend fun onShown() { | ||
| Logger.debug("Backup sheet shown", context = TAG) | ||
| } | ||
|
|
||
| @OptIn(ExperimentalTime::class) | ||
| override suspend fun onDismissed() { | ||
| val currentTime = nowMillis() | ||
| settingsStore.update { | ||
| it.copy(backupWarningIgnoredMillis = currentTime) | ||
| } | ||
| Logger.debug("Backup sheet dismissed", context = TAG) | ||
| } | ||
|
|
||
| companion object { | ||
| private const val TAG = "BackupTimedSheet" | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.