Change-Id: Ia14c225cc1632ded38abffc201b673a6a60f4451pull/2/head
parent
9f11f6ae8b
commit
cbcb24c08f
@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* 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.data.local.dao
|
||||||
|
|
||||||
|
import androidx.room.Dao
|
||||||
|
import androidx.room.Insert
|
||||||
|
import androidx.room.Query
|
||||||
|
import com.google.samples.apps.nowinandroid.data.local.entities.AuthorEntity
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DAO for [AuthorEntity] access
|
||||||
|
*/
|
||||||
|
@Dao
|
||||||
|
interface AuthorDao {
|
||||||
|
@Query(value = "SELECT * FROM authors")
|
||||||
|
fun getAuthorsStream(): Flow<List<AuthorEntity>>
|
||||||
|
|
||||||
|
@Insert
|
||||||
|
suspend fun saveAuthorEntities(entities: List<AuthorEntity>)
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
* 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.data.local.dao
|
||||||
|
|
||||||
|
import androidx.room.Dao
|
||||||
|
import androidx.room.Insert
|
||||||
|
import androidx.room.Query
|
||||||
|
import com.google.samples.apps.nowinandroid.data.local.entities.EpisodeEntity
|
||||||
|
import com.google.samples.apps.nowinandroid.data.model.Episode
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DAO for [EpisodeEntity] and [Episode] access
|
||||||
|
*/
|
||||||
|
@Dao
|
||||||
|
interface EpisodeDao {
|
||||||
|
@Query(value = "SELECT * FROM episodes")
|
||||||
|
fun getEpisodesStream(): Flow<List<Episode>>
|
||||||
|
|
||||||
|
@Insert
|
||||||
|
suspend fun saveEpisodeEntities(entities: List<EpisodeEntity>)
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
* 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.data.local.dao
|
||||||
|
|
||||||
|
import androidx.room.Dao
|
||||||
|
import androidx.room.Insert
|
||||||
|
import androidx.room.Query
|
||||||
|
import com.google.samples.apps.nowinandroid.data.local.entities.NewsResourceEntity
|
||||||
|
import com.google.samples.apps.nowinandroid.data.model.NewsResource
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DAO for [NewsResource] and [NewsResourceEntity] access
|
||||||
|
*/
|
||||||
|
@Dao
|
||||||
|
interface NewsResourceDao {
|
||||||
|
@Query(value = "SELECT * FROM news_resources")
|
||||||
|
fun getNewsResourcesStream(): Flow<List<NewsResource>>
|
||||||
|
|
||||||
|
@Query(
|
||||||
|
value = """
|
||||||
|
SELECT * FROM news_resources
|
||||||
|
WHERE id IN (:filterTopicIds)
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
fun getNewsResourcesStream(filterTopicIds: Set<Int>): Flow<List<NewsResource>>
|
||||||
|
|
||||||
|
@Insert
|
||||||
|
suspend fun saveNewsResourceEntities(entities: List<NewsResourceEntity>)
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* 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.data.local.dao
|
||||||
|
|
||||||
|
import androidx.room.Dao
|
||||||
|
import androidx.room.Insert
|
||||||
|
import androidx.room.Query
|
||||||
|
import com.google.samples.apps.nowinandroid.data.local.entities.TopicEntity
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DAO for [TopicEntity] access
|
||||||
|
*/
|
||||||
|
@Dao
|
||||||
|
interface TopicDao {
|
||||||
|
@Query(value = "SELECT * FROM topics")
|
||||||
|
fun getTopicEntitiesStream(): Flow<List<TopicEntity>>
|
||||||
|
|
||||||
|
@Query(
|
||||||
|
value = """
|
||||||
|
SELECT * FROM topics
|
||||||
|
WHERE id IN (:ids)
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
fun getTopicEntitiesStream(ids: Set<Int>): Flow<List<TopicEntity>>
|
||||||
|
|
||||||
|
@Insert
|
||||||
|
suspend fun saveTopics(entities: List<TopicEntity>)
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* 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.di
|
||||||
|
|
||||||
|
import com.google.samples.apps.nowinandroid.data.local.NiADatabase
|
||||||
|
import com.google.samples.apps.nowinandroid.data.local.dao.AuthorDao
|
||||||
|
import com.google.samples.apps.nowinandroid.data.local.dao.EpisodeDao
|
||||||
|
import com.google.samples.apps.nowinandroid.data.local.dao.NewsResourceDao
|
||||||
|
import com.google.samples.apps.nowinandroid.data.local.dao.TopicDao
|
||||||
|
import dagger.Module
|
||||||
|
import dagger.Provides
|
||||||
|
import dagger.hilt.InstallIn
|
||||||
|
import dagger.hilt.components.SingletonComponent
|
||||||
|
|
||||||
|
@Module
|
||||||
|
@InstallIn(SingletonComponent::class)
|
||||||
|
interface DaosModule {
|
||||||
|
companion object {
|
||||||
|
@Provides
|
||||||
|
fun providesAuthorDao(
|
||||||
|
database: NiADatabase,
|
||||||
|
): AuthorDao = database.authorDao()
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
fun providesTopicsDao(
|
||||||
|
database: NiADatabase,
|
||||||
|
): TopicDao = database.topicDao()
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
fun providesEpisodeDao(
|
||||||
|
database: NiADatabase,
|
||||||
|
): EpisodeDao = database.episodeDao()
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
fun providesNewsResourceDao(
|
||||||
|
database: NiADatabase,
|
||||||
|
): NewsResourceDao = database.newsResourceDao()
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* 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.di
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import androidx.room.Room
|
||||||
|
import com.google.samples.apps.nowinandroid.data.local.NiADatabase
|
||||||
|
import dagger.Module
|
||||||
|
import dagger.Provides
|
||||||
|
import dagger.hilt.InstallIn
|
||||||
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||||
|
import dagger.hilt.components.SingletonComponent
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
@Module
|
||||||
|
@InstallIn(SingletonComponent::class)
|
||||||
|
interface DatabaseModule {
|
||||||
|
companion object {
|
||||||
|
@Provides
|
||||||
|
@Singleton
|
||||||
|
fun providesNiADatabase(
|
||||||
|
@ApplicationContext context: Context,
|
||||||
|
): NiADatabase = Room.databaseBuilder(
|
||||||
|
context,
|
||||||
|
NiADatabase::class.java,
|
||||||
|
"nia-database"
|
||||||
|
).build()
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue