Sync on demand
parent
1be8760c00
commit
d31d302896
@ -1,9 +1,16 @@
|
|||||||
package org.koitharu.kotatsu.core.db
|
package org.koitharu.kotatsu.core.db
|
||||||
|
|
||||||
|
import androidx.room.InvalidationTracker
|
||||||
import org.koin.android.ext.koin.androidContext
|
import org.koin.android.ext.koin.androidContext
|
||||||
import org.koin.dsl.module
|
import org.koin.dsl.module
|
||||||
|
|
||||||
val databaseModule
|
val databaseModule
|
||||||
get() = module {
|
get() = module {
|
||||||
single { MangaDatabase(androidContext()) }
|
single {
|
||||||
|
MangaDatabase(androidContext()).also { db ->
|
||||||
|
getAll<InvalidationTracker.Observer>().forEach {
|
||||||
|
db.invalidationTracker.addObserver(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -1,14 +1,19 @@
|
|||||||
package org.koitharu.kotatsu.sync
|
package org.koitharu.kotatsu.sync
|
||||||
|
|
||||||
|
import androidx.room.InvalidationTracker
|
||||||
import org.koin.android.ext.koin.androidContext
|
import org.koin.android.ext.koin.androidContext
|
||||||
import org.koin.androidx.viewmodel.dsl.viewModel
|
import org.koin.androidx.viewmodel.dsl.viewModel
|
||||||
|
import org.koin.dsl.bind
|
||||||
import org.koin.dsl.module
|
import org.koin.dsl.module
|
||||||
import org.koitharu.kotatsu.sync.data.SyncAuthApi
|
import org.koitharu.kotatsu.sync.data.SyncAuthApi
|
||||||
|
import org.koitharu.kotatsu.sync.domain.SyncController
|
||||||
import org.koitharu.kotatsu.sync.ui.SyncAuthViewModel
|
import org.koitharu.kotatsu.sync.ui.SyncAuthViewModel
|
||||||
|
|
||||||
val syncModule
|
val syncModule
|
||||||
get() = module {
|
get() = module {
|
||||||
|
|
||||||
|
single { SyncController(androidContext()) } bind InvalidationTracker.Observer::class
|
||||||
|
|
||||||
factory { SyncAuthApi(androidContext(), get()) }
|
factory { SyncAuthApi(androidContext(), get()) }
|
||||||
|
|
||||||
viewModel { SyncAuthViewModel(get()) }
|
viewModel { SyncAuthViewModel(get()) }
|
||||||
|
|||||||
@ -0,0 +1,71 @@
|
|||||||
|
package org.koitharu.kotatsu.sync.domain
|
||||||
|
|
||||||
|
import android.accounts.Account
|
||||||
|
import android.accounts.AccountManager
|
||||||
|
import android.content.ContentResolver
|
||||||
|
import android.content.Context
|
||||||
|
import android.os.Bundle
|
||||||
|
import androidx.room.InvalidationTracker
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
import org.koitharu.kotatsu.R
|
||||||
|
import org.koitharu.kotatsu.core.db.TABLE_FAVOURITES
|
||||||
|
import org.koitharu.kotatsu.core.db.TABLE_FAVOURITE_CATEGORIES
|
||||||
|
import org.koitharu.kotatsu.core.db.TABLE_HISTORY
|
||||||
|
import org.koitharu.kotatsu.utils.ext.processLifecycleScope
|
||||||
|
|
||||||
|
class SyncController(
|
||||||
|
context: Context,
|
||||||
|
) : InvalidationTracker.Observer(arrayOf(TABLE_HISTORY, TABLE_FAVOURITES, TABLE_FAVOURITE_CATEGORIES)) {
|
||||||
|
|
||||||
|
private val am = AccountManager.get(context)
|
||||||
|
private val accountType = context.getString(R.string.account_type_sync)
|
||||||
|
|
||||||
|
override fun onInvalidated(tables: MutableSet<String>) {
|
||||||
|
requestSync(
|
||||||
|
favourites = TABLE_FAVOURITES in tables || TABLE_FAVOURITE_CATEGORIES in tables,
|
||||||
|
history = TABLE_HISTORY in tables,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun requestFullSync() = withContext(Dispatchers.Default) {
|
||||||
|
requestSyncImpl(favourites = true, history = true)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun requestSync(favourites: Boolean, history: Boolean) = processLifecycleScope.launch(Dispatchers.Default) {
|
||||||
|
requestSyncImpl(favourites, history)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Synchronized
|
||||||
|
private fun requestSyncImpl(favourites: Boolean, history: Boolean) {
|
||||||
|
if (!favourites && !history) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
val account = peekAccount() ?: return
|
||||||
|
if (!ContentResolver.getMasterSyncAutomatically()) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// TODO limit frequency
|
||||||
|
if (favourites) {
|
||||||
|
requestSyncForAuthority(account, AUTHORITY_FAVOURITES)
|
||||||
|
}
|
||||||
|
if (history) {
|
||||||
|
requestSyncForAuthority(account, AUTHORITY_HISTORY)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun requestSyncForAuthority(account: Account, authority: String) {
|
||||||
|
if (
|
||||||
|
ContentResolver.getSyncAutomatically(account, AUTHORITY_FAVOURITES) &&
|
||||||
|
!ContentResolver.isSyncActive(account, authority) &&
|
||||||
|
!ContentResolver.isSyncPending(account, authority)
|
||||||
|
) {
|
||||||
|
ContentResolver.requestSync(account, authority, Bundle.EMPTY)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun peekAccount(): Account? {
|
||||||
|
return am.getAccountsByType(accountType).firstOrNull()
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,10 +1,15 @@
|
|||||||
package org.koitharu.kotatsu.widget
|
package org.koitharu.kotatsu.widget
|
||||||
|
|
||||||
|
import androidx.room.InvalidationTracker
|
||||||
|
import org.koin.android.ext.koin.androidContext
|
||||||
import org.koin.androidx.viewmodel.dsl.viewModel
|
import org.koin.androidx.viewmodel.dsl.viewModel
|
||||||
import org.koin.dsl.module
|
import org.koin.dsl.module
|
||||||
import org.koitharu.kotatsu.widget.shelf.ShelfConfigViewModel
|
import org.koitharu.kotatsu.widget.shelf.ShelfConfigViewModel
|
||||||
|
|
||||||
val appWidgetModule
|
val appWidgetModule
|
||||||
get() = module {
|
get() = module {
|
||||||
|
|
||||||
|
single<InvalidationTracker.Observer> { WidgetUpdater(androidContext()) }
|
||||||
|
|
||||||
viewModel { ShelfConfigViewModel(get()) }
|
viewModel { ShelfConfigViewModel(get()) }
|
||||||
}
|
}
|
||||||
@ -1,10 +1,2 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<PreferenceScreen
|
<PreferenceScreen />
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android">
|
|
||||||
|
|
||||||
<Preference
|
|
||||||
android:persistent="false"
|
|
||||||
android:summary="Preference stub"
|
|
||||||
android:title="TODO" />
|
|
||||||
|
|
||||||
</PreferenceScreen>
|
|
||||||
Loading…
Reference in New Issue