MihaelIsaev / UIKitPlus

🏰 Declarative UIKit with LivePreview for iOS9+ (best alternative to SwiftUI)
MIT License
596 stars 35 forks source link

How to set color with UState? #9

Closed mohok closed 3 years ago

mohok commented 4 years ago

I try to change view's backgorund color by networking state.

enum NetworkConnectivityLevel: Int {
  case none
  case low
  case medium
  case high

  var color: UIColor {
    switch self {
    case .none: return .gray
    case .low: return .red
    case .medium: return .yellow
    case .high: return .mintyGreen
    }
  }
}

ViewController and body in buildView

  @UState var connectiviyLevel: NetworkConnectivityLevel = .medium

...
body {
  connectivtyStatusView.background($connectiviyLevel.map { $0.color } )
  // not working :(

  connectivtyStatusView.background($connectiviyLevel.map {
    if $0 == .none { return UIColor.gray }
    else if $0 == .low { return UIColor.red}
    else if $0 == .medium { return UIColor.yellow }
   else { return UIColor.mintyGreen} }

  // also not working :(
}

There is no effect on background color on the connectivtyStatusView. if I change to basic operator, it is okay.

connectivtyStatusView.background($connectiviyLevel.map { $0 == .high ? .yellow : .gray })
// working!!

connectivtyStatusView.background($connectiviyLevel.map {
  if $0 == .none { return UIColor.gray }
  else { return UIColor.mintyGreen} }
// also working!!

I don't know what is correct way to use UState property wrapper.