Skip to content
Snippets Groups Projects
Commit 6586b303 authored by Romain Bertozzi's avatar Romain Bertozzi
Browse files

me screen with new account service

Change-Id: Iac86f2a0bf643511d8a43cb1cd6b64cbe6abdb29
parent 97350685
Branches
No related tags found
No related merge requests found
......@@ -44,11 +44,11 @@ class MeViewController: EditProfileViewController, StoryboardBased, ViewModelBas
}
override func setupUI() {
self.viewModel.userName
self.viewModel.accountUsernameObservable
.bind(to: self.nameLabel.rx.text)
.disposed(by: disposeBag)
self.viewModel.ringId.asObservable()
self.viewModel.accountRingIdObservable
.bind(to: self.ringIdLabel.rx.text)
.disposed(by: disposeBag)
......@@ -97,7 +97,8 @@ class MeViewController: EditProfileViewController, StoryboardBased, ViewModelBas
}
let settingsItemDataSource = RxTableViewSectionedReloadDataSource<SettingsSection>(configureCell: configureCell)
self.viewModel.settings
self.viewModel
.accountSettingsObservable
.bind(to: self.settingsTable.rx.items(dataSource: settingsItemDataSource))
.disposed(by: disposeBag)
......
......@@ -3,6 +3,7 @@
*
* Author: Thibault Wittemberg <thibault.wittemberg@savoirfairelinux.com>
* Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
* Author: Romain Bertozzi <romain.bertozzi@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -19,7 +20,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
import Foundation
import SwiftyBeaver
import RxSwift
import RxDataSources
......@@ -68,92 +69,82 @@ enum SettingsSection: SectionModelType {
}
}
class MeViewModel: ViewModel, Stateable {
final class MeViewModel: ViewModel, Stateable {
// MARK: - Rx Stateable
private let stateSubject = PublishSubject<State>()
lazy var userName: Observable<String?> = {
// return username if exists, is no start name lookup
let accountName = self.accountService.currentAccount?.volatileDetails?.get(withConfigKeyModel: ConfigKeyModel(withKey: ConfigKey.accountRegisteredName))
if accountName != nil && !accountName!.isEmpty {
return Observable.from(optional: accountName)
}
guard let account = self.accountService.currentAccount else {
return Observable.from(optional: accountName)
}
let accountHelper = AccountModelHelper(withAccount: account)
guard let uri = accountHelper.ringId else {
return Observable.from(optional: accountName)
}
let time = DispatchTime.now() + 2
DispatchQueue.main.asyncAfter(deadline: time) {
self.nameService.lookupAddress(withAccount: "", nameserver: "", address: uri)
}
return self.nameService.usernameLookupStatus
.filter({ lookupNameResponse in
return lookupNameResponse.address != nil &&
lookupNameResponse.address == uri && lookupNameResponse.state == .found
})
.map({ lookupNameResponse in
return lookupNameResponse.name
})
lazy var state: Observable<State> = {
return self.stateSubject.asObservable()
}()
lazy var ringId: Observable<String?> = {
return Observable.from(optional: self.accountService.currentAccount?.details?.get(withConfigKeyModel: ConfigKeyModel(withKey: .accountUsername)))
private let disposeBag = DisposeBag()
private let accountService: NewAccountsService
private let nameService: NameService
private let log = SwiftyBeaver.self
private let accountUsername = Variable<String>("")
lazy var accountUsernameObservable: Observable<String> = {
return self.accountUsername.asObservable()
}()
lazy var state: Observable<State> = {
return self.stateSubject.asObservable()
private let accountRingId = Variable<String>("")
lazy var accountRingIdObservable: Observable<String> = {
return self.accountRingId.asObservable()
}()
let disposeBag = DisposeBag()
let accountService: AccountsService
let nameService: NameService
//table section
lazy var settings: Observable<[SettingsSection]> = {
if let account = self.accountService.currentAccount {
let accountHelper = AccountModelHelper(withAccount: account)
let uri = accountHelper.ringId
let devices = Observable.from(optional: account.devices)
let accountDevice: Observable<[DeviceModel]> = self.accountService
.sharedResponseStream
.filter({ (event) in
return event.eventType == ServiceEventType.knownDevicesChanged &&
event.getEventInput(ServiceEventInput.uri) == uri
}).map({ _ in
return account.devices
})
return devices.concat(accountDevice)
.map { devices in
let addNewDevice = SettingsSection.linkNewDevice(header: "", items: [SettingsSection.SectionRow.linkNew])
var rows: [SettingsSection.SectionRow]?
private let accountSettings = Variable<[SettingsSection]>([])
lazy var accountSettingsObservable: Observable<[SettingsSection]> = {
return self.accountSettings.asObservable()
}()
if !devices.isEmpty {
rows = [SettingsSection.SectionRow.device(device: devices[0])]
for i in 1 ..< devices.count {
let device = devices[i]
rows!.append (SettingsSection.SectionRow.device(device: device))
}
}
required init (with injectionBag: InjectionBag) {
self.accountService = injectionBag.newAccountsService
self.nameService = injectionBag.nameService
if rows != nil {
let devicesSection = SettingsSection.linkedDevices(header: L10n.Accountpage.devicesListHeader, items: rows!)
return [devicesSection, addNewDevice]
self.accountService.currentAccount()
.do(onNext: { [weak self] (account) in
let accountUsernameKey = ConfigKeyModel(withKey: ConfigKey.accountUsername)
let ringId = account.details?.get(withConfigKeyModel: accountUsernameKey)
self?.accountRingId.value = ringId ?? "No RingId found"
let addNewDevice = SettingsSection.linkNewDevice(header: "",
items: [SettingsSection.SectionRow.linkNew])
var rows: [SettingsSection.SectionRow]
if !account.devices.isEmpty {
rows = [SettingsSection.SectionRow.device(device: account.devices[0])]
for i in 1 ..< account.devices.count {
let device = account.devices[i]
rows.append (SettingsSection.SectionRow.device(device: device))
}
let devicesSection = SettingsSection.linkedDevices(header: L10n.Accountpage.devicesListHeader,
items: rows)
self?.accountSettings.value = [devicesSection, addNewDevice]
} else {
return [addNewDevice]
self?.accountSettings.value = [addNewDevice]
}
}, onError: { [weak self] (error) in
self?.accountRingId.value = "No RingId found"
self?.log.error("No RingId found - \(error.localizedDescription)")
})
.flatMap { (account) -> PrimitiveSequence<SingleTrait, String> in
let registeredNameKey = ConfigKeyModel(withKey: ConfigKey.accountRegisteredName)
if let registeredName = account.volatileDetails?.get(withConfigKeyModel: registeredNameKey) {
return Single.just(registeredName)
} else {
//TODO: call nameserver single
return Single.just("")
}
}
return Observable.just([SettingsSection]())
}()
required init (with injectionBag: InjectionBag) {
self.accountService = injectionBag.accountService
self.nameService = injectionBag.nameService
.subscribe(onSuccess: { [weak self] (username) in
self?.accountUsername.value = username
}, onError: { [weak self] (error) in
self?.accountUsername.value = "No username found"
self?.log.error("No username found - \(error.localizedDescription)")
})
.disposed(by: self.disposeBag)
}
func linkDevice() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment