如何使用MVVM和RxSwift快速显示/隐藏progressHUD

use*_*372 5 mvvm ios svprogresshud swift rx-swift

我从MVVM开始,以便将逻辑代码从视图中分离出来.但我有点担心在点击发出请求的按钮时将progressHUD相关代码放在哪里.

之前,我曾经这样做过:

//Before 
@IBAction func startRequestTapped() {
   SVProgressHUD.show()
    self.apiClient.requestObservable().subscribe(onError: { (error) in
        SVProgressHUD.hide()                    
    }, onCompleted: { 
        SVProgressHUD.hide()                                        
    })
}
Run Code Online (Sandbox Code Playgroud)

但是当我使用mvvm时,我确实喜欢这样:

//In the viewModel
public var validateButtonDidTap = PublishSubject<Void>()
init() {
    validateButtonDidTap.flatMap { (_)
        return self.apiClient.requestObservable()
    }
}


   // In the viewController
viewDidLoad() {
    let tap = self.validateButton.rx.tap
    tap.bindTo(self.viewModel.validateButtonDidTap)
}
Run Code Online (Sandbox Code Playgroud)

其中,我不知道将ProgressHUD隐藏或显示放在哪里.

XFr*_*ire 9

马克答案是对的,但我会一步一步地指导你.

让我们打算你试着登录.

  1. 复制项目中的ActivityIndi​​cator.swift文件.

  2. viewModel:

    //MARK: - Properties
    
    /// The http client
    private let apiClient: YourApiClient
    
    /// Clousure when button is tapped
    var didTappedButton: () -> Void = {}
    
    /// The user 
    var user: Observable<User>
    
    /// Is signing process in progress
    let signingIn: Observable<Bool> = ActivityIndicator().asObservable()
    
    //MARK: - Initialization
    
    init(client: YourApiClient) {
        self.client = client
    
        self.didTappedButton = { [weak self] in
            self.user = self.apiClient
                            .yourSignInRequest()
                            .trackActivity(self.signingIn)
                            .observeOn(MainScheduler.instance)
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 创建SVProgressHUD的扩展:(我不知道SVProgressHUD库,但它会是这样的.请在需要时修复它)

    extension Reactive where Base: SVProgressHUD {
    
        /// Bindable sink for `show()`, `hide()` methods.
        public static var isAnimating: UIBindingObserver<Base, Bool> {
            return UIBindingObserver(UIElement: self.base) { progressHUD, isVisible in
                if isVisible {
                    progressHUD.show() // or other show methods
                } else {
                    progressHUD.dismiss() // or other hide methods
                }
            }
        }
    
    }
    
    Run Code Online (Sandbox Code Playgroud)
  4. 在你的viewController:

    @IBAction func startRequestTapped() {
        viewModel.didTappedButton()
    }
    
    override func viewDidLoad() {
    
        // ...
    
        viewModel.signingIn
                 .bindTo(SVProgressHUD.rx.isAnimating)
                 .addDisposableTo(disposeBag)
    }
    
    Run Code Online (Sandbox Code Playgroud)


jul*_*adi 9

接受的答案更新为Swift 4,RxSwift 4.0.0和SVProgressHUD 2.2.2:

3-扩展:

extension Reactive where Base: SVProgressHUD {

   public static var isAnimating: Binder<Bool> {
      return Binder(UIApplication.shared) {progressHUD, isVisible in
         if isVisible {
            SVProgressHUD.show()
         } else {
            SVProgressHUD.dismiss()
         }
      }
   }

}
Run Code Online (Sandbox Code Playgroud)

4-控制器:

viewModel.signingIn.asObservable().bind(to: SVProgressHUD.rx.isAnimating).disposed(by: disposeBag)
Run Code Online (Sandbox Code Playgroud)