Swiftpack.co - JD-man/JD as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by JD-man.
JD-man/JD 1.0.2
JD SwiftUI ViewModel Library / Research
โญ๏ธ 2
๐Ÿ•“ 3 weeks ago
iOS
.package(url: "https://github.com/JD-man/JD.git", from: "1.0.2")

JDA

์กฐ๋™์˜ SwiftUI ๊ตฌ์กฐ ์—ฐ๊ตฌ ๋Œ€์†Œ๋™


Template ์„ค์น˜

  • ๋‹ค์šด๋กœ๋“œ ํ›„ Toolings ๋””๋ ‰ํ† ๋ฆฌ์˜ install-JDA ์‹คํ–‰
  • sh install-JDA.sh

ViewModel

ViewModelType

  • ๋ทฐ๋ชจ๋ธ ํด๋ž˜์Šค์— ViewModelType ํ”„๋กœํ† ์ฝœ ์ฑ„ํƒ
import JDA

final class ContentViewModel: ViewModelType { }

Property

  • ๊ธฐ๋ณธ ํ”„๋กœํผํ‹ฐ 3ํ˜•์ œ ์ดˆ๊ธฐํ™”
  @Published var state = State()
  var action = ActionSubject<Action>()
  var cancelable = Set<AnyCancellable>()

Action

  • View์— Binding๋˜๋Š” Action (enum)
enum Action {
  case buttonDidTap
  case validationText(String)
}

effect()

  • Action์— ์˜ํ•œ Networking๊ณผ ๊ฐ™์€ effect๊ฐ€ ์ผ์–ด๋‚จ
  • Publisher<Effect, Never>๋ฅผ ๋ฐ˜ํ™˜
func effect(action: Action) -> any Publisher<Effect, Never> {
  switch action {
  case .buttonDidTap:
    guard let id = Int(state.inputText) else { 
      return Empty().eraseToAnyPublisher()
    }
    return APIService.shared.singleUser(id: id)
      .map { _ in .reqres }
      .catch { _ in return Just<Effect>(.reqresError) }
    case .validationText(let text):
      return Just(.validation(text))
  }
}

Effect

  • effect()์˜ ๊ฒฐ๊ณผ๋กœ ์‹คํ–‰๋˜๋Š” Effect (enum)
enum Effect {
  case reqres
  case reqresError
  case validation(String)
}

reduce()

  • Effect์˜ ์‹คํ–‰์œผ๋กœ State ๋ณ€๊ฒฝ
func reduce(effect: Effect, state: State) -> State {
  var newState = state
  switch effect {
  case .reqres:
    newState.response = "reqres"
  case .reqresError:
    newState.response = "Error Occur"
  case .validation(let text):
    newState.inputText = text
    newState.validText = text.uppercased()
  }
  return newState
}

State

  • View์˜ ์ƒํƒœ
struct State {
  var response: String = "response"
  var inputText: String = ""
  var validText: String = "validtext"
}

View

ViewType Protocol

  • View๋Š” ViewType ํ”„๋กœํ† ์ฝœ ์ฑ„ํƒ
struct ContentView: ViewType { }

ViewModel ์ •์˜

typealias ViewModel = ContentViewModel
@StateObject var viewModel: ContentViewModel

Action Binding

  • Button ๋“ฑ์˜ Action
Button("Request") {
  viewModel.action.send(.buttonDidTap)
}
  • TextField ๋“ฑ์˜ Binding
TextField("Validation Text", text: viewModel.value({
  $0.inputText
}, action: {
  ContentViewModel.Action.validationText($0)
}))

State Usage

  • ํ•„์š”ํ•œ๊ณณ์— ViewModel State ์‚ฌ์šฉ
Text(viewModel.state.response)        
Text(viewModel.state.validText)

GitHub

link
Stars: 2
Last commit: 3 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

v1.0.2
3 weeks ago
  • ๋ถˆํ•„์š”ํ•œ ์ฝ”๋“œ ์‚ญ์ œ
  • ํ…œํ”Œ๋ฆฟ ์ œ์ž‘
  • ๊ฐœ๋ฐœ ๋ณ‘ํ–‰ํ•  ์ƒ˜ํ”Œ์•ฑ ์ œ์ž‘

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics