diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/SyncSettingsFragment.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/SyncSettingsFragment.kt index 60a609307..86ae24845 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/SyncSettingsFragment.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/SyncSettingsFragment.kt @@ -30,7 +30,7 @@ class SyncSettingsFragment : BasePreferenceFragment(R.string.sync_settings), Fra override fun onPreferenceTreeClick(preference: Preference): Boolean { return when (preference.key) { SyncSettings.KEY_HOST -> { - SyncHostDialogFragment.show(childFragmentManager) + SyncHostDialogFragment.show(childFragmentManager, null) true } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/sync/domain/SyncHelper.kt b/app/src/main/kotlin/org/koitharu/kotatsu/sync/domain/SyncHelper.kt index d29760fa8..4051ae822 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/sync/domain/SyncHelper.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/sync/domain/SyncHelper.kt @@ -7,6 +7,7 @@ import android.content.ContentProviderResult import android.content.Context import android.content.OperationApplicationException import android.content.SyncResult +import android.content.SyncStats import android.database.Cursor import android.net.Uri import androidx.annotation.WorkerThread @@ -67,7 +68,7 @@ class SyncHelper @AssistedInject constructor( get() = TimeUnit.DAYS.toMillis(4) @WorkerThread - fun syncFavourites(syncResult: SyncResult) { + fun syncFavourites(stats: SyncStats) { val data = JSONObject() data.put(TABLE_FAVOURITE_CATEGORIES, getFavouriteCategories()) data.put(TABLE_FAVOURITES, getFavourites()) @@ -81,17 +82,18 @@ class SyncHelper @AssistedInject constructor( val timestamp = response.getLong(FIELD_TIMESTAMP) val categoriesResult = upsertFavouriteCategories(response.getJSONArray(TABLE_FAVOURITE_CATEGORIES), timestamp) - syncResult.stats.numDeletes += categoriesResult.first().count?.toLong() ?: 0L - syncResult.stats.numInserts += categoriesResult.drop(1).sumOf { it.count?.toLong() ?: 0L } + stats.numDeletes += categoriesResult.first().count?.toLong() ?: 0L + stats.numInserts += categoriesResult.drop(1).sumOf { it.count?.toLong() ?: 0L } val favouritesResult = upsertFavourites(response.getJSONArray(TABLE_FAVOURITES), timestamp) - syncResult.stats.numDeletes += favouritesResult.first().count?.toLong() ?: 0L - syncResult.stats.numInserts += favouritesResult.drop(1).sumOf { it.count?.toLong() ?: 0L } + stats.numDeletes += favouritesResult.first().count?.toLong() ?: 0L + stats.numInserts += favouritesResult.drop(1).sumOf { it.count?.toLong() ?: 0L } + stats.numEntries += stats.numInserts + stats.numDeletes } gcFavourites() } @WorkerThread - fun syncHistory(syncResult: SyncResult) { + fun syncHistory(stats: SyncStats) { val data = JSONObject() data.put(TABLE_HISTORY, getHistory()) data.put(FIELD_TIMESTAMP, System.currentTimeMillis()) @@ -105,8 +107,9 @@ class SyncHelper @AssistedInject constructor( json = response.getJSONArray(TABLE_HISTORY), timestamp = response.getLong(FIELD_TIMESTAMP), ) - syncResult.stats.numDeletes += result.first().count?.toLong() ?: 0L - syncResult.stats.numInserts += result.drop(1).sumOf { it.count?.toLong() ?: 0L } + stats.numDeletes += result.first().count?.toLong() ?: 0L + stats.numInserts += result.drop(1).sumOf { it.count?.toLong() ?: 0L } + stats.numEntries += stats.numInserts + stats.numDeletes } gcHistory() } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/sync/ui/SyncAuthActivity.kt b/app/src/main/kotlin/org/koitharu/kotatsu/sync/ui/SyncAuthActivity.kt index a7cd65491..2f90ac328 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/sync/ui/SyncAuthActivity.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/sync/ui/SyncAuthActivity.kt @@ -104,7 +104,7 @@ class SyncAuthActivity : BaseActivity(), View.OnClickLi } R.id.button_settings -> { - SyncHostDialogFragment.show(supportFragmentManager) + SyncHostDialogFragment.show(supportFragmentManager, viewModel.host.value) } } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/sync/ui/SyncAuthViewModel.kt b/app/src/main/kotlin/org/koitharu/kotatsu/sync/ui/SyncAuthViewModel.kt index fd02134fa..be890d55c 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/sync/ui/SyncAuthViewModel.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/sync/ui/SyncAuthViewModel.kt @@ -10,7 +10,6 @@ import org.koitharu.kotatsu.R import org.koitharu.kotatsu.core.ui.BaseViewModel import org.koitharu.kotatsu.core.util.ext.MutableEventFlow import org.koitharu.kotatsu.core.util.ext.call -import org.koitharu.kotatsu.core.util.ext.ifNullOrEmpty import org.koitharu.kotatsu.sync.data.SyncAuthApi import org.koitharu.kotatsu.sync.domain.SyncAuthResult import javax.inject.Inject @@ -23,9 +22,7 @@ class SyncAuthViewModel @Inject constructor( val onAccountAlreadyExists = MutableEventFlow() val onTokenObtained = MutableEventFlow() - val host = MutableStateFlow("") - - private val defaultHost = context.getString(R.string.sync_host_default) + val host = MutableStateFlow(context.getString(R.string.sync_host_default)) init { launchJob(Dispatchers.Default) { @@ -38,7 +35,7 @@ class SyncAuthViewModel @Inject constructor( } fun obtainToken(email: String, password: String) { - val hostValue = host.value.ifNullOrEmpty { defaultHost } + val hostValue = host.value launchLoadingJob(Dispatchers.Default) { val token = api.authenticate(hostValue, email, password) val result = SyncAuthResult(host.value, email, password, token) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/sync/ui/SyncHostDialogFragment.kt b/app/src/main/kotlin/org/koitharu/kotatsu/sync/ui/SyncHostDialogFragment.kt index 2de3183f4..6c22040a7 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/sync/ui/SyncHostDialogFragment.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/sync/ui/SyncHostDialogFragment.kt @@ -13,6 +13,8 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder import dagger.hilt.android.AndroidEntryPoint import org.koitharu.kotatsu.R import org.koitharu.kotatsu.core.ui.AlertDialogFragment +import org.koitharu.kotatsu.core.util.ext.ifNullOrEmpty +import org.koitharu.kotatsu.core.util.ext.withArgs import org.koitharu.kotatsu.databinding.PreferenceDialogAutocompletetextviewBinding import org.koitharu.kotatsu.settings.utils.validation.DomainValidator import org.koitharu.kotatsu.sync.data.SyncSettings @@ -50,7 +52,7 @@ class SyncHostDialogFragment : AlertDialogFragment syncResult.onError(e) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/sync/ui/history/HistorySyncAdapter.kt b/app/src/main/kotlin/org/koitharu/kotatsu/sync/ui/history/HistorySyncAdapter.kt index 58fa801fb..fe4dbe74a 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/sync/ui/history/HistorySyncAdapter.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/sync/ui/history/HistorySyncAdapter.kt @@ -28,7 +28,7 @@ class HistorySyncAdapter(context: Context) : AbstractThreadedSyncAdapter(context val entryPoint = EntryPointAccessors.fromApplication(context, SyncAdapterEntryPoint::class.java) val syncHelper = entryPoint.syncHelperFactory.create(account, provider) runCatchingCancellable { - syncHelper.syncHistory(syncResult) + syncHelper.syncHistory(syncResult.stats) SyncController.setLastSync(context, account, authority, System.currentTimeMillis()) }.onFailure { e -> syncResult.onError(e)