Fix sync server address configuration

This commit is contained in:
Koitharu
2023-09-15 11:14:06 +03:00
parent bf01a4d1ab
commit 4ab40566f7
7 changed files with 23 additions and 19 deletions

View File

@@ -30,7 +30,7 @@ class SyncSettingsFragment : BasePreferenceFragment(R.string.sync_settings), Fra
override fun onPreferenceTreeClick(preference: Preference): Boolean { override fun onPreferenceTreeClick(preference: Preference): Boolean {
return when (preference.key) { return when (preference.key) {
SyncSettings.KEY_HOST -> { SyncSettings.KEY_HOST -> {
SyncHostDialogFragment.show(childFragmentManager) SyncHostDialogFragment.show(childFragmentManager, null)
true true
} }

View File

@@ -7,6 +7,7 @@ import android.content.ContentProviderResult
import android.content.Context import android.content.Context
import android.content.OperationApplicationException import android.content.OperationApplicationException
import android.content.SyncResult import android.content.SyncResult
import android.content.SyncStats
import android.database.Cursor import android.database.Cursor
import android.net.Uri import android.net.Uri
import androidx.annotation.WorkerThread import androidx.annotation.WorkerThread
@@ -67,7 +68,7 @@ class SyncHelper @AssistedInject constructor(
get() = TimeUnit.DAYS.toMillis(4) get() = TimeUnit.DAYS.toMillis(4)
@WorkerThread @WorkerThread
fun syncFavourites(syncResult: SyncResult) { fun syncFavourites(stats: SyncStats) {
val data = JSONObject() val data = JSONObject()
data.put(TABLE_FAVOURITE_CATEGORIES, getFavouriteCategories()) data.put(TABLE_FAVOURITE_CATEGORIES, getFavouriteCategories())
data.put(TABLE_FAVOURITES, getFavourites()) data.put(TABLE_FAVOURITES, getFavourites())
@@ -81,17 +82,18 @@ class SyncHelper @AssistedInject constructor(
val timestamp = response.getLong(FIELD_TIMESTAMP) val timestamp = response.getLong(FIELD_TIMESTAMP)
val categoriesResult = val categoriesResult =
upsertFavouriteCategories(response.getJSONArray(TABLE_FAVOURITE_CATEGORIES), timestamp) upsertFavouriteCategories(response.getJSONArray(TABLE_FAVOURITE_CATEGORIES), timestamp)
syncResult.stats.numDeletes += categoriesResult.first().count?.toLong() ?: 0L stats.numDeletes += categoriesResult.first().count?.toLong() ?: 0L
syncResult.stats.numInserts += categoriesResult.drop(1).sumOf { it.count?.toLong() ?: 0L } stats.numInserts += categoriesResult.drop(1).sumOf { it.count?.toLong() ?: 0L }
val favouritesResult = upsertFavourites(response.getJSONArray(TABLE_FAVOURITES), timestamp) val favouritesResult = upsertFavourites(response.getJSONArray(TABLE_FAVOURITES), timestamp)
syncResult.stats.numDeletes += favouritesResult.first().count?.toLong() ?: 0L stats.numDeletes += favouritesResult.first().count?.toLong() ?: 0L
syncResult.stats.numInserts += favouritesResult.drop(1).sumOf { it.count?.toLong() ?: 0L } stats.numInserts += favouritesResult.drop(1).sumOf { it.count?.toLong() ?: 0L }
stats.numEntries += stats.numInserts + stats.numDeletes
} }
gcFavourites() gcFavourites()
} }
@WorkerThread @WorkerThread
fun syncHistory(syncResult: SyncResult) { fun syncHistory(stats: SyncStats) {
val data = JSONObject() val data = JSONObject()
data.put(TABLE_HISTORY, getHistory()) data.put(TABLE_HISTORY, getHistory())
data.put(FIELD_TIMESTAMP, System.currentTimeMillis()) data.put(FIELD_TIMESTAMP, System.currentTimeMillis())
@@ -105,8 +107,9 @@ class SyncHelper @AssistedInject constructor(
json = response.getJSONArray(TABLE_HISTORY), json = response.getJSONArray(TABLE_HISTORY),
timestamp = response.getLong(FIELD_TIMESTAMP), timestamp = response.getLong(FIELD_TIMESTAMP),
) )
syncResult.stats.numDeletes += result.first().count?.toLong() ?: 0L stats.numDeletes += result.first().count?.toLong() ?: 0L
syncResult.stats.numInserts += result.drop(1).sumOf { it.count?.toLong() ?: 0L } stats.numInserts += result.drop(1).sumOf { it.count?.toLong() ?: 0L }
stats.numEntries += stats.numInserts + stats.numDeletes
} }
gcHistory() gcHistory()
} }

View File

@@ -104,7 +104,7 @@ class SyncAuthActivity : BaseActivity<ActivitySyncAuthBinding>(), View.OnClickLi
} }
R.id.button_settings -> { R.id.button_settings -> {
SyncHostDialogFragment.show(supportFragmentManager) SyncHostDialogFragment.show(supportFragmentManager, viewModel.host.value)
} }
} }
} }

View File

@@ -10,7 +10,6 @@ import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.ui.BaseViewModel import org.koitharu.kotatsu.core.ui.BaseViewModel
import org.koitharu.kotatsu.core.util.ext.MutableEventFlow import org.koitharu.kotatsu.core.util.ext.MutableEventFlow
import org.koitharu.kotatsu.core.util.ext.call 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.data.SyncAuthApi
import org.koitharu.kotatsu.sync.domain.SyncAuthResult import org.koitharu.kotatsu.sync.domain.SyncAuthResult
import javax.inject.Inject import javax.inject.Inject
@@ -23,9 +22,7 @@ class SyncAuthViewModel @Inject constructor(
val onAccountAlreadyExists = MutableEventFlow<Unit>() val onAccountAlreadyExists = MutableEventFlow<Unit>()
val onTokenObtained = MutableEventFlow<SyncAuthResult>() val onTokenObtained = MutableEventFlow<SyncAuthResult>()
val host = MutableStateFlow("") val host = MutableStateFlow(context.getString(R.string.sync_host_default))
private val defaultHost = context.getString(R.string.sync_host_default)
init { init {
launchJob(Dispatchers.Default) { launchJob(Dispatchers.Default) {
@@ -38,7 +35,7 @@ class SyncAuthViewModel @Inject constructor(
} }
fun obtainToken(email: String, password: String) { fun obtainToken(email: String, password: String) {
val hostValue = host.value.ifNullOrEmpty { defaultHost } val hostValue = host.value
launchLoadingJob(Dispatchers.Default) { launchLoadingJob(Dispatchers.Default) {
val token = api.authenticate(hostValue, email, password) val token = api.authenticate(hostValue, email, password)
val result = SyncAuthResult(host.value, email, password, token) val result = SyncAuthResult(host.value, email, password, token)

View File

@@ -13,6 +13,8 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import org.koitharu.kotatsu.R import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.ui.AlertDialogFragment 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.databinding.PreferenceDialogAutocompletetextviewBinding
import org.koitharu.kotatsu.settings.utils.validation.DomainValidator import org.koitharu.kotatsu.settings.utils.validation.DomainValidator
import org.koitharu.kotatsu.sync.data.SyncSettings import org.koitharu.kotatsu.sync.data.SyncSettings
@@ -50,7 +52,7 @@ class SyncHostDialogFragment : AlertDialogFragment<PreferenceDialogAutocompletet
binding.message.setText(R.string.sync_host_description) binding.message.setText(R.string.sync_host_description)
val entries = binding.root.resources.getStringArray(R.array.sync_host_list) val entries = binding.root.resources.getStringArray(R.array.sync_host_list)
val editText = binding.edit val editText = binding.edit
editText.setText(syncSettings.host) editText.setText(arguments?.getString(KEY_HOST).ifNullOrEmpty { syncSettings.host })
editText.threshold = 0 editText.threshold = 0
editText.setAdapter(ArrayAdapter(binding.root.context, android.R.layout.simple_spinner_dropdown_item, entries)) editText.setAdapter(ArrayAdapter(binding.root.context, android.R.layout.simple_spinner_dropdown_item, entries))
binding.dropdown.setOnClickListener { binding.dropdown.setOnClickListener {
@@ -76,6 +78,8 @@ class SyncHostDialogFragment : AlertDialogFragment<PreferenceDialogAutocompletet
const val REQUEST_KEY = "sync_host" const val REQUEST_KEY = "sync_host"
const val KEY_HOST = "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)
} }
} }

View File

@@ -28,7 +28,7 @@ class FavouritesSyncAdapter(context: Context) : AbstractThreadedSyncAdapter(cont
val entryPoint = EntryPointAccessors.fromApplication(context, SyncAdapterEntryPoint::class.java) val entryPoint = EntryPointAccessors.fromApplication(context, SyncAdapterEntryPoint::class.java)
val syncHelper = entryPoint.syncHelperFactory.create(account, provider) val syncHelper = entryPoint.syncHelperFactory.create(account, provider)
runCatchingCancellable { runCatchingCancellable {
syncHelper.syncFavourites(syncResult) syncHelper.syncFavourites(syncResult.stats)
SyncController.setLastSync(context, account, authority, System.currentTimeMillis()) SyncController.setLastSync(context, account, authority, System.currentTimeMillis())
}.onFailure { e -> }.onFailure { e ->
syncResult.onError(e) syncResult.onError(e)

View File

@@ -28,7 +28,7 @@ class HistorySyncAdapter(context: Context) : AbstractThreadedSyncAdapter(context
val entryPoint = EntryPointAccessors.fromApplication(context, SyncAdapterEntryPoint::class.java) val entryPoint = EntryPointAccessors.fromApplication(context, SyncAdapterEntryPoint::class.java)
val syncHelper = entryPoint.syncHelperFactory.create(account, provider) val syncHelper = entryPoint.syncHelperFactory.create(account, provider)
runCatchingCancellable { runCatchingCancellable {
syncHelper.syncHistory(syncResult) syncHelper.syncHistory(syncResult.stats)
SyncController.setLastSync(context, account, authority, System.currentTimeMillis()) SyncController.setLastSync(context, account, authority, System.currentTimeMillis())
}.onFailure { e -> }.onFailure { e ->
syncResult.onError(e) syncResult.onError(e)