-
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.
- Loading branch information
1 parent
03e178a
commit 08cbaae
Showing
25 changed files
with
393 additions
and
29 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
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
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
19 changes: 19 additions & 0 deletions
19
...src/commonMain/sqldelightTimeline/social/androiddev/common/timeline/ConflictResolution.sq
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,19 @@ | ||
CREATE TABLE failedWrite ( | ||
key TEXT NOT NULL PRIMARY KEY, | ||
datetime INTEGER AS Long | ||
); | ||
|
||
get: | ||
SELECT * | ||
FROM failedWrite | ||
WHERE key = ?; | ||
|
||
upsert: | ||
INSERT OR REPLACE INTO failedWrite VALUES ?; | ||
|
||
delete: | ||
DELETE FROM failedWrite | ||
WHERE key = ?; | ||
|
||
deleteAll: | ||
DELETE FROM failedWrite; |
17 changes: 17 additions & 0 deletions
17
...rsistence/src/commonMain/sqldelightTimeline/social/androiddev/common/timeline/Timeline.sq
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,17 @@ | ||
CREATE TABLE TimelineItem ( | ||
statusId Text NOT NULL PRIMARY KEY, | ||
type TEXT NOT NULL, | ||
createdAt TEXT NOT NULL | ||
); | ||
|
||
insertFeedItem: | ||
INSERT OR REPLACE INTO TimelineItem | ||
VALUES ?; | ||
|
||
selectHomeItems: | ||
SELECT * FROM TimelineItem | ||
WHERE type = "HOME" | ||
ORDER BY createdAt; | ||
|
||
deleteAll: | ||
DELETE FROM TimelineItem; |
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
37 changes: 37 additions & 0 deletions
37
.../commonMain/kotlin/social/androiddev/common/repository/timeline/HomeTimelineRepository.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,37 @@ | ||
package social.androiddev.common.repository.timeline | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.distinctUntilChanged | ||
import org.mobilenativefoundation.store.store5.Market | ||
import org.mobilenativefoundation.store.store5.MarketResponse | ||
import org.mobilenativefoundation.store.store5.ReadRequest | ||
import social.androiddev.common.timeline.TimelineItem | ||
|
||
interface HomeTimelineRepository { | ||
suspend fun read(): Flow<MarketResponse<List<TimelineItem>>> | ||
} | ||
|
||
class RealHomeTimelineRepository( | ||
private val market: Market<FeedType, List<TimelineItem>, List<TimelineItem>> | ||
) : HomeTimelineRepository { | ||
/** | ||
* returns a flow of home feed items from a database | ||
* anytime table rows are created/updated will return a new list of timeline items | ||
* on first return will also call network fetcher to get | ||
* latest from network and update local storage with it] | ||
*/ | ||
|
||
|
||
override suspend fun read(): Flow<MarketResponse<List<TimelineItem>>> { | ||
return market.read(ReadRequest.of( | ||
FeedType.Home, | ||
emptyList(), | ||
null, | ||
true)) | ||
.distinctUntilChanged() | ||
} | ||
|
||
|
||
} | ||
|
||
|
125 changes: 125 additions & 0 deletions
125
.../src/commonMain/kotlin/social/androiddev/common/repository/timeline/TimelineRepoModule.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,125 @@ | ||
/* | ||
* This file is part of Dodo. | ||
* | ||
* Dodo is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. | ||
* | ||
* Dodo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with Dodo. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
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.koin.core.module.Module | ||
import org.koin.dsl.module | ||
import org.mobilenativefoundation.store.store5.Bookkeeper | ||
import org.mobilenativefoundation.store.store5.Market | ||
import org.mobilenativefoundation.store.store5.NetworkFetcher | ||
import org.mobilenativefoundation.store.store5.NetworkUpdater | ||
import org.mobilenativefoundation.store.store5.OnNetworkCompletion | ||
import org.mobilenativefoundation.store.store5.Store | ||
import social.androiddev.common.network.MastodonApi | ||
import social.androiddev.common.network.model.Status | ||
import social.androiddev.common.persistence.localstorage.DodoAuthStorage | ||
import social.androiddev.common.timeline.TimelineDatabase | ||
import social.androiddev.common.timeline.TimelineItem | ||
|
||
/** | ||
* Koin module containing all koin/bean definitions for | ||
* timeline repository delegates. | ||
*/ | ||
val timelineRepoModule: Module = module { | ||
|
||
factory<HomeTimelineRepository> { RealHomeTimelineRepository(get()) } | ||
|
||
factory<Store<FeedType, List<TimelineItem>, List<TimelineItem>>> { | ||
val database = get<TimelineDatabase>() | ||
Store.by( | ||
reader = { key: FeedType -> | ||
when(key){ | ||
is FeedType.Home -> get<TimelineDatabase>() | ||
.timelineQueries | ||
.selectHomeItems() | ||
.asFlow() | ||
.mapToList().map { | ||
it.ifEmpty { throw Exception("Empty list") } | ||
} | ||
} | ||
|
||
}, | ||
writer = { _, input -> | ||
input.forEach(database::tryWriteItem) | ||
true | ||
}, | ||
deleter = { TODO() }, | ||
clearer = { TODO() } | ||
) | ||
} | ||
factory { | ||
//Todo Add logic for conflict resolution handling for when we start posting toots | ||
Bookkeeper.by( | ||
read = { _: FeedType -> null }, | ||
write = { _, _ -> true }, | ||
delete = { TODO() }, | ||
deleteAll = { TODO() } | ||
) | ||
} | ||
|
||
factory { | ||
NetworkFetcher.by( | ||
get = { key: FeedType -> | ||
when(key) { | ||
is FeedType.Home -> { | ||
val authStorage = get<DodoAuthStorage>() | ||
get<MastodonApi>() | ||
.getHomeFeed(authStorage.currentDomain!!, authStorage.getAccessToken(authStorage.currentDomain!!)!!) | ||
.getOrThrow() | ||
.map(::timelineItem) | ||
} | ||
} | ||
}, | ||
post = { key, item -> TODO() }, | ||
converter = { it } | ||
) | ||
} | ||
|
||
|
||
factory { | ||
NetworkUpdater.by( | ||
post = { key: FeedType, _: List<Status> -> | ||
get<MastodonApi>() | ||
TODO() | ||
}, | ||
onCompletion = OnNetworkCompletion( | ||
onSuccess = {}, | ||
onFailure = {} | ||
), | ||
converter = { TODO() } | ||
) | ||
} | ||
|
||
factory<Market<FeedType, List<TimelineItem>, List<TimelineItem>>> { | ||
Market.of<FeedType, List<TimelineItem>, List<TimelineItem>>( | ||
stores = listOf(get()), //TODO MIKE: ADD memory cache | ||
bookkeeper = get(), | ||
fetcher = get(), | ||
updater = get() | ||
) | ||
} | ||
} | ||
|
||
private fun timelineItem(it: Status) = | ||
TimelineItem(it.id, FeedType.Home.type, it.createdAt) | ||
|
||
fun TimelineDatabase.tryWriteItem(timelineItem: TimelineItem): Boolean = try { | ||
timelineQueries.insertFeedItem(timelineItem) | ||
true | ||
} catch (t: Throwable) { | ||
throw RuntimeException(t) | ||
} | ||
|
||
sealed class FeedType(val type:String){ | ||
object Home: FeedType("HOME") | ||
} |
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.