Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import io.nekohasekai.sagernet.widget.ListListener
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlin.coroutines.coroutineContext

Expand Down Expand Up @@ -174,7 +175,7 @@ class AppListActivity : ThemedActivity() {
@UiThread
private fun loadApps() {
loader?.cancel()
loader = lifecycleScope.launchWhenCreated {
loader = lifecycleScope.launch {
loading.crossFadeFrom(binding.list)
val adapter = binding.list.adapter as AppsAdapter
withContext(Dispatchers.IO) { adapter.reload() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,6 @@ class AssetsActivity : ThemedActivity() {
return true
}

override fun onBackPressed() {
finish()
}

override fun onResume() {
super.onResume()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import androidx.appcompat.widget.SearchView
import androidx.appcompat.widget.Toolbar
import androidx.core.graphics.ColorUtils
import androidx.core.net.toUri
import androidx.core.os.BundleCompat
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.core.view.size
Expand Down Expand Up @@ -1180,7 +1181,7 @@ class ConfigurationFragment @JvmOverloads constructor(
override fun onViewStateRestored(savedInstanceState: Bundle?) {
super.onViewStateRestored(savedInstanceState)

savedInstanceState?.getParcelable<ProxyGroup>("proxyGroup")?.also {
savedInstanceState?.let { BundleCompat.getParcelable(it, "proxyGroup", ProxyGroup::class.java) }?.also {
proxyGroup = it
onViewCreated(requireView(), null)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import androidx.activity.addCallback
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AlertDialog
Expand Down Expand Up @@ -246,6 +247,11 @@ class GroupSettingsActivity(
@SuppressLint("CommitTransaction")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
onBackPressedDispatcher.addCallback(this) {
if (needSave()) {
UnsavedChangesDialogFragment().apply { key() }.show(supportFragmentManager, null)
} else finish()
}
setSupportActionBar(findViewById(R.id.toolbar))
supportActionBar?.apply {
setTitle(R.string.group_settings)
Expand Down Expand Up @@ -330,14 +336,10 @@ class GroupSettingsActivity(

override fun onOptionsItemSelected(item: MenuItem) = child.onOptionsItemSelected(item)

override fun onBackPressed() {
if (needSave()) {
UnsavedChangesDialogFragment().apply { key() }.show(supportFragmentManager, null)
} else super.onBackPressed()
}

override fun onSupportNavigateUp(): Boolean {
if (!super.onSupportNavigateUp()) finish()
// Route the action-bar up button through the back dispatcher so it honors the
// unsaved-changes guard instead of finishing directly (avoids data loss).
onBackPressedDispatcher.onBackPressed()
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.nekohasekai.sagernet.ui

import android.content.Intent
import android.os.Bundle
import androidx.core.content.IntentCompat
import io.nekohasekai.sagernet.R
import io.nekohasekai.sagernet.database.ProxyEntity

Expand All @@ -16,7 +17,7 @@ class ProfileSelectActivity : ThemedActivity(R.layout.layout_empty),
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val selected = intent.getParcelableExtra<ProxyEntity>(EXTRA_SELECTED)
val selected = IntentCompat.getParcelableExtra(intent, EXTRA_SELECTED, ProxyEntity::class.java)

supportFragmentManager.beginTransaction()
.replace(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import androidx.activity.addCallback
import androidx.activity.result.component1
import androidx.activity.result.component2
import androidx.activity.result.contract.ActivityResultContracts
Expand Down Expand Up @@ -221,6 +222,11 @@ class RouteSettingsActivity(

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
onBackPressedDispatcher.addCallback(this) {
if (needSave()) {
UnsavedChangesDialogFragment().apply { key() }.show(supportFragmentManager, null)
} else finish()
}
setSupportActionBar(findViewById(R.id.toolbar))
supportActionBar?.apply {
setTitle(R.string.cag_route)
Expand Down Expand Up @@ -300,14 +306,10 @@ class RouteSettingsActivity(

override fun onOptionsItemSelected(item: MenuItem) = child.onOptionsItemSelected(item)

override fun onBackPressed() {
if (needSave()) {
UnsavedChangesDialogFragment().apply { key() }.show(supportFragmentManager, null)
} else super.onBackPressed()
}

override fun onSupportNavigateUp(): Boolean {
if (!super.onSupportNavigateUp()) finish()
// Route the action-bar up button through the back dispatcher so it honors the
// unsaved-changes guard instead of finishing directly (avoids data loss).
onBackPressedDispatcher.onBackPressed()
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.view.Menu
import android.view.MenuItem
import android.view.ViewGroup.MarginLayoutParams
import android.widget.LinearLayout
import androidx.activity.addCallback
import androidx.appcompat.app.AlertDialog
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
Expand Down Expand Up @@ -53,6 +54,10 @@ class ConfigEditActivity : ThemedActivity() {
@SuppressLint("InlinedApi")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
onBackPressedDispatcher.addCallback(this) {
if (dirty) UnsavedChangesDialogFragment().apply { key() }
.show(supportFragmentManager, null) else finish()
}

intent?.extras?.apply {
getString("key")?.let { key = it }
Expand Down Expand Up @@ -165,13 +170,10 @@ class ConfigEditActivity : ThemedActivity() {
}
}

override fun onBackPressed() {
if (dirty) UnsavedChangesDialogFragment().apply { key() }
.show(supportFragmentManager, null) else super.onBackPressed()
}

override fun onSupportNavigateUp(): Boolean {
if (!super.onSupportNavigateUp()) finish()
// Route the action-bar up button through the back dispatcher so it honors the
// unsaved-changes guard instead of finishing directly (avoids data loss).
onBackPressedDispatcher.onBackPressed()
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.view.View
import android.widget.LinearLayout
import android.widget.ScrollView
import android.widget.Toast
import androidx.activity.addCallback
import androidx.activity.result.component1
import androidx.activity.result.component2
import androidx.activity.result.contract.ActivityResultContracts
Expand Down Expand Up @@ -92,6 +93,10 @@ abstract class ProfileSettingsActivity<T : AbstractBean>(

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
onBackPressedDispatcher.addCallback(this) {
if (DataStore.dirty) UnsavedChangesDialogFragment().apply { key() }
.show(supportFragmentManager, null) else finish()
}
setSupportActionBar(findViewById(R.id.toolbar))
supportActionBar?.apply {
setTitle(R.string.profile_config)
Expand Down Expand Up @@ -174,13 +179,10 @@ abstract class ProfileSettingsActivity<T : AbstractBean>(

override fun onOptionsItemSelected(item: MenuItem) = child.onOptionsItemSelected(item)

override fun onBackPressed() {
if (DataStore.dirty) UnsavedChangesDialogFragment().apply { key() }
.show(supportFragmentManager, null) else super.onBackPressed()
}

override fun onSupportNavigateUp(): Boolean {
if (!super.onSupportNavigateUp()) finish()
// Route the action-bar up button through the same back dispatcher so it honors
// the unsaved-changes guard instead of finishing directly (avoids data loss).
onBackPressedDispatcher.onBackPressed()
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.appcompat.widget.TooltipCompat
import androidx.dynamicanimation.animation.DynamicAnimation
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.withStarted
import androidx.vectordrawable.graphics.drawable.Animatable2Compat
import androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat
import com.google.android.material.floatingactionbutton.FloatingActionButton
Expand All @@ -21,6 +22,7 @@ import io.nekohasekai.sagernet.bg.BaseService
import io.nekohasekai.sagernet.ktx.getColorAttr
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
Comment thread
hawkff marked this conversation as resolved.
import java.util.*

class ServiceButton @JvmOverloads constructor(
Expand Down Expand Up @@ -64,10 +66,14 @@ class ServiceButton @JvmOverloads constructor(
private val iconConnecting by lazy {
AnimatedState(R.drawable.ic_service_connecting) {
hideProgress()
delayedAnimation = (context as LifecycleOwner).lifecycleScope.launchWhenStarted {
delayedAnimation = (context as LifecycleOwner).lifecycleScope.launch {
delay(context.resources.getInteger(android.R.integer.config_mediumAnimTime) + 1000L)
isIndeterminate = true
show()
// Gate the UI mutation on STARTED so a delayed progress reveal doesn't run
// while the activity is stopped (the old launchWhenStarted suspended here).
(context as LifecycleOwner).withStarted {
isIndeterminate = true
show()
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/io/nekohasekai/sagernet/widget/StatsBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import android.widget.TextView
import androidx.appcompat.widget.TooltipCompat
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.whenStarted
import androidx.lifecycle.withStarted
import com.google.android.material.bottomappbar.BottomAppBar
import io.nekohasekai.sagernet.R
import io.nekohasekai.sagernet.bg.BaseService
Expand Down Expand Up @@ -125,7 +125,7 @@ class StatsBar @JvmOverloads constructor(
val activity = context as MainActivity
fun postWhenStarted(what: () -> Unit) = activity.lifecycleScope.launch(Dispatchers.Main) {
delay(100L)
activity.whenStarted { what() }
activity.withStarted { what() }
}
if ((state == BaseService.State.Connected).also { hideOnScroll = it }) {
postWhenStarted {
Expand Down
Loading