Commit e455c9bf authored by Kateryna Kostiuk's avatar Kateryna Kostiuk Committed by Andreas Traczyk

UI: refactor link device page

Change-Id: Iec62254d4e0645eab2eb7dc978b8fc0214a85ccc
Reviewed-by: Andreas Traczyk's avatarAndreas Traczyk <andreas.traczyk@savoirfairelinux.com>
parent 0d5aeda3
......@@ -136,7 +136,7 @@ enum L10n {
static let networkError = L10n.tr("Localizable", "linkDevice.networkError")
/// The password you entered does not unlock this account
static let passwordError = L10n.tr("Localizable", "linkDevice.passwordError")
/// Link new device
/// Link a new device
static let title = L10n.tr("Localizable", "linkDevice.title")
}
......
......@@ -25,42 +25,14 @@ import PKHUD
class LinkNewDeviceViewController: UIViewController, StoryboardBased, ViewModelBased {
@IBOutlet weak var titleLable: UILabel!
@IBOutlet weak var passwordField: UITextField!
@IBOutlet weak var okButton: UIButton!
@IBOutlet weak var cancelButton: UIButton!
@IBOutlet weak var pinLabel: UILabel!
@IBOutlet weak var explanationMessage: UILabel!
@IBOutlet weak var errorMessage: UILabel!
@IBOutlet weak var background: UIImageView!
@IBOutlet weak var containerView: UIView!
var viewModel: LinkNewDeviceViewModel!
let disposeBag = DisposeBag()
override func viewDidLoad() {
self.background.image = self.view.convertViewToImage()
self.applyL10n()
// initial state
self.viewModel.isInitialState
.bind(to: self.titleLable.rx.isHidden)
.disposed(by: self.disposeBag)
self.viewModel.isInitialState.bind(to: self.passwordField.rx.isHidden)
.disposed(by: self.disposeBag)
self.viewModel.isInitialState.bind(to: self.cancelButton.rx.isHidden)
.disposed(by: self.disposeBag)
// error state
self.viewModel.isErrorState.bind(to: self.errorMessage.rx.isVisible)
.disposed(by: self.disposeBag)
// success state
self.viewModel.isSuccessState
.bind(to: self.explanationMessage.rx.isVisible)
.disposed(by: self.disposeBag)
self.viewModel.isSuccessState
.bind(to: self.pinLabel.rx.isVisible)
.disposed(by: self.disposeBag)
self.view.backgroundColor = UIColor.white.withAlphaComponent(0.0)
super.viewDidLoad()
self.showInitialAlert()
self.viewModel.observableState
.observeOn(MainScheduler.instance)
......@@ -69,43 +41,66 @@ class LinkNewDeviceViewController: UIViewController, StoryboardBased, ViewModelB
case .generatingPin:
self?.showProgress()
case .success(let pin):
self?.pinLabel.text = pin
self?.hideHud()
self?.showSuccessAlert(pin: pin)
case .error(let pinError):
self?.errorMessage.text = pinError.description
self?.showErrorAlert(error: pinError.description)
self?.hideHud()
default:
break
}
}).disposed(by: self.disposeBag)
cancelButton.rx.tap.subscribe(onNext: { [unowned self] in
self.dismiss(animated: true, completion: nil)
}).disposed(by: disposeBag)
okButton.rx.tap.subscribe(onNext: { [unowned self] in
if !self.passwordField.isHidden {
self.viewModel.linkDevice(with: self.passwordField.text)
self.passwordField.text = ""
} else if !self.errorMessage.isHidden {
self.viewModel.refresh()
} else {
self.dismiss(animated: true, completion: nil)
}
}).disposed(by: disposeBag)
super.viewDidLoad()
}
private func showProgress() {
HUD.show(.labeledProgress(title: L10n.Linkdevice.hudMessage, subtitle: nil), onView: self.containerView)
HUD.show(.labeledProgress(title: L10n.Linkdevice.hudMessage, subtitle: nil))
}
private func hideHud() {
HUD.hide(animated: true)
HUD.hide(animated: false)
}
private func applyL10n() {
self.titleLable.text = self.viewModel.linkDeviceTitleTitle
self.explanationMessage.text = self.viewModel.explanationMessage
func showSuccessAlert(pin: String) {
let alert = UIAlertController(title: pin,
message: self.viewModel.explanationMessage,
preferredStyle: .alert)
let action = UIAlertAction(title: L10n.Global.ok, style: .default) { _ in
self.dismiss(animated: true, completion: nil)
}
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
func showInitialAlert() {
let alert = UIAlertController(title: self.viewModel.linkDeviceTitleTitle,
message: nil,
preferredStyle: .alert)
let actionCancel = UIAlertAction(title: L10n.Actions.cancelAction, style: .cancel) { _ in
self.dismiss(animated: true, completion: nil)
}
let actionLink = UIAlertAction(title: L10n.Global.ok, style: .default) { _ in
if let textFields = alert.textFields {
self.viewModel.linkDevice(with: textFields[0].text)
}
alert.dismiss(animated: false, completion: nil)
}
alert.addAction(actionCancel)
alert.addAction(actionLink)
alert.addTextField { (textField) in
textField.isSecureTextEntry = true
textField.placeholder = L10n.Linktoaccount.passwordLabel
}
self.present(alert, animated: true, completion: nil)
}
func showErrorAlert(error: String) {
let alert = UIAlertController(title: Error,
message: error,
preferredStyle: .alert)
let action = UIAlertAction(title: L10n.Global.ok, style: .cancel) { _ in
self.dismiss(animated: true, completion: nil)
}
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
}
......@@ -84,30 +84,6 @@ class LinkNewDeviceViewModel: ViewModel, Stateable {
return self.generatingState.asObservable()
}()
lazy var isInitialState: Observable<Bool> = {
return self.observableState.map { state in
return !state.isStateOfType(type: "INITIAL")
}
}().share()
lazy var isSuccessState: Observable<Bool> = {
return self.observableState.map { state in
return !state.isStateOfType(type: "SUCCESS")
}
}().share()
lazy var isErrorState: Observable<Bool> = {
return self.observableState.map { state in
return !state.isStateOfType(type: "ERROR")
}
}().share()
lazy var isGeneratedPinState: Observable<Bool> = {
return self.observableState.map { state in
return !state.isStateOfType(type: "GENERATING_PIN")
}
}().share()
let accountService: AccountsService
let disposeBag = DisposeBag()
......
......@@ -25,6 +25,7 @@ import RxSwift
class LinkNewDeviceCell: UITableViewCell, NibReusable {
@IBOutlet weak var addDeviceButton: UIButton!
@IBOutlet weak var addDeviceTitle: UIButton!
var disposeBag = DisposeBag()
override func prepareForReuse() {
......
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="13196" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13174"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
......@@ -19,30 +19,31 @@
<autoresizingMask key="autoresizingMask"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="contactAdd" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="qQU-vB-ZuD" customClass="ButtonTransparentBackground" customModule="Ring" customModuleProvider="target">
<rect key="frame" x="65.5" y="4.5" width="35" height="35"/>
<rect key="frame" x="73.5" y="4.5" width="35" height="35"/>
<constraints>
<constraint firstAttribute="width" constant="35" id="XIg-PF-p2r"/>
<constraint firstAttribute="height" constant="35" id="rZ3-3g-1AV"/>
</constraints>
<color key="tintColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</button>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Link Another Device" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Aw5-4m-yjX">
<rect key="frame" x="110.5" y="4.5" width="154" height="35"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="5Gy-EN-BpG">
<rect key="frame" x="118.5" y="4.5" width="138" height="35"/>
<state key="normal" title="Link Another Device">
<color key="titleColor" white="0.0" alpha="1" colorSpace="calibratedWhite"/>
</state>
</button>
</subviews>
<constraints>
<constraint firstItem="Aw5-4m-yjX" firstAttribute="top" secondItem="qQU-vB-ZuD" secondAttribute="top" id="5nH-9W-o0J"/>
<constraint firstItem="Aw5-4m-yjX" firstAttribute="centerY" secondItem="fhq-ul-0n8" secondAttribute="centerY" id="AaA-ZQ-i3N"/>
<constraint firstItem="Aw5-4m-yjX" firstAttribute="centerX" secondItem="fhq-ul-0n8" secondAttribute="centerX" id="Q1G-cd-PcV"/>
<constraint firstItem="Aw5-4m-yjX" firstAttribute="leading" secondItem="qQU-vB-ZuD" secondAttribute="trailing" constant="10" id="Qnp-Hb-9Fq"/>
<constraint firstItem="Aw5-4m-yjX" firstAttribute="bottom" secondItem="qQU-vB-ZuD" secondAttribute="bottom" id="bzk-WA-kKO"/>
<constraint firstItem="5Gy-EN-BpG" firstAttribute="centerY" secondItem="fhq-ul-0n8" secondAttribute="centerY" id="MFh-yt-NY1"/>
<constraint firstItem="5Gy-EN-BpG" firstAttribute="top" secondItem="qQU-vB-ZuD" secondAttribute="top" id="XnJ-Nr-eWE"/>
<constraint firstItem="5Gy-EN-BpG" firstAttribute="centerX" secondItem="fhq-ul-0n8" secondAttribute="centerX" id="dXj-UN-Lws"/>
<constraint firstItem="5Gy-EN-BpG" firstAttribute="bottom" secondItem="qQU-vB-ZuD" secondAttribute="bottom" id="e91-bu-CRx"/>
<constraint firstItem="5Gy-EN-BpG" firstAttribute="leading" secondItem="qQU-vB-ZuD" secondAttribute="trailing" constant="10" id="qXb-0H-C6u"/>
</constraints>
</tableViewCellContentView>
<connections>
<outlet property="addDeviceButton" destination="qQU-vB-ZuD" id="63f-uV-UkV"/>
<outlet property="addDeviceTitle" destination="5Gy-EN-BpG" id="Rzf-H1-FW1"/>
</connections>
<point key="canvasLocation" x="38.5" y="124"/>
</tableViewCell>
......
......@@ -91,6 +91,9 @@ class MeViewController: EditProfileViewController, StoryboardBased, ViewModelBas
cell.addDeviceButton.rx.tap.subscribe(onNext: { [unowned self] in
self.viewModel.linkDevice()
}).disposed(by: cell.disposeBag)
cell.addDeviceTitle.rx.tap.subscribe(onNext: { [unowned self] in
self.viewModel.linkDevice()
}).disposed(by: cell.disposeBag)
cell.selectionStyle = .none
return cell
}
......
......@@ -72,6 +72,6 @@ class MeCoordinator: Coordinator, StateableResponsive {
private func showLinkDeviceWindow() {
let linkDeviceVC = LinkNewDeviceViewController.instantiate(with: self.injectionBag)
self.present(viewController: linkDeviceVC, withStyle: .appear, withAnimation: false)
self.present(viewController: linkDeviceVC, withStyle: .popup, withAnimation: false)
}
}
......@@ -107,7 +107,7 @@
"accountPage.devicesListHeader" = "Devices";
//Link New Device
"linkDevice.title" = "Link new device";
"linkDevice.title" = "Link a new device";
"linkDevice.passwordError" = "The password you entered does not unlock this account";
"linkDevice.networkError" = "A network error occured during the export";
"linkDevice.defaultError" = "An error occured during the export";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment