-
Notifications
You must be signed in to change notification settings - Fork 1
/
ScriptsTableViewController.swift
133 lines (106 loc) · 4.64 KB
/
ScriptsTableViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//
// ScriptsTableViewController.swift
// Extension
//
// Created by Julian Moorhouse on 14/08/2019.
// Copyright © 2019 Mindwarp Consultancy Ltd. All rights reserved.
//
import UIKit
import MobileCoreServices
class ScriptsTableViewController: UITableViewController {
var items: [SiteScript] = []
var pageTitle = ""
var pageURL = ""
override func viewDidLoad() {
super.viewDidLoad()
title = "Scripts"
load()
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addNewScript))
if pageURL == "" {
if let inputItem = extensionContext?.inputItems.first as? NSExtensionItem {
if let itemProvider = inputItem.attachments?.first {
itemProvider.loadItem(forTypeIdentifier: kUTTypePropertyList as String) { [weak self] (dict, error) in
guard let itemDictionary = dict as? NSDictionary else { return }
guard let javaScriptValues = itemDictionary[NSExtensionJavaScriptPreprocessingResultsKey] as? NSDictionary else { return }
self?.pageTitle = javaScriptValues["title"] as? String ?? ""
self?.pageURL = javaScriptValues["URL"] as? String ?? ""
}
}
}
}
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Detail", for: indexPath)
let item = items[indexPath.row]
cell.textLabel?.text = item.scriptName
cell.detailTextLabel?.text = item.url?.absoluteString
cell.accessoryType = .detailButton
return cell
}
override func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
print("accessoryButtonTappedForRowWith")
if let vc = storyboard?.instantiateViewController(withIdentifier: "sb_Script") as? ActionViewController {
vc.initialItem = items[indexPath.row]
vc.didUpdateItem = {
[weak self] (item) in
// update
self?.items[indexPath.row] = item
self?.save()
self?.tableView.reloadData()
}
navigationController?.pushViewController(vc, animated: true)
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("didSelectRowAt")
let scriptItem = items[indexPath.row]
let item = NSExtensionItem()
let argument: NSDictionary = ["customJavaScript" : scriptItem.script ?? ""]
let webDictionary: NSDictionary = [NSExtensionJavaScriptFinalizeArgumentKey: argument]
let customJavaScript = NSItemProvider(item: webDictionary, typeIdentifier: kUTTypePropertyList as String)
item.attachments = [customJavaScript]
extensionContext?.completeRequest(returningItems: [item])
}
@objc func addNewScript() {
if let vc = storyboard?.instantiateViewController(withIdentifier: "sb_Script") as? ActionViewController {
vc.initialItem = SiteScript(urlPageTitle: pageTitle, url: pageURL)
vc.didUpdateItem = {
[weak self] (item) in
// add
self?.items.append(item)
self?.save()
self?.tableView.reloadData()
}
navigationController?.pushViewController(vc, animated: true)
}
}
func save() {
let jsonEncoder = JSONEncoder()
if let savedData = try? jsonEncoder.encode(items) {
let defaults = UserDefaults.standard
defaults.set(savedData, forKey: "items")
print("Saved data")
} else {
print("Failed to save items")
}
}
func load() {
let defaults = UserDefaults.standard
if let savedItems = defaults.object(forKey: "items") as? Data {
let jsonDecoder = JSONDecoder()
do {
items = try jsonDecoder.decode([SiteScript].self, from: savedItems)
print("Loaded data")
} catch {
print("Failed to load items.")
}
}
}
}