-
Notifications
You must be signed in to change notification settings - Fork 8
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
Showing
15 changed files
with
428 additions
and
12 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
70 changes: 70 additions & 0 deletions
70
...commonMain/kotlin/dev/dimension/flare/data/datasource/vvo/DiscoverStatusRemoteMediator.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,70 @@ | ||
package dev.dimension.flare.data.datasource.vvo | ||
|
||
import androidx.paging.ExperimentalPagingApi | ||
import androidx.paging.LoadType | ||
import androidx.paging.PagingState | ||
import androidx.paging.RemoteMediator | ||
import dev.dimension.flare.data.cache.DbPagingTimelineWithStatusView | ||
import dev.dimension.flare.data.database.cache.CacheDatabase | ||
import dev.dimension.flare.data.database.cache.mapper.VVO | ||
import dev.dimension.flare.data.network.vvo.VVOService | ||
import dev.dimension.flare.model.MicroBlogKey | ||
|
||
@OptIn(ExperimentalPagingApi::class) | ||
internal class DiscoverStatusRemoteMediator( | ||
private val service: VVOService, | ||
private val database: CacheDatabase, | ||
private val accountKey: MicroBlogKey, | ||
private val pagingKey: String, | ||
) : RemoteMediator<Int, DbPagingTimelineWithStatusView>() { | ||
private var page = 0 | ||
private val containerId = "102803" | ||
|
||
override suspend fun load( | ||
loadType: LoadType, | ||
state: PagingState<Int, DbPagingTimelineWithStatusView>, | ||
): MediatorResult { | ||
return try { | ||
val response = | ||
when (loadType) { | ||
LoadType.REFRESH -> { | ||
page = 0 | ||
service.getContainerIndex(containerId = containerId).also { | ||
database.transaction { | ||
database.dbPagingTimelineQueries.deletePaging(accountKey, pagingKey) | ||
} | ||
} | ||
} | ||
|
||
LoadType.PREPEND -> { | ||
return MediatorResult.Success( | ||
endOfPaginationReached = true, | ||
) | ||
} | ||
LoadType.APPEND -> { | ||
page++ | ||
service.getContainerIndex(containerId = containerId, sinceId = page.toString()) | ||
} | ||
} | ||
|
||
val status = response.data?.cards?.mapNotNull { it.mblog }.orEmpty() | ||
|
||
VVO.save( | ||
database = database, | ||
accountKey = accountKey, | ||
pagingKey = pagingKey, | ||
statuses = status, | ||
sortIdProvider = { | ||
val index = status.indexOf(it) | ||
-(index + page * state.config.pageSize).toLong() | ||
}, | ||
) | ||
|
||
MediatorResult.Success( | ||
endOfPaginationReached = status.isEmpty(), | ||
) | ||
} catch (e: Throwable) { | ||
MediatorResult.Error(e) | ||
} | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
...src/commonMain/kotlin/dev/dimension/flare/data/datasource/vvo/SearchStatusPagingSource.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,93 @@ | ||
package dev.dimension.flare.data.datasource.vvo | ||
|
||
import androidx.paging.ExperimentalPagingApi | ||
import androidx.paging.LoadType | ||
import androidx.paging.PagingState | ||
import androidx.paging.RemoteMediator | ||
import dev.dimension.flare.data.cache.DbPagingTimelineWithStatusView | ||
import dev.dimension.flare.data.database.cache.CacheDatabase | ||
import dev.dimension.flare.data.database.cache.mapper.VVO | ||
import dev.dimension.flare.data.network.vvo.VVOService | ||
import dev.dimension.flare.model.MicroBlogKey | ||
import kotlinx.serialization.Required | ||
import kotlinx.serialization.Serializable | ||
|
||
@OptIn(ExperimentalPagingApi::class) | ||
internal class SearchStatusRemoteMediator( | ||
private val service: VVOService, | ||
private val database: CacheDatabase, | ||
private val accountKey: MicroBlogKey, | ||
private val pagingKey: String, | ||
private val query: String, | ||
) : RemoteMediator<Int, DbPagingTimelineWithStatusView>() { | ||
private var page = 1 | ||
private val containerId by lazy { | ||
"100103type=1&q=$query&t=" | ||
} | ||
|
||
override suspend fun load( | ||
loadType: LoadType, | ||
state: PagingState<Int, DbPagingTimelineWithStatusView>, | ||
): MediatorResult { | ||
return try { | ||
val response = | ||
when (loadType) { | ||
LoadType.REFRESH -> { | ||
page = 1 | ||
service.getContainerIndex( | ||
containerId = containerId, | ||
pageType = "searchall", | ||
).also { | ||
database.transaction { | ||
database.dbPagingTimelineQueries.deletePaging(accountKey, pagingKey) | ||
} | ||
} | ||
} | ||
LoadType.PREPEND -> { | ||
return MediatorResult.Success( | ||
endOfPaginationReached = true, | ||
) | ||
} | ||
|
||
LoadType.APPEND -> { | ||
page++ | ||
service.getContainerIndex( | ||
containerId = containerId, | ||
pageType = "searchall", | ||
page = page, | ||
) | ||
} | ||
} | ||
val status = response.data?.cards?.mapNotNull { it.mblog }.orEmpty() | ||
|
||
VVO.save( | ||
accountKey = accountKey, | ||
pagingKey = pagingKey, | ||
database = database, | ||
statuses = status, | ||
sortIdProvider = { | ||
val index = status.indexOf(it) | ||
-(index + page * state.config.pageSize).toLong() | ||
}, | ||
) | ||
|
||
MediatorResult.Success( | ||
endOfPaginationReached = status.isEmpty(), | ||
) | ||
} catch (e: Throwable) { | ||
e.printStackTrace() | ||
MediatorResult.Error(e) | ||
} | ||
} | ||
} | ||
|
||
@Serializable | ||
data class SearchRequest( | ||
val rawQuery: String? = null, | ||
val count: Long? = null, | ||
val cursor: String? = null, | ||
@Required | ||
val querySource: String = "typed_query", | ||
@Required | ||
val product: String = "Top", | ||
) |
46 changes: 46 additions & 0 deletions
46
...d/src/commonMain/kotlin/dev/dimension/flare/data/datasource/vvo/SearchUserPagingSource.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,46 @@ | ||
package dev.dimension.flare.data.datasource.vvo | ||
|
||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import dev.dimension.flare.data.network.vvo.VVOService | ||
import dev.dimension.flare.model.MicroBlogKey | ||
import dev.dimension.flare.ui.model.UiUser | ||
import dev.dimension.flare.ui.model.mapper.toUi | ||
|
||
internal class SearchUserPagingSource( | ||
private val service: VVOService, | ||
private val accountKey: MicroBlogKey, | ||
private val query: String, | ||
) : PagingSource<Int, UiUser>() { | ||
private val containerId by lazy { | ||
"100103type=3&q=$query&t=" | ||
} | ||
|
||
override fun getRefreshKey(state: PagingState<Int, UiUser>): Int? { | ||
return null | ||
} | ||
|
||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, UiUser> { | ||
try { | ||
val response = | ||
service.getContainerIndex( | ||
containerId = containerId, | ||
pageType = "searchall", | ||
page = params.key, | ||
) | ||
val users = | ||
response.data?.cards?.flatMap { | ||
it.cardGroup.orEmpty() | ||
}?.mapNotNull { | ||
it.user | ||
}.orEmpty() | ||
return LoadResult.Page( | ||
data = users.map { it.toUi(accountKey = accountKey) }, | ||
prevKey = null, | ||
nextKey = if (users.isEmpty()) null else params.key?.plus(1), | ||
) | ||
} catch (e: Throwable) { | ||
return LoadResult.Error(e) | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...src/commonMain/kotlin/dev/dimension/flare/data/datasource/vvo/TrendHashtagPagingSource.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,49 @@ | ||
package dev.dimension.flare.data.datasource.vvo | ||
|
||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import dev.dimension.flare.data.network.vvo.VVOService | ||
import dev.dimension.flare.ui.model.UiHashtag | ||
|
||
internal class TrendHashtagPagingSource( | ||
private val service: VVOService, | ||
) : PagingSource<Int, UiHashtag>() { | ||
private val containerId = "106003type=25&filter_type=realtimehot" | ||
|
||
override fun getRefreshKey(state: PagingState<Int, UiHashtag>): Int? { | ||
return null | ||
} | ||
|
||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, UiHashtag> { | ||
try { | ||
service.getContainerIndex(containerId = containerId) | ||
.data | ||
?.cards | ||
?.flatMap { | ||
it.cardGroup.orEmpty() | ||
} | ||
?.mapNotNull { | ||
it.desc | ||
} | ||
?.map { | ||
UiHashtag( | ||
hashtag = it, | ||
description = null, | ||
searchContent = "#$it#", | ||
) | ||
} | ||
?.toList() | ||
.orEmpty() | ||
.let { | ||
return LoadResult.Page( | ||
data = it, | ||
prevKey = null, | ||
nextKey = null, | ||
) | ||
} | ||
} catch (e: Throwable) { | ||
e.printStackTrace() | ||
return LoadResult.Error(e) | ||
} | ||
} | ||
} |
Oops, something went wrong.