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

Make ChartHighlighter a protocol, publicly accessible #567

Closed
wants to merge 2 commits into from

Conversation

iheart2code
Copy link

Relates to #357

@pmairoldi
Copy link
Collaborator

Pretty sweet stuff man 👍. Could this be used from any graph type and not just BarLineChartViewBase?

@iheart2code
Copy link
Author

Yes, it can. Although I had some difficulty figuring out how to get a downcast version of the chart into some of the classes. See the TODO here: https://github.com/danielgindi/ios-charts/pull/567/files#diff-ac32fb888c389b4e17eca7e5ac42579dR20

I am assuming that the solution involves generics somehow. Never been very strong on generics.

@pmairoldi
Copy link
Collaborator

Something like this could possibly work

public protocol ChartHighlighter {
    public typealias ChartViewType
    weak var chart: ChartViewType? { get set }
}

@danielgindi
Copy link
Collaborator

I'm going to hold this a bit, as Phil and I are working on some changes that make the library more interface-oriented, mainly to allow querying for data (instead of pushing an array of entries).
We're trying to keep it 100% backwards compatible, but things could happen.

Anyway, I'll try to add this into the mix before we merge to master!

@iheart2code
Copy link
Author

No worries. Good luck with your work. I'm excited to see what you've got in store.

@danielgindi
Copy link
Collaborator

Don't you think that the use of an extension in this case is a little bit problematic?

I mean if we want to have an interface with some base functionality implemented, we should probably have an "abstract" base class...

What will happen if an implementation has a function with the same name as one implemented in the extension?

@danielgindi
Copy link
Collaborator

@iheart2code Actually I don't get why not make the original class' properties public..?

@pmairoldi
Copy link
Collaborator

defining a protocol and then defining an extension of that protocol is the same as an abstract base class. Like can be seen below.

protocol CoolType {
    func superFunction() -> String
}

extension CoolType {
    func superFunction() -> String {
        return "blue"
    }
}

struct AType: CoolType {

}

struct BType: CoolType {

    func superFunction() -> String {
        return "red"
    }
}

let a = AType().superFunction()
let b = BType().superFunction()

The output looks like so:
screen shot 2016-01-10 at 1 10 10 am

@danielgindi
Copy link
Collaborator

@petester42 thanks for explaining!

Is there an advantage of having this as an abstract class and not just making those properties public?
Because as I see it, if the class conforms to a protocol, then its methods are public anyway as those are protocol methods. Which means that just making the properties public in the first place will achieve the same desired effect.

@danielgindi
Copy link
Collaborator

I just want to be finished with this one correctly on the iOS and Android side, before merging in dynamic-preparations branch which introduces interfaces between current dataset classes and their implementations, and easy Realm.io integration.

@pmairoldi
Copy link
Collaborator

The advantage is that you have a flatter hierarchy instead of more nested one with subclass of a subclass etc.

You can also set the access control to protocol methods like so:

public struct BType: CoolType {

    internal func superFunction() -> String {
        return "red"
    }
}

that function would only be public inside the library which for what this is trying to accomplish probably doesn't make sense but you can do it.

@danielgindi
Copy link
Collaborator

Remember that the library is also used from ObjC, so if anyone tries to call a protocol method from ObjC, it will let him...

Also that protocol should actually be exposed to ObjC, which is not the case right now.

Anyway, I still don't see why we need the additional hierarchy if we just want to make things public!

@pmairoldi
Copy link
Collaborator

The point would be that a user would not have to subclass something from the library in order set their own chart highlighter. Subclassing add a level of complexity since you never know if you need to call super or not (unless the function is properly annotated). Having it be a protocol makes it more flexible for a user since it is more straight forward. Implement these methods and it will work.

@danielgindi
Copy link
Collaborator

Well in such a case we would have to rename the protocol methods to specify the highlighter context, as one can (and often do) implement multiple protocols in one class.

@PhilJay what do you say about this? Basically this means making ChartHighlighter an interface, moving the implementation to DefaultChartHighlighter.

The thing that I mostly don't like about this is that this would mean a major inconsistency API with the Android side.
As an "extension" is something that only iOS allows, and so all current Highlighters would have to subclass DefaultChartHighlighter, while on the iOS side they just implement the base interface and get the functionality by default.
The iOS implementation is like a combination of an interface and an abstract class...

@petester42 I now get why these changes were implemented in the PR, but as you probably know the biggest strength of the framework is being cross-platform :-) We just need to carefully consider this before implementing.

@pmairoldi
Copy link
Collaborator

For sure, from the issues I know a bunch of people use this with objc and of course the API consistency with android is a priority.

Without having actually gone through it I would say that this should probably be rejected based on the things you mentioned. I don't know about protocol extensions in objc but I would assume they don't work.

@danielgindi
Copy link
Collaborator

@petester42 Oh no, I haven't event thought about protocol extensions in ObjC... No they don't work. That's an issue on iOS even before considering Android.

So sadly this PR is going to be rejected... @iheart2code do you want to modify the PR to just make things public, make another PR, or just leave that to us?

danielgindi added a commit to danielgindi/MPAndroidChart that referenced this pull request Jan 10, 2016
danielr pushed a commit to danielr/ios-charts that referenced this pull request Jan 25, 2016
regas99 pushed a commit to regas99/MPAndroidChart that referenced this pull request Apr 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants