All Projects → oney → Mixin

oney / Mixin

Licence: MIT license
React.js like Mixin. More powerful Protocol-Oriented Programming.

Programming Languages

swift
15916 projects
objective c
16641 projects - #2 most used programming language
c
50402 projects - #5 most used programming language
shell
77523 projects
ruby
36898 projects - #4 most used programming language

Labels

Projects that are alternatives of or similar to Mixin

NCop
Composite-aspect oriented framework for .NET
Stars: ✭ 30 (-33.33%)
Mutual labels:  mixins
Cometary
Roslyn extensions, with a touch of meta-programming.
Stars: ✭ 31 (-31.11%)
Mutual labels:  mixins
extjs-reactjs-examples
Code examples for ExtJS to React transition
Stars: ✭ 48 (+6.67%)
Mutual labels:  mixins
Sledgehammer
Smashes the stupid out of the client & server.
Stars: ✭ 13 (-71.11%)
Mutual labels:  mixins
less-mix
LESS-Mix - is a functional, powerful and convenient library LESS-mixins.
Stars: ✭ 22 (-51.11%)
Mutual labels:  mixins
typescript-mix
A tweaked implementation of TypeScript's default applyMixins(...) idea using ES7 decorators
Stars: ✭ 81 (+80%)
Mutual labels:  mixins
tale-pug
Tale Pug is the popular JavaScript Template Engine Pug, formerly Jade, for PHP!
Stars: ✭ 32 (-28.89%)
Mutual labels:  mixins
hagrid
📏 Hagrid is a mixin library for responsive websites and web applications.
Stars: ✭ 30 (-33.33%)
Mutual labels:  mixins
rainbowify
Fabric mod for rainbow and blur backgrounds in minecraft guis
Stars: ✭ 18 (-60%)
Mutual labels:  mixins
ekzo
💫 Functional Sass framework for rapid and painless development
Stars: ✭ 32 (-28.89%)
Mutual labels:  mixins
sass-boilerplate
A collection of common use Sass stylesheets, mixins and functions.
Stars: ✭ 60 (+33.33%)
Mutual labels:  mixins
plazar-js
Modular framework built with enterprise in mind - http://www.plazarjs.com
Stars: ✭ 25 (-44.44%)
Mutual labels:  mixins
loopback-row-count-mixin
A loopback mixin to get total count of a model
Stars: ✭ 13 (-71.11%)
Mutual labels:  mixins
go-traits
A concept package that helps implement mixin behavior using embedded structs and hook interfaces.
Stars: ✭ 21 (-53.33%)
Mutual labels:  mixins
buttono
A flexible Sass mixin for creating BEM-style buttons.
Stars: ✭ 82 (+82.22%)
Mutual labels:  mixins
Grimoire-legacy
General-purpose Mixin loader framework, which allows to properly implement mixins on 1.7.10/1.12.2 versions of Minecraft. For updated version check out https://github.com/Aizistral-Studios/Grimoire
Stars: ✭ 27 (-40%)
Mutual labels:  mixins
chameleon-sdk
Chameleon Software Development Kit
Stars: ✭ 12 (-73.33%)
Mutual labels:  mixins
Orion
Mixin loader for Paper
Stars: ✭ 46 (+2.22%)
Mutual labels:  mixins
orchparty
Write your own orchestration config with a Ruby DSL that allows you to have mixins, imports and variables.
Stars: ✭ 37 (-17.78%)
Mutual labels:  mixins
manila-mixins
A bunch of really cool Sass Mixins
Stars: ✭ 15 (-66.67%)
Mutual labels:  mixins

Mixin 🍹

Version License Platform

Why?

Swift is Protocol-Oriented Programming, and it's more powerful by default implementations of extensions of protocols. You can mixin methods to classes like Ruby's Mixin.

However, iOS as a UI framework, objects like UIViewController have their own life cyle, if you can't listen life cyle methods, extensions as mixin don't really help.

For example, I write a protocol with extension to listen keyboard events

protocol KeyboardMixin {
    var keyboardHeight: CGFloat? { set get }
    func registerKeyboard()
    func deregisterKeyboard()
}

extension KeyboardMixin {
    func registerKeyboard() {
        NotificationCenter.default.addObserver(forName: NSNotification.Name.UIKeyboardWillHide, object: nil, queue: nil) { [weak self] notification in
            self?.keyboardHeight = nil
        }
        NotificationCenter.default.addObserver(forName: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil, queue: nil) { [weak self] notification in
            if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
                self?.keyboardHeight = keyboardSize.height
            }
        }
    }
    func deregisterKeyboard() {
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
    }
}

But I still need to register and deregister by myself

class ViewController: UIViewController, KeyboardMixin {
    var keyboardHeight: CGFloat? {
        didSet { }
    }
    override func viewWillAppear(_ animated: Bool) {
        registerKeyboard()
    }
    override func viewWillDisappear(_ animated: Bool) {
        deregisterKeyboard()
    }
}

The problem is why can't I mixin something to existing methods e.g. UIViewController life cyle?

If you have programmed React.js, you'll find its Mixin mechanism is very useful. So I just copy the idea to iOS. After using this package, you can write a mixin like this.

public protocol KeyboardMixin: ViewControllerMixinable {
    var keyboardHeight: CGFloat? { set get }
}

public extension KeyboardMixin {
    private func registerKeyboard() {
        NotificationCenter.default.addObserver(forName: NSNotification.Name.UIKeyboardWillHide, object: nil, queue: nil) { [weak self] notification in
            self?.keyboardHeight = nil
        }
        NotificationCenter.default.addObserver(forName: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil, queue: nil) { [weak self] notification in
            if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
                self?.keyboardHeight = keyboardSize.height
            }
        }
    }
    private func deregisterKeyboard() {
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
    }
    fileprivate func viewWillAppear(_ animated: Bool) {
        registerKeyboard()
    }
    fileprivate func viewWillDisappear(_ animated: Bool) {
        deregisterKeyboard()
    }
}

And use it like below

class ViewController: UIViewController, KeyboardMixin {
    var keyboardHeight: CGFloat? {
        didSet { }
    }
    override func viewWillAppear(_ animated: Bool) {
        // Don't worry, you can still do things here...
    }
}

It can't be simpler!

How it works

This package uses iOS runtime to swizzle methods, so all override methods and mixins' methods will be called simultaneously.

Support

This package support mixin to

Example

Check out Example ViewController, it shows how amazing to use Mixin

Requirements

Only tested in Swift 4

Installation

Mixin is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Mixin'

Author

Howard Yang, [email protected]

License

Mixin is available under the MIT license. See the LICENSE file for more info.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].