Skip to content

Commit

Permalink
Merge pull request #408 from joeloewi7178/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
joeloewi7178 authored Apr 20, 2024
2 parents 7c1c7bb + 64920e8 commit 9ceb91f
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 91 deletions.
5 changes: 1 addition & 4 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ android {

defaultConfig {
applicationId = "com.joeloewi.croissant"
versionCode = 60
versionCode = 61
versionName = "1.3.0"
targetSdk = 34

Expand Down Expand Up @@ -134,9 +134,6 @@ dependencies {
//paging
implementation(libs.androidx.paging.compose)

//html parsing
implementation(libs.jsoup)

//in-app update
implementation(libs.android.play.app.update.ktx)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.jsoup.Jsoup
import javax.inject.Inject

@HiltViewModel
class RedemptionCodesViewModel @Inject constructor(
private val getArticleArcaLiveAppUseCase: ArcaLiveAppUseCase.GetArticle
private val getRedeemCodeUseCase: ArcaLiveAppUseCase.GetRedeemCode
) : ViewModel() {
private val _hoYoLABGameRedemptionCodesState =
MutableStateFlow<LCE<ImmutableList<Pair<HoYoLABGame, AnnotatedString>>>>(LCE.Loading)
Expand All @@ -42,71 +40,17 @@ class RedemptionCodesViewModel @Inject constructor(
_hoYoLABGameRedemptionCodesState.value = LCE.Loading
viewModelScope.launch(Dispatchers.IO) {
_hoYoLABGameRedemptionCodesState.value = HoYoLABGame.entries.filter {
!listOf(HoYoLABGame.Unknown, HoYoLABGame.TearsOfThemis).contains(it)
it !in listOf(HoYoLABGame.Unknown, HoYoLABGame.TearsOfThemis)
}.runCatching {
map {
async(SupervisorJob() + Dispatchers.IO) {
it to HtmlCompat.fromHtml(
getRedemptionCodesFromHtml(it).getOrThrow(),
getRedeemCodeUseCase(it).getOrThrow(),
HtmlCompat.FROM_HTML_MODE_COMPACT
).toAnnotatedString()
}
}
}.mapCatching {
it.awaitAll().toImmutableList()
}.awaitAll().toImmutableList()
}.foldAsLce()
}
}

private suspend fun getRedemptionCodesFromHtml(hoYoLABGame: HoYoLABGame): Result<String> =
withContext(Dispatchers.IO) {
//Jsoup's nth-child works differently than expected
when (hoYoLABGame) {
HoYoLABGame.HonkaiImpact3rd -> {
getArticleArcaLiveAppUseCase(
slug = "hk3rd",
articleId = 85815048
).mapCatching { content ->
Jsoup.parse(content).apply {
select("*:has(> img)").remove()
repeat(5) {
select("body > p:last-child").remove()
}
}.html()
}
}

HoYoLABGame.GenshinImpact -> {
getArticleArcaLiveAppUseCase(
slug = "genshin",
articleId = 95519559
).mapCatching { content ->
Jsoup.parse(content).apply {
select("img").remove()
}.select("table:first-of-type").apply {
select("tr:last-child").remove()
}.html().replace("https://oo.pe/", "")
}
}

HoYoLABGame.HonkaiStarRail -> {
getArticleArcaLiveAppUseCase(
slug = "hkstarrail",
articleId = 69911111
).mapCatching { content ->
Jsoup.parse(content).apply {
repeat(9) {
select("p:last-child").remove()
}
}.select("p:nth-child(n+47)").html().replace("https://oo.pe/", "")
}
}

else -> {
runCatching {
""
}
}
}
}
}
3 changes: 3 additions & 0 deletions data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ dependencies {
implementation(libs.androidx.startup)

implementation(libs.androidx.lifecycle.process)

//html parsing
implementation(libs.jsoup)
}

protobuf {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@
package com.joeloewi.croissant.data.repository

import com.joeloewi.croissant.data.repository.remote.ArcaLiveAppDataSource
import com.joeloewi.croissant.domain.common.HoYoLABGame
import com.joeloewi.croissant.domain.repository.ArcaLiveAppRepository
import com.skydoves.sandwich.getOrThrow
import javax.inject.Inject

class ArcaLiveAppRepositoryImpl @Inject constructor(
private val arcaLiveAppDataSource: ArcaLiveAppDataSource
) : ArcaLiveAppRepository {
override suspend fun getArticle(slug: String, articleId: Long): Result<String> =
arcaLiveAppDataSource.runCatching {
getArticle(slug, articleId).getOrThrow()
}.mapCatching { articleResponse ->
articleResponse.content
}
override suspend fun getRedeemCode(
game: HoYoLABGame
): Result<String> = arcaLiveAppDataSource.runCatching { getRedeemCode(game).getOrThrow() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@

package com.joeloewi.croissant.data.repository.remote

import com.joeloewi.croissant.data.api.model.response.ArticleResponse
import com.joeloewi.croissant.domain.common.HoYoLABGame
import com.skydoves.sandwich.ApiResponse

interface ArcaLiveAppDataSource {
suspend fun getArticle(
slug: String,
articleId: Long
): ApiResponse<ArticleResponse>
suspend fun getRedeemCode(game: HoYoLABGame): ApiResponse<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,67 @@
package com.joeloewi.croissant.data.repository.remote.impl

import com.joeloewi.croissant.data.api.dao.ArcaLiveAppService
import com.joeloewi.croissant.data.api.model.response.ArticleResponse
import com.joeloewi.croissant.data.repository.remote.ArcaLiveAppDataSource
import com.joeloewi.croissant.data.util.runAndRetryWithExponentialBackOff
import com.joeloewi.croissant.domain.common.HoYoLABGame
import com.skydoves.sandwich.ApiResponse
import com.skydoves.sandwich.mapSuccess
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.jsoup.Jsoup
import javax.inject.Inject

class ArcaLiveAppDataSourceImpl @Inject constructor(
private val arcaLiveAppService: ArcaLiveAppService,
) : ArcaLiveAppDataSource {
override suspend fun getArticle(slug: String, articleId: Long): ApiResponse<ArticleResponse> =
withContext(Dispatchers.IO) {
runAndRetryWithExponentialBackOff {
arcaLiveAppService.getArticle(
slug = slug,
articleId = articleId
)

override suspend fun getRedeemCode(
game: HoYoLABGame
): ApiResponse<String> = withContext(Dispatchers.IO) {
runAndRetryWithExponentialBackOff {
when (game) {
HoYoLABGame.HonkaiImpact3rd -> {
arcaLiveAppService.getArticle(
slug = "hk3rd",
articleId = 85815048
).mapSuccess {
Jsoup.parse(content).apply {
select("*:has(> img)").remove()
repeat(5) {
select("body > p:last-child").remove()
}
}.html()
}
}

HoYoLABGame.GenshinImpact -> {
arcaLiveAppService.getArticle(
slug = "genshin",
articleId = 95519559
).mapSuccess {
Jsoup.parse(content).apply {
select("img").remove()
}.select("table:first-of-type").apply {
select("tr:last-child").remove()
}.html().replace("https://oo.pe/", "")
}
}

HoYoLABGame.HonkaiStarRail -> {
arcaLiveAppService.getArticle(
slug = "hkstarrail",
articleId = 72618649
).mapSuccess {
Jsoup.parse(content)
.apply { select("img").remove() }
.select("td:first-of-type")
.html()
.replace("https://oo.pe/", "")
}
}

HoYoLABGame.TearsOfThemis, HoYoLABGame.Unknown -> throw IllegalStateException()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.joeloewi.croissant.domain.repository

import com.joeloewi.croissant.domain.common.HoYoLABGame

interface ArcaLiveAppRepository {
suspend fun getArticle(
slug: String,
articleId: Long
): Result<String>
suspend fun getRedeemCode(game: HoYoLABGame): Result<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@

package com.joeloewi.croissant.domain.usecase

import com.joeloewi.croissant.domain.common.HoYoLABGame
import com.joeloewi.croissant.domain.repository.ArcaLiveAppRepository
import javax.inject.Inject

sealed class ArcaLiveAppUseCase {
class GetArticle @Inject constructor(
class GetRedeemCode @Inject constructor(
private val arcaLiveAppRepository: ArcaLiveAppRepository
) : ArcaLiveAppUseCase() {

suspend operator fun invoke(
slug: String,
articleId: Long
) = arcaLiveAppRepository.getArticle(slug, articleId)
game: HoYoLABGame
) = arcaLiveAppRepository.getRedeemCode(game)
}
}

0 comments on commit 9ceb91f

Please sign in to comment.