Skip to content

Commit 84ce399

Browse files
committed
Migrate Use custom sync server preference
Use custom sync server preference was the master switch for both collection and media custom sync servers. These preferences now have individual switches; this migration applies the old master switch to both individual switches, as long as the custom sync URLs are set.
1 parent 80f2684 commit 84ce399

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

AnkiDroid/src/main/java/com/ichi2/anki/servicelayer/PreferenceUpgradeService.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ object PreferenceUpgradeService {
8787
yield(UpgradeGesturesToControls())
8888
yield(UpgradeDayAndNightThemes())
8989
yield(UpgradeCustomCollectionSyncUrl())
90+
yield(UpgradeCustomSyncServerEnabled())
9091
}
9192

9293
/** Returns a list of preference upgrade classes which have not been applied */
@@ -389,6 +390,26 @@ object PreferenceUpgradeService {
389390
}
390391
}
391392
}
393+
394+
internal class UpgradeCustomSyncServerEnabled : PreferenceUpgrade(8) {
395+
override fun upgrade(preferences: SharedPreferences) {
396+
val customSyncServerEnabled = preferences.getBoolean(RemovedPreferences.PREFERENCE_ENABLE_CUSTOM_SYNC_SERVER, false)
397+
val customCollectionSyncUrl = preferences.getString(CustomSyncServer.PREFERENCE_CUSTOM_COLLECTION_SYNC_URL, null)
398+
val customMediaSyncUrl = preferences.getString(CustomSyncServer.PREFERENCE_CUSTOM_MEDIA_SYNC_URL, null)
399+
400+
preferences.edit {
401+
remove(RemovedPreferences.PREFERENCE_ENABLE_CUSTOM_SYNC_SERVER)
402+
putBoolean(
403+
CustomSyncServer.PREFERENCE_CUSTOM_COLLECTION_SYNC_SERVER_ENABLED,
404+
customSyncServerEnabled && !customCollectionSyncUrl.isNullOrEmpty()
405+
)
406+
putBoolean(
407+
CustomSyncServer.PREFERENCE_CUSTOM_MEDIA_SYNC_SERVER_ENABLED,
408+
customSyncServerEnabled && !customMediaSyncUrl.isNullOrEmpty()
409+
)
410+
}
411+
}
412+
}
392413
}
393414
}
394415

AnkiDroid/src/test/java/com/ichi2/anki/servicemodel/PreferenceUpgradeServiceTest.kt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import com.ichi2.anki.servicelayer.PreferenceUpgradeService.PreferenceUpgrade
2828
import com.ichi2.anki.servicelayer.RemovedPreferences
2929
import com.ichi2.anki.web.CustomSyncServer
3030
import com.ichi2.libanki.Consts
31-
import com.ichi2.testutils.EmptyApplication
3231
import com.ichi2.utils.HashUtil
3332
import org.hamcrest.CoreMatchers.equalTo
3433
import org.hamcrest.MatcherAssert.assertThat
@@ -37,10 +36,8 @@ import org.junit.Assert.assertEquals
3736
import org.junit.Before
3837
import org.junit.Test
3938
import org.junit.runner.RunWith
40-
import org.robolectric.annotation.Config
4139

4240
@RunWith(AndroidJUnit4::class)
43-
@Config(application = EmptyApplication::class) // no point in Application init if we don't use it
4441
class PreferenceUpgradeServiceTest : RobolectricTest() {
4542

4643
private lateinit var mPrefs: SharedPreferences
@@ -184,4 +181,22 @@ class PreferenceUpgradeServiceTest : RobolectricTest() {
184181
assertThat(mPrefs.contains(RemovedPreferences.PREFERENCE_CUSTOM_SYNC_BASE), equalTo(false))
185182
assertThat(mPrefs.getString(CustomSyncServer.PREFERENCE_CUSTOM_COLLECTION_SYNC_URL, ""), equalTo("http://foo/sync/"))
186183
}
184+
185+
@Test
186+
fun `Removed Use custom sync server preference is applied to both sync URL preferences after upgrade`() {
187+
mPrefs.edit {
188+
putString(CustomSyncServer.PREFERENCE_CUSTOM_COLLECTION_SYNC_URL, "http://foo/sync/")
189+
putBoolean(RemovedPreferences.PREFERENCE_ENABLE_CUSTOM_SYNC_SERVER, true)
190+
}
191+
192+
assertThat(CustomSyncServer.getCollectionSyncUrlIfSetAndEnabledOrNull(mPrefs), equalTo(null))
193+
assertThat(CustomSyncServer.getMediaSyncUrlIfSetAndEnabledOrNull(mPrefs), equalTo(null))
194+
195+
PreferenceUpgrade.UpgradeCustomSyncServerEnabled().performUpgrade(mPrefs)
196+
197+
assertThat(mPrefs.getBoolean(CustomSyncServer.PREFERENCE_CUSTOM_COLLECTION_SYNC_SERVER_ENABLED, false), equalTo(true))
198+
assertThat(mPrefs.getBoolean(CustomSyncServer.PREFERENCE_CUSTOM_MEDIA_SYNC_SERVER_ENABLED, false), equalTo(false))
199+
assertThat(CustomSyncServer.getCollectionSyncUrlIfSetAndEnabledOrNull(mPrefs), equalTo("http://foo/sync/"))
200+
assertThat(CustomSyncServer.getMediaSyncUrlIfSetAndEnabledOrNull(mPrefs), equalTo(null))
201+
}
187202
}

0 commit comments

Comments
 (0)