Skip to content

Commit 0238396

Browse files
authored
[Jetcaster] Create a BaseDao interface for DAOs (#1025)
Add a base DAO interface. DAOs can implement this interface for basic insert, update, & delete operations.
2 parents 146c5b4 + e511eca commit 0238396

File tree

6 files changed

+47
-128
lines changed

6 files changed

+47
-128
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2022 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.jetcaster.data.room
18+
19+
import androidx.room.Delete
20+
import androidx.room.Insert
21+
import androidx.room.OnConflictStrategy
22+
import androidx.room.Update
23+
24+
/**
25+
* Base DAO.
26+
*/
27+
interface BaseDao<T> {
28+
@Insert(onConflict = OnConflictStrategy.REPLACE)
29+
suspend fun insert(entity: T): Long
30+
31+
@Insert(onConflict = OnConflictStrategy.REPLACE)
32+
suspend fun insertAll(vararg entity: T)
33+
34+
@Insert(onConflict = OnConflictStrategy.REPLACE)
35+
suspend fun insertAll(entities: Collection<T>)
36+
37+
@Update(onConflict = OnConflictStrategy.REPLACE)
38+
suspend fun update(entity: T)
39+
40+
@Delete
41+
suspend fun delete(entity: T): Int
42+
}

Jetcaster/app/src/main/java/com/example/jetcaster/data/room/CategoriesDao.kt

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,15 @@
1717
package com.example.jetcaster.data.room
1818

1919
import androidx.room.Dao
20-
import androidx.room.Delete
21-
import androidx.room.Insert
22-
import androidx.room.OnConflictStrategy
2320
import androidx.room.Query
24-
import androidx.room.Update
2521
import com.example.jetcaster.data.Category
2622
import kotlinx.coroutines.flow.Flow
2723

2824
/**
2925
* [Room] DAO for [Category] related operations.
3026
*/
3127
@Dao
32-
abstract class CategoriesDao {
28+
abstract class CategoriesDao : BaseDao<Category> {
3329
@Query(
3430
"""
3531
SELECT categories.* FROM categories
@@ -47,25 +43,4 @@ abstract class CategoriesDao {
4743

4844
@Query("SELECT * FROM categories WHERE name = :name")
4945
abstract suspend fun getCategoryWithName(name: String): Category?
50-
51-
/**
52-
* The following methods should really live in a base interface. Unfortunately the Kotlin
53-
* Compiler which we need to use for Compose doesn't work with that.
54-
* TODO: remove this once we move to a more recent Kotlin compiler
55-
*/
56-
57-
@Insert(onConflict = OnConflictStrategy.REPLACE)
58-
abstract suspend fun insert(entity: Category): Long
59-
60-
@Insert(onConflict = OnConflictStrategy.REPLACE)
61-
abstract suspend fun insertAll(vararg entity: Category)
62-
63-
@Insert(onConflict = OnConflictStrategy.REPLACE)
64-
abstract suspend fun insertAll(entities: Collection<Category>)
65-
66-
@Update(onConflict = OnConflictStrategy.REPLACE)
67-
abstract suspend fun update(entity: Category)
68-
69-
@Delete
70-
abstract suspend fun delete(entity: Category): Int
7146
}

Jetcaster/app/src/main/java/com/example/jetcaster/data/room/EpisodesDao.kt

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,8 @@
1717
package com.example.jetcaster.data.room
1818

1919
import androidx.room.Dao
20-
import androidx.room.Delete
21-
import androidx.room.Insert
22-
import androidx.room.OnConflictStrategy
2320
import androidx.room.Query
2421
import androidx.room.Transaction
25-
import androidx.room.Update
2622
import com.example.jetcaster.data.Episode
2723
import com.example.jetcaster.data.EpisodeToPodcast
2824
import kotlinx.coroutines.flow.Flow
@@ -31,7 +27,7 @@ import kotlinx.coroutines.flow.Flow
3127
* [Room] DAO for [Episode] related operations.
3228
*/
3329
@Dao
34-
abstract class EpisodesDao {
30+
abstract class EpisodesDao : BaseDao<Episode> {
3531

3632
@Query(
3733
"""
@@ -69,25 +65,4 @@ abstract class EpisodesDao {
6965

7066
@Query("SELECT COUNT(*) FROM episodes")
7167
abstract suspend fun count(): Int
72-
73-
/**
74-
* The following methods should really live in a base interface. Unfortunately the Kotlin
75-
* Compiler which we need to use for Compose doesn't work with that.
76-
* TODO: remove this once we move to a more recent Kotlin compiler
77-
*/
78-
79-
@Insert(onConflict = OnConflictStrategy.REPLACE)
80-
abstract suspend fun insert(entity: Episode): Long
81-
82-
@Insert(onConflict = OnConflictStrategy.REPLACE)
83-
abstract suspend fun insertAll(vararg entity: Episode)
84-
85-
@Insert(onConflict = OnConflictStrategy.REPLACE)
86-
abstract suspend fun insertAll(entities: Collection<Episode>)
87-
88-
@Update(onConflict = OnConflictStrategy.REPLACE)
89-
abstract suspend fun update(entity: Episode)
90-
91-
@Delete
92-
abstract suspend fun delete(entity: Episode): Int
9368
}

Jetcaster/app/src/main/java/com/example/jetcaster/data/room/PodcastCategoryEntryDao.kt

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,10 @@
1717
package com.example.jetcaster.data.room
1818

1919
import androidx.room.Dao
20-
import androidx.room.Delete
21-
import androidx.room.Insert
22-
import androidx.room.OnConflictStrategy
23-
import androidx.room.Update
2420
import com.example.jetcaster.data.PodcastCategoryEntry
2521

2622
/**
2723
* [Room] DAO for [PodcastCategoryEntry] related operations.
2824
*/
2925
@Dao
30-
abstract class PodcastCategoryEntryDao {
31-
/**
32-
* The following methods should really live in a base interface. Unfortunately the Kotlin
33-
* Compiler which we need to use for Compose doesn't work with that.
34-
* TODO: remove this once we move to a more recent Kotlin compiler
35-
*/
36-
@Insert(onConflict = OnConflictStrategy.REPLACE)
37-
abstract suspend fun insert(entity: PodcastCategoryEntry): Long
38-
39-
@Insert(onConflict = OnConflictStrategy.REPLACE)
40-
abstract suspend fun insertAll(vararg entity: PodcastCategoryEntry)
41-
42-
@Insert(onConflict = OnConflictStrategy.REPLACE)
43-
abstract suspend fun insertAll(entities: Collection<PodcastCategoryEntry>)
44-
45-
@Update(onConflict = OnConflictStrategy.REPLACE)
46-
abstract suspend fun update(entity: PodcastCategoryEntry)
47-
48-
@Delete
49-
abstract suspend fun delete(entity: PodcastCategoryEntry): Int
50-
}
26+
abstract class PodcastCategoryEntryDao : BaseDao<PodcastCategoryEntry>

Jetcaster/app/src/main/java/com/example/jetcaster/data/room/PodcastFollowedEntryDao.kt

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,11 @@
1717
package com.example.jetcaster.data.room
1818

1919
import androidx.room.Dao
20-
import androidx.room.Delete
21-
import androidx.room.Insert
22-
import androidx.room.OnConflictStrategy
2320
import androidx.room.Query
24-
import androidx.room.Update
2521
import com.example.jetcaster.data.PodcastFollowedEntry
2622

2723
@Dao
28-
abstract class PodcastFollowedEntryDao {
24+
abstract class PodcastFollowedEntryDao : BaseDao<PodcastFollowedEntry> {
2925
@Query("DELETE FROM podcast_followed_entries WHERE podcast_uri = :podcastUri")
3026
abstract suspend fun deleteWithPodcastUri(podcastUri: String)
3127

@@ -35,24 +31,4 @@ abstract class PodcastFollowedEntryDao {
3531
suspend fun isPodcastFollowed(podcastUri: String): Boolean {
3632
return podcastFollowRowCount(podcastUri) > 0
3733
}
38-
39-
/**
40-
* The following methods should really live in a base interface. Unfortunately the Kotlin
41-
* Compiler which we need to use for Compose doesn't work with.
42-
* TODO: remove this once we move to a more recent Kotlin compiler
43-
*/
44-
@Insert(onConflict = OnConflictStrategy.REPLACE)
45-
abstract suspend fun insert(entity: PodcastFollowedEntry): Long
46-
47-
@Insert(onConflict = OnConflictStrategy.REPLACE)
48-
abstract suspend fun insertAll(vararg entity: PodcastFollowedEntry)
49-
50-
@Insert(onConflict = OnConflictStrategy.REPLACE)
51-
abstract suspend fun insertAll(entities: Collection<PodcastFollowedEntry>)
52-
53-
@Update(onConflict = OnConflictStrategy.REPLACE)
54-
abstract suspend fun update(entity: PodcastFollowedEntry)
55-
56-
@Delete
57-
abstract suspend fun delete(entity: PodcastFollowedEntry): Int
5834
}

Jetcaster/app/src/main/java/com/example/jetcaster/data/room/PodcastsDao.kt

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,8 @@
1717
package com.example.jetcaster.data.room
1818

1919
import androidx.room.Dao
20-
import androidx.room.Delete
21-
import androidx.room.Insert
22-
import androidx.room.OnConflictStrategy
2320
import androidx.room.Query
2421
import androidx.room.Transaction
25-
import androidx.room.Update
2622
import com.example.jetcaster.data.Podcast
2723
import com.example.jetcaster.data.PodcastWithExtraInfo
2824
import kotlinx.coroutines.flow.Flow
@@ -31,7 +27,7 @@ import kotlinx.coroutines.flow.Flow
3127
* [Room] DAO for [Podcast] related operations.
3228
*/
3329
@Dao
34-
abstract class PodcastsDao {
30+
abstract class PodcastsDao : BaseDao<Podcast> {
3531
@Query("SELECT * FROM podcasts WHERE uri = :uri")
3632
abstract fun podcastWithUri(uri: String): Flow<Podcast>
3733

@@ -95,25 +91,4 @@ abstract class PodcastsDao {
9591

9692
@Query("SELECT COUNT(*) FROM podcasts")
9793
abstract suspend fun count(): Int
98-
99-
/**
100-
* The following methods should really live in a base interface. Unfortunately the Kotlin
101-
* Compiler which we need to use for Compose doesn't work with that.
102-
* TODO: remove this once we move to a more recent Kotlin compiler
103-
*/
104-
105-
@Insert(onConflict = OnConflictStrategy.REPLACE)
106-
abstract suspend fun insert(entity: Podcast): Long
107-
108-
@Insert(onConflict = OnConflictStrategy.REPLACE)
109-
abstract suspend fun insertAll(vararg entity: Podcast)
110-
111-
@Insert(onConflict = OnConflictStrategy.REPLACE)
112-
abstract suspend fun insertAll(entities: Collection<Podcast>)
113-
114-
@Update(onConflict = OnConflictStrategy.REPLACE)
115-
abstract suspend fun update(entity: Podcast)
116-
117-
@Delete
118-
abstract suspend fun delete(entity: Podcast): Int
11994
}

0 commit comments

Comments
 (0)