Skip to content

Commit

Permalink
Merge pull request #6 from karthyks/fix-issue/5-configuration-change
Browse files Browse the repository at this point in the history
[ISSUE-5] Fix orientation change issue
  • Loading branch information
karthyks authored Mar 17, 2023
2 parents 8b18bda + af09279 commit b9c9a11
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 26 deletions.
3 changes: 3 additions & 0 deletions composed/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@ dependencies {
implementation "androidx.compose.ui:ui"
implementation "androidx.compose.foundation:foundation"
implementation "androidx.compose.ui:ui-tooling"

def lifecycle_version = "2.5.1"
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycle_version"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,28 @@ package io.github.karthyks.composed
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewmodel.compose.viewModel

val LocalOverlayProvider =
staticCompositionLocalOf<OverlayProvider> { error("No overlay provided!") }

@Composable
fun ProvideOverlay(content: @Composable OverlayScope.() -> Unit) {
val viewModel: InternalViewModel = viewModel()
CompositionLocalProvider(
LocalOverlayProvider provides OverlayProviderImpl(),
LocalOverlayProvider provides viewModel.overlayProvider,
) {
with(OverlayScopeInstance) {
content()
OverlayManager()
}
}
}

/**
* ViewModel to hold overlayProvider, so it can survive on configurationChange.
*/
internal class InternalViewModel : ViewModel() {
internal val overlayProvider = OverlayProviderImpl()
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,17 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import io.github.karthyks.composed.ProvideOverlay

class SampleActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ProvideOverlay {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
RenderSampleScreen()
}
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
RenderSampleScreen()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@ import androidx.compose.ui.unit.dp
import com.karthyks.composed.samples.overlays.FullScreenOverlay
import com.karthyks.composed.samples.test.TestTag
import io.github.karthyks.composed.LocalOverlayProvider
import io.github.karthyks.composed.ProvideOverlay

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun RenderSampleScreen() {
Scaffold(
topBar = {
RenderTopBar()
}
) { padding ->
val overlayProvider = LocalOverlayProvider.current
RenderContent(modifier = Modifier.padding(padding)) {
overlayProvider.showOverlay(FullScreenOverlay())
ProvideOverlay {
Scaffold(
topBar = {
RenderTopBar()
}
) { padding ->
val overlayProvider = LocalOverlayProvider.current
RenderContent(modifier = Modifier.padding(padding)) {
overlayProvider.showOverlay(FullScreenOverlay())
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion test-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ android {

defaultConfig {
applicationId "com.karthyks.composed"
testApplicationId "com.karthyks.composed.test"
minSdk 21
targetSdk 33
versionCode 1
Expand Down Expand Up @@ -64,7 +65,6 @@ dependencies {
implementation 'androidx.compose.material:material-icons-extended'
implementation 'androidx.compose.material3:material3-window-size-class'

implementation(project(":composed"))
implementation(project(":samples"))

implementation "androidx.compose.ui:ui-tooling-preview"
Expand All @@ -73,6 +73,7 @@ dependencies {
debugImplementation "androidx.compose.ui:ui-test-manifest"

testImplementation 'junit:junit:4.13.2'
androidTestImplementation(project(":composed"))
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation "androidx.compose.ui:ui-test-junit4"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,17 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import com.karthyks.composed.samples.RenderSampleScreen
import io.github.karthyks.composed.ProvideOverlay

class TestActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ProvideOverlay {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
RenderSampleScreen()
}
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
RenderSampleScreen()
}
}
}
Expand Down

0 comments on commit b9c9a11

Please sign in to comment.