2018-10-11 07:13:19 +00:00
|
|
|
//
|
|
|
|
// AccountViewController.swift
|
|
|
|
// Passepartout-iOS
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 6/12/18.
|
2020-01-11 08:27:22 +00:00
|
|
|
// Copyright (c) 2020 Davide De Rosa. All rights reserved.
|
2018-10-11 07:13:19 +00:00
|
|
|
//
|
2018-11-03 21:33:30 +00:00
|
|
|
// https://github.com/passepartoutvpn
|
2018-10-11 07:13:19 +00:00
|
|
|
//
|
|
|
|
// This file is part of Passepartout.
|
|
|
|
//
|
|
|
|
// Passepartout is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Passepartout is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2019-05-26 06:56:39 +00:00
|
|
|
import PassepartoutCore
|
2019-10-11 08:56:23 +00:00
|
|
|
import Convenience
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
protocol AccountViewControllerDelegate: class {
|
|
|
|
func accountController(_: AccountViewController, didEnterCredentials credentials: Credentials)
|
|
|
|
|
|
|
|
func accountControllerDidComplete(_: AccountViewController)
|
|
|
|
}
|
|
|
|
|
2019-10-11 08:56:23 +00:00
|
|
|
class AccountViewController: UIViewController, StrongTableHost {
|
2018-10-11 07:13:19 +00:00
|
|
|
@IBOutlet private weak var tableView: UITableView?
|
|
|
|
|
|
|
|
private weak var cellUsername: FieldTableViewCell?
|
|
|
|
|
|
|
|
private weak var cellPassword: FieldTableViewCell?
|
|
|
|
|
|
|
|
var currentCredentials: Credentials?
|
|
|
|
|
|
|
|
var usernamePlaceholder: String?
|
|
|
|
|
|
|
|
var infrastructureName: Infrastructure.Name? {
|
|
|
|
didSet {
|
2019-03-24 23:03:24 +00:00
|
|
|
reloadModel()
|
|
|
|
tableView?.reloadData()
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var credentials: Credentials {
|
|
|
|
let username = cellUsername?.field.text ?? ""
|
|
|
|
let password = cellPassword?.field.text ?? ""
|
|
|
|
return Credentials(username, password).trimmed()
|
|
|
|
}
|
2019-03-03 13:11:12 +00:00
|
|
|
|
2019-04-05 14:17:34 +00:00
|
|
|
private var guidanceString: String? {
|
2019-11-28 09:58:19 +00:00
|
|
|
guard let name = infrastructureName, let metadata = InfrastructureFactory.shared.metadata(forName: name) else {
|
2019-04-05 14:17:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-02-29 16:56:24 +00:00
|
|
|
return metadata.guidanceString
|
2019-04-05 14:17:34 +00:00
|
|
|
}
|
|
|
|
|
2019-03-24 23:03:24 +00:00
|
|
|
private var guidanceURL: String? {
|
|
|
|
guard let name = infrastructureName else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return AppConstants.URLs.guidances[name]
|
|
|
|
}
|
|
|
|
|
2019-03-03 13:11:12 +00:00
|
|
|
private var referralURL: String? {
|
|
|
|
guard let name = infrastructureName else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return AppConstants.URLs.referrals[name]
|
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
weak var delegate: AccountViewControllerDelegate?
|
|
|
|
|
2019-10-11 08:56:23 +00:00
|
|
|
// MARK: StrongTableHost
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2019-10-11 08:56:23 +00:00
|
|
|
var model: StrongTableModel<SectionType, RowType> = StrongTableModel()
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
func reloadModel() {
|
2019-03-24 23:03:24 +00:00
|
|
|
model.clear()
|
|
|
|
|
|
|
|
model.add(.credentials)
|
2019-10-11 08:56:23 +00:00
|
|
|
model.setHeader(L10n.App.Account.Sections.Credentials.header, forSection: .credentials)
|
|
|
|
model.set([.username, .password], forSection: .credentials)
|
2019-03-24 23:03:24 +00:00
|
|
|
|
2019-09-17 15:22:26 +00:00
|
|
|
if let _ = infrastructureName {
|
2019-04-05 14:17:34 +00:00
|
|
|
if let guidanceString = guidanceString {
|
2019-03-24 23:03:24 +00:00
|
|
|
if let _ = guidanceURL {
|
2019-04-05 14:17:34 +00:00
|
|
|
model.add(.guidance)
|
2019-10-11 08:56:23 +00:00
|
|
|
model.setFooter(guidanceString, forSection: .guidance)
|
|
|
|
model.set([.openGuide], forSection: .guidance)
|
2019-03-24 23:03:24 +00:00
|
|
|
} else {
|
2019-10-11 08:56:23 +00:00
|
|
|
model.setFooter(guidanceString, forSection: .credentials)
|
2019-03-24 23:03:24 +00:00
|
|
|
}
|
2019-10-11 08:56:23 +00:00
|
|
|
model.setHeader("", forSection: .registration)
|
2019-03-24 23:03:24 +00:00
|
|
|
}
|
2019-09-13 20:17:43 +00:00
|
|
|
// if let _ = referralURL {
|
|
|
|
// model.add(.registration)
|
2019-10-11 08:56:23 +00:00
|
|
|
// model.setFooter(L10n.Core.Account.Sections.Registration.footer(name.rawValue), forSection: .registration)
|
|
|
|
// model.set([.signUp], forSection: .registration)
|
2019-09-13 20:17:43 +00:00
|
|
|
// }
|
2019-03-24 23:03:24 +00:00
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: UIViewController
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2019-06-21 21:15:43 +00:00
|
|
|
title = L10n.Core.Account.title
|
2018-10-11 07:13:19 +00:00
|
|
|
cellUsername?.field.text = currentCredentials?.username
|
|
|
|
cellPassword?.field.text = currentCredentials?.password
|
2019-03-24 23:03:24 +00:00
|
|
|
|
|
|
|
reloadModel()
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidAppear(_ animated: Bool) {
|
|
|
|
super.viewDidAppear(animated)
|
|
|
|
|
|
|
|
cellUsername?.field.becomeFirstResponder()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Actions
|
|
|
|
|
|
|
|
private func commit() {
|
|
|
|
let newCredentials = credentials
|
|
|
|
// guard !credentials.isEmpty else {
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
currentCredentials = newCredentials
|
|
|
|
delegate?.accountController(self, didEnterCredentials: newCredentials)
|
|
|
|
}
|
|
|
|
|
2019-04-05 14:17:34 +00:00
|
|
|
private func openGuidanceURL() {
|
|
|
|
guard let urlString = guidanceURL else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
UIApplication.shared.open(URL(string: urlString)!, options: [:], completionHandler: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func openReferralURL() {
|
|
|
|
guard let urlString = referralURL else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
UIApplication.shared.open(URL(string: urlString)!, options: [:], completionHandler: nil)
|
|
|
|
}
|
|
|
|
|
2018-10-11 07:13:19 +00:00
|
|
|
@IBAction private func done() {
|
|
|
|
view.endEditing(true)
|
|
|
|
delegate?.accountControllerDidComplete(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: -
|
|
|
|
|
|
|
|
extension AccountViewController: UITableViewDataSource, UITableViewDelegate, FieldTableViewCellDelegate {
|
|
|
|
enum SectionType: Int {
|
2018-10-22 20:17:21 +00:00
|
|
|
case credentials
|
2019-03-24 23:03:24 +00:00
|
|
|
|
2019-04-05 14:17:34 +00:00
|
|
|
case guidance
|
|
|
|
|
|
|
|
case registration
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum RowType: Int {
|
|
|
|
case username
|
|
|
|
|
|
|
|
case password
|
2019-04-05 14:17:34 +00:00
|
|
|
|
|
|
|
case openGuide
|
|
|
|
|
|
|
|
case signUp
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static let footerButtonTag = 1000
|
|
|
|
|
2019-03-24 23:03:24 +00:00
|
|
|
func numberOfSections(in tableView: UITableView) -> Int {
|
2019-10-11 08:56:23 +00:00
|
|
|
return model.numberOfSections
|
2019-03-24 23:03:24 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 14:17:34 +00:00
|
|
|
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
2019-10-11 08:56:23 +00:00
|
|
|
return model.header(forSection: section)
|
2019-04-05 14:17:34 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 07:13:19 +00:00
|
|
|
func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
|
2019-10-11 08:56:23 +00:00
|
|
|
return model.footer(forSection: section)
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
2019-04-04 17:53:08 +00:00
|
|
|
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
|
|
|
|
return model.headerHeight(for: section)
|
|
|
|
}
|
|
|
|
|
2018-10-11 07:13:19 +00:00
|
|
|
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
2019-10-11 08:56:23 +00:00
|
|
|
return model.numberOfRows(forSection: section)
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
switch model.row(at: indexPath) {
|
|
|
|
case .username:
|
2019-04-05 14:17:34 +00:00
|
|
|
let cell = Cells.field.dequeue(from: tableView, for: indexPath)
|
2018-10-11 07:13:19 +00:00
|
|
|
cellUsername = cell
|
2019-06-21 21:15:43 +00:00
|
|
|
cell.caption = L10n.Core.Account.Cells.Username.caption
|
|
|
|
cell.field.placeholder = usernamePlaceholder ?? L10n.Core.Account.Cells.Username.placeholder
|
2018-10-11 07:13:19 +00:00
|
|
|
cell.field.clearButtonMode = .always
|
|
|
|
cell.field.isSecureTextEntry = false
|
|
|
|
cell.field.text = currentCredentials?.username
|
|
|
|
cell.field.keyboardType = .emailAddress
|
|
|
|
cell.field.returnKeyType = .next
|
2019-03-05 08:45:29 +00:00
|
|
|
cell.field.textContentType = .username
|
2019-04-05 14:17:34 +00:00
|
|
|
cell.captionWidth = 120.0
|
|
|
|
cell.delegate = self
|
|
|
|
return cell
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
case .password:
|
2019-04-05 14:17:34 +00:00
|
|
|
let cell = Cells.field.dequeue(from: tableView, for: indexPath)
|
2018-10-11 07:13:19 +00:00
|
|
|
cellPassword = cell
|
2019-06-21 21:15:43 +00:00
|
|
|
cell.caption = L10n.Core.Account.Cells.Password.caption
|
|
|
|
cell.field.placeholder = L10n.Core.Account.Cells.Password.placeholder
|
2018-10-11 07:13:19 +00:00
|
|
|
cell.field.clearButtonMode = .always
|
|
|
|
cell.field.isSecureTextEntry = true
|
|
|
|
cell.field.text = currentCredentials?.password
|
|
|
|
cell.field.returnKeyType = .done
|
2019-03-05 08:45:29 +00:00
|
|
|
cell.field.textContentType = .password
|
2019-04-05 14:17:34 +00:00
|
|
|
cell.captionWidth = 120.0
|
|
|
|
cell.delegate = self
|
|
|
|
return cell
|
|
|
|
|
|
|
|
case .openGuide:
|
|
|
|
let cell = Cells.setting.dequeue(from: tableView, for: indexPath)
|
2019-06-21 21:15:43 +00:00
|
|
|
cell.leftText = L10n.Core.Account.Cells.OpenGuide.caption
|
2019-10-25 17:30:21 +00:00
|
|
|
cell.applyAction(.current)
|
2019-04-05 14:17:34 +00:00
|
|
|
return cell
|
|
|
|
|
|
|
|
case .signUp:
|
|
|
|
guard let name = infrastructureName else {
|
|
|
|
fatalError("Sign-up shown when not a provider profile")
|
|
|
|
}
|
|
|
|
let cell = Cells.setting.dequeue(from: tableView, for: indexPath)
|
2019-11-23 13:22:34 +00:00
|
|
|
cell.leftText = L10n.Core.Account.Cells.Signup.caption(name)
|
2019-10-25 17:30:21 +00:00
|
|
|
cell.applyAction(.current)
|
2019-04-05 14:17:34 +00:00
|
|
|
return cell
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
switch model.row(at: indexPath) {
|
|
|
|
case .openGuide:
|
|
|
|
openGuidanceURL()
|
|
|
|
|
|
|
|
case .signUp:
|
|
|
|
openReferralURL()
|
|
|
|
|
|
|
|
default:
|
|
|
|
break
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
2019-04-05 14:17:34 +00:00
|
|
|
tableView.deselectRow(at: indexPath, animated: true)
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func fieldCellDidEdit(_: FieldTableViewCell) {
|
|
|
|
commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func fieldCellDidEnter(_ cell: FieldTableViewCell) {
|
|
|
|
switch cell {
|
|
|
|
case cellUsername:
|
|
|
|
cellPassword?.field.becomeFirstResponder()
|
|
|
|
|
|
|
|
case cellPassword:
|
2018-10-22 20:16:59 +00:00
|
|
|
cellPassword?.field.resignFirstResponder()
|
2018-10-11 07:13:19 +00:00
|
|
|
done()
|
|
|
|
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|