Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#184 #195 Add flow-resources with StringDesc bindings for Flow #200

Merged
merged 6 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ dependencies {
commonMainApi("dev.icerock.moko:mvvm-livedata:0.13.1") // api mvvm-core, LiveData and extensions
commonMainApi("dev.icerock.moko:mvvm-state:0.13.1") // api mvvm-livedata, ResourceState class and extensions
commonMainApi("dev.icerock.moko:mvvm-livedata-resources:0.13.1") // api mvvm-core, moko-resources, extensions for LiveData with moko-resources
commonMainApi("dev.icerock.moko:mvvm-flow-resources:0.13.1") // api mvvm-core, moko-resources, extensions for Flow with moko-resources

androidMainApi("dev.icerock.moko:mvvm-flow-compose:0.13.1") // api mvvm-flow, binding extensions for Jetpack Compose (jvm, js, android)
androidMainApi("dev.icerock.moko:mvvm-livedata-compose:0.13.1") // api mvvm-livedata, binding extensions for Jetpack Compose (jvm, js, android)
Expand Down
14 changes: 14 additions & 0 deletions mvvm-flow-resources/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright 2022 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

plugins {
id("kmm-library-convention")
id("detekt-convention")
id("publication-convention")
}

dependencies {
commonMainApi(projects.mvvmFlow)
commonMainApi(libs.mokoResources)
}
2 changes: 2 additions & 0 deletions mvvm-flow-resources/src/androidMain/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="dev.icerock.moko.mvvm.flow.resources" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2022 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.mvvm.flow.resources

import android.widget.TextView
import androidx.lifecycle.LifecycleOwner
import dev.icerock.moko.mvvm.flow.CStateFlow
import dev.icerock.moko.mvvm.flow.binding.bind
import dev.icerock.moko.resources.desc.StringDesc
import kotlinx.coroutines.DisposableHandle

fun <T : StringDesc?> TextView.bindText(
lifecycleOwner: LifecycleOwner,
flow: CStateFlow<T>
): DisposableHandle {
Alex009 marked this conversation as resolved.
Show resolved Hide resolved
return flow.bind(lifecycleOwner) { this.text = it?.toString(this.context) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2022 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.mvvm.flow.resources

import dev.icerock.moko.mvvm.flow.CStateFlow
import dev.icerock.moko.mvvm.flow.DisposableHandle
import dev.icerock.moko.mvvm.flow.binding.bind
import dev.icerock.moko.resources.desc.StringDesc
import platform.UIKit.UIButton
import platform.UIKit.UIControlStateNormal

fun <T : StringDesc?> UIButton.bindTitle(
flow: CStateFlow<T>
): DisposableHandle {
return bind(flow) {
this.setTitle(
it?.localized(),
forState = UIControlStateNormal
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright 2022 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.mvvm.flow.resources

import dev.icerock.moko.mvvm.flow.CStateFlow
import dev.icerock.moko.mvvm.flow.DisposableHandle
import dev.icerock.moko.mvvm.flow.binding.bind
import dev.icerock.moko.resources.desc.StringDesc
import platform.UIKit.UILabel

fun <T : StringDesc?> UILabel.bindText(
flow: CStateFlow<T>
): DisposableHandle {
return bind(flow) { this.text = it?.localized() }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2022 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.mvvm.flow.resources

import dev.icerock.moko.mvvm.flow.CStateFlow
import dev.icerock.moko.mvvm.flow.DisposableHandle
import dev.icerock.moko.mvvm.flow.binding.bind
import dev.icerock.moko.resources.desc.StringDesc
import platform.UIKit.UITextField

fun <T : StringDesc?> UITextField.bindText(
flow: CStateFlow<T>
): DisposableHandle {
return bind(flow) { value ->
val newValue = value?.localized().orEmpty()
if (this.text == newValue) return@bind
this.text = newValue
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2022 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.mvvm.flow.resources

import dev.icerock.moko.mvvm.flow.CStateFlow
import dev.icerock.moko.mvvm.flow.DisposableHandle
import dev.icerock.moko.mvvm.flow.binding.bind
import dev.icerock.moko.resources.desc.StringDesc
import platform.UIKit.UITextView

fun <T : StringDesc?> UITextView.bindText(
flow: CStateFlow<T>
): DisposableHandle {
return bind(flow) { value ->
val newValue = value?.localized().orEmpty()
if (this.text == newValue) return@bind
this.text = newValue
}
}
57 changes: 57 additions & 0 deletions mvvm-flow-resources/src/iosTest/kotlin/UIButtonBindingsTests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2021 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

import dev.icerock.moko.mvvm.flow.cStateFlow
import dev.icerock.moko.mvvm.flow.resources.bindTitle
import dev.icerock.moko.resources.desc.StringDesc
import dev.icerock.moko.resources.desc.desc
import kotlinx.cinterop.readValue
import kotlinx.coroutines.flow.MutableStateFlow
import platform.CoreGraphics.CGRectZero
import platform.UIKit.UIButton
import kotlin.test.BeforeTest
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertEquals

@Ignore
class UIButtonBindingsTests {

private lateinit var destination: UIButton

@BeforeTest
fun setup() {
destination = UIButton(frame = CGRectZero.readValue())
}

@Test
fun `nonnullable stringdesc title`() {
val source: MutableStateFlow<StringDesc> = MutableStateFlow("init".desc())
destination.bindTitle(source.cStateFlow())
assertEquals(
expected = "init",
actual = destination.currentTitle
)
source.value = "second".desc()
assertEquals(
expected = "second",
actual = destination.currentTitle
)
}

@Test
fun `nullable stringdesc title`() {
val source: MutableStateFlow<StringDesc?> = MutableStateFlow(null)
destination.bindTitle(source.cStateFlow())
assertEquals(
expected = null,
actual = destination.currentTitle
)
source.value = "value".desc()
assertEquals(
expected = "value",
actual = destination.currentTitle
)
}
}
57 changes: 57 additions & 0 deletions mvvm-flow-resources/src/iosTest/kotlin/UILabelBindingsTests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2021 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

import dev.icerock.moko.mvvm.flow.cStateFlow
import dev.icerock.moko.mvvm.flow.resources.bindText
import dev.icerock.moko.resources.desc.StringDesc
import dev.icerock.moko.resources.desc.desc
import kotlinx.cinterop.readValue
import kotlinx.coroutines.flow.MutableStateFlow
import platform.CoreGraphics.CGRectZero
import platform.UIKit.UILabel
import kotlin.test.BeforeTest
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertEquals

@Ignore
class UILabelBindingsTests {

private lateinit var destination: UILabel

@BeforeTest
fun setup() {
destination = UILabel(frame = CGRectZero.readValue())
}

@Test
fun `nonnullable stringdesc text`() {
val source: MutableStateFlow<StringDesc> = MutableStateFlow("init".desc())
destination.bindText(source.cStateFlow())
assertEquals(
expected = "init",
actual = destination.text
)
source.value = "second".desc()
assertEquals(
expected = "second",
actual = destination.text
)
}

@Test
fun `nullable stringdesc text`() {
val source: MutableStateFlow<StringDesc?> = MutableStateFlow(null)
destination.bindText(source.cStateFlow())
assertEquals(
expected = null,
actual = destination.text
)
source.value = "value".desc()
assertEquals(
expected = "value",
actual = destination.text
)
}
}
57 changes: 57 additions & 0 deletions mvvm-flow-resources/src/iosTest/kotlin/UITextFieldBindingsTests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2021 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

import dev.icerock.moko.mvvm.flow.cStateFlow
import dev.icerock.moko.mvvm.flow.resources.bindText
import dev.icerock.moko.resources.desc.StringDesc
import dev.icerock.moko.resources.desc.desc
import kotlinx.cinterop.readValue
import kotlinx.coroutines.flow.MutableStateFlow
import platform.CoreGraphics.CGRectZero
import platform.UIKit.UITextField
import kotlin.test.BeforeTest
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertEquals

@Ignore
class UITextFieldBindingsTests {

private lateinit var destination: UITextField

@BeforeTest
fun setup() {
destination = UITextField(frame = CGRectZero.readValue())
}

@Test
fun `nonnullable stringdesc text`() {
val source: MutableStateFlow<StringDesc> = MutableStateFlow("init".desc())
destination.bindText(source.cStateFlow())
assertEquals(
expected = "init",
actual = destination.text
)
source.value = "second".desc()
assertEquals(
expected = "second",
actual = destination.text
)
}

@Test
fun `nullable stringdesc text`() {
val source: MutableStateFlow<StringDesc?> = MutableStateFlow(null)
destination.bindText(source.cStateFlow())
assertEquals(
expected = "",
actual = destination.text
)
source.value = "value".desc()
assertEquals(
expected = "value",
actual = destination.text
)
}
}
57 changes: 57 additions & 0 deletions mvvm-flow-resources/src/iosTest/kotlin/UITextViewBindingsTests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2021 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

import dev.icerock.moko.mvvm.flow.cStateFlow
import dev.icerock.moko.mvvm.flow.resources.bindText
import dev.icerock.moko.resources.desc.StringDesc
import dev.icerock.moko.resources.desc.desc
import kotlinx.cinterop.readValue
import kotlinx.coroutines.flow.MutableStateFlow
import platform.CoreGraphics.CGRectZero
import platform.UIKit.UITextView
import kotlin.test.BeforeTest
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertEquals

@Ignore
class UITextViewBindingsTests {

private lateinit var destination: UITextView

@BeforeTest
fun setup() {
destination = UITextView(frame = CGRectZero.readValue())
}

@Test
fun `nonnullable stringdesc text`() {
val source: MutableStateFlow<StringDesc> = MutableStateFlow("init".desc())
destination.bindText(source.cStateFlow())
assertEquals(
expected = "init",
actual = destination.text
)
source.value = "second".desc()
assertEquals(
expected = "second",
actual = destination.text
)
}

@Test
fun `nullable stringdesc text`() {
val source: MutableStateFlow<StringDesc?> = MutableStateFlow(null)
destination.bindText(source.cStateFlow())
assertEquals(
expected = "",
actual = destination.text
)
source.value = "value".desc()
assertEquals(
expected = "value",
actual = destination.text
)
}
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ include(":mvvm-core")
include(":mvvm-flow")
include(":mvvm-flow:apple")
include(":mvvm-flow-compose")
include(":mvvm-flow-resources")
include(":mvvm-livedata")
include(":mvvm-livedata-material")
include(":mvvm-livedata-resources")
Expand Down