-
Notifications
You must be signed in to change notification settings - Fork 53
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
f72c3db
commit 6d808a3
Showing
11 changed files
with
137 additions
and
65 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
5 changes: 5 additions & 0 deletions
5
app/src/main/java/com/imcys/bilibilias/home/ui/viewmodel/AsVideoState.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,5 @@ | ||
package com.imcys.bilibilias.home.ui.viewmodel | ||
|
||
import com.imcys.bilibilias.common.network.danmaku.VideoInfoV2 | ||
|
||
data class AsVideoState(val videoInfoV2: VideoInfoV2 = VideoInfoV2()) |
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
23 changes: 23 additions & 0 deletions
23
common/src/main/java/com/imcys/bilibilias/common/base/extend/HttpClient.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,23 @@ | ||
package com.imcys.bilibilias.common.base.extend | ||
|
||
import io.ktor.client.HttpClient | ||
import io.ktor.client.call.body | ||
import io.ktor.client.request.HttpRequestBuilder | ||
import io.ktor.client.request.get | ||
import io.ktor.client.statement.bodyAsText | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
|
||
internal suspend inline fun <reified T> HttpClient.safeGet( | ||
url: String, | ||
crossinline block: HttpRequestBuilder.() -> Unit = {}, | ||
): Flow<Result<T>> = | ||
httpBlock { get(url, block).body() } | ||
|
||
internal suspend fun HttpClient.safeGetText( | ||
url: String, | ||
block: HttpRequestBuilder.() -> Unit = {}, | ||
): Flow<Result<String>> = httpBlock { get(url, block).bodyAsText() } | ||
|
||
private fun <T> httpBlock(block: suspend () -> T): Flow<Result<T>> = | ||
flow { emit(block()) }.asResult() |
21 changes: 21 additions & 0 deletions
21
common/src/main/java/com/imcys/bilibilias/common/base/extend/Result.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,21 @@ | ||
package com.imcys.bilibilias.common.base.extend | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.onStart | ||
|
||
sealed interface Result<out T> { | ||
data class Success<T>(val data: T) : Result<T> | ||
data class Error(val exception: Throwable? = null) : Result<Nothing> | ||
data object Loading : Result<Nothing> | ||
} | ||
|
||
fun <T> Flow<T>.asResult(): Flow<Result<T>> { | ||
return this | ||
.map<T, Result<T>> { | ||
Result.Success(it) | ||
} | ||
.onStart { emit(Result.Loading) } | ||
.catch { emit(Result.Error(it)) } | ||
} |
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
20 changes: 20 additions & 0 deletions
20
common/src/main/java/com/imcys/bilibilias/common/network/danmaku/Danmaku.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,20 @@ | ||
package com.imcys.bilibilias.common.network.danmaku | ||
|
||
import com.imcys.bilibilias.common.base.api.BilibiliApi | ||
import com.imcys.bilibilias.common.base.extend.Result | ||
import com.imcys.bilibilias.common.base.extend.safeGet | ||
import com.imcys.bilibilias.common.network.base.ResBean | ||
import io.ktor.client.HttpClient | ||
import io.ktor.client.request.parameter | ||
import kotlinx.coroutines.flow.Flow | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class DanmakuRepository @Inject constructor(private val http: HttpClient) { | ||
suspend fun getCideoInfoV2(avid: Long, cid: Long): Flow<Result<ResBean<VideoInfoV2>>> = | ||
http.safeGet(BilibiliApi.videoInfoV2) { | ||
parameter("aid", avid) | ||
parameter("cid", cid) | ||
} | ||
} |
Oops, something went wrong.