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

Convert the Convenience Inits to designated Inits to use them for subclassing. #47

Merged
merged 3 commits into from Jan 18, 2018

Conversation

Narsail
Copy link
Contributor

@Narsail Narsail commented Dec 31, 2017

I tried to subclass the CollectionProvider today and got an error that I can't use the convenience Inits (like used in the documentation) for subclassing.

My proposal is to convert the convenience inits to designated inits. Sadly simply removing the convenience mark resulted in an error:

Designated initializer for cannot ... delegate (with 'self.init')

I know that simply duplicating code isn't really helpful so if someone has a better solution to make those Inits subclassable, you'r welcome :)

@codecov-io
Copy link

codecov-io commented Dec 31, 2017

Codecov Report

Merging #47 into master will decrease coverage by 0.98%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #47      +/-   ##
==========================================
- Coverage   57.17%   56.18%   -0.99%     
==========================================
  Files          32       32              
  Lines        1018     1018              
==========================================
- Hits          582      572      -10     
- Misses        436      446      +10
Impacted Files Coverage Δ
Sources/Provider/CollectionProvider.swift 75.38% <100%> (-15.39%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2254438...5694904. Read the comment docs.

@jindulys
Copy link
Collaborator

You don't need to change CollectionProvider's convenient initializers to a designated one.

What you can do is provide a subclass that override CollectionProvider's designated one and then you can get CollectionProvider's convenient initializers for free.

class MyProvider<Data, View: UIView>: CollectionProvider<Data, View> {
    override init(identifier: String? = nil,
                dataProvider: DataProvider,
                viewProvider: ViewProvider,
                layout: Layout = FlowLayout<Data>(),
                sizeProvider: @escaping SizeProvider = defaultSizeProvider,
                presenter: Presenter? = nil,
                willReloadHandler: (() -> Void)? = nil,
                didReloadHandler: (() -> Void)? = nil,
                tapHandler: TapHandler? = nil) {
        super.init(identifier: identifier,
                   dataProvider: dataProvider,
                   viewProvider: viewProvider,
                   layout: layout,
                   sizeProvider: sizeProvider,
                   presenter:presenter,
                   willReloadHandler: willReloadHandler,
                   didReloadHandler: didReloadHandler,
                   tapHandler: tapHandler)
    }
}

Then you can use MyProvider in your view controller.

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let provider = MyProvider(
            data: testData,
            viewUpdater: { (view: ContentView, data: OffsetData, at: Int) in
                view.populate(data: data)
            }
        )
        provider.layout = OffsetLayout()
        provider.sizeProvider = OffsetDataSizeProvider
        collectionView.provider = provider
    }

@Narsail
Copy link
Contributor Author

Narsail commented Dec 31, 2017

I don’t want to get them for free. I want to use the convenience inits within my own simple init function. But you can’t call a convenience init within a designated custom init of the subclass.

class CustomProvider: CollectionProvider<Data, View> {
    init() {
        super.init(
            data: [],
            viewUpdater: ...
        )
    }
}

That is not possible. I basically want to reproduce the Reusable Provider of the documentation which is not working.

@lkzhao
Copy link
Collaborator

lkzhao commented Jan 4, 2018

I think this kinda make sense. I also felt the need to call one of these convenient initializer in one of my subclass. @jindulys should we consider merging this in?

willReloadHandler: (() -> Void)? = nil,
didReloadHandler: (() -> Void)? = nil,
tapHandler: TapHandler? = nil) {

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra blank line

willReloadHandler: (() -> Void)? = nil,
didReloadHandler: (() -> Void)? = nil,
tapHandler: TapHandler? = nil) {

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

Copy link
Collaborator

@jindulys jindulys left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been merged I think

@Narsail
Copy link
Contributor Author

Narsail commented Jan 8, 2018

Sry i was on holiday and wasn‘t able to correct the code. Will happen in the next days.

Sent with GitHawk

@lkzhao lkzhao merged commit 935332a into SoySauceLab:master Jan 18, 2018
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.

None yet

4 participants