Created
February 6, 2020 06:42
-
-
Save M-Miyazako/810facacfc436e50da265a7116e5b322 to your computer and use it in GitHub Desktop.
# ダイアログ 要RxSwift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RxSwift | |
import UIKit | |
struct HogeDialog { | |
// MARK: - Completables | |
/// OKをタップしたイベントのストリーム | |
var ok: Observable<String> { | |
return okSubject.asObservable() | |
} | |
// MARK: - Private fields | |
/// アラート | |
private let alert: UIAlertController | |
/// OKを流すためのSubject | |
private let okSubject = PublishSubject<String>() | |
// MARK: - Constructors | |
/// MicrochipInputDialogのインスタンスを生成する | |
/// | |
/// - Parameter message: 表示するメッセージ | |
init(message: HogeDialogMessage) { | |
alert = UIAlertController(title: "title", message: message.rawValue, preferredStyle: .alert) | |
setupAlert() | |
} | |
// MARK: - Setting methods | |
/// アラートの設定を行う | |
private func setupAlert() { | |
addCancelAction() | |
addOkAction() | |
addTextField() | |
} | |
/// キャンセルタップ時のアクションを追加する | |
private func addCancelAction() { | |
let action = UIAlertAction(title: "Cancel", style: .cancel) | |
alert.addAction(action) | |
} | |
/// OKタップ時のアクションを追加する | |
private func addOkAction() { | |
let action = UIAlertAction(title: "OK", style: .default, handler: okTapped) | |
alert.addAction(action) | |
} | |
/// OKがタップされたらOKイベントを流す | |
private func okTapped(action: UIAlertAction) { | |
okSubject.onNext(alert.textFields?.first?.text ?? "") | |
okSubject.onCompleted() | |
} | |
/// テキストフィールドを追加する | |
private func addTextField() { | |
alert.addTextField() | |
} | |
// MARK: - Service methods | |
/// 確認ダイアログを表示する | |
/// | |
/// - Parameter vc: 表示元のViewController | |
func show(on vc: UIViewController) { | |
vc.present(alert, animated: true, completion: nil) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum HogeDialogMessage: String { | |
case hoge = "hoge" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment