Skip to content

Commit

Permalink
Ensure code only run when created
Browse files Browse the repository at this point in the history
  • Loading branch information
Mygod committed Jan 19, 2020
1 parent 3d079f2 commit e9dc275
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.whenCreated
import androidx.preference.*
import com.github.shadowsocks.database.Profile
import com.github.shadowsocks.database.ProfileManager
Expand Down Expand Up @@ -171,7 +172,7 @@ class ProfileConfigFragment : PreferenceFragmentCompat(),
super.onAttach(context)
receiver = context.listenForPackageChanges(false) {
lifecycleScope.launch(Dispatchers.Main) { // wait until changes were flushed
lifecycleScope.launchWhenCreated { initPlugins() }
whenCreated { initPlugins() }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ import androidx.appcompat.widget.TooltipCompat
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.observe
import androidx.lifecycle.whenStarted
import com.github.shadowsocks.MainActivity
import com.github.shadowsocks.R
import com.github.shadowsocks.bg.BaseService
import com.github.shadowsocks.net.HttpsTest
import com.google.android.material.bottomappbar.BottomAppBar
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class StatsBar @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null,
Expand Down Expand Up @@ -76,8 +78,8 @@ class StatsBar @JvmOverloads constructor(context: Context, attrs: AttributeSet?

fun changeState(state: BaseService.State) {
val activity = context as MainActivity
fun postWhenStarted(what: () -> Unit) = activity.lifecycleScope.launchWhenStarted {
withContext(Dispatchers.Main) { what() }
fun postWhenStarted(what: () -> Unit) = activity.lifecycleScope.launch {
withContext(Dispatchers.Main) { activity.whenStarted { what() } }
}
if ((state == BaseService.State.Connected).also { hideOnScroll = it }) {
postWhenStarted { performShow() }
Expand Down

0 comments on commit e9dc275

Please sign in to comment.