Skip to content

Commit

Permalink
[ADDED] Support for deeplinking from onCreate()
Browse files Browse the repository at this point in the history
Extension of
* #199
* #192
  • Loading branch information
hossain-khan committed Jan 20, 2025
1 parent 9a6dbc9 commit ec9c8a0
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion app/src/main/java/dev/hossain/weatheralert/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.material3.adaptive.currentWindowAdaptiveInfo
import androidx.compose.runtime.remember
import androidx.core.os.BundleCompat
import com.slack.circuit.backstack.rememberSaveableBackStack
import com.slack.circuit.foundation.Circuit
Expand All @@ -24,6 +25,8 @@ import dev.hossain.weatheralert.network.NetworkMonitor
import dev.hossain.weatheralert.ui.alertslist.CurrentWeatherAlertScreen
import dev.hossain.weatheralert.ui.theme.WeatherAlertAppTheme
import dev.hossain.weatheralert.ui.theme.dimensions
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
import timber.log.Timber
import javax.inject.Inject

Expand All @@ -50,7 +53,11 @@ class MainActivity
val windowSizeClass = currentWindowAdaptiveInfo().windowSizeClass
WeatherAlertAppTheme(dimensions = windowSizeClass.windowWidthSizeClass.dimensions()) {
// See https://slackhq.github.io/circuit/navigation/
val backStack = rememberSaveableBackStack(root = CurrentWeatherAlertScreen("root"))
val stack: ImmutableList<Screen> =
remember {
parseDeepLinkedScreens(intent) ?: persistentListOf(CurrentWeatherAlertScreen("root"))
}
val backStack = rememberSaveableBackStack(stack)
navigator = rememberCircuitNavigator(backStack)

// See https://slackhq.github.io/circuit/circuit-content/
Expand Down Expand Up @@ -97,4 +104,16 @@ class MainActivity
navigator.goTo(destinationScreen)
}
}

private fun parseDeepLinkedScreens(intent: Intent): ImmutableList<Screen>? {
val bundle: Bundle = intent.extras ?: return null
val screen: Screen? =
BundleCompat.getParcelable(
bundle,
BUNDLE_KEY_DEEP_LINK_DESTINATION_SCREEN,
Screen::class.java,
)
// Builds stack of screens to navigate to.
return screen?.let { persistentListOf(CurrentWeatherAlertScreen("root"), it) }
}
}

0 comments on commit ec9c8a0

Please sign in to comment.