Skip to content

Commit

Permalink
Update Conductor
Browse files Browse the repository at this point in the history
  • Loading branch information
ztimms73 committed Apr 21, 2023
1 parent 2352bce commit e28c62a
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 55 deletions.
7 changes: 5 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,10 @@ dependencies {
implementation("com.mikepenz:aboutlibraries-core:${BuildPluginsVersion.ABOUTLIB_PLUGIN}")

// Conductor
val conductorVersion = "3.1.7"
val conductorVersion = "4.0.0-preview-2"
implementation("com.bluelinelabs:conductor:$conductorVersion")
implementation("com.bluelinelabs:conductor-viewpager:$conductorVersion")
implementation("com.github.tachiyomiorg:conductor-support-preference:$conductorVersion")
implementation("com.github.tachiyomiorg:conductor-support-preference:3.1.7")

// Crash reports/analytics
implementation("ch.acra:acra-http:5.9.7")
Expand All @@ -268,6 +268,9 @@ dependencies {

// Markdown
implementation("io.noties.markwon:core:4.6.2")

// Memory leaks
implementation("com.squareup.leakcanary:leakcanary-android:2.7")
}

tasks {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

<application
android:name=".App"
android:enableOnBackInvokedCallback="true"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import android.content.Intent
import android.os.IBinder
import android.os.PowerManager
import androidx.core.content.ContextCompat
import java.io.File
import logcat.LogPriority
import org.xtimms.ridebus.BuildConfig
import org.xtimms.ridebus.R
Expand All @@ -26,6 +25,7 @@ import org.xtimms.ridebus.util.system.acquireWakeLock
import org.xtimms.ridebus.util.system.isServiceRunning
import org.xtimms.ridebus.util.system.logcat
import uy.kohesive.injekt.injectLazy
import java.io.File

class DatabaseUpdateService : Service() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.annotation.CallSuper
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.bluelinelabs.conductor.Controller
Expand All @@ -24,6 +25,10 @@ abstract class BaseController<VB : ViewBinding>(bundle: Bundle? = null) :

lateinit var viewScope: CoroutineScope

init {
watchForLeaks()
}

init {
addLifecycleListener(
object : LifecycleListener() {
Expand Down Expand Up @@ -59,7 +64,8 @@ abstract class BaseController<VB : ViewBinding>(bundle: Bundle? = null) :
return binding.root
}

open fun onViewCreated(view: View) {}
@CallSuper
open fun onViewCreated(view: View) = Unit

override fun onChangeStarted(handler: ControllerChangeHandler, type: ControllerChangeType) {
view?.hideKeyboard()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,10 @@ import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.view.View
import android.view.ViewGroup
import com.bluelinelabs.conductor.ControllerChangeHandler
import com.bluelinelabs.conductor.changehandler.FadeChangeHandler
import com.bluelinelabs.conductor.changehandler.AnimatorChangeHandler

/**
* A variation of [FadeChangeHandler] that only fades in.
*/
class OneWayFadeChangeHandler : FadeChangeHandler {
constructor()
constructor(removesFromViewOnPush: Boolean) : super(removesFromViewOnPush)
constructor(duration: Long) : super(duration)
constructor(duration: Long, removesFromViewOnPush: Boolean) : super(
duration,
removesFromViewOnPush
)
class OneWayFadeChangeHandler :
AnimatorChangeHandler(DEFAULT_ANIMATION_DURATION, true) {

override fun getAnimator(
container: ViewGroup,
Expand All @@ -33,14 +23,12 @@ class OneWayFadeChangeHandler : FadeChangeHandler {
animator.play(ObjectAnimator.ofFloat(to, View.ALPHA, start, 1f))
}

if (from != null && (!isPush || removesFromViewOnPush())) {
if (from != null) {
from.alpha = 0f
}

return animator
}

override fun copy(): ControllerChangeHandler {
return OneWayFadeChangeHandler(animationDuration, removesFromViewOnPush())
}
override fun resetFromView(from: View) = Unit
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.xtimms.ridebus.ui.base.controller

import android.view.View
import com.bluelinelabs.conductor.Controller
import com.bluelinelabs.conductor.ControllerChangeHandler
import com.bluelinelabs.conductor.ControllerChangeType
import leakcanary.AppWatcher

private class RefWatchingControllerLifecycleListener : Controller.LifecycleListener() {

private var hasExited = false

override fun postDestroy(controller: Controller) {
if (hasExited) {
controller.expectWeaklyReachable()
}
}

override fun preDestroyView(controller: Controller, view: View) {
AppWatcher.objectWatcher.expectWeaklyReachable(
view,
"A destroyed controller view should have only weak references."
)
}

override fun onChangeEnd(
controller: Controller,
changeHandler: ControllerChangeHandler,
changeType: ControllerChangeType
) {
hasExited = !changeType.isEnter
if (controller.isDestroyed) {
controller.expectWeaklyReachable()
}
}

private fun Controller.expectWeaklyReachable() {
AppWatcher.objectWatcher.expectWeaklyReachable(
this,
"A destroyed controller should have only weak references."
)
}
}

fun Controller.watchForLeaks() {
addLifecycleListener(RefWatchingControllerLifecycleListener())
}
39 changes: 5 additions & 34 deletions app/src/main/java/org/xtimms/ridebus/ui/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import android.os.Build
import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.animation.doOnEnd
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
Expand All @@ -22,16 +21,14 @@ import androidx.core.view.isVisible
import androidx.interpolator.view.animation.FastOutSlowInInterpolator
import androidx.interpolator.view.animation.LinearOutSlowInInterpolator
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.whenResumed
import androidx.lifecycle.withResumed
import androidx.preference.PreferenceDialogController
import com.bluelinelabs.conductor.Conductor
import com.bluelinelabs.conductor.Controller
import com.bluelinelabs.conductor.ControllerChangeHandler
import com.bluelinelabs.conductor.Router
import com.google.android.material.navigation.NavigationBarView
import dev.chrisbanes.insetter.applyInsetter
import kotlin.collections.set
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.launchIn
import logcat.LogPriority
import org.xtimms.ridebus.BuildConfig
Expand Down Expand Up @@ -64,6 +61,7 @@ import org.xtimms.ridebus.util.system.isTablet
import org.xtimms.ridebus.util.system.logcat
import org.xtimms.ridebus.util.system.toast
import org.xtimms.ridebus.util.view.setNavigationBarTransparentCompat
import kotlin.collections.set

class MainActivity : BaseActivity() {

Expand Down Expand Up @@ -171,6 +169,8 @@ class MainActivity : BaseActivity() {

val container: ViewGroup = binding.controllerContainer
router = Conductor.attachRouter(this, container, savedInstanceState)
.setPopRootControllerMode(Router.PopRootControllerMode.NEVER)
.setOnBackPressedDispatcherEnabled(true)
router.addChangeListener(
object : ControllerChangeHandler.ControllerChangeListener {
override fun onChangeStarted(
Expand Down Expand Up @@ -209,7 +209,7 @@ class MainActivity : BaseActivity() {
launchUI {
requestNotificationsPermission()
when (preferences.city().defaultValue) {
"-1" -> whenResumed {
"-1" -> withResumed {
WelcomeDialogController().showDialog(router)
}
}
Expand Down Expand Up @@ -373,35 +373,6 @@ class MainActivity : BaseActivity() {
binding.toolbar.setNavigationOnClickListener(null)
}

override fun onBackPressed() {
val backstackSize = router.backstackSize
if (backstackSize == 1 && router.getControllerWithTag("$startScreenId") == null) {
// Return to start screen
setSelectedNavItem(startScreenId)
} else if (shouldHandleExitConfirmation()) {
// Exit confirmation (resets after 2 seconds)
lifecycleScope.launchUI { resetExitConfirmation() }
} else if (backstackSize == 1 || !router.handleBack()) {
// Regular back
super.onBackPressed()
}
}

private suspend fun resetExitConfirmation() {
isConfirmingExit = true
val toast = toast(R.string.confirm_exit, Toast.LENGTH_LONG)
delay(2000)
toast.cancel()
isConfirmingExit = false
}

private fun shouldHandleExitConfirmation(): Boolean {
return router.backstackSize == 1 &&
router.getControllerWithTag("$startScreenId") != null &&
preferences.confirmExit() &&
!isConfirmingExit
}

private fun setSelectedNavItem(itemId: Int) {
if (!isFinishing) {
nav.selectedItemId = itemId
Expand Down

0 comments on commit e28c62a

Please sign in to comment.