Skip to content

Instantly share code, notes, and snippets.

View plnice's full-sized avatar

Miłosz Lewandowski plnice

View GitHub Profile
@Composable
fun ClickableLinkRow(url: String) {
val customTabs = LocalCustomTabs.current
Row(
modifier = Modifier
.clickable { customTabs.launch(url) }
) {
...
}
@AndroidEntryPoint
class MainActivity {
@Inject
lateinit var customTabs: CustomTabs
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val LocalCustomTabs = staticCompositionLocalOf<CustomTabs> {
error("CompositionLocal LocalCustomTabs not present")
}
// MainActivity.onCreate
setContent {
First(customTabs)
}
// Module :first
@Composable
fun First(customTabs: CustomTabs) {
Second(customTabs)
}
@AndroidEntryPoint
class MainActivity {
@Inject
lateinit var customTabs: CustomTabs
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
...
}
@Module
@InstallIn(ActivityComponent::class)
interface CustomTabsModule {
@Binds
@ActivityScoped
fun bindCustomTabs(impl: CustomTabsImpl): CustomTabs
}
interface CustomTabs {
fun launch(url: String)
}
class CustomTabsImpl @Inject constructor(
@ActivityContext private val context: Context
) : CustomTabs {
fun launch(url: String) {
// The implementation goes here
@plnice
plnice / EnumWithDefault.kt
Last active February 18, 2021 09:39
EnumWithDefault
@Inherited
@Target(CLASS)
@Retention(RUNTIME)
annotation class EnumWithDefault(
val value: String = "UNKNOWN"
)
@EnumWithDefault // Defaults to UNKNOWN
enum class Example {
FIRST, SECOND, UNKNOWN
@plnice
plnice / activityresultscall.kt
Created March 19, 2020 17:27
Simple call using Activity Results APIs
class MyActivity : AppCompatActivity() {
private val myActionCall = prepareCall(MyContract()) { result ->
Log.i("MyActivity", "Obtained result: $result")
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
...
button.setOnClickListener {
@plnice
plnice / activityresultcontract.kt
Last active November 6, 2021 23:25
Simple ActivityResultContract
class MyContract : ActivityResultContract<Int, String>() {
companion object {
const val ACTION = "com.myapp.action.MY_ACTION"
const val INPUT_INT = "input_int"
const val OUTPUT_STRING = "output_string"
}
override fun createIntent(input: Int): Intent {
return Intent(ACTION)