commit
6765d79065
@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 2022 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.google.samples.apps.nowinandroid.core.datastore
|
||||
|
||||
import androidx.datastore.core.DataMigration
|
||||
|
||||
/**
|
||||
* Migrates from using lists to maps for user data.
|
||||
*/
|
||||
object ListToMapMigration : DataMigration<UserPreferences> {
|
||||
|
||||
override suspend fun cleanUp() = Unit
|
||||
|
||||
override suspend fun migrate(currentData: UserPreferences): UserPreferences =
|
||||
currentData.copy {
|
||||
// Migrate topic id lists
|
||||
followedTopicIds.clear()
|
||||
followedTopicIds.putAll(
|
||||
currentData.deprecatedFollowedTopicIdsList.associateWith { true }
|
||||
)
|
||||
deprecatedFollowedTopicIds.clear()
|
||||
|
||||
// Migrate author ids
|
||||
followedAuthorIds.clear()
|
||||
followedAuthorIds.putAll(
|
||||
currentData.deprecatedFollowedAuthorIdsList.associateWith { true }
|
||||
)
|
||||
deprecatedFollowedAuthorIds.clear()
|
||||
|
||||
// Migrate bookmarks
|
||||
bookmarkedNewsResourceIds.clear()
|
||||
bookmarkedNewsResourceIds.putAll(
|
||||
currentData.deprecatedBookmarkedNewsResourceIdsList.associateWith { true }
|
||||
)
|
||||
deprecatedBookmarkedNewsResourceIds.clear()
|
||||
|
||||
// Mark migration as complete
|
||||
hasDoneListToMapMigration = true
|
||||
}
|
||||
|
||||
override suspend fun shouldMigrate(currentData: UserPreferences): Boolean {
|
||||
return !currentData.hasDoneListToMapMigration
|
||||
}
|
||||
}
|
@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Copyright 2022 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.google.samples.apps.nowinandroid.core.datastore
|
||||
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Assert
|
||||
import org.junit.Test
|
||||
|
||||
class ListToMapMigrationTest {
|
||||
|
||||
@Test
|
||||
fun ListToMapMigration_should_migrate_topic_ids() = runTest {
|
||||
// Set up existing preferences with topic ids
|
||||
val preMigrationUserPreferences = userPreferences {
|
||||
deprecatedFollowedTopicIds.addAll(listOf("1", "2", "3"))
|
||||
}
|
||||
// Assert that there are no topic ids in the map yet
|
||||
Assert.assertEquals(
|
||||
emptyMap<String, Boolean>(),
|
||||
preMigrationUserPreferences.followedTopicIdsMap
|
||||
)
|
||||
|
||||
// Run the migration
|
||||
val postMigrationUserPreferences =
|
||||
ListToMapMigration.migrate(preMigrationUserPreferences)
|
||||
|
||||
// Assert the deprecated topic ids have been migrated to the topic ids map
|
||||
Assert.assertEquals(
|
||||
mapOf("1" to true, "2" to true, "3" to true),
|
||||
postMigrationUserPreferences.followedTopicIdsMap
|
||||
)
|
||||
|
||||
// Assert that the migration has been marked complete
|
||||
Assert.assertTrue(postMigrationUserPreferences.hasDoneListToMapMigration)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun ListToMapMigration_should_migrate_author_ids() = runTest {
|
||||
// Set up existing preferences with author ids
|
||||
val preMigrationUserPreferences = userPreferences {
|
||||
deprecatedFollowedAuthorIds.addAll(listOf("4", "5", "6"))
|
||||
}
|
||||
// Assert that there are no author ids in the map yet
|
||||
Assert.assertEquals(
|
||||
emptyMap<String, Boolean>(),
|
||||
preMigrationUserPreferences.followedAuthorIdsMap
|
||||
)
|
||||
|
||||
// Run the migration
|
||||
val postMigrationUserPreferences =
|
||||
ListToMapMigration.migrate(preMigrationUserPreferences)
|
||||
|
||||
// Assert the deprecated author ids have been migrated to the author ids map
|
||||
Assert.assertEquals(
|
||||
mapOf("4" to true, "5" to true, "6" to true),
|
||||
postMigrationUserPreferences.followedAuthorIdsMap
|
||||
)
|
||||
|
||||
// Assert that the migration has been marked complete
|
||||
Assert.assertTrue(postMigrationUserPreferences.hasDoneListToMapMigration)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun ListToMapMigration_should_migrate_bookmarks() = runTest {
|
||||
// Set up existing preferences with bookmarks
|
||||
val preMigrationUserPreferences = userPreferences {
|
||||
deprecatedBookmarkedNewsResourceIds.addAll(listOf("7", "8", "9"))
|
||||
}
|
||||
// Assert that there are no bookmarks in the map yet
|
||||
Assert.assertEquals(
|
||||
emptyMap<String, Boolean>(),
|
||||
preMigrationUserPreferences.bookmarkedNewsResourceIdsMap
|
||||
)
|
||||
|
||||
// Run the migration
|
||||
val postMigrationUserPreferences =
|
||||
ListToMapMigration.migrate(preMigrationUserPreferences)
|
||||
|
||||
// Assert the deprecated bookmarks have been migrated to the bookmarks map
|
||||
Assert.assertEquals(
|
||||
mapOf("7" to true, "8" to true, "9" to true),
|
||||
postMigrationUserPreferences.bookmarkedNewsResourceIdsMap
|
||||
)
|
||||
|
||||
// Assert that the migration has been marked complete
|
||||
Assert.assertTrue(postMigrationUserPreferences.hasDoneListToMapMigration)
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright 2022 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.google.samples.apps.nowinandroid.core.ui
|
||||
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
|
||||
/**
|
||||
* Multipreview annotation that represents various device sizes. Add this annotation to a composable
|
||||
* to render various devices.
|
||||
*/
|
||||
@Preview(name = "phone", device = "spec:shape=Normal,width=360,height=640,unit=dp,dpi=480")
|
||||
@Preview(name = "landscape", device = "spec:shape=Normal,width=640,height=360,unit=dp,dpi=480")
|
||||
@Preview(name = "foldable", device = "spec:shape=Normal,width=673,height=841,unit=dp,dpi=480")
|
||||
@Preview(name = "tablet", device = "spec:shape=Normal,width=1280,height=800,unit=dp,dpi=480")
|
||||
annotation class DevicePreviews
|
Loading…
Reference in new issue