Skip to content

Commit

Permalink
things
Browse files Browse the repository at this point in the history
  • Loading branch information
tominsam committed Nov 27, 2021
1 parent 4a8f824 commit e6b5f25
Show file tree
Hide file tree
Showing 33 changed files with 1,036 additions and 888 deletions.
11 changes: 11 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,14 @@ disabled_rules:
- function_body_length
- line_length
- identifier_name
- cyclomatic_complexity

custom_rules:
restrict_non_ascii:
included: ".*\\.swift"
name: "Restrict Unicode"
regex: "([^\u0000-\u007F]+)"
match_kinds:
- identifier
message: "Non-ascii not allowed"
severity: error
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ https://movieos.org/code/flame/

## TODO

* Some services (eg those broadcast my iOS in sleep mode but charging) seem to disappear and appear. Hosts should have some hysteresis so that the left pane doesn't jitter so much
* Some services (eg those broadcast by iOS in sleep mode but charging) seem to disappear and appear. Hosts should have some hysteresis so that the left pane doesn't jitter so much
* When services appear and disappear the selected row in the left pane deselected
* New tab keyboard shortcut on mac, or remove tabbing. Either.

8 changes: 1 addition & 7 deletions flametouch/Base UI/AboutSceneDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
//
// AboutSceneDelegate.swift
// Flame
//
// Created by tominsam on 10/18/19.
// Copyright © 2019 tominsam. All rights reserved.
//
// Copyright 2019 Thomas Insam. All rights reserved.

import UIKit

Expand Down
109 changes: 36 additions & 73 deletions flametouch/Base UI/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,44 +1,34 @@
//
// AppDelegate.swift
// flametouch
//
// Created by tominsam on 10/10/15.
// Copyright © 2015 tominsam. All rights reserved.
//
// Copyright 2015 Thomas Insam. All rights reserved.

import UIKit
import SafariServices

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

let browser = ServiceBrowser()

static func instance() -> AppDelegate {
// swiftlint:disable:next force_cast
return UIApplication.shared.delegate as! AppDelegate
}

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
browser.resume()
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
) -> Bool {
return true
}

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
func application(
_ application: UIApplication,
configurationForConnecting connectingSceneSession: UISceneSession,
options: UIScene.ConnectionOptions
) -> UISceneConfiguration {
switch options.userActivities.first?.activityType {
case "org.jerakeen.flametouch.about":
return UISceneConfiguration(name: "About", sessionRole: .windowApplication)
return UISceneConfiguration(name: "About", sessionRole: connectingSceneSession.role)
default:
return UISceneConfiguration(name: "Main", sessionRole: .windowApplication)
return UISceneConfiguration(name: "Main", sessionRole: connectingSceneSession.role)
}

}

func applicationDidEnterBackground(_ application: UIApplication) {
browser.pause()
}

func applicationWillEnterForeground(_ application: UIApplication) {
browser.resume()
}

override func buildMenu(with builder: UIMenuBuilder) {
Expand All @@ -62,60 +52,33 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
builder.remove(menu: .help)
}

func exportData() -> URL? {

var groupsJson: [Any] = []
var hostCount = 0
var serviceCount = 0
for serviceGroup in browser.serviceGroups {
hostCount += 1
var groupJson: [String: Any] = [:]
groupJson["name"] = serviceGroup.title
var addressesJson: [String] = []
for address in serviceGroup.addresses {
addressesJson.append(address)
}
groupJson["addresses"] = addressesJson

var servicesJson: [Any] = []
for service in serviceGroup.services {
serviceCount += 1
var serviceJson: [String: Any] = [:]
serviceJson["name"] = service.name
serviceJson["port"] = service.port
serviceJson["type"] = service.type
var addressesJson: [String] = []
let addresses = service.addresses!.compactMap { getIFAddress($0) }
for address in addresses {
addressesJson.append(address)
func openUrl(_ url: URL?, from presentingViewController: UIViewController) {
guard let url = url, let scheme = url.scheme else {
return
}
switch scheme {
case "http", "https":
// If there's a universal link handler for this URL, use that for preference
#if targetEnvironment(macCatalyst)
UIApplication.shared.open(url)
#else
UIApplication.shared.open(url, options: [.universalLinksOnly: true]) { result in
if !result {
let vc = SFSafariViewController(url: url)
vc.preferredControlTintColor = .systemRed
presentingViewController.present(vc, animated: true)
}
serviceJson["addresses"] = addressesJson
for (key, value) in service.txtData {
serviceJson[key] = value
}
#endif
default:
UIApplication.shared.open(url, options: [:]) { result in
if !result {
let alertController = UIAlertController(title: "Can't open URL", message: "I couldn't open that URL - maybe you need a particular app installed", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default))
presentingViewController.present(alertController, animated: true)
}

servicesJson.append(serviceJson)
}
groupJson["services"] = servicesJson

groupsJson.append(groupJson)
}

let file = "services_export.json"

guard let dir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .allDomainsMask, true).first,
let path = NSURL(fileURLWithPath: dir).appendingPathComponent(file)
else {
return nil
}

NSLog("path is \(path.path)")
let output = OutputStream(toFileAtPath: path.path, append: false)!
output.open()
JSONSerialization.writeJSONObject(groupsJson, to: output, options: JSONSerialization.WritingOptions.prettyPrinted, error: nil)
output.close()

return path
}

}
13 changes: 9 additions & 4 deletions flametouch/Base UI/CustomSplitViewController.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// Copyright 2019 Thomas Insam. All rights reserved.

import UIKit

class CustomSplitViewController: UISplitViewController {

public lazy var master = StaticNavigationController().configured {
let serviceController: ServiceController

public lazy var master = configure(StaticNavigationController()) {
$0.theme()
}

private lazy var emptyViewController = UIViewController().configured {
private lazy var emptyViewController = configure(UIViewController()) {
$0.view.backgroundColor = .systemGroupedBackground
}

Expand All @@ -15,7 +19,8 @@ class CustomSplitViewController: UISplitViewController {
set { fatalError(String(describing: newValue)) }
}

init() {
init(serviceController: ServiceController) {
self.serviceController = serviceController
super.init(nibName: nil, bundle: nil)
super.delegate = self
preferredDisplayMode = .oneBesideSecondary
Expand Down Expand Up @@ -55,7 +60,7 @@ class CustomSplitViewController: UISplitViewController {
// in the responder chain but still has a window/vc to present from.
@objc
func saveExportedData() {
guard let url = AppDelegate.instance().exportData() else { return }
guard let url = ServiceExporter.export(hosts: serviceController.hosts) else { return }
let controller = UIDocumentPickerViewController(forExporting: [url])
present(controller, animated: true)
}
Expand Down
25 changes: 16 additions & 9 deletions flametouch/Base UI/SceneDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
//
// SceneDelegate.swift
// Flame
//
// Created by tominsam on 10/18/19.
// Copyright © 2019 tominsam. All rights reserved.
//
// Copyright 2019 Thomas Insam. All rights reserved.

import UIKit

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?

let serviceController = ServiceController()

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
NSLog("Scene started")
guard let windowScene = scene as? UIWindowScene else { fatalError() }
Expand All @@ -24,16 +20,27 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
#endif

window = UIWindow(windowScene: windowScene)
window?.rootViewController = CustomSplitViewController().configured {
window?.rootViewController = configure(CustomSplitViewController(serviceController: serviceController)) {
$0.maximumPrimaryColumnWidth = 640
$0.minimumPrimaryColumnWidth = 320
$0.preferredPrimaryColumnWidthFraction = 0.4
$0.primaryBackgroundStyle = .none // Or .sidebar but I hate it.
$0.setMasterViewController(ServicesViewController())
$0.setMasterViewController(BrowseViewController(serviceController: serviceController))
}
window?.tintColor = .systemRed
window?.makeKeyAndVisible()
}

func sceneDidEnterBackground(_ scene: UIScene) {
ELog("sceneDidEnterBackground")
#if !targetEnvironment(macCatalyst)
serviceController.stop()
#endif
}

func sceneWillEnterForeground(_ scene: UIScene) {
ELog("sceneWillEnterForeground")
serviceController.start()
}

}
24 changes: 9 additions & 15 deletions flametouch/Base UI/SimpleCell.swift
Original file line number Diff line number Diff line change
@@ -1,47 +1,41 @@
//
// SimpleCell.swift
// Flame
//
// Created by tominsam on 9/24/16.
// Copyright © 2016 tominsam. All rights reserved.
//
// Copyright 2016 Thomas Insam. All rights reserved.

import Foundation
import UIKit

class SimpleCell: UITableViewCell, Reusable {
static var reuseId: String = "SimpleCell"

lazy var titleView = UILabel().configured {
lazy var titleView = configure(UILabel()) {
$0.font = UIFont.preferredFont(forTextStyle: .body)
$0.textColor = .label
}

lazy var subtitleView = UILabel().configured {
lazy var subtitleView = configure(UILabel()) {
$0.font = UIFont.preferredFont(forTextStyle: .body)
$0.textColor = .secondaryLabel
$0.highlightedTextColor = .label
}

lazy var titleStack = UIStackView(arrangedSubviews: [titleView, subtitleView]).configured {
lazy var titleStack = configure(UIStackView(arrangedSubviews: [titleView, subtitleView])) {
$0.axis = .vertical
$0.alignment = .fill
$0.spacing = 8
}

lazy var iconView = UIImageView(frame: CGRect(x: 0, y: 0, width: 24, height: 24)).configured {
lazy var iconView = configure(UIImageView(frame: CGRect(x: 0, y: 0, width: 24, height: 24))) {
$0.contentMode = .scaleAspectFit
$0.image = nil
$0.tintColor = .label
}

lazy var rightView = UILabel().configured {
lazy var rightView = configure(UILabel()) {
$0.font = UIFont.preferredFont(forTextStyle: .body)
$0.textColor = .secondaryLabel
$0.highlightedTextColor = .label
}

lazy var outerStack = UIStackView(arrangedSubviews: [iconView, titleStack, rightView]).configured {
lazy var outerStack = configure(UIStackView(arrangedSubviews: [iconView, titleStack, rightView])) {
$0.axis = .horizontal
$0.alignment = .center
$0.distribution = .fill
Expand All @@ -61,8 +55,8 @@ class SimpleCell: UITableViewCell, Reusable {
super.init(style: .default, reuseIdentifier: reuseIdentifier)

// Force a background color to avoid buggy mac selection styles
selectedBackgroundView = UIView().configured {
$0.backgroundColor = .tertiarySystemGroupedBackground
selectedBackgroundView = configure(UIView()) {
$0.backgroundColor = .systemGray5
}

contentView.addSubviewWithConstraints(outerStack, [
Expand Down
8 changes: 1 addition & 7 deletions flametouch/Base UI/UITableView.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
//
// UITableView.swift
// Flame
//
// Created by tominsam on 3/16/19.
// Copyright © 2019 tominsam. All rights reserved.
//
// Copyright 2019 Thomas Insam. All rights reserved.

import UIKit

Expand Down
Loading

0 comments on commit e6b5f25

Please sign in to comment.