You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
192 lines
6.3 KiB
192 lines
6.3 KiB
//
|
|
// InstancesTableViewController.swift
|
|
// elpha-ios
|
|
//
|
|
// Created by Dwayne Harris on 8/29/18.
|
|
// Copyright © 2018 Elpha. All rights reserved.
|
|
//
|
|
|
|
import CoreData
|
|
import UIKit
|
|
|
|
struct Instance {
|
|
var id: String?
|
|
var name: String?
|
|
var addedAt: String?
|
|
var updatedAt: String?
|
|
var checkedAt: String?
|
|
var uptime: Int?
|
|
var up: Bool?
|
|
var dead: Bool?
|
|
var version: String?
|
|
var ipv6: Bool?
|
|
var httpsScore: Int?
|
|
var httpsRank: String?
|
|
var users: String?
|
|
var statuses: String?
|
|
var connections: Int?
|
|
var openRegistrations: Bool?
|
|
var thumbnail: String?
|
|
var thumbnailProxy: String?
|
|
var activeUsers: Int?
|
|
var shortDescription: String?
|
|
var fullDescription: String?
|
|
var topic: String?
|
|
var languages: [String]?
|
|
var categories: [String]?
|
|
}
|
|
|
|
class InstancesTableViewController: UITableViewController, UIViewControllerPreviewingDelegate {
|
|
@IBOutlet var mainNavigationItem: UINavigationItem!
|
|
|
|
var loading: Bool = false {
|
|
didSet {
|
|
DispatchQueue.main.async {
|
|
if self.loading {
|
|
UIApplication.shared.isNetworkActivityIndicatorVisible = true
|
|
self.refreshControl?.beginRefreshing()
|
|
} else {
|
|
UIApplication.shared.isNetworkActivityIndicatorVisible = false
|
|
self.refreshControl?.endRefreshing()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
|
|
|
|
mainNavigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(done))
|
|
|
|
registerForPreviewing(with: self, sourceView: tableView)
|
|
refreshControl?.addTarget(self, action: #selector(self.reloadInstances), for: .valueChanged)
|
|
|
|
loadInstances()
|
|
}
|
|
|
|
@objc func done() {
|
|
dismiss(animated: true, completion: nil)
|
|
}
|
|
|
|
@objc func reloadInstances() {
|
|
loading = true
|
|
|
|
InstancesDataManager.shared.reloadInstances() { error in
|
|
self.loading = false
|
|
|
|
guard error == nil else {
|
|
return
|
|
}
|
|
|
|
DispatchQueue.main.async {
|
|
self.tableView.reloadData()
|
|
}
|
|
}
|
|
}
|
|
|
|
func loadInstances() {
|
|
loading = true
|
|
|
|
InstancesDataManager.shared.loadInstances() { error in
|
|
self.loading = false
|
|
|
|
guard error == nil else {
|
|
return
|
|
}
|
|
|
|
DispatchQueue.main.async {
|
|
self.tableView.reloadData()
|
|
}
|
|
}
|
|
}
|
|
|
|
func getInstancesRequest() -> NSFetchRequest<ISInstanceMO> {
|
|
let request = NSFetchRequest<ISInstanceMO>(entityName: "ISInstance")
|
|
let sort = NSSortDescriptor(key: "users", ascending: false)
|
|
request.sortDescriptors = [sort]
|
|
|
|
return request
|
|
}
|
|
|
|
func getInstance(for indexPath: IndexPath) -> ISInstanceMO? {
|
|
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
|
|
|
|
do {
|
|
let instances = try context.fetch(self.getInstancesRequest())
|
|
return instances[indexPath.row]
|
|
} catch {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
override func numberOfSections(in tableView: UITableView) -> Int {
|
|
return 1
|
|
}
|
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
|
|
|
|
do {
|
|
return try context.count(for: self.getInstancesRequest())
|
|
} catch {
|
|
return 0
|
|
}
|
|
}
|
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
|
|
|
|
guard let cell = tableView.dequeueReusableCell(withIdentifier: "InstancesTableViewCell", for: indexPath) as? InstancesTableViewCell else {
|
|
fatalError("Unable to find reusable cell")
|
|
}
|
|
|
|
do {
|
|
let instances = try context.fetch(self.getInstancesRequest())
|
|
let instance = instances[indexPath.row]
|
|
|
|
cell.instanceNameLabel.text = instance.name
|
|
cell.statusesLabel.text = NumberFormatter.localizedString(from: NSNumber(value: instance.statuses), number: .decimal)
|
|
cell.usersLabel.text = NumberFormatter.localizedString(from: NSNumber(value: instance.users), number: .decimal)
|
|
|
|
if let thumbnail = instance.thumbnail {
|
|
cell.thumbnailURL = thumbnail
|
|
}
|
|
|
|
if indexPath.row == instances.count - 1 && !loading && !InstancesDataManager.shared.finished {
|
|
loadInstances()
|
|
}
|
|
|
|
return cell
|
|
} catch {
|
|
return cell
|
|
}
|
|
}
|
|
|
|
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
|
if segue.identifier == "InstanceDetailSegue", let cell = segue.destination as? InstanceViewController {
|
|
if let indexPath = tableView.indexPathForSelectedRow {
|
|
cell.instance = getInstance(for: indexPath)
|
|
}
|
|
}
|
|
}
|
|
|
|
public func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
|
|
guard let indexPath = tableView.indexPathForRow(at: location) else {
|
|
return nil
|
|
}
|
|
|
|
let storyboard = UIStoryboard(name: "Main", bundle: nil)
|
|
if let detailViewController = storyboard.instantiateViewController(withIdentifier: "InstanceViewController") as? InstanceViewController {
|
|
detailViewController.instance = getInstance(for: indexPath)
|
|
return detailViewController
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
public func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
|
|
navigationController?.pushViewController(viewControllerToCommit, animated: true)
|
|
}
|
|
}
|