Merge pull request #42 from lihenggui/compose_multiplatform
Remove datastore and datastore-test modulepull/1323/head
commit
7fca23b3f7
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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.database.dao
|
||||
|
||||
import com.google.samples.apps.nowinandroid.core.database.model.NewsResourceEntity
|
||||
import com.google.samples.apps.nowinandroid.core.database.model.NewsResourceTopicCrossRef
|
||||
import com.google.samples.apps.nowinandroid.core.database.model.PopulatedNewsResource
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface NewsResourceDaoInterface {
|
||||
fun getNewsResources(
|
||||
useFilterTopicIds: Boolean = false,
|
||||
filterTopicIds: Set<String> = emptySet(),
|
||||
useFilterNewsIds: Boolean = false,
|
||||
filterNewsIds: Set<String> = emptySet(),
|
||||
): Flow<List<PopulatedNewsResource>>
|
||||
|
||||
fun getNewsResourceIds(
|
||||
useFilterTopicIds: Boolean = false,
|
||||
filterTopicIds: Set<String> = emptySet(),
|
||||
useFilterNewsIds: Boolean = false,
|
||||
filterNewsIds: Set<String> = emptySet(),
|
||||
): Flow<List<String>>
|
||||
|
||||
suspend fun insertOrIgnoreNewsResources(entities: List<NewsResourceEntity>): List<Long>
|
||||
suspend fun upsertNewsResources(newsResourceEntities: List<NewsResourceEntity>)
|
||||
suspend fun insertOrIgnoreTopicCrossRefEntities(newsResourceTopicCrossReferences: List<NewsResourceTopicCrossRef>)
|
||||
suspend fun deleteNewsResources(ids: List<String>)
|
||||
}
|
||||
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* 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.database.dao
|
||||
|
||||
import com.google.samples.apps.nowinandroid.core.database.model.TopicEntity
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface TopicDaoInterface {
|
||||
fun getTopicEntity(topicId: String): Flow<TopicEntity>
|
||||
fun getTopicEntities(): Flow<List<TopicEntity>>
|
||||
suspend fun getOneOffTopicEntities(): List<TopicEntity>
|
||||
fun getTopicEntities(ids: Set<String>): Flow<List<TopicEntity>>
|
||||
suspend fun insertOrIgnoreTopics(topicEntities: List<TopicEntity>): List<Long>
|
||||
suspend fun upsertTopics(entities: List<TopicEntity>)
|
||||
suspend fun deleteTopics(ids: List<String>)
|
||||
}
|
||||
@ -1,50 +0,0 @@
|
||||
/*
|
||||
* Copyright 2023 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,53 +0,0 @@
|
||||
/*
|
||||
* 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<String> = emptySet(),
|
||||
val followedAuthorIds: Set<String> = emptySet(),
|
||||
val bookmarkedNewsResourceIds: Set<String> = emptySet(),
|
||||
val viewedNewsResourceIds: Set<String> = 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,
|
||||
)
|
||||
}
|
||||
}
|
||||
@ -1,27 +0,0 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
@ -1,26 +0,0 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
@ -1,53 +0,0 @@
|
||||
/*
|
||||
* 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<string, bool> followed_topic_ids = 13;
|
||||
map<string, bool> followed_author_ids = 14;
|
||||
map<string, bool> bookmarked_news_resource_ids = 15;
|
||||
map<string, bool> 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
|
||||
}
|
||||
@ -1 +0,0 @@
|
||||
/build
|
||||
@ -1,3 +0,0 @@
|
||||
# :core:datastore-test module
|
||||
|
||||

|
||||
@ -1,17 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
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
|
||||
|
||||
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.
|
||||
-->
|
||||
<manifest />
|
||||
@ -1,61 +0,0 @@
|
||||
/*
|
||||
* 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.test
|
||||
|
||||
import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.core.DataStoreFactory
|
||||
import com.google.samples.apps.nowinandroid.core.datastore.UserPreferences
|
||||
import com.google.samples.apps.nowinandroid.core.datastore.UserPreferencesSerializer
|
||||
import com.google.samples.apps.nowinandroid.core.datastore.di.DataStoreModule
|
||||
import com.google.samples.apps.nowinandroid.core.di.ApplicationScope
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import dagger.hilt.testing.TestInstallIn
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import org.junit.rules.TemporaryFolder
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Module
|
||||
@TestInstallIn(
|
||||
components = [SingletonComponent::class],
|
||||
replaces = [DataStoreModule::class],
|
||||
)
|
||||
internal object TestDataStoreModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun providesUserPreferencesDataStore(
|
||||
@ApplicationScope scope: CoroutineScope,
|
||||
userPreferencesSerializer: UserPreferencesSerializer,
|
||||
tmpFolder: TemporaryFolder,
|
||||
): DataStore<UserPreferences> =
|
||||
tmpFolder.testUserPreferencesDataStore(
|
||||
coroutineScope = scope,
|
||||
userPreferencesSerializer = userPreferencesSerializer,
|
||||
)
|
||||
}
|
||||
|
||||
fun TemporaryFolder.testUserPreferencesDataStore(
|
||||
coroutineScope: CoroutineScope,
|
||||
userPreferencesSerializer: UserPreferencesSerializer = UserPreferencesSerializer(),
|
||||
) = DataStoreFactory.create(
|
||||
serializer = userPreferencesSerializer,
|
||||
scope = coroutineScope,
|
||||
) {
|
||||
newFile("user_preferences_test.pb")
|
||||
}
|
||||
Loading…
Reference in new issue