-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split timelineRepoModule into delegates
- Loading branch information
1 parent
3d7ba4c
commit 00e7618
Showing
14 changed files
with
159 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...sitory/src/commonMain/kotlin/social/androiddev/common/repository/timeline/ModelMappers.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package social.androiddev.common.repository.timeline | ||
|
||
import social.androiddev.common.network.model.Status | ||
import social.androiddev.common.timeline.StatusDB | ||
import social.androiddev.domain.timeline.FeedType | ||
import social.androiddev.domain.timeline.model.StatusLocal | ||
|
||
fun StatusDB.toLocal( | ||
key: FeedType | ||
) = StatusLocal( | ||
remoteId = remoteId, | ||
feedType = key, | ||
createdAt = createdAt, | ||
repliesCount = repliesCount, | ||
reblogsCount = favouritesCount, | ||
favoritesCount = favouritesCount, | ||
content = content, | ||
sensitive = sensitive ?: false, | ||
spoilerText = spoilerText, | ||
visibility = visibility, | ||
avatarUrl = avatarUrl, | ||
accountAddress = accountAddress, | ||
userName = userName | ||
) | ||
|
||
|
||
fun Status.statusDB() = | ||
StatusDB( | ||
type = FeedType.Home.type, | ||
remoteId = id, | ||
uri = uri, | ||
createdAt = createdAt, | ||
content = content, | ||
accountId = account?.id, | ||
visibility = visibility.name, | ||
sensitive = sensitive, | ||
spoilerText = spoilerText, | ||
applicationName = application?.name ?: "", | ||
repliesCount = repliesCount?.toLong(), | ||
reblogsCount = reblogsCount?.toLong(), | ||
favouritesCount = favouritesCount?.toLong(), | ||
avatarUrl = account?.avatar?:"", | ||
accountAddress = account?.acct?:"", | ||
userName = account?.username?:" " | ||
) |
26 changes: 26 additions & 0 deletions
26
...ory/src/commonMain/kotlin/social/androiddev/common/repository/timeline/TimelineFetcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package social.androiddev.common.repository.timeline | ||
|
||
import org.mobilenativefoundation.store.store5.Fetcher | ||
import social.androiddev.common.network.MastodonApi | ||
import social.androiddev.common.persistence.localstorage.DodoAuthStorage | ||
import social.androiddev.common.timeline.StatusDB | ||
import social.androiddev.domain.timeline.FeedType | ||
|
||
/** | ||
* Wrapper for [MastodonApi.getHomeFeed] while also getting an auth token from storage | ||
* and mapping result to list of [StatusDB] | ||
*/ | ||
|
||
fun MastodonApi.timelineFetcher(authStorage: DodoAuthStorage): Fetcher<FeedType, List<StatusDB>> = | ||
Fetcher.of { key: FeedType -> | ||
when (key) { | ||
is FeedType.Home -> { | ||
getHomeFeed( | ||
authStorage.currentDomain!!, | ||
authStorage.getAccessToken(authStorage.currentDomain!!)!! | ||
) | ||
.getOrThrow() | ||
.map { it.statusDB() } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...c/commonMain/kotlin/social/androiddev/common/repository/timeline/TimelineSourceOfTruth.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package social.androiddev.common.repository.timeline | ||
|
||
import com.squareup.sqldelight.runtime.coroutines.asFlow | ||
import com.squareup.sqldelight.runtime.coroutines.mapToList | ||
import kotlinx.coroutines.flow.map | ||
import org.mobilenativefoundation.store.store5.SourceOfTruth | ||
import social.androiddev.common.timeline.StatusDB | ||
import social.androiddev.common.timeline.TimelineDatabase | ||
import social.androiddev.common.timeline.TimelineQueries | ||
import social.androiddev.domain.timeline.FeedType | ||
import social.androiddev.domain.timeline.model.StatusLocal | ||
|
||
fun TimelineDatabase.asSourceOfTruth(): SourceOfTruth<FeedType, List<StatusDB>, List<StatusLocal>> = | ||
SourceOfTruth.of( | ||
reader = reader(), | ||
writer = { key, input -> | ||
input.forEach { item -> tryWriteItem(item, key) } | ||
} | ||
) | ||
|
||
private fun TimelineDatabase.reader() = { key: FeedType -> | ||
when (key) { | ||
is FeedType.Home -> | ||
timelineQueries.homeItemsAsLocal(key) | ||
} | ||
} | ||
|
||
private fun TimelineQueries.homeItemsAsLocal(key: FeedType) = selectHomeItems() | ||
.asFlow() | ||
.mapToList() | ||
.map { | ||
it.ifEmpty { return@map null } //treat empty list as no result otherwise | ||
it.map { item -> item.toLocal(key) } | ||
} | ||
|
||
fun TimelineDatabase.tryWriteItem(it: StatusDB, type: FeedType): Boolean = try { | ||
timelineQueries.insertFeedItem( | ||
type = type.type, | ||
remoteId = it.remoteId, | ||
uri = it.uri, | ||
createdAt = it.createdAt, | ||
content = it.content, | ||
accountId = it.accountId, | ||
visibility = it.visibility, | ||
sensitive = it.sensitive, | ||
spoilerText = it.spoilerText, | ||
applicationName = it.applicationName, | ||
repliesCount = it.repliesCount, | ||
favouritesCount = it.favouritesCount, | ||
reblogsCount = it.reblogsCount, | ||
avatarUrl = it.avatarUrl, | ||
accountAddress = it.accountAddress, | ||
userName = it.userName | ||
) | ||
true | ||
} catch (t: Throwable) { | ||
throw RuntimeException(t) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.