Skip to content

Commit 7b7fa95

Browse files
committed
Restructure data layer. Custom models in :data-common, GraphQL schema and queries in :data-runtime-cloud with mappers.
1 parent 7d465c9 commit 7b7fa95

File tree

19 files changed

+400
-129
lines changed

19 files changed

+400
-129
lines changed

kmm/apollo-models/build.gradle.kts

Lines changed: 0 additions & 23 deletions
This file was deleted.

kmm/data-common/build.gradle.kts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
11
plugins {
22
id("kstreamlined.kmm.jvm-and-ios")
33
}
4-
5-
kotlin {
6-
sourceSets {
7-
commonMain {
8-
dependencies {
9-
api(project(":kmm:apollo-models"))
10-
implementation(libs.kotlinx.coroutines.core)
11-
}
12-
}
13-
}
14-
}
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package io.github.reactivecircus.kstreamlined.kmm.data.feed
22

3-
import io.github.reactivecircus.kstreamlined.kmm.apollo.FeedEntriesQuery
4-
import io.github.reactivecircus.kstreamlined.kmm.apollo.FeedSourcesQuery
5-
import io.github.reactivecircus.kstreamlined.kmm.apollo.type.FeedSourceKey
3+
import io.github.reactivecircus.kstreamlined.kmm.data.feed.model.FeedEntry
4+
import io.github.reactivecircus.kstreamlined.kmm.data.feed.model.FeedSource
65

76
interface FeedRepo {
87

98
suspend fun loadFeedSources(
109
refresh: Boolean = false
11-
): List<FeedSourcesQuery.FeedSource>
10+
): List<FeedSource>
1211

1312
suspend fun loadFeedEntries(
14-
filters: List<FeedSourceKey>? = null,
13+
filters: List<FeedSource.Key>? = null,
1514
refresh: Boolean = false,
16-
): List<FeedEntriesQuery.FeedEntry>
15+
): List<FeedEntry>
1716
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io.github.reactivecircus.kstreamlined.kmm.data.feed.model
2+
3+
sealed interface FeedEntry {
4+
val id: String
5+
val title: String
6+
val publishTimestamp: String
7+
val contentUrl: String
8+
9+
data class KotlinBlog(
10+
override val id: String,
11+
override val title: String,
12+
override val publishTimestamp: String,
13+
override val contentUrl: String,
14+
val featuredImageUrl: String,
15+
val description: String,
16+
) : FeedEntry
17+
18+
data class KotlinYouTube(
19+
override val id: String,
20+
override val title: String,
21+
override val publishTimestamp: String,
22+
override val contentUrl: String,
23+
val thumbnailUrl: String,
24+
val description: String,
25+
) : FeedEntry
26+
27+
data class TalkingKotlin(
28+
override val id: String,
29+
override val title: String,
30+
override val publishTimestamp: String,
31+
override val contentUrl: String,
32+
val podcastLogoUrl: String,
33+
val tags: List<String>,
34+
) : FeedEntry
35+
36+
data class KotlinWeekly(
37+
override val id: String,
38+
override val title: String,
39+
override val publishTimestamp: String,
40+
override val contentUrl: String,
41+
val newsletterLogoUrl: String,
42+
) : FeedEntry
43+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.github.reactivecircus.kstreamlined.kmm.data.feed.model
2+
3+
data class FeedSource(
4+
val key: Key,
5+
val title: String,
6+
val description: String,
7+
) {
8+
enum class Key {
9+
KotlinBlog,
10+
KotlinYouTubeChannel,
11+
TalkingKotlinPodcast,
12+
KotlinWeekly,
13+
}
14+
}

kmm/data-runtime-cloud/build.gradle.kts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
plugins {
22
id("kstreamlined.kmm.jvm-and-ios")
33
id("kstreamlined.kmm.test")
4+
id("com.apollographql.apollo3")
5+
}
6+
7+
apollo {
8+
service("kstreamlined") {
9+
packageName.set("io.github.reactivecircus.kstreamlined.graphql")
10+
codegenModels.set("responseBased")
11+
flattenModels.set(true)
12+
generateDataBuilders.set(true)
13+
}
414
}
515

616
kotlin {
File renamed without changes.
File renamed without changes.
File renamed without changes.

kmm/data-runtime-cloud/src/commonMain/kotlin/io/github/reactivecircus/kstreamlined/kmm/data/feed/CloudFeedRepo.kt

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,42 @@ package io.github.reactivecircus.kstreamlined.kmm.data.feed
33
import co.touchlab.kermit.Logger
44
import com.apollographql.apollo3.ApolloClient
55
import com.apollographql.apollo3.api.Optional
6-
import io.github.reactivecircus.kstreamlined.kmm.apollo.FeedEntriesQuery
7-
import io.github.reactivecircus.kstreamlined.kmm.apollo.FeedSourcesQuery
8-
import io.github.reactivecircus.kstreamlined.kmm.apollo.type.FeedSourceKey
6+
import io.github.reactivecircus.kstreamlined.graphql.FeedEntriesQuery
7+
import io.github.reactivecircus.kstreamlined.graphql.FeedSourcesQuery
8+
import io.github.reactivecircus.kstreamlined.kmm.data.feed.mapper.toApollo
9+
import io.github.reactivecircus.kstreamlined.kmm.data.feed.mapper.toModel
10+
import io.github.reactivecircus.kstreamlined.kmm.data.feed.model.FeedEntry
11+
import io.github.reactivecircus.kstreamlined.kmm.data.feed.model.FeedSource
912
import io.github.reactivecircus.kstreamlined.kmm.data.networking.defaultFetchPolicy
1013

1114
class CloudFeedRepo(private val apolloClient: ApolloClient) : FeedRepo {
1215

13-
override suspend fun loadFeedSources(refresh: Boolean): List<FeedSourcesQuery.FeedSource> {
16+
override suspend fun loadFeedSources(refresh: Boolean): List<FeedSource> {
1417
return runCatching {
1518
apolloClient.query(FeedSourcesQuery())
1619
.defaultFetchPolicy(refresh)
1720
.execute()
1821
.dataAssertNoErrors.feedSources
1922
}.onFailure {
2023
Logger.w("Query failed", it)
21-
}.getOrThrow()
24+
}.getOrThrow().map { it.toModel() }
2225
}
2326

2427
override suspend fun loadFeedEntries(
25-
filters: List<FeedSourceKey>?,
28+
filters: List<FeedSource.Key>?,
2629
refresh: Boolean,
27-
): List<FeedEntriesQuery.FeedEntry> {
30+
): List<FeedEntry> {
2831
return runCatching {
29-
apolloClient.query(FeedEntriesQuery(filters = Optional.presentIfNotNull(filters)))
32+
apolloClient.query(
33+
FeedEntriesQuery(
34+
filters = Optional.presentIfNotNull(filters?.map { it.toApollo() })
35+
)
36+
)
3037
.defaultFetchPolicy(refresh)
3138
.execute()
3239
.dataAssertNoErrors.feedEntries
3340
}.onFailure {
3441
Logger.w("Query failed", it)
35-
}.getOrThrow()
42+
}.getOrThrow().map { it.toModel() }
3643
}
3744
}

0 commit comments

Comments
 (0)