Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development #330

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ android {

defaultConfig {
applicationId = "com.joeloewi.croissant"
versionCode = 52
versionName = "1.2.3"
versionCode = 53
versionName = "1.2.4"
targetSdk = 34

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import android.app.Dialog
import android.content.Intent
import android.graphics.Bitmap
import android.net.http.SslError
import android.os.Build
import android.os.Message
import android.view.View
import android.view.ViewGroup
import android.webkit.*
import androidx.compose.foundation.layout.*
Expand Down Expand Up @@ -251,6 +253,9 @@ fun LoginHoYoLABContent(
navigator = webViewNavigator,
onCreated = { webView ->
with(webView) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
importantForAutofill = View.IMPORTANT_FOR_AUTOFILL_NO
}
settings.apply {
javaScriptEnabled = true
domStorageEnabled = true
Expand Down Expand Up @@ -354,6 +359,9 @@ fun LoginHoYoLABContent(
resultMsg: Message?
): Boolean {
val popUpWebView = WebView(activity).apply {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
importantForAutofill = View.IMPORTANT_FOR_AUTOFILL_NO
}
settings.apply {
javaScriptEnabled = true
domStorageEnabled = true
Expand Down
28 changes: 28 additions & 0 deletions app/src/main/kotlin/com/joeloewi/croissant/util/WorkExtensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.joeloewi.croissant.util

import android.net.ConnectivityManager
import android.os.Build
import androidx.core.content.getSystemService
import androidx.work.CoroutineWorker

suspend fun <T> CoroutineWorker.withBoundNetwork(block: suspend () -> T): T {
val connectivityManager = applicationContext.getSystemService<ConnectivityManager>()

return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
if (network != null) {
val shouldBeUnbound = connectivityManager?.bindProcessToNetwork(network)

try {
block()
} finally {
if (shouldBeUnbound == true) {
connectivityManager.bindProcessToNetwork(null)
}
}
} else {
block()
}
} else {
block()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.joeloewi.croissant.domain.usecase.FailureLogUseCase
import com.joeloewi.croissant.domain.usecase.SuccessLogUseCase
import com.joeloewi.croissant.domain.usecase.WorkerExecutionLogUseCase
import com.joeloewi.croissant.util.NotificationGenerator
import com.joeloewi.croissant.util.withBoundNetwork
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import kotlinx.coroutines.CancellationException
Expand Down Expand Up @@ -87,146 +88,149 @@ class AttendCheckInEventWorker @AssistedInject constructor(

override suspend fun doWork(): Result = withContext(Dispatchers.IO) {
setForeground(notificationGenerator.createForegroundInfo(_attendanceId.toInt()))
_attendanceId.runCatching {
takeIf { it != Long.MIN_VALUE }!!
}.mapCatching { attendanceId ->
//check session is valid
val attendanceWithGames = getOneAttendanceUseCase(attendanceId)
val cookie = attendanceWithGames.attendance.cookie

//attend check in events
attendanceWithGames.games.forEach { game ->
try {
when (game.type) {
HoYoLABGame.HonkaiImpact3rd -> {
attendCheckInHonkaiImpact3rdUseCase(cookie = cookie)
}

HoYoLABGame.GenshinImpact -> {
attendCheckInGenshinImpactUseCase(cookie = cookie)
}

HoYoLABGame.TearsOfThemis -> {
attendCheckInTearsOfThemisUseCase(cookie = cookie)
}
withBoundNetwork {
_attendanceId.runCatching {
takeIf { it != Long.MIN_VALUE }!!
}.mapCatching { attendanceId ->
//check session is valid
val attendanceWithGames = getOneAttendanceUseCase(attendanceId)
val cookie = attendanceWithGames.attendance.cookie

//attend check in events
attendanceWithGames.games.forEach { game ->
try {
when (game.type) {
HoYoLABGame.HonkaiImpact3rd -> {
attendCheckInHonkaiImpact3rdUseCase(cookie = cookie)
}

HoYoLABGame.HonkaiStarRail -> {
attendCheckInHonkaiStarRail(cookie = cookie)
}
HoYoLABGame.GenshinImpact -> {
attendCheckInGenshinImpactUseCase(cookie = cookie)
}

HoYoLABGame.Unknown -> {
throw Exception()
}
}.getOrThrow().also { response ->
notificationGenerator.createSuccessfulAttendanceNotification(
nickname = attendanceWithGames.attendance.nickname,
hoYoLABGame = game.type,
region = game.region,
message = response.message,
retCode = response.retCode
).let { notification ->
notificationGenerator.safeNotify(
UUID.randomUUID().toString(),
game.type.gameId,
notification
)
}
HoYoLABGame.TearsOfThemis -> {
attendCheckInTearsOfThemisUseCase(cookie = cookie)
}

val executionLogId = insertWorkerExecutionLogUseCase(
WorkerExecutionLog(
attendanceId = attendanceId,
state = WorkerExecutionLogState.SUCCESS,
loggableWorker = LoggableWorker.ATTEND_CHECK_IN_EVENT
)
)

insertSuccessLogUseCase(
SuccessLog(
executionLogId = executionLogId,
gameName = game.type,
retCode = response.retCode,
message = response.message
)
)
}
} catch (cause: CancellationException) {
throw cause
} catch (cause: Throwable) {
if (cause is HoYoLABUnsuccessfulResponseException) {
when (HoYoLABRetCode.findByCode(cause.retCode)) {
HoYoLABRetCode.AlreadyCheckedIn, HoYoLABRetCode.CharacterNotExists -> {
//do not log to crashlytics
HoYoLABGame.HonkaiStarRail -> {
attendCheckInHonkaiStarRail(cookie = cookie)
}

else -> {
Firebase.crashlytics.apply {
log([email protected])
recordException(cause)
}
HoYoLABGame.Unknown -> {
throw Exception()
}
}
}.getOrThrow().also { response ->
notificationGenerator.createSuccessfulAttendanceNotification(
nickname = attendanceWithGames.attendance.nickname,
hoYoLABGame = game.type,
region = game.region,
message = response.message,
retCode = response.retCode
).let { notification ->
notificationGenerator.safeNotify(
UUID.randomUUID().toString(),
game.type.gameId,
notification
)
}

val executionLogId = insertWorkerExecutionLogUseCase(
WorkerExecutionLog(
attendanceId = attendanceId,
state = WorkerExecutionLogState.SUCCESS,
loggableWorker = LoggableWorker.ATTEND_CHECK_IN_EVENT
)
)

createUnsuccessfulAttendanceNotification(
nickname = attendanceWithGames.attendance.nickname,
hoYoLABGame = game.type,
region = game.region,
hoYoLABUnsuccessfulResponseException = cause
).let { notification ->
notificationGenerator.safeNotify(
UUID.randomUUID().toString(),
game.type.gameId,
notification
insertSuccessLogUseCase(
SuccessLog(
executionLogId = executionLogId,
gameName = game.type,
retCode = response.retCode,
message = response.message
)
)
}
} else {
//if result is unsuccessful with unknown error
//retry for three times
} catch (cause: CancellationException) {
throw cause
} catch (cause: Throwable) {
if (cause is HoYoLABUnsuccessfulResponseException) {
when (HoYoLABRetCode.findByCode(cause.retCode)) {
HoYoLABRetCode.AlreadyCheckedIn, HoYoLABRetCode.CharacterNotExists -> {
//do not log to crashlytics
}

/*if (runAttemptCount > 3) {
addFailureLog(attendanceId, cause)
else -> {
Firebase.crashlytics.apply {
log([email protected])
recordException(cause)
}
}
}

createUnsuccessfulAttendanceNotification(
nickname = attendanceWithGames.attendance.nickname,
hoYoLABGame = game.type,
region = game.region,
hoYoLABUnsuccessfulResponseException = cause
).let { notification ->
notificationGenerator.safeNotify(
UUID.randomUUID().toString(),
game.type.gameId,
notification
)
}
} else {
//if result is unsuccessful with unknown error
//retry for three times

}*/
Firebase.crashlytics.apply {
log([email protected])
recordException(cause)
}
/*if (runAttemptCount > 3) {
addFailureLog(attendanceId, cause)
} else {

notificationGenerator.createUnsuccessfulAttendanceNotification(
nickname = attendanceWithGames.attendance.nickname,
hoYoLABGame = game.type,
attendanceId = _attendanceId
).let { notification ->
notificationGenerator.safeNotify(
UUID.randomUUID().toString(),
game.type.gameId,
notification
)
}*/
Firebase.crashlytics.apply {
log([email protected])
recordException(cause)
}

notificationGenerator.createUnsuccessfulAttendanceNotification(
nickname = attendanceWithGames.attendance.nickname,
hoYoLABGame = game.type,
attendanceId = _attendanceId
).let { notification ->
notificationGenerator.safeNotify(
UUID.randomUUID().toString(),
game.type.gameId,
notification
)
}
}
}

addFailureLog(attendanceId, cause)
}
}
}.fold(
onSuccess = {
Result.success()
},
onFailure = { cause ->
if (cause is CancellationException) {
throw cause
addFailureLog(attendanceId, cause)
}
}
}.fold(
onSuccess = {
Result.success()
},
onFailure = { cause ->
if (cause is CancellationException) {
throw cause
}

Firebase.crashlytics.apply {
log([email protected])
recordException(cause)
}
Firebase.crashlytics.apply {
log([email protected])
recordException(cause)
}

addFailureLog(_attendanceId, cause)
addFailureLog(_attendanceId, cause)

Result.failure()
}
)
Result.failure()
}
)
}
}

companion object {
Expand Down
Loading
Loading