-
Notifications
You must be signed in to change notification settings - Fork 0
/
noteDetailsVC.swift
167 lines (131 loc) · 5.39 KB
/
noteDetailsVC.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//
// noteDetailsVC.swift
// Jot.
//
// Created by Suchit on 15/07/17.
// Copyright © 2017 Suchit. All rights reserved.
//
import UIKit
class noteDetailsVC: UIViewController {
@IBOutlet weak var titleEdit: UITextField!
@IBOutlet weak var tagEdit: UITextField!
@IBOutlet weak var noteEdit: UITextView!
var itemToEdit: Note?
var temp = 1
override func viewDidLoad() {
super.viewDidLoad()
self.createToolbar(textField: self.noteEdit)
if itemToEdit != nil{
loadItemData()
}
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: Notification.Name.UIKeyboardWillHide, object: nil)
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: Notification.Name.UIKeyboardWillChangeFrame, object: nil)
// Do any additional setup after loading the view.
}
@IBAction func savePressed(_ sender: Any) {
view.endEditing(true)
if itemToEdit == nil && (titleEdit.text != "") && (noteEdit.text != "write here.." || noteEdit.text != ""){
itemToEdit = Note(context: context)
if var title = titleEdit.text{
title = title.uppercased()
itemToEdit?.titleName = title
}
if var tag = tagEdit.text{
tag = tag.lowercased()
itemToEdit?.tagName = tag
}
if let field = noteEdit.text{
itemToEdit?.field = field
}
ad.saveContext()
dismiss(animated: true)
}
else if itemToEdit == nil && (titleEdit.text == "") && (noteEdit.text != "write here.." && noteEdit.text != ""){
itemToEdit = Note(context: context)
if let title = titleEdit.text{
if title == "" && itemToEdit?.field != ""
{
itemToEdit?.titleName = noteEdit.text.uppercased()
}
else if title == "" && itemToEdit?.field == ""
{
temp = 0
}
else
{
itemToEdit?.titleName = title.uppercased()
}
}
if let tag = tagEdit.text{
itemToEdit?.tagName = tag.lowercased()
}
if let field = noteEdit.text{
itemToEdit?.field = field
}
if temp != 0
{
ad.saveContext()
}
dismiss(animated: true)
}
else if itemToEdit != nil && (titleEdit.text == "") && (noteEdit.text == "write here.." || noteEdit.text == ""){
itemToEdit = nil
dismiss(animated: true)
}
else if itemToEdit != nil && (titleEdit.text != "") && (noteEdit.text != "write here.." || noteEdit.text != ""){
if var title = titleEdit.text{
title = title.uppercased()
itemToEdit?.titleName = title
}
if var tag = tagEdit.text{
tag = tag.lowercased()
itemToEdit?.tagName = tag
}
if let field = noteEdit.text{
itemToEdit?.field = field
}
ad.saveContext()
dismiss(animated: true)
}
else
{
dismiss(animated: true)
}
}
func loadItemData(){
if let item = itemToEdit {
titleEdit.text = item.titleName
tagEdit.text = item.tagName
noteEdit.text = item.field
}
}
func createToolbar(textField : UITextView) {
let toolbar = UIToolbar()
toolbar.barStyle = UIBarStyle.default
toolbar.sizeToFit()
let save = UIBarButtonItem(title: "Save Note", style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.savePressed(_:)))
save.tintColor = UIColor.darkGray
let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let doneBtn: UIBarButtonItem = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(self.doneButtonAction))
doneBtn.tintColor = UIColor.darkGray
toolbar.items = [save,flexSpace,doneBtn]
textField.inputAccessoryView = toolbar
}
func doneButtonAction() {
self.view.endEditing(true)
}
func adjustForKeyboard(notification: Notification) {
let userInfo = notification.userInfo!
let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let keyboardViewEndFrame = view.convert(keyboardScreenEndFrame, from: view.window)
if notification.name == Notification.Name.UIKeyboardWillHide {
noteEdit.contentInset = UIEdgeInsets.zero
} else {
noteEdit.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height, right: 0)
}
noteEdit.scrollIndicatorInsets = noteEdit.contentInset
let selectedRange = noteEdit.selectedRange
noteEdit.scrollRangeToVisible(selectedRange)
}
}