Skip to content

wayne90040/Moon

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Moon

Swift Macro for Elegant ViewModel & Action Management

Installation

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/wayne90040/Moon")
]

Features

@MoAction

Automatically generates computed properties for extracting enum associated values.

Example

@MoAction
enum Action {
    case test1
    case test2(String)
    case test3(gg: String)
    case test4(String, Int)
    case test5(g1: String, g2: Int)
    case test6(String, g2: Int)
}

Generated code:

var test1: Void? {
    if case .test1 = self { return () }
    return nil
}

var test2: String? {
    if case .test2(let p1) = self { return p1 }
    return nil
}

var test5: (g1: String, g2: Int)? {
    if case .test5(let g1, let g2) = self { return (g1, g2) }
    return nil
}

@MoViewModel

Automatically injects standard ViewModel members (action and cancellables).

  1. Finds the first enum marked with @MoAction

  2. Generates:

    • let action = PassthroughSubject<Action, Never>()
    • var cancellables = Set<AnyCancellable>()

Example

@MoViewModel
class MyViewModel {
    @MoAction
    enum Action {
        case loadData
        case showError(String)
    }
}

Generated code:

let action = PassthroughSubject<Action, Never>()
var cancellables = Set<AnyCancellable>()

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages