2018-10-11 07:13:19 +00:00
|
|
|
//
|
|
|
|
// AccountViewController.swift
|
2021-01-01 16:53:28 +00:00
|
|
|
// Passepartout
|
2018-10-11 07:13:19 +00:00
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 6/12/18.
|
2020-12-27 16:30:25 +00:00
|
|
|
// Copyright (c) 2021 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
|
2021-10-27 16:47:08 +00:00
|
|
|
import ConvenienceUI
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2021-06-26 07:49:48 +00:00
|
|
|
protocol AccountViewControllerDelegate: AnyObject {
|
2018-10-11 07:13:19 +00:00
|
|
|
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?
|
|
|
|
|
2021-10-27 16:47:08 +00:00
|
|
|
var infrastructureName: InfrastructureName? {
|
2018-10-11 07:13:19 +00:00
|
|
|
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
|
|
|
|
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)
|
2021-08-07 11:57:22 +00:00
|
|
|
model.setHeader(L10n.Account.Sections.Credentials.header, forSection: .credentials)
|
2019-10-11 08:56:23 +00:00
|
|
|
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
|
|
|
}
|
2020-05-19 17:26:02 +00:00
|
|
|
// model.setHeader("", forSection: .registration)
|
|
|
|
} else if let _ = guidanceURL {
|
|
|
|
model.add(.guidance)
|
|
|
|
model.set([.openGuide], forSection: .guidance)
|
|
|
|
// 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)
|
2021-08-07 11:57:22 +00:00
|
|
|
// model.setFooter(L10n.Account.Sections.Registration.footer(name.rawValue), forSection: .registration)
|
2019-10-11 08:56:23 +00:00
|
|
|
// 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()
|
|
|
|
|
2021-08-07 11:57:22 +00:00
|
|
|
title = L10n.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() {
|
2021-04-10 21:24:51 +00:00
|
|
|
guard let url = guidanceURL else {
|
2019-04-05 14:17:34 +00:00
|
|
|
return
|
|
|
|
}
|
2021-04-10 21:24:51 +00:00
|
|
|
UIApplication.shared.open(url, options: [:], completionHandler: nil)
|
2019-04-05 14:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func openReferralURL() {
|
2021-04-10 21:24:51 +00:00
|
|
|
guard let url = referralURL else {
|
2019-04-05 14:17:34 +00:00
|
|
|
return
|
|
|
|
}
|
2021-04-10 21:24:51 +00:00
|
|
|
UIApplication.shared.open(url, options: [:], completionHandler: nil)
|
2019-04-05 14:17:34 +00:00
|
|
|
}
|
|
|
|
|
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
|
2021-08-07 11:57:22 +00:00
|
|
|
cell.caption = L10n.Account.Cells.Username.caption
|
|
|
|
cell.field.placeholder = usernamePlaceholder ?? L10n.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
|
2021-08-07 11:57:22 +00:00
|
|
|
cell.caption = L10n.Account.Cells.Password.caption
|
|
|
|
cell.field.placeholder = L10n.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)
|
2021-08-07 11:57:22 +00:00
|
|
|
cell.leftText = L10n.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)
|
2021-08-07 11:57:22 +00:00
|
|
|
cell.leftText = L10n.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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-10 21:24:51 +00:00
|
|
|
|
|
|
|
extension AccountViewController {
|
|
|
|
private var guidanceString: String? {
|
|
|
|
return metadata?.guidanceString
|
|
|
|
}
|
|
|
|
|
|
|
|
private var guidanceURL: URL? {
|
|
|
|
return metadata?.guidanceURL
|
|
|
|
}
|
|
|
|
|
|
|
|
private var referralURL: URL? {
|
|
|
|
return metadata?.referralURL
|
|
|
|
}
|
|
|
|
|
|
|
|
private var metadata: Infrastructure.Metadata? {
|
|
|
|
guard let name = infrastructureName else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return InfrastructureFactory.shared.metadata(forName: name)
|
|
|
|
}
|
|
|
|
}
|