Fix sync server address configuration
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -104,7 +104,7 @@ class SyncAuthActivity : BaseActivity<ActivitySyncAuthBinding>(), View.OnClickLi
|
||||
}
|
||||
|
||||
R.id.button_settings -> {
|
||||
SyncHostDialogFragment.show(supportFragmentManager)
|
||||
SyncHostDialogFragment.show(supportFragmentManager, viewModel.host.value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Unit>()
|
||||
val onTokenObtained = MutableEventFlow<SyncAuthResult>()
|
||||
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)
|
||||
|
||||
@@ -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<PreferenceDialogAutocompletet
|
||||
binding.message.setText(R.string.sync_host_description)
|
||||
val entries = binding.root.resources.getStringArray(R.array.sync_host_list)
|
||||
val editText = binding.edit
|
||||
editText.setText(syncSettings.host)
|
||||
editText.setText(arguments?.getString(KEY_HOST).ifNullOrEmpty { syncSettings.host })
|
||||
editText.threshold = 0
|
||||
editText.setAdapter(ArrayAdapter(binding.root.context, android.R.layout.simple_spinner_dropdown_item, entries))
|
||||
binding.dropdown.setOnClickListener {
|
||||
@@ -76,6 +78,8 @@ class SyncHostDialogFragment : AlertDialogFragment<PreferenceDialogAutocompletet
|
||||
const val REQUEST_KEY = "sync_host"
|
||||
const val KEY_HOST = "host"
|
||||
|
||||
fun show(fm: FragmentManager) = SyncHostDialogFragment().show(fm, TAG)
|
||||
fun show(fm: FragmentManager, host: String?) = SyncHostDialogFragment().withArgs(1) {
|
||||
putString(KEY_HOST, host)
|
||||
}.show(fm, TAG)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ class FavouritesSyncAdapter(context: Context) : AbstractThreadedSyncAdapter(cont
|
||||
val entryPoint = EntryPointAccessors.fromApplication(context, SyncAdapterEntryPoint::class.java)
|
||||
val syncHelper = entryPoint.syncHelperFactory.create(account, provider)
|
||||
runCatchingCancellable {
|
||||
syncHelper.syncFavourites(syncResult)
|
||||
syncHelper.syncFavourites(syncResult.stats)
|
||||
SyncController.setLastSync(context, account, authority, System.currentTimeMillis())
|
||||
}.onFailure { e ->
|
||||
syncResult.onError(e)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user