-
Notifications
You must be signed in to change notification settings - Fork 1
/
ActionViewController.swift
62 lines (47 loc) · 2.13 KB
/
ActionViewController.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
//
// ActionViewController.swift
// Extension
//
// Created by Julian Moorhouse on 14/08/2019.
// Copyright © 2019 Mindwarp Consultancy Ltd. All rights reserved.
//
import UIKit
import MobileCoreServices
class ActionViewController: UIViewController {
@IBOutlet var script: UITextView!
@IBOutlet var scriptName: UITextField!
var pageTitle = ""
var pageURL = ""
var initialItem: SiteScript!
var didUpdateItem: ((_ item: SiteScript) -> Void)?
override func viewDidLoad() {
super.viewDidLoad()
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillHideNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
title = initialItem.urlPageTitle
script.text = initialItem.script
scriptName.text = initialItem.scriptName
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if var site = initialItem {
site.scriptName = self.scriptName.text!
site.script = self.script.text
didUpdateItem?(site)
}
}
@objc func adjustForKeyboard(notification: Notification) {
guard let keyboardValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
let keyboardScreenEndFrame = keyboardValue.cgRectValue
let keyboardViewEndFrame = view.convert(keyboardScreenEndFrame, from: view.window)
if notification.name == UIResponder.keyboardWillHideNotification {
script.contentInset = .zero
} else {
script.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height - view.safeAreaInsets.bottom, right: 0)
}
script.scrollIndicatorInsets = script.contentInset
let selectedRange = script.selectedRange
script.scrollRangeToVisible(selectedRange)
}
}