Commit 0feadc67 authored by domenicw's avatar domenicw
Browse files

Improvements to networking

parent 94e438ac
......@@ -17,7 +17,7 @@
B050E14E21516AFE0090CB79 /* AppNavigator.swift in Sources */ = {isa = PBXBuildFile; fileRef = B050E14D21516AFE0090CB79 /* AppNavigator.swift */; };
B050E15021516BA60090CB79 /* OnboardingNavigator.swift in Sources */ = {isa = PBXBuildFile; fileRef = B050E14F21516BA60090CB79 /* OnboardingNavigator.swift */; };
B050E15621516D4D0090CB79 /* HomeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B050E15521516D4D0090CB79 /* HomeViewController.swift */; };
B050E15921516E230090CB79 /* EventModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = B050E15821516E230090CB79 /* EventModel.swift */; };
B050E15921516E230090CB79 /* EventViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = B050E15821516E230090CB79 /* EventViewModel.swift */; };
B050E15C215171F70090CB79 /* HomeNavigator.swift in Sources */ = {isa = PBXBuildFile; fileRef = B050E15B215171F70090CB79 /* HomeNavigator.swift */; };
B050E15F2151735A0090CB79 /* UIColor+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = B050E15E2151735A0090CB79 /* UIColor+Extension.swift */; };
B050E168215176D50090CB79 /* AmivApplicationActionDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B050E167215176D50090CB79 /* AmivApplicationActionDelegate.swift */; };
......@@ -106,7 +106,7 @@
B050E14F21516BA60090CB79 /* OnboardingNavigator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingNavigator.swift; sourceTree = "<group>"; };
B050E15121516C520090CB79 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
B050E15521516D4D0090CB79 /* HomeViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeViewController.swift; sourceTree = "<group>"; };
B050E15821516E230090CB79 /* EventModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventModel.swift; sourceTree = "<group>"; };
B050E15821516E230090CB79 /* EventViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventViewModel.swift; sourceTree = "<group>"; };
B050E15B215171F70090CB79 /* HomeNavigator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeNavigator.swift; sourceTree = "<group>"; };
B050E15E2151735A0090CB79 /* UIColor+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIColor+Extension.swift"; sourceTree = "<group>"; };
B050E167215176D50090CB79 /* AmivApplicationActionDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AmivApplicationActionDelegate.swift; sourceTree = "<group>"; };
......@@ -338,7 +338,7 @@
isa = PBXGroup;
children = (
B050E1882151A4850090CB79 /* Settings Model */,
B050E15821516E230090CB79 /* EventModel.swift */,
B050E15821516E230090CB79 /* EventViewModel.swift */,
);
path = Model;
sourceTree = "<group>";
......@@ -786,7 +786,7 @@
B050E1902151A6000090CB79 /* SettingsModel.swift in Sources */,
B050E15621516D4D0090CB79 /* HomeViewController.swift in Sources */,
B050E1922151A61F0090CB79 /* SettingsSectionModel.swift in Sources */,
B050E15921516E230090CB79 /* EventModel.swift in Sources */,
B050E15921516E230090CB79 /* EventViewModel.swift in Sources */,
B0D3F92B21552E8E005209FF /* UIButton+Extension.swift in Sources */,
B0AF91322157B38F008F3B80 /* ParameterEncoding.swift in Sources */,
B050E168215176D50090CB79 /* AmivApplicationActionDelegate.swift in Sources */,
......
//
// EventModel.swift
// EventViewModel.swift
// Amiv
//
// Created by Domenic Wüthrich on 18.09.18.
......@@ -8,8 +8,17 @@
import Foundation
public struct EventModel {
public struct EventViewModel {
let title: String
let eventResponse: EventsResponse?
}
extension EventViewModel {
public static func create(with response: EventsResponse?) -> EventViewModel {
return self.init(title: "Events", eventResponse: response)
}
}
......@@ -24,20 +24,12 @@ public class EventsNavigator: Navigator {
// MARK: - Initializers
public init() {
let home = EventsViewController(model: nil)
self.navigationController = UINavigationController(rootViewController: home)
let events = EventsViewController(model: .create(with: nil))
self.navigationController = UINavigationController(rootViewController: events)
self.navigationController.navigationBar.tintColor = .amivRed
home.delegate = self
events.delegate = self
self.networkManager.getEvents { (response, error) in
if let response = response {
DispatchQueue.main.async {
if let top = self.navigationController.topViewController as? EventsViewController {
top.model = response
}
}
}
}
self.refreshData(events)
}
// MARK: - Navigation
......@@ -52,7 +44,7 @@ public class EventsNavigator: Navigator {
extension EventsNavigator: EventsViewControllerDelegate {
public func didSelectEvent(section: Int, index: Int) {
public func didSelectEvent(_ viewController: EventsViewController, section: Int, index: Int) {
debugPrint("didSelect section: \(section) and index: \(index)")
// TODO: - Retrieve event, convert into GenericInfoViewControllerModel and show Detail View
......@@ -60,16 +52,13 @@ extension EventsNavigator: EventsViewControllerDelegate {
self.goToEventDetailView(model: .createTestModel())
}
public func refreshData() {
public func refreshData(_ viewController: EventsViewController) {
debugPrint("Refreshing Events Data")
self.networkManager.getEvents { (response, error) in
if let response = response {
DispatchQueue.main.async {
if let top = self.navigationController.topViewController as? EventsViewController {
top.model = response
top.tableView.refreshControl?.endRefreshing()
}
viewController.model = .create(with: response)
}
}
}
......@@ -79,7 +68,7 @@ extension EventsNavigator: EventsViewControllerDelegate {
extension EventsNavigator: GenericInfoViewControllerDelegate {
public func buttonTapped() {
public func buttonTapped(_ viewController: GenericInfoViewController) {
debugPrint("Info View button tapped")
// TODO: - Sign up for event
......
......@@ -22,7 +22,7 @@ public class HomeNavigator: Navigator {
// MARK: - Initializers
public init() {
let model = EventModel()
let model = ""
let home = HomeViewController(model: model)
self.navigationController = UINavigationController(rootViewController: home)
self.navigationController.navigationBar.tintColor = .amivRed
......
......@@ -43,7 +43,7 @@ public class JobsNavigator: Navigator {
extension JobsNavigator: JobsViewControllerDelegate {
public func didSelectJob(section: Int, index: Int) {
public func didSelectJob(_ viewController: JobsViewController, section: Int, index: Int) {
debugPrint("didSelect section: \(section) and index: \(index)")
// TODO: - Retrieve job, convert into GenericInfoViewControllerModel and show Detail View
......@@ -51,7 +51,7 @@ extension JobsNavigator: JobsViewControllerDelegate {
self.goToJobsDetailView(model: .createTestModel())
}
public func refreshData() {
public func refreshData(_ viewController: JobsViewController) {
debugPrint("Refreshing Jobs Data")
}
......@@ -59,7 +59,7 @@ extension JobsNavigator: JobsViewControllerDelegate {
extension JobsNavigator: GenericInfoViewControllerDelegate {
public func buttonTapped() {
public func buttonTapped(_ viewController: GenericInfoViewController) {
debugPrint("Info View button tapped")
// TODO: - Show job description pdf using QLPreviewController
......
......@@ -22,12 +22,12 @@ public struct NetworkManager {
case serverError = "Something went wrong at the AMIV server."
}
public enum Result<String> {
public enum RequestResult<String> {
case success
case failure(String)
}
fileprivate func handleNetworkRequest(_ response: HTTPURLResponse) -> Result<String> {
fileprivate func handleNetworkRequest(_ response: HTTPURLResponse) -> RequestResult<String> {
switch response.statusCode {
case 200...299:
return .success
......
......@@ -15,18 +15,18 @@ public class EventsViewController: UITableViewController {
public var delegate: EventsViewControllerDelegate?
public var model: EventsResponse? {
public var model: EventViewModel {
didSet {
self.tableView.reloadData()
self.updateView()
}
}
// MARK: - Initializers
public init(model: EventsResponse?) {
public init(model: EventViewModel) {
self.model = model
super.init(style: .plain)
self.title = "Events"
self.title = model.title
}
required init?(coder aDecoder: NSCoder) {
......@@ -56,10 +56,18 @@ public class EventsViewController: UITableViewController {
self.refreshControl?.endRefreshing()
}
private func updateView() {
DispatchQueue.main.async {
self.title = self.model.title
self.tableView.refreshControl?.endRefreshing()
self.tableView.reloadData()
}
}
// MARK: - View Interaction
@objc private func refreshData() {
self.delegate?.refreshData()
self.delegate?.refreshData(self)
}
}
......@@ -73,7 +81,7 @@ extension EventsViewController {
}
public override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let model = self.model {
if let model = self.model.eventResponse {
return model.events.count
}
return 0
......@@ -87,7 +95,7 @@ extension EventsViewController {
return cell
}()
guard let model = self.model else {
guard let model = self.model.eventResponse else {
return cell
}
......@@ -108,7 +116,7 @@ extension EventsViewController {
extension EventsViewController {
public override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.delegate?.didSelectEvent(section: indexPath.section, index: indexPath.row)
self.delegate?.didSelectEvent(self, section: indexPath.section, index: indexPath.row)
tableView.deselectRow(at: indexPath, animated: true)
}
......
......@@ -10,8 +10,8 @@ import Foundation
public protocol EventsViewControllerDelegate {
func didSelectEvent(section: Int, index: Int)
func didSelectEvent(_ viewController: EventsViewController, section: Int, index: Int)
func refreshData()
func refreshData(_ viewController: EventsViewController)
}
......@@ -197,7 +197,7 @@ public class GenericInfoViewController: UIViewController {
@objc private func buttonTapped() {
debugPrint("button tapped")
self.delegate?.buttonTapped()
self.delegate?.buttonTapped(self)
}
@objc private func expandImage() {
......
......@@ -11,6 +11,6 @@ import UIKit
public protocol GenericInfoViewControllerDelegate {
func buttonTapped()
func buttonTapped(_ viewController: GenericInfoViewController)
}
......@@ -13,7 +13,7 @@ public class HomeViewController: UITableViewController {
// MARK: - Variables
public var model: EventModel {
public var model: String {
didSet {
self.tableView.reloadData()
}
......@@ -21,7 +21,7 @@ public class HomeViewController: UITableViewController {
// MARK: - Initializers
public init(model: EventModel) {
public init(model: String) {
self.model = model
super.init(style: .plain)
......
......@@ -52,7 +52,7 @@ public class JobsViewController: UITableViewController {
// MARK: - View Interaction
@objc private func refreshData() {
self.delegate?.refreshData()
self.delegate?.refreshData(self)
}
}
......@@ -84,7 +84,7 @@ extension JobsViewController {
extension JobsViewController {
public override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.delegate?.didSelectJob(section: indexPath.section, index: indexPath.row)
self.delegate?.didSelectJob(self, section: indexPath.section, index: indexPath.row)
tableView.deselectRow(at: indexPath, animated: true)
}
......
......@@ -10,8 +10,8 @@ import Foundation
public protocol JobsViewControllerDelegate {
func didSelectJob(section: Int, index: Int)
func didSelectJob(_ viewController: JobsViewController, section: Int, index: Int)
func refreshData()
func refreshData(_ viewController: JobsViewController)
}
# Amiv iOS App
This is the official AMIV iOS client. It uses the new AMIV [Api](https://github.com/amiv-eth/amivapi/blob/master/docs/User_Guide.md).
TODO
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