Giter VIP home page Giter VIP logo

Comments (16)

xgrommx avatar xgrommx commented on May 22, 2024

I found how can I to use certain this

lineChart {
 setOnChartValueSelectedListener(this@MainActivity)
}

But my other issues still to be resolved.

from anko.

yanex avatar yanex commented on May 22, 2024

Hello, @xgrommx!

You can make use of ctx and act extension properties in any class that extends Activity (explained in Instance shorthands part of manual) as well as using this@MainActivity syntax.

As for replacing setData() with data property, Kotlin does not provide native properties for Java classes automatically. Properties for the Views in Android SDK are contained in Koan, though you must write your own for the custom types.

For example,

var <T> Chart<T>.data: T
    get() = getData()
    set(value) = setData(value)

from anko.

xgrommx avatar xgrommx commented on May 22, 2024

@yanex I don't know when I should put this code =(

var <T> Chart<T>.data: T
    set(value) = setData(value)

from anko.

xgrommx avatar xgrommx commented on May 22, 2024

Also I have yet another question. Now I use this construction:

                    var set1 = RadarDataSet(generateSourceY(2), "Set 1")
                    set1.setColor(Color.RED)
                    set1.setDrawFilled(true)
                    set1.setLineWidth(2f)

But I would like use it as in C#(I'm .net developer, but kotlin of interest to me)

                    RadarDataSet() {
                        source = generateSourceY(2)
                        label = "Set 1"
                        color = Color.RED
                        drawFilled = true
                        lineWidth = 2f
                    }

or

                    RadarDataSet(generateSourceY(2), "Set 1") {
                        color = Color.RED
                        drawFilled = true
                        lineWidth = 2f
                    }

Could you please explain how can I do it?

from anko.

yanex avatar yanex commented on May 22, 2024

I don't know when I should put this code =(

You could put it in any .kt file in your project from where you could import it.
If you have lots of custom components, it would be easier to manage them if put in the separate file (CustomDslComponents.kt or similar).

By the way, your property declaration doesn't contain get() clause. As for now, val property in Kotlin must contain a getter, and var (mutable) property — a pair of getter and setter.

Could you please explain how can I do it?

The code is similar as for getData() except it has different types:

var RadarDataSet.lineWidth: Float
    get() = getLineWidth()
    set(value) = setLineWidth(value)

And so on. Also you could use a Kotlin's with() function:

with(RadarDataSet()) {
    // `this` now is a RadarDataSet instance
    color = Color.RED
    lineWidth = 2f
}

You can read more about extension functions and properties here.

from anko.

xgrommx avatar xgrommx commented on May 22, 2024

But with return Unit type instead of a new instance RadarDataSet =( and if I use without with operator I got errors =>

var set = RadarDataSet(generateSourceY(2), "Set 1") {
                        color = Color.RED
                    }

from anko.

yanex avatar yanex commented on May 22, 2024

Well, you can either return this from lambda:

val dataSet = with(RadarDataSet()) {
    color = Color.RED
    this
}

or replace with function with the custom function that returns the argument instead of lambda calculation result:

inline fun <T> have(obj: T, init: T.() -> Unit): T {
    obj.init()
    return obj
}

val dataSet = have(RadarDataSet()) {
    color: Color.RED
}

As an alternative, you could make a new function specially for the RadarDataSet() type that imitates constructor:

fun RadarDataSet(init: RadarDataSet.() -> Unit): RadarDataSet {
    val obj = RadarDataSet()
    obj.init()
    return obj
}

val dataSet = RadarDataSet {
    color: Color.RED
}

from anko.

xgrommx avatar xgrommx commented on May 22, 2024

Thanks! It's work now!)

    public var RadarDataSet.color: Int
        get() = getColor()
        set(value) = setColor(value)

    public var RadarDataSet.lineWidth: Float
        get() = getLineWidth()
        set(value) = setLineWidth(value)

    public var RadarDataSet.drawFilled: Boolean
        get() = isDrawFilledEnabled()
        set(value) = setDrawFilled(value)

    fun RadarDataSet(list: ArrayList<Entry>, label: String, init: RadarDataSet.() -> Unit): RadarDataSet {
        val obj = RadarDataSet(list, label)
        obj.init()
        return obj;
    }
    var set = RadarDataSet(generateSourceY(2), "Set1") {
        color = Color.RED
        lineWidth = 2f
        drawFilled = true
    }
}

As I understood the same way for radarChart?

    fun ViewManager.radarChart(init: RadarChart.() -> Unit = {}) = __dslAddView({ RadarChart(it)}, init, this)

I just need create similar function and getters, setters and I could be use it like a RadarDataSet?

from anko.

yanex avatar yanex commented on May 22, 2024

Yes, that's it!

from anko.

xgrommx avatar xgrommx commented on May 22, 2024

But that is not working in this case. RadarChart has the method getXLabels() which return XLabels class.

    public var XLabels.textSize: Float
        get() = getTextSize()
        set(value) = setTextSize(value)

And this code doesn't work now. No errors but and no results of work

    getXLabels() {
        textSize = 9f
    }

How can I solve this case?

from anko.

yanex avatar yanex commented on May 22, 2024

Which getXLabels() function do you use? If one defined in RadarChart.java, the code you provided is unlikely to compile.

It would be better if you show some additional code.

from anko.

xgrommx avatar xgrommx commented on May 22, 2024

Sure, all files you can see in my repository. Now I use java way https://github.com/xgrommx/KoanMPAndroidChartExample/blob/master/app/src/main/kotlin/com/gromm/koanexample/MainActivity.kt#L152 but will be good if I can use a declarative style.

from anko.

yanex avatar yanex commented on May 22, 2024

Well, you could write this (the fastest way if you don't need the result):

with (getXLabels()) {
    setTextSize(9f)
}

Or write an another extension function:

fun RadarChart.xLabels(init: XLabels.() -> Unit): XLabels {
    val obj = getXLabels()
    obj.init()
    return obj
}

xLabels {
    setTextSize(9f)
}

from anko.

xgrommx avatar xgrommx commented on May 22, 2024

Awesome! Thanks, as for me Kotlin looks very clearly unlike Scala and it's good. And for Android Scala has very big runtime library. My choise for Android it's Kotlin =) Could I to ask you in future if I have questions?

from anko.

xgrommx avatar xgrommx commented on May 22, 2024

Also I have one small question. This is correct syntax or I could you use smth instead of a ViewManager

fun ViewManager.radarChart(init: RadarChart.() -> Unit = {}) = __dslAddView({ RadarChart(it)}, init, this)

Now I cannot extract a declaration of my radarChart in another function because this one required parent layout like a verticalLayout or etc.

from anko.

yanex avatar yanex commented on May 22, 2024

Sorry for the long response.
You could extract part of your DSL as an extension function.

verticalLayout {
  frameLayout {
    textView("Text")
  }
}

And extracted version:

verticalLayout {
  extractedFunction()
}

fun LinearLayout.framedText() {
  frameLayout {
    textView("Text")
  }
}

from anko.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.