-
-
Notifications
You must be signed in to change notification settings - Fork 718
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compose KoinIsolatedContext to help run child composables using a iso…
…lated Koin context
- Loading branch information
1 parent
49b298c
commit 8ca591b
Showing
6 changed files
with
101 additions
and
0 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
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
37 changes: 37 additions & 0 deletions
37
...se/sample-android-compose/src/main/java/org/koin/sample/androidx/compose/SDKComposable.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,37 @@ | ||
package org.koin.sample.androidx.compose | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import org.koin.compose.KoinIsolatedContext | ||
import org.koin.compose.rememberKoinInject | ||
import org.koin.sample.androidx.compose.data.sdk.SDKData | ||
import org.koin.sample.androidx.compose.di.IsolatedContextSDK | ||
|
||
|
||
@Composable | ||
fun IsolatedSDKComposable( | ||
parentStatus: String = "- status -", | ||
) { | ||
KoinIsolatedContext(IsolatedContextSDK.koinApp) { | ||
SDKComposable(parentStatus) | ||
} | ||
} | ||
|
||
@Composable | ||
private fun SDKComposable( | ||
parentStatus: String = "- status -", | ||
sdkData: SDKData = rememberKoinInject() | ||
) { | ||
var created by remember { mutableStateOf(false) } | ||
|
||
if (created) { | ||
clickComponent("sdkData", sdkData.id, parentStatus) { | ||
ButtonForCreate("-X- sdkData") { created = !created } | ||
} | ||
} else { | ||
ButtonForCreate("(+) sdkData") { created = !created } | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...sample-android-compose/src/main/java/org/koin/sample/androidx/compose/data/sdk/SDKData.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,7 @@ | ||
package org.koin.sample.androidx.compose.data.sdk | ||
|
||
import java.util.UUID | ||
|
||
class SDKData { | ||
val id = UUID.randomUUID().toString() | ||
} |
17 changes: 17 additions & 0 deletions
17
...mple-android-compose/src/main/java/org/koin/sample/androidx/compose/di/IsolatedContext.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,17 @@ | ||
package org.koin.sample.androidx.compose.di | ||
|
||
import org.koin.dsl.koinApplication | ||
import org.koin.dsl.module | ||
import org.koin.sample.androidx.compose.data.sdk.SDKData | ||
|
||
val sdkModule = module { | ||
single { SDKData() } | ||
} | ||
|
||
object IsolatedContextSDK { | ||
|
||
val koinApp = koinApplication { | ||
modules(sdkModule) | ||
} | ||
|
||
} |