aboutsummaryrefslogtreecommitdiffstats
path: root/Wireguard/ViewControllers/ConnectionsTableViewController.swift
diff options
context:
space:
mode:
authorJeroen Leenarts <jeroen.leenarts@gmail.com>2018-05-24 20:14:01 +0200
committerJeroen Leenarts <jeroen.leenarts@gmail.com>2018-05-24 20:16:17 +0200
commite6c6fd0b347f969749184f7192906eaf5d6a601b (patch)
tree0fc332eb910ecafb802375321d7e6807ee1af300 /Wireguard/ViewControllers/ConnectionsTableViewController.swift
parentFix casing from Wireguard to WireGuard. (diff)
downloadwireguard-apple-e6c6fd0b347f969749184f7192906eaf5d6a601b.tar.xz
wireguard-apple-e6c6fd0b347f969749184f7192906eaf5d6a601b.zip
Basic setup of ConnectionsTableViewController.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'Wireguard/ViewControllers/ConnectionsTableViewController.swift')
-rw-r--r--Wireguard/ViewControllers/ConnectionsTableViewController.swift152
1 files changed, 145 insertions, 7 deletions
diff --git a/Wireguard/ViewControllers/ConnectionsTableViewController.swift b/Wireguard/ViewControllers/ConnectionsTableViewController.swift
index bfd7942..931945d 100644
--- a/Wireguard/ViewControllers/ConnectionsTableViewController.swift
+++ b/Wireguard/ViewControllers/ConnectionsTableViewController.swift
@@ -13,9 +13,9 @@ import BNRCoreDataStack
protocol ConnectionsTableViewControllerDelegate: class {
func addProvider(connectionsTableViewController: ConnectionsTableViewController)
- func settings(connectionsTableViewController: ConnectionsTableViewController)
- func connect(profile: Profile)
- func delete(profile: Profile)
+ func connect(profile: Profile, connectionsTableViewController: ConnectionsTableViewController)
+ func configure(profile: Profile, connectionsTableViewController: ConnectionsTableViewController)
+ func delete(profile: Profile, connectionsTableViewController: ConnectionsTableViewController)
}
class ConnectionsTableViewController: UITableViewController {
@@ -23,16 +23,154 @@ class ConnectionsTableViewController: UITableViewController {
var viewContext: NSManagedObjectContext!
+ private lazy var fetchedResultsController: FetchedResultsController<Profile> = {
+ let fetchRequest = NSFetchRequest<Profile>()
+ fetchRequest.entity = Profile.entity()
+ fetchRequest.sortDescriptors = [NSSortDescriptor(key: "title", ascending: true)]
+ let frc = FetchedResultsController<Profile>(fetchRequest: fetchRequest,
+ managedObjectContext: viewContext)
+ frc.setDelegate(self.frcDelegate)
+ return frc
+ }()
+
+ private lazy var frcDelegate: ProfileFetchedResultsControllerDelegate = { // swiftlint:disable:this weak_delegate
+ return ProfileFetchedResultsControllerDelegate(tableView: self.tableView)
+ }()
+
override func viewDidLoad() {
super.viewDidLoad()
- // Do any additional setup after loading the view, typically from a nib.
+ do {
+ try fetchedResultsController.performFetch()
+ } catch {
+ print("Failed to fetch objects: \(error)")
+ }
+ }
+
+ @IBAction func addProvider(_ sender: Any) {
+ delegate?.addProvider(connectionsTableViewController: self)
+ }
+
+ override func numberOfSections(in tableView: UITableView) -> Int {
+ return 1
+ }
+
+ override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
+ return fetchedResultsController.sections?[0].objects.count ?? 0
+ }
+
+ override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
+ let cell = tableView.dequeueReusableCell(type: ProfileTableViewCell.self, for: indexPath)
+
+ guard let sections = fetchedResultsController.sections else {
+ fatalError("FetchedResultsController \(fetchedResultsController) should have sections, but found nil")
+ }
+
+ let section = sections[indexPath.section]
+ let profile = section.objects[indexPath.row]
+
+ cell.textLabel?.text = profile.title
+
+ return cell
}
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
+ override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
+ guard let sections = fetchedResultsController.sections else {
+ fatalError("FetchedResultsController \(fetchedResultsController) should have sections, but found nil")
+ }
+
+ let section = sections[indexPath.section]
+ let profile = section.objects[indexPath.row]
+
+ delegate?.connect(profile: profile, connectionsTableViewController: self)
+
+ tableView.deselectRow(at: indexPath, animated: true)
}
+ override func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
+ guard let sections = fetchedResultsController.sections else {
+ fatalError("FetchedResultsController \(fetchedResultsController) should have sections, but found nil")
+ }
+
+ let section = sections[indexPath.section]
+ let profile = section.objects[indexPath.row]
+
+ delegate?.configure(profile: profile, connectionsTableViewController: self)
+
+ }
+
+ override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
+ return true
+ }
+
+ override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
+ if editingStyle == .delete {
+
+ guard let sections = fetchedResultsController.sections else {
+ fatalError("FetchedResultsController \(fetchedResultsController) should have sections, but found nil")
+ }
+
+ let section = sections[indexPath.section]
+ let profile = section.objects[indexPath.row]
+
+ delegate?.delete(profile: profile, connectionsTableViewController: self)
+ }
+ }
}
extension ConnectionsTableViewController: Identifyable {}
+
+class ProfileFetchedResultsControllerDelegate: NSObject, FetchedResultsControllerDelegate {
+
+ private weak var tableView: UITableView?
+
+ // MARK: - Lifecycle
+ init(tableView: UITableView) {
+ self.tableView = tableView
+ }
+
+ func fetchedResultsControllerDidPerformFetch(_ controller: FetchedResultsController<Profile>) {
+ tableView?.reloadData()
+ }
+
+ func fetchedResultsControllerWillChangeContent(_ controller: FetchedResultsController<Profile>) {
+ tableView?.beginUpdates()
+ }
+
+ func fetchedResultsControllerDidChangeContent(_ controller: FetchedResultsController<Profile>) {
+ tableView?.endUpdates()
+ }
+
+ func fetchedResultsController(_ controller: FetchedResultsController<Profile>, didChangeObject change: FetchedResultsObjectChange<Profile>) {
+ guard let tableView = tableView else { return }
+ switch change {
+ case let .insert(_, indexPath):
+ tableView.insertRows(at: [indexPath], with: .automatic)
+
+ case let .delete(_, indexPath):
+ tableView.deleteRows(at: [indexPath], with: .automatic)
+
+ case let .move(_, fromIndexPath, toIndexPath):
+ tableView.moveRow(at: fromIndexPath, to: toIndexPath)
+
+ case let .update(_, indexPath):
+ tableView.reloadRows(at: [indexPath], with: .automatic)
+ }
+ }
+
+ func fetchedResultsController(_ controller: FetchedResultsController<Profile>, didChangeSection change: FetchedResultsSectionChange<Profile>) {
+ guard let tableView = tableView else { return }
+ switch change {
+ case let .insert(_, index):
+ tableView.insertSections(IndexSet(integer: index), with: .automatic)
+
+ case let .delete(_, index):
+ tableView.deleteSections(IndexSet(integer: index), with: .automatic)
+ }
+ }
+}
+
+class ProfileTableViewCell: UITableViewCell {
+
+}
+
+extension ProfileTableViewCell: Identifyable {}