-
-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from adrielcafe/fix/custom-view-context
fix: find activity when inside compose custom view in legacy code
- Loading branch information
Showing
11 changed files
with
251 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
sample/src/main/java/cafe/adriel/voyager/sample/androidLegacy/LegacyActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package cafe.adriel.voyager.sample.androidLegacy | ||
|
||
import android.os.Bundle | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.compose.ui.platform.ComposeView | ||
import cafe.adriel.voyager.navigator.Navigator | ||
import cafe.adriel.voyager.sample.R | ||
import dagger.hilt.android.AndroidEntryPoint | ||
|
||
@AndroidEntryPoint | ||
class LegacyActivity : AppCompatActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_legacy) | ||
val composeView = findViewById<ComposeView>(R.id.composeView) | ||
composeView.setContent { | ||
Navigator(LegacyScreenOne()) | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
sample/src/main/java/cafe/adriel/voyager/sample/androidLegacy/LegacyModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package cafe.adriel.voyager.sample.androidLegacy | ||
|
||
import cafe.adriel.voyager.core.model.ScreenModel | ||
import cafe.adriel.voyager.hilt.ScreenModelKey | ||
import dagger.Binds | ||
import dagger.Module | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.components.ActivityComponent | ||
import dagger.multibindings.IntoMap | ||
|
||
@Module | ||
@InstallIn(ActivityComponent::class) | ||
abstract class LegacyModule { | ||
@Binds | ||
@IntoMap | ||
@ScreenModelKey(LegacyOneScreenModel::class) | ||
abstract fun bindLegacyOneScreenModel(legacyOneScreenModel: LegacyOneScreenModel): ScreenModel | ||
|
||
@Binds | ||
@IntoMap | ||
@ScreenModelKey(LegacyTwoScreenModel::class) | ||
abstract fun bindLegacyTwoScreenModel(legacyTwoScreenModel: LegacyTwoScreenModel): ScreenModel | ||
} |
13 changes: 13 additions & 0 deletions
13
sample/src/main/java/cafe/adriel/voyager/sample/androidLegacy/LegacyOneScreenModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package cafe.adriel.voyager.sample.androidLegacy | ||
|
||
import cafe.adriel.voyager.core.model.ScreenModel | ||
import javax.inject.Inject | ||
|
||
class LegacyOneScreenModel @Inject constructor() : ScreenModel { | ||
val text = "I'm legacy one screen model" | ||
|
||
override fun onDispose() { | ||
println(">>>> disposing $this") | ||
super.onDispose() | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
sample/src/main/java/cafe/adriel/voyager/sample/androidLegacy/LegacyScreenOne.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package cafe.adriel.voyager.sample.androidLegacy | ||
|
||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.material.Button | ||
import androidx.compose.material.MaterialTheme | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
import cafe.adriel.voyager.androidx.AndroidScreen | ||
import cafe.adriel.voyager.hilt.getScreenModel | ||
import cafe.adriel.voyager.navigator.LocalNavigator | ||
import cafe.adriel.voyager.navigator.currentOrThrow | ||
|
||
class LegacyScreenOne : AndroidScreen() { | ||
|
||
@Composable | ||
override fun Content() { | ||
val navigator = LocalNavigator.currentOrThrow | ||
val model: LegacyOneScreenModel = getScreenModel() | ||
|
||
Column( | ||
verticalArrangement = Arrangement.Center, | ||
horizontalAlignment = Alignment.CenterHorizontally, | ||
modifier = Modifier.fillMaxSize() | ||
) { | ||
Text( | ||
text = model.text, | ||
style = MaterialTheme.typography.h5 | ||
) | ||
|
||
Spacer(modifier = Modifier.height(16.dp)) | ||
|
||
Text( | ||
text = model.toString().substringAfterLast('.'), | ||
style = MaterialTheme.typography.body2 | ||
) | ||
|
||
Spacer(modifier = Modifier.height(16.dp)) | ||
|
||
Button( | ||
onClick = { navigator.push(LegacyScreenTwo()) }, | ||
content = { Text(text = "Go to Two") } | ||
) | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
sample/src/main/java/cafe/adriel/voyager/sample/androidLegacy/LegacyScreenTwo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package cafe.adriel.voyager.sample.androidLegacy | ||
|
||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.material.Button | ||
import androidx.compose.material.MaterialTheme | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
import cafe.adriel.voyager.androidx.AndroidScreen | ||
import cafe.adriel.voyager.hilt.getScreenModel | ||
import cafe.adriel.voyager.navigator.LocalNavigator | ||
import cafe.adriel.voyager.navigator.currentOrThrow | ||
|
||
class LegacyScreenTwo : AndroidScreen() { | ||
|
||
@Composable | ||
override fun Content() { | ||
val navigator = LocalNavigator.currentOrThrow | ||
val model: LegacyTwoScreenModel = getScreenModel() | ||
|
||
Column( | ||
verticalArrangement = Arrangement.Center, | ||
horizontalAlignment = Alignment.CenterHorizontally, | ||
modifier = Modifier.fillMaxSize() | ||
) { | ||
Text( | ||
text = model.text, | ||
style = MaterialTheme.typography.h5 | ||
) | ||
|
||
Spacer(modifier = Modifier.height(16.dp)) | ||
|
||
Text( | ||
text = model.toString().substringAfterLast('.'), | ||
style = MaterialTheme.typography.body2 | ||
) | ||
|
||
Spacer(modifier = Modifier.height(16.dp)) | ||
|
||
Button( | ||
onClick = navigator::pop, | ||
content = { Text(text = "Go to One") } | ||
) | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
sample/src/main/java/cafe/adriel/voyager/sample/androidLegacy/LegacyTwoScreenModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package cafe.adriel.voyager.sample.androidLegacy | ||
|
||
import cafe.adriel.voyager.core.model.ScreenModel | ||
import javax.inject.Inject | ||
|
||
class LegacyTwoScreenModel @Inject constructor() : ScreenModel { | ||
val text = "I'm legacy two screen model" | ||
|
||
override fun onDispose() { | ||
println(">>>> disposing $this") | ||
super.onDispose() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".androidLegacy.LegacyActivity"> | ||
|
||
<View | ||
android:id="@+id/emptyView" | ||
android:layout_width="0dp" | ||
android:layout_height="1dp" | ||
android:layout_alignParentStart="true" | ||
android:layout_alignParentEnd="true" | ||
android:layout_centerInParent="true" /> | ||
|
||
<FrameLayout | ||
android:layout_width="0dp" | ||
android:layout_height="0dp" | ||
android:layout_above="@id/emptyView" | ||
android:layout_alignParentStart="true" | ||
android:layout_alignParentTop="true" | ||
android:layout_alignParentEnd="true" | ||
android:background="#FF888888"> | ||
|
||
<TextView | ||
style="@style/TextAppearance.AppCompat.Display3" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_gravity="center" | ||
android:text="@string/compose_view_title" /> | ||
|
||
</FrameLayout> | ||
|
||
<androidx.compose.ui.platform.ComposeView | ||
android:id="@+id/composeView" | ||
android:layout_width="0dp" | ||
android:layout_height="0dp" | ||
android:layout_below="@id/emptyView" | ||
android:layout_alignParentStart="true" | ||
android:layout_alignParentEnd="true" | ||
android:layout_alignParentBottom="true" /> | ||
|
||
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
<resources> | ||
<string name="app_name">Voyager</string> | ||
</resources> | ||
<resources> | ||
<string name="app_name">Voyager</string> | ||
<string name="compose_view_title">This is a legacy content</string> | ||
</resources> |
37 changes: 28 additions & 9 deletions
37
voyager-hilt/src/main/java/cafe/adriel/voyager/hilt/internal/ContextExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,28 @@ | ||
package cafe.adriel.voyager.hilt.internal | ||
|
||
import android.content.Context | ||
import androidx.activity.ComponentActivity | ||
|
||
@PublishedApi | ||
internal val Context.componentActivity: ComponentActivity | ||
get() = this as? ComponentActivity | ||
?: error("Invalid local context. It must be a ComponentActivity") | ||
package cafe.adriel.voyager.hilt.internal | ||
|
||
import android.content.Context | ||
import android.content.ContextWrapper | ||
import androidx.activity.ComponentActivity | ||
import androidx.lifecycle.ViewModelProvider | ||
|
||
// Unfortunately findOwner function is internal in activity-compose | ||
// TODO: Maybe move to androidx module because we'll need this function when implement onCloseRequest support | ||
internal inline fun <reified T> findOwner(context: Context): T? { | ||
var innerContext = context | ||
while (innerContext is ContextWrapper) { | ||
if (innerContext is T) { | ||
return innerContext | ||
} | ||
innerContext = innerContext.baseContext | ||
} | ||
return null | ||
} | ||
|
||
@PublishedApi | ||
internal val Context.componentActivity: ComponentActivity | ||
get() = findOwner<ComponentActivity>(this) | ||
?: error("Context must be a androidx.activity.ComponentActivity. Current is $this") | ||
|
||
@PublishedApi | ||
internal val Context.defaultViewModelProviderFactory: ViewModelProvider.Factory | ||
get() = componentActivity.defaultViewModelProviderFactory |