Commit 90a90d80 authored by Kateryna Kostiuk's avatar Kateryna Kostiuk

account: enable notifications

Enable notifications by default when link device to an existing
account.

Change-Id: Iec620f9571e6c652a9963f1f7b09fc749f917101
parent b0bfc16b
......@@ -98,6 +98,21 @@ enum AccountCreationState {
return ""
}
}
static func ==(lhs: AccountCreationState, rhs: AccountCreationState) -> Bool {
switch (lhs, rhs) {
case (.unknown, .unknown):
return true
case (.started, .started):
return true
case (.success, .success):
return true
case ( .error(_), .error(_)):
return true
default:
return false
}
}
}
enum AccountCreationError: Error {
......@@ -293,10 +308,9 @@ class CreateAccountViewModel: Stateable, ViewModel {
}
func enablePushNotifications(enable: Bool) {
if enable {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: NotificationName.enablePushNotifications.rawValue), object: nil)
if !enable {
return
}
NotificationCenter.default.post(name: NSNotification.Name(rawValue: NotificationName.disablePushNotifications.rawValue), object: nil)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: NotificationName.enablePushNotifications.rawValue), object: nil)
}
}
......@@ -140,13 +140,27 @@
<constraint firstItem="3CA-fY-2S2" firstAttribute="leading" secondItem="BLV-X1-oI7" secondAttribute="leading" id="w87-bV-caL"/>
</constraints>
</view>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Enable notifications" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="xYz-Rm-Qsp" userLabel="Enable Notifications Label">
<rect key="frame" x="114" y="339" width="156" height="21"/>
<fontDescription key="fontDescription" type="system" weight="thin" pointSize="18"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="0.5" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" contentHorizontalAlignment="center" contentVerticalAlignment="center" on="YES" translatesAutoresizingMaskIntoConstraints="NO" id="1UV-Vn-dMF" userLabel="Notifications Switch">
<rect key="frame" x="565" y="334" width="51" height="31"/>
<color key="onTintColor" red="0.1215686275" green="0.28627450980000002" blue="0.4431372549" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</switch>
</subviews>
<constraints>
<constraint firstAttribute="bottom" secondItem="HlM-9Z-7gA" secondAttribute="bottom" id="69B-Pe-mL7"/>
<constraint firstItem="HlM-9Z-7gA" firstAttribute="centerX" secondItem="2ZO-zp-5uC" secondAttribute="centerX" id="A0t-uV-C5f"/>
<constraint firstItem="HlM-9Z-7gA" firstAttribute="top" secondItem="2ZO-zp-5uC" secondAttribute="top" constant="18" id="Dig-j9-XSJ"/>
<constraint firstItem="1UV-Vn-dMF" firstAttribute="top" secondItem="BLV-X1-oI7" secondAttribute="bottom" constant="30" id="Ew8-6y-ogo"/>
<constraint firstAttribute="trailing" secondItem="HlM-9Z-7gA" secondAttribute="trailing" constant="-20" id="LZa-xU-Eh2"/>
<constraint firstItem="HlM-9Z-7gA" firstAttribute="leading" secondItem="2ZO-zp-5uC" secondAttribute="leading" constant="-20" id="NvH-W1-SN5"/>
<constraint firstItem="xYz-Rm-Qsp" firstAttribute="leading" secondItem="FyJ-Ke-ba3" secondAttribute="leading" id="aRq-7u-k2Z"/>
<constraint firstItem="xYz-Rm-Qsp" firstAttribute="centerY" secondItem="1UV-Vn-dMF" secondAttribute="centerY" id="bCp-Qm-SeS"/>
<constraint firstItem="1UV-Vn-dMF" firstAttribute="trailing" secondItem="FyJ-Ke-ba3" secondAttribute="trailing" id="yaA-K4-rfo"/>
</constraints>
</scrollView>
<visualEffectView opaque="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="GxF-1Y-H7V" userLabel="Background Navigation Bar">
......@@ -176,8 +190,10 @@
<connections>
<outlet property="backgroundNavigationBarHeightConstraint" destination="tVI-wi-4Lx" id="OyH-sV-V86"/>
<outlet property="containerViewBottomConstraint" destination="69B-Pe-mL7" id="6gO-aI-ofm"/>
<outlet property="enableNotificationsLabel" destination="xYz-Rm-Qsp" id="4N5-pW-3J0"/>
<outlet property="linkButton" destination="BLV-X1-oI7" id="6kA-dj-aEA"/>
<outlet property="linkDeviceTitle" destination="NpV-yY-PPY" id="49f-oC-MTs"/>
<outlet property="notificationsSwitch" destination="1UV-Vn-dMF" id="K0l-sc-v8w"/>
<outlet property="passwordLabel" destination="9AD-b5-bun" id="Neh-tt-Ui8"/>
<outlet property="passwordTextField" destination="FyJ-Ke-ba3" id="tGg-8m-QWZ"/>
<outlet property="pinInfoButton" destination="rtk-w4-zfQ" id="eo1-z8-7RN"/>
......
......@@ -39,6 +39,8 @@ class LinkDeviceViewController: UIViewController, StoryboardBased, ViewModelBase
@IBOutlet weak var pinLabel: UILabel!
@IBOutlet weak var passwordLabel: UILabel!
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var notificationsSwitch: UISwitch!
@IBOutlet weak var enableNotificationsLabel: UILabel!
// MARK: members
private let disposeBag = DisposeBag()
var viewModel: LinkDeviceViewModel!
......@@ -101,6 +103,7 @@ class LinkDeviceViewController: UIViewController, StoryboardBased, ViewModelBase
// bind view to view model
self.pinTextField.rx.text.orEmpty.bind(to: self.viewModel.pin).disposed(by: self.disposeBag)
self.passwordTextField.rx.text.orEmpty.bind(to: self.viewModel.password).disposed(by: self.disposeBag)
self.notificationsSwitch.rx.isOn.bind(to: self.viewModel.notificationSwitch).disposed(by: self.disposeBag)
// handle keyboard
self.adaptToKeyboardState(for: self.scrollView, with: self.disposeBag)
......@@ -157,6 +160,7 @@ class LinkDeviceViewController: UIViewController, StoryboardBased, ViewModelBase
self.pinTextField.placeholder = L10n.LinkToAccount.pinPlaceholder
self.passwordTextField.placeholder = L10n.LinkToAccount.passwordPlaceholder
self.linkDeviceTitle.text = L10n.LinkToAccount.linkButtonTitle
self.enableNotificationsLabel.text = self.viewModel.enableNotificationsTitle
}
private func showCreationHUD() {
......
......@@ -30,6 +30,7 @@ class LinkDeviceViewModel: Stateable, ViewModel {
}()
private let accountService: AccountsService
private let accountCreationState = Variable<AccountCreationState>(.unknown)
let enableNotificationsTitle = L10n.CreateAccount.enableNotifications
lazy var createState: Observable<AccountCreationState> = {
return self.accountCreationState.asObservable()
}()
......@@ -42,18 +43,25 @@ class LinkDeviceViewModel: Stateable, ViewModel {
let pin = Variable<String>("")
let password = Variable<String>("")
let notificationSwitch = Variable<Bool>(true)
let disposeBag = DisposeBag()
required init (with injectionBag: InjectionBag) {
self.accountService = injectionBag.accountService
let accountCreated = createState.filter { newState in
return newState == .success
}
//Account creation state observer
self.accountService
.sharedResponseStream
.takeUntil(accountCreated)
.subscribe(onNext: { [unowned self] event in
if event.getEventInput(ServiceEventInput.registrationState) == Registered {
self.accountCreationState.value = .success
Observable<Int>.timer(Durations.alertFlashDuration.value, period: nil, scheduler: MainScheduler.instance).subscribe(onNext: { [unowned self] (_) in
self.enablePushNotifications(enable: self.notificationSwitch.value)
self.stateSubject.onNext(WalkthroughState.deviceLinked)
}).disposed(by: self.disposeBag)
} else if event.getEventInput(ServiceEventInput.registrationState) == ErrorGeneric {
......@@ -69,6 +77,13 @@ class LinkDeviceViewModel: Stateable, ViewModel {
func linkDevice () {
self.accountCreationState.value = .started
self.accountService.linkToRingAccount(withPin: self.pin.value,
password: self.password.value)
password: self.password.value,
enable: self.notificationSwitch.value)
}
func enablePushNotifications(enable: Bool) {
if !enable {
return
}
NotificationCenter.default.post(name: NSNotification.Name(rawValue: NotificationName.enablePushNotifications.rawValue), object: nil)
}
}
......@@ -242,11 +242,12 @@ class AccountsService: AccountAdapterDelegate {
setAccountDetails(forAccountId: accountId, withDetails: details)
}
func linkToRingAccount(withPin pin: String, password: String) {
func linkToRingAccount(withPin pin: String, password: String, enable: Bool) {
do {
var ringDetails = try self.getRingInitialAccountDetails()
ringDetails.updateValue(password, forKey: ConfigKey.archivePassword.rawValue)
ringDetails.updateValue(pin, forKey: ConfigKey.archivePIN.rawValue)
ringDetails.updateValue(enable.toString(), forKey: ConfigKey.proxyEnabled.rawValue)
let accountId = self.accountAdapter.addAccount(ringDetails)
guard accountId != nil else {
throw AddAccountError.unknownError
......
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