diff --git a/app/src/main/java/com/github/kr328/clash/BaseActivity.kt b/app/src/main/java/com/github/kr328/clash/BaseActivity.kt index 77d3f6ac..402dc4c2 100644 --- a/app/src/main/java/com/github/kr328/clash/BaseActivity.kt +++ b/app/src/main/java/com/github/kr328/clash/BaseActivity.kt @@ -29,32 +29,18 @@ import java.util.concurrent.atomic.AtomicInteger import kotlin.coroutines.resume import kotlin.coroutines.suspendCoroutine -abstract class BaseActivity> : - AppCompatActivity(), +abstract class BaseActivity> : AppCompatActivity(), CoroutineScope by MainScope(), Broadcasts.Observer { - enum class Event { - ServiceRecreated, - ActivityStart, - ActivityStop, - ClashStop, - ClashStart, - ProfileLoaded, - ProfileChanged, - ProfileUpdateCompleted, - ProfileUpdateFailed - } - - + protected val uiStore by lazy { UiStore(this) } protected val events = Channel(Channel.UNLIMITED) protected var activityStarted: Boolean = false protected val clashRunning: Boolean get() = Remote.broadcasts.clashRunning protected var design: D? = null - private set(value) { + set(value) { field = value - if (value != null) { setContentView(value.root) } else { @@ -75,14 +61,14 @@ abstract class BaseActivity> : suspend fun startActivityForResult( contracts: ActivityResultContract, - input: I + input: I, ): O = withContext(Dispatchers.Main) { val requestKey = nextRequestKey.getAndIncrement().toString() ActivityResultLifecycle().use { lifecycle, start -> suspendCoroutine { c -> activityResultRegistry.register(requestKey, lifecycle, contracts) { - c.resumeWith(Result.success(it)) + c.resume(it) }.apply { start() }.launch(input) } } @@ -92,7 +78,6 @@ abstract class BaseActivity> : suspendCoroutine { window.decorView.post { this.design = design - it.resume(Unit) } } @@ -100,49 +85,35 @@ abstract class BaseActivity> : override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - applyDayNight() launch { main() - - finish() } } override fun onStart() { super.onStart() - activityStarted = true - Remote.broadcasts.addObserver(this) - events.trySend(Event.ActivityStart) } override fun onStop() { super.onStop() - activityStarted = false - Remote.broadcasts.removeObserver(this) - events.trySend(Event.ActivityStop) } override fun onDestroy() { design?.cancel() - cancel() - super.onDestroy() } override fun finish() { - if (deferRunning) { - return - } - + if (deferRunning) return deferRunning = true launch { @@ -172,7 +143,6 @@ abstract class BaseActivity> : override fun onSupportNavigateUp(): Boolean { this.onBackPressed() - return true } @@ -212,49 +182,45 @@ abstract class BaseActivity> : private fun queryDayNight(config: Configuration = resources.configuration): DayNight { return when (uiStore.darkMode) { - DarkMode.Auto -> { - if (config.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES) - DayNight.Night - else - DayNight.Day - } - DarkMode.ForceLight -> { - DayNight.Day - } - DarkMode.ForceDark -> { - DayNight.Night - } + DarkMode.Auto -> if (config.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES) DayNight.Night else DayNight.Day + DarkMode.ForceLight -> DayNight.Day + DarkMode.ForceDark -> DayNight.Night } } private fun applyDayNight(config: Configuration = resources.configuration) { val dayNight = queryDayNight(config) - when (dayNight) { - DayNight.Night -> { - theme.applyStyle(R.style.AppThemeDark, true) - } - DayNight.Day -> { - theme.applyStyle(R.style.AppThemeLight, true) - } + DayNight.Night -> theme.applyStyle(R.style.AppThemeDark, true) + DayNight.Day -> theme.applyStyle(R.style.AppThemeLight, true) } window.isAllowForceDarkCompat = false window.isSystemBarsTranslucentCompat = true - + window.statusBarColor = resolveThemedColor(android.R.attr.statusBarColor) window.navigationBarColor = resolveThemedColor(android.R.attr.navigationBarColor) if (Build.VERSION.SDK_INT >= 23) { - window.isLightStatusBarsCompat = - resolveThemedBoolean(android.R.attr.windowLightStatusBar) + window.isLightStatusBarsCompat = resolveThemedBoolean(android.R.attr.windowLightStatusBar) } if (Build.VERSION.SDK_INT >= 27) { - window.isLightNavigationBarCompat = - resolveThemedBoolean(android.R.attr.windowLightNavigationBar) + window.isLightNavigationBarCompat = resolveThemedBoolean(android.R.attr.windowLightNavigationBar) } this.dayNight = dayNight } + + enum class Event { + ServiceRecreated, + ActivityStart, + ActivityStop, + ClashStop, + ClashStart, + ProfileLoaded, + ProfileChanged, + ProfileUpdateCompleted, + ProfileUpdateFailed, + } }