icerockdev / moko-widgets

Multiplatform UI DSL with screen management in common code for mobile (android & ios) Kotlin Multiplatform development
https://moko.icerock.dev
Apache License 2.0
388 stars 32 forks source link

Help set formatted value to TextWidget #258

Closed Diy2210 closed 4 years ago

Diy2210 commented 4 years ago

Hi, I try convert value to byte size to Gb and set to TextWidget in UnitItem. But in block text = data.map { ... } I get an error. And I don't understand how add in this block value + "String" like text = data.map { it.total.desc() + "Gb" as StringDesc() } Always return Error. In Android I use android.text.format.Formatter.formatShortFileSize and all works.

Value("total" in Kb): Снимок экрана 2020-07-08 в 12 27 57

My List: Снимок экрана 2020-07-08 в 12 29 26

My fun TableUnits: Снимок экрана 2020-07-08 в 12 29 37

Class unit items where I create TextWidget: Снимок экрана 2020-07-08 в 12 28 42

Convert fun to Mb/Gb: Снимок экрана 2020-07-08 в 12 28 20

Dorofeev commented 4 years ago

If I understand correctly, what are you trying to do, you can use string template feature as shown below

text = data.map {
    if (it.total.isNotEmpty()) {
        "${bytesToGB(it.total.toLong())} Gb".desc()
    } else {
        it.total.desc()
    }
}
Diy2210 commented 4 years ago

If I understand correctly, what are you trying to do, you can use string template feature as shown below

text = data.map {
    if (it.total.isNotEmpty()) {
        "${bytesToGB(it.total.toLong())} Gb".desc()
    } else {
        it.total.desc()
    }
}

I try it, but dont work, error returns!

Dorofeev commented 4 years ago

Can you provide more information about what kind of error is it?

Tetraquark commented 4 years ago

@Diy2210 Try to use this construction:

text = data.map {
    if (it.total.isNotEmpty()) {
        "${bytesToGB(it.total.toLong())} Gb".desc()
    } else {
        it.total.desc()
    } as StringDesc
}
Diy2210 commented 4 years ago

Try to use this construction:

Oh, dont work too

Print error: Снимок экрана 2020-07-08 в 16 25 03

Error: Снимок экрана 2020-07-08 в 16 24 38

My Mutable Live Data: Снимок экрана 2020-07-08 в 16 25 28 Снимок экрана 2020-07-08 в 16 27 49

Save Value to Data: Снимок экрана 2020-07-08 в 16 27 56

Dorofeev commented 4 years ago

Looks like problem is that it.total is not a number. Something like this should resolve problem.

text = data.map {
    val longTotal = it.total.toLongOrNull()
    if (longTotal != null) {
        "${bytesToGb(longTotal)} Gb".desc()
    } else {
        it.total.desc()
    } as StringDesc
}
Diy2210 commented 4 years ago

Something like this should resolve problem.

Yeah, its work! Tnx bro! Снимок экрана 2020-07-08 в 17 04 47

Diy2210 commented 4 years ago

And one more question, I try get percents *_(free 100) / total_, but an error is returned**.

Снимок экрана 2020-07-09 в 10 42 59

Снимок экрана 2020-07-09 в 10 43 15

Dorofeev commented 4 years ago

longFree!! will cause crash if it is null, you should move it inside if statement

if (longFree != null && longTotal != null) {
   val freePerCents = (longFree * 100) / longTotal
   "${freePerCents} %".desc()
}

You can read more about null-safety in Kotlin documentation https://kotlinlang.org/docs/reference/null-safety.html

Diy2210 commented 4 years ago

I get it, all works goog, tnx again! Снимок экрана 2020-07-09 в 11 03 51