Skip to content

Commit

Permalink
feat: handle all exceptions in resource and player activity
Browse files Browse the repository at this point in the history
  • Loading branch information
urFate committed Apr 2, 2024
1 parent 3280652 commit cc710ff
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package live.shirabox.shirabox.ui.activity.player

import android.app.Activity
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
Expand Down Expand Up @@ -31,6 +33,7 @@ class PlayerActivity : ComponentActivity() {
) {
val arguments = intent.extras
val context = LocalContext.current
val activity = context as? Activity

rememberSystemUiController().apply {
setStatusBarColor(
Expand All @@ -40,21 +43,26 @@ class PlayerActivity : ComponentActivity() {
Util.hideSystemUi(this)
}

lateinit var model: PlayerViewModel

val model: PlayerViewModel = viewModel(factory = Util.viewModelFactory {
PlayerViewModel(
try {
model = PlayerViewModel(
context = context,
contentUid = arguments?.getInt("content_uid") ?: -1,
contentName = arguments?.getString("name").toString(),
episode = arguments?.getInt("episode") ?: 0,
startIndex = arguments?.getInt("start_index") ?: 0,
contentUid = arguments!!.getLong("content_uid"),
contentName = arguments.getString("name").toString(),
episode = arguments.getInt("episode"),
startIndex = arguments.getInt("start_index"),
playlist = Json.decodeFromString(
arguments?.getString("playlist") ?: ""
arguments.getString("playlist") ?: ""
)
)
})
} catch (ex: Exception) {
ex.printStackTrace()
activity?.finish()
Toast.makeText(context, ex.localizedMessage, Toast.LENGTH_LONG).show()
}

ShiraPlayer(model = model)
ShiraPlayer(model = viewModel(factory = Util.viewModelFactory { model }))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.AnimatedVisibility
Expand Down Expand Up @@ -99,21 +100,28 @@ class ResourceActivity : ComponentActivity() {
ShiraBoxTheme(
transparentStatusBar = true
) {
val activity = LocalContext.current as Activity?

val arguments = intent.extras
if(arguments == null) {
activity?.finish()
return@ShiraBoxTheme
}

val resourceId = arguments.getInt("id")
val type = arguments.getString("type")!!.let { ContentType.fromString(it) }

Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
val context = LocalContext.current
val activity = context as Activity?

var resourceId: Int = -1
lateinit var type: ContentType

try {
val arguments = intent.extras!!

resourceId = arguments.getInt("id")
type = arguments.getString("type").toString()
.let { ContentType.fromString(it) }
} catch (ex: Exception) {
ex.printStackTrace()
activity?.finish()
Toast.makeText(context, ex.localizedMessage, Toast.LENGTH_LONG).show()
}

Resource(resourceId, type, LocalContext.current)
}
}
Expand Down

0 comments on commit cc710ff

Please sign in to comment.