From 8d3aa5c5c620e11c6960baddde13352d99a91603 Mon Sep 17 00:00:00 2001 From: lihenggui Date: Wed, 13 Mar 2024 11:22:37 -0700 Subject: [PATCH] Add back datastore-proto module --- core/datastore-proto/build.gradle.kts | 50 +++++++++++++++++ .../core/datastore/DarkThemeConfigProto.kt | 24 +++++++++ .../core/datastore/ThemeBrandProto.kt | 23 ++++++++ .../core/datastore/UserPreferences.kt | 53 +++++++++++++++++++ .../nowinandroid/data/dark_theme_config.proto | 27 ++++++++++ .../apps/nowinandroid/data/theme_brand.proto | 26 +++++++++ .../nowinandroid/data/user_preferences.proto | 53 +++++++++++++++++++ core/datastore/build.gradle.kts | 1 + settings.gradle.kts | 1 + 9 files changed, 258 insertions(+) create mode 100644 core/datastore-proto/build.gradle.kts create mode 100644 core/datastore-proto/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/core/datastore/DarkThemeConfigProto.kt create mode 100644 core/datastore-proto/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/core/datastore/ThemeBrandProto.kt create mode 100644 core/datastore-proto/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/core/datastore/UserPreferences.kt create mode 100644 core/datastore-proto/src/commonMain/proto/com/google/samples/apps/nowinandroid/data/dark_theme_config.proto create mode 100644 core/datastore-proto/src/commonMain/proto/com/google/samples/apps/nowinandroid/data/theme_brand.proto create mode 100644 core/datastore-proto/src/commonMain/proto/com/google/samples/apps/nowinandroid/data/user_preferences.proto diff --git a/core/datastore-proto/build.gradle.kts b/core/datastore-proto/build.gradle.kts new file mode 100644 index 000000000..cd160b9ba --- /dev/null +++ b/core/datastore-proto/build.gradle.kts @@ -0,0 +1,50 @@ +/* + * Copyright 2024 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. + */ + +plugins { + alias(libs.plugins.nowinandroid.kmp.library) + alias(libs.plugins.protobuf) + id("kotlinx-serialization") +} + +android { + namespace = "com.google.samples.apps.nowinandroid.core.datastore.proto" +} + +// Setup protobuf configuration, generating lite Java and Kotlin classes +protobuf { + protoc { + artifact = libs.protobuf.protoc.get().toString() + } + generateProtoTasks { + all().forEach { task -> + task.builtins { + register("kotlin") { + option("lite") + } + } + } + } +} + +kotlin { + sourceSets { + commonMain.dependencies { + api(libs.protobuf.kotlin.lite) + implementation(libs.kotlinx.serialization.core) + } + } +} diff --git a/core/datastore-proto/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/core/datastore/DarkThemeConfigProto.kt b/core/datastore-proto/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/core/datastore/DarkThemeConfigProto.kt new file mode 100644 index 000000000..38c047828 --- /dev/null +++ b/core/datastore-proto/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/core/datastore/DarkThemeConfigProto.kt @@ -0,0 +1,24 @@ +/* + * Copyright 2024 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 + +enum class DarkThemeConfigProto { + DARK_THEME_CONFIG_UNSPECIFIED, + DARK_THEME_CONFIG_FOLLOW_SYSTEM, + DARK_THEME_CONFIG_LIGHT, + DARK_THEME_CONFIG_DARK, +} diff --git a/core/datastore-proto/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/core/datastore/ThemeBrandProto.kt b/core/datastore-proto/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/core/datastore/ThemeBrandProto.kt new file mode 100644 index 000000000..1b10258ff --- /dev/null +++ b/core/datastore-proto/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/core/datastore/ThemeBrandProto.kt @@ -0,0 +1,23 @@ +/* + * Copyright 2024 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 + +enum class ThemeBrandProto { + THEME_BRAND_UNSPECIFIED, + THEME_BRAND_DEFAULT, + THEME_BRAND_ANDROID, +} diff --git a/core/datastore-proto/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/core/datastore/UserPreferences.kt b/core/datastore-proto/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/core/datastore/UserPreferences.kt new file mode 100644 index 000000000..f8168fc0c --- /dev/null +++ b/core/datastore-proto/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/core/datastore/UserPreferences.kt @@ -0,0 +1,53 @@ +/* + * Copyright 2024 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 com.google.samples.apps.nowinandroid.core.datastore.DarkThemeConfigProto.DARK_THEME_CONFIG_FOLLOW_SYSTEM +import com.google.samples.apps.nowinandroid.core.datastore.ThemeBrandProto.THEME_BRAND_UNSPECIFIED +import kotlinx.serialization.Serializable + +// A lot of workaround brought by Proto +@Serializable +data class UserPreferences( + val topicChangeListVersion: Int, + val authorChangeListVersion: Int, + val newsResourceChangeListVersion: Int, + val hasDoneIntToStringIdMigration: Boolean, + val hasDoneListToMapMigration: Boolean, + val followedTopicIds: Set = emptySet(), + val followedAuthorIds: Set = emptySet(), + val bookmarkedNewsResourceIds: Set = emptySet(), + val viewedNewsResourceIds: Set = emptySet(), + val themeBrand: ThemeBrandProto, + val darkThemeConfig: DarkThemeConfigProto, + val shouldHideOnboarding: Boolean, + val useDynamicColor: Boolean, +) { + companion object { + val DEFAULT = UserPreferences( + topicChangeListVersion = 0, + authorChangeListVersion = 0, + newsResourceChangeListVersion = 0, + hasDoneIntToStringIdMigration = false, + hasDoneListToMapMigration = false, + themeBrand = THEME_BRAND_UNSPECIFIED, + darkThemeConfig = DARK_THEME_CONFIG_FOLLOW_SYSTEM, + shouldHideOnboarding = false, + useDynamicColor = false, + ) + } +} diff --git a/core/datastore-proto/src/commonMain/proto/com/google/samples/apps/nowinandroid/data/dark_theme_config.proto b/core/datastore-proto/src/commonMain/proto/com/google/samples/apps/nowinandroid/data/dark_theme_config.proto new file mode 100644 index 000000000..82bac8366 --- /dev/null +++ b/core/datastore-proto/src/commonMain/proto/com/google/samples/apps/nowinandroid/data/dark_theme_config.proto @@ -0,0 +1,27 @@ +/* + * Copyright (C) 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 + * + * http://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. + */ + +syntax = "proto3"; + +option java_package = "com.google.samples.apps.nowinandroid.core.datastore"; +option java_multiple_files = true; + +enum DarkThemeConfigProto { + DARK_THEME_CONFIG_UNSPECIFIED = 0; + DARK_THEME_CONFIG_FOLLOW_SYSTEM = 1; + DARK_THEME_CONFIG_LIGHT = 2; + DARK_THEME_CONFIG_DARK = 3; +} diff --git a/core/datastore-proto/src/commonMain/proto/com/google/samples/apps/nowinandroid/data/theme_brand.proto b/core/datastore-proto/src/commonMain/proto/com/google/samples/apps/nowinandroid/data/theme_brand.proto new file mode 100644 index 000000000..8bcf5859b --- /dev/null +++ b/core/datastore-proto/src/commonMain/proto/com/google/samples/apps/nowinandroid/data/theme_brand.proto @@ -0,0 +1,26 @@ +/* + * Copyright (C) 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 + * + * http://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. + */ + +syntax = "proto3"; + +option java_package = "com.google.samples.apps.nowinandroid.core.datastore"; +option java_multiple_files = true; + +enum ThemeBrandProto { + THEME_BRAND_UNSPECIFIED = 0; + THEME_BRAND_DEFAULT = 1; + THEME_BRAND_ANDROID = 2; +} diff --git a/core/datastore-proto/src/commonMain/proto/com/google/samples/apps/nowinandroid/data/user_preferences.proto b/core/datastore-proto/src/commonMain/proto/com/google/samples/apps/nowinandroid/data/user_preferences.proto new file mode 100644 index 000000000..11386613c --- /dev/null +++ b/core/datastore-proto/src/commonMain/proto/com/google/samples/apps/nowinandroid/data/user_preferences.proto @@ -0,0 +1,53 @@ +/* + * Copyright (C) 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 + * + * http://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. + */ + +syntax = "proto3"; + +import "com/google/samples/apps/nowinandroid/data/dark_theme_config.proto"; +import "com/google/samples/apps/nowinandroid/data/theme_brand.proto"; + +option java_package = "com.google.samples.apps.nowinandroid.core.datastore"; +option java_multiple_files = true; + +message UserPreferences { + reserved 2; + repeated int32 deprecated_int_followed_topic_ids = 1; + int32 topicChangeListVersion = 3; + int32 authorChangeListVersion = 4; + int32 newsResourceChangeListVersion = 6; + repeated int32 deprecated_int_followed_author_ids = 7; + bool has_done_int_to_string_id_migration = 8; + repeated string deprecated_followed_topic_ids = 9; + repeated string deprecated_followed_author_ids = 10; + repeated string deprecated_bookmarked_news_resource_ids = 11; + bool has_done_list_to_map_migration = 12; + + // Each map is used to store a set of string IDs. The bool has no meaning, but proto3 doesn't + // have a Set type so this is the closest we can get to a Set. + map followed_topic_ids = 13; + map followed_author_ids = 14; + map bookmarked_news_resource_ids = 15; + map viewed_news_resource_ids = 20; + + ThemeBrandProto theme_brand = 16; + DarkThemeConfigProto dark_theme_config = 17; + + bool should_hide_onboarding = 18; + + bool use_dynamic_color = 19; + + // NEXT AVAILABLE ID: 21 +} diff --git a/core/datastore/build.gradle.kts b/core/datastore/build.gradle.kts index 9e28bae6e..ffa4331da 100644 --- a/core/datastore/build.gradle.kts +++ b/core/datastore/build.gradle.kts @@ -42,6 +42,7 @@ kotlin { implementation(libs.kotlinx.serialization.core) implementation(projects.core.model) implementation(projects.core.common) + implementation(projects.core.datastoreProto) } commonTest.dependencies { implementation(libs.multiplatform.settings.test) diff --git a/settings.gradle.kts b/settings.gradle.kts index e1b0eddfa..e7b6d8b06 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -45,6 +45,7 @@ include(":core:data") include(":core:data-test") include(":core:database") include(":core:datastore") +include(":core:datastore-proto") include(":core:designsystem") include(":core:domain") include(":core:model")