Skip to content

Commit

Permalink
Handle java.lang.IOException with network requests by `TwinteBacken…
Browse files Browse the repository at this point in the history
…dHttpClien t`
  • Loading branch information
private-yusuke committed Apr 18, 2024
1 parent e9aad58 commit 2973325
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 28 deletions.
37 changes: 25 additions & 12 deletions app/src/main/java/net/twinte/android/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.webkit.ValueCallback
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.webkit.WebSettingsCompat
Expand Down Expand Up @@ -72,13 +73,17 @@ class MainActivity : AppCompatActivity(), SubWebViewFragment.Callback {
UpdateScheduleWorker.scheduleNextUpdate(workManager)
scheduleNotificationDataStore.schedule()
GlobalScope.launch {
try {
scheduleDataStore.update()
} catch (e: NotLoggedInException) {
// 未ログイン時は失敗するが何もしない
} catch (e: Exception) {
// それ以外の予期せぬエラー
}
kotlin.runCatching { scheduleDataStore.update() }
.fold(onSuccess = {}, onFailure = {
when (it) {
is NotLoggedInException -> {
// 未ログイン時は失敗するが何もしない
}
else -> {
// それ以外の予期せぬエラー
}
}
})
WidgetUpdater.updateAllWidget(this@MainActivity)
WidgetUpdater.scheduleAllIfExists(this@MainActivity)
}
Expand Down Expand Up @@ -197,7 +202,10 @@ class MainActivity : AppCompatActivity(), SubWebViewFragment.Callback {
val account = GoogleSignIn.getSignedInAccountFromIntent(data).result
GlobalScope.launch {
account?.idToken?.let {
userDataStore.validateGoogleIdToken(it)
kotlin.runCatching { userDataStore.validateGoogleIdToken(it) }
.onFailure {
Toast.makeText(applicationContext, R.string.common_google_play_services_unknown_issue, Toast.LENGTH_SHORT).show()
}
}
withContext(Dispatchers.Main) {
binding.mainWebview.loadUrl(twinteUrlBuilder(serverSettings).buildUrl())
Expand All @@ -219,11 +227,16 @@ class MainActivity : AppCompatActivity(), SubWebViewFragment.Callback {
super.onPause()
cookieManager.flush()
GlobalScope.launch {
try {
kotlin.runCatching {
scheduleDataStore.update()
} catch (e: NotLoggedInException) {
// 未ログイン時は失敗するが何もしない
}
}.fold(onSuccess = {}, onFailure = { e ->
when (e) {
is NotLoggedInException -> {
// 未ログイン時は失敗するが何もしない
}
else -> { }
}
})
WidgetUpdater.updateAllWidget(this@MainActivity)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,22 @@ class SharedPreferencesScheduleDataStore @Inject constructor(
with(pref.edit()) {
clear()
calendar.map { simpleDateFormat.format(it) }.forEach { d ->
val res = twinteBackendHttpClient.get("/api/v3/timetable/$d")

if (!res.isSuccessful) {
if (res.code == 401) {
throw NotLoggedInException()
} else {
throw IOException("API call failed with code ${res.code}\n ${res.body?.string()}")
}
}
putString(d, res.body?.string())
// TODO: replace `android.util.Log` with Timber
// Log.d(TAG, "schedule updated $d $res")
kotlin.runCatching { twinteBackendHttpClient.get("/api/v3/timetable/$d") }
.fold(onSuccess = {
if (!it.isSuccessful) {
if (it.code == 401) {
throw NotLoggedInException()
} else {
throw IOException("API call failed with code ${it.code}\n ${it.body.string()}")
}
}
putString(d, it.body.string())
}, onFailure = {
if (it !is IOException) {
throw it
}
return@withContext
})
}
commit()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,11 @@ class V3LargeWidgetRemoteViewService @Inject constructor() : RemoteViewsService(

override fun onCreate() {}

override fun onDataSetChanged() = runBlocking {
override fun onDataSetChanged(): Unit = runBlocking {
Log.d("LargeFactory", "onDataSetChanged")
val (current, _) = WidgetUpdater.getShouldShowCurrentDate()
schedule = scheduleDataStore.getSchedule(current.time)
kotlin.runCatching { scheduleDataStore.getSchedule(current.time) }
.onSuccess { schedule = it }
}

override fun onDestroy() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,11 @@ class V3MediumWidgetRemoteViewService @Inject constructor() : RemoteViewsService

override fun onCreate() {}

override fun onDataSetChanged() = runBlocking {
override fun onDataSetChanged(): Unit = runBlocking {
Log.d("MediumFactory", "onDataSetChanged")
val (current, _) = WidgetUpdater.getShouldShowCurrentDate()
schedule = scheduleDataStore.getSchedule(current.time)
kotlin.runCatching { scheduleDataStore.getSchedule(current.time) }
.onSuccess { schedule = it }
}

override fun onDestroy() {}
Expand Down

0 comments on commit 2973325

Please sign in to comment.