Skip to content

Commit

Permalink
Merge pull request #331 from joeloewi7178/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
joeloewi7178 authored Jan 16, 2024
2 parents 4787d4a + 65d8892 commit 4a7f962
Show file tree
Hide file tree
Showing 7 changed files with 317 additions and 268 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package com.joeloewi.croissant.util

import android.os.Build
import com.google.firebase.Firebase
import com.google.firebase.crashlytics.crashlytics

//Nexus 5x occurs errors when use these functions;
//launch in app review, launch in app update, start oss licenses activity
fun isDeviceNexus5X(): Boolean = "Nexus 5X".uppercase() == Build.MODEL.uppercase()
fun isDeviceNexus5X(): Boolean =
"Nexus 5X".uppercase() == Build.MODEL.also { Firebase.crashlytics.log(it) }.uppercase()
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(this@AttendCheckInEventWorker.javaClass.simpleName)
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(this@AttendCheckInEventWorker.javaClass.simpleName)
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(this@AttendCheckInEventWorker.javaClass.simpleName)
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(this@AttendCheckInEventWorker.javaClass.simpleName)
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(this@AttendCheckInEventWorker.javaClass.simpleName)
recordException(cause)
}
Firebase.crashlytics.apply {
log(this@AttendCheckInEventWorker.javaClass.simpleName)
recordException(cause)
}

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

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

companion object {
Expand Down
Loading

0 comments on commit 4a7f962

Please sign in to comment.