-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddCourseworkViewController.swift
108 lines (81 loc) · 3.92 KB
/
AddCourseworkViewController.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
//
// AddCourseworkViewController.swift
// iPadd Planner
//
// Created by Md Kamrul Amin on 09/05/2018.
// Copyright © 2018 Md Kamrul Amin. All rights reserved.
//
import UIKit
import CoreData
class AddCourseworkViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var textCwTitle: UITextField!
@IBOutlet weak var textModuleName: UITextField!
@IBOutlet weak var textLevel: UITextField!
@IBOutlet weak var textDueDate: UITextField!
@IBOutlet weak var textWeight: UITextField!
@IBOutlet weak var lblMarks: UILabel!
@IBOutlet weak var textNotes: UITextView!
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
var dateEnd: Date?
var dateStart: Date?
var currentValue: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
textCwTitle.becomeFirstResponder()
dateStart = Date()
textLevel.delegate = self
textWeight.delegate = self
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func dateChanged(_ sender: UITextField) {
let datePickerView: UIDatePicker = UIDatePicker()
// datePickerView.datePickerMode = UIDatePickerMode.date
sender.inputView = datePickerView
datePickerView.addTarget(self, action: #selector(AddCourseworkViewController.datePickerValueChanged), for: UIControlEvents.valueChanged)
}
@objc func datePickerValueChanged(sender:UIDatePicker){
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = DateFormatter.Style.medium
dateFormatter.timeStyle = DateFormatter.Style.none
textDueDate.text = dateFormatter.string(from: sender.date)
dateEnd = sender.date
}
@IBAction func sliderChanged(_ sender: UISlider) {
currentValue = Int(sender.value)
lblMarks.text = "\(currentValue)/100"
}
@IBAction func saveCoursework(_ sender: UIButton) {
let newCoursework = Coursework(context: context)
if (textCwTitle.text!.isEmpty) || (textDueDate.text!.isEmpty) || (textWeight.text!.isEmpty) || (textModuleName.text!.isEmpty) || (textLevel.text!.isEmpty){
let alert = UIAlertController(title: "Missing Information", message: "Please fill every fields", preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(OKAction)
self.present(alert, animated: true, completion: nil)
}else{
//Alert
newCoursework.courseTitle = textCwTitle.text
newCoursework.moduleName = textModuleName.text
newCoursework.level = Int16(textLevel.text!) ?? 0
newCoursework.dueDate = dateEnd
newCoursework.weight = Int16(textWeight.text!) ?? 0
newCoursework.actualMark = Int16(currentValue)
newCoursework.notes = textNotes.text
(UIApplication.shared.delegate as! AppDelegate).saveContext()
self.dismiss(animated: true, completion: nil)
}
}
@IBAction func cancelTask(_ sender: UIButton) {
self.dismiss(animated: true, completion: nil)
}
//https://stackoverflow.com/questions/30973044/how-to-restrict-uitextfield-to-take-only-numbers-in-swift?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
{
let allowedCharacters = CharacterSet.decimalDigits
let characterSet = CharacterSet(charactersIn: string)
return allowedCharacters.isSuperset(of: characterSet)
}
}