generated from JetBrains/compose-multiplatform-template
-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refresh feeds based on when they are last updated when app is opened (#…
…104) * Refresh feeds based on when they are last updated when app is opened At this point we are using a unified last updated at, but we can break it down for individual feeds if we want to in future. * Update last updated at after running refresh worker on Android * Update last updated at after doing background refresh on iOS
- Loading branch information
1 parent
f2542af
commit 6c1c117
Showing
6 changed files
with
102 additions
and
5 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
58 changes: 58 additions & 0 deletions
58
shared/src/commonMain/kotlin/dev/sasikanth/rss/reader/refresh/LastUpdatedAt.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,58 @@ | ||
/* | ||
* Copyright 2023 Sasikanth Miriyampalli | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dev.sasikanth.rss.reader.refresh | ||
|
||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.edit | ||
import androidx.datastore.preferences.core.stringPreferencesKey | ||
import dev.sasikanth.rss.reader.di.scopes.AppScope | ||
import kotlin.time.Duration.Companion.minutes | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.datetime.Clock | ||
import kotlinx.datetime.toInstant | ||
import me.tatarka.inject.annotations.Inject | ||
|
||
@Inject | ||
@AppScope | ||
class LastUpdatedAt(private val dataStore: DataStore<Preferences>) { | ||
|
||
companion object { | ||
private val UPDATE_DURATION = 60.minutes | ||
} | ||
|
||
private val lastUpdatedAtKey = stringPreferencesKey("pref_last_updated_at") | ||
|
||
suspend fun refresh() { | ||
dataStore.edit { preferences -> preferences[lastUpdatedAtKey] = Clock.System.now().toString() } | ||
} | ||
|
||
suspend fun hasExpired(): Boolean { | ||
val lastUpdatedAt = fetchLastUpdatedAt() ?: return true | ||
val currentTime = Clock.System.now() | ||
val lastUpdateDuration = currentTime - lastUpdatedAt | ||
|
||
return lastUpdateDuration > UPDATE_DURATION | ||
} | ||
|
||
private suspend fun fetchLastUpdatedAt() = | ||
dataStore.data | ||
.map { preferences -> preferences[lastUpdatedAtKey] ?: return@map null } | ||
.first() | ||
?.toInstant() | ||
} |