Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
Expand Down
18 changes: 16 additions & 2 deletions app/src/main/java/me/ash/reader/domain/data/DiffMapHolder.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package me.ash.reader.domain.data

import android.content.Context
import android.net.ConnectivityManager
import android.net.Network
import androidx.compose.runtime.mutableStateMapOf
import androidx.compose.runtime.snapshotFlow
import com.google.gson.Gson
Expand All @@ -13,6 +15,10 @@ import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.Job
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.merge
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.mapNotNull
Expand Down Expand Up @@ -110,9 +116,17 @@ class DiffMapHolder @Inject constructor(

private fun syncOnChange() {
remoteJob = applicationScope.launch(ioDispatcher) {
pendingSyncDiffsSnapshotFlow.debounce(2_000).collect {
val networkAvailable = callbackFlow {
val manager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val callback = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) { trySend(Unit) }
}
manager.registerDefaultNetworkCallback(callback)
awaitClose { manager.unregisterNetworkCallback(callback) }
}
merge(pendingSyncDiffsSnapshotFlow.map { Unit }, networkAvailable).debounce(2_000).collect {
withContext(ioDispatcher) {
syncDiffsWithRemote(it)
syncDiffsWithRemote(pendingSyncDiffs.toMap())
}
}
}
Expand Down
Loading