-
Notifications
You must be signed in to change notification settings - Fork 0
/
SetyourGoalsScren3ViewController.swift
474 lines (378 loc) · 16 KB
/
SetyourGoalsScren3ViewController.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//
// SetyourGoalsScren3ViewController.swift
// Nail Goals
//
// Created by Sparty on 5/20/15.
// Copyright (c) 2015 Think Better Labs, Inc. All rights reserved.
//
import UIKit
class SetyourGoalsScren3ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet var claendarHeaderView: UIView!
@IBOutlet var monthAndYearLabel: UILabel!// label for month and year in the calendar header view. To display the name of the month and year on the calendar
var startDateFlag = false
var startDate = NSUserDefaults.standardUserDefaults().objectForKey(kstartDate)as! NSString
var endDate = ""
var skipDays = 0
var currentMonth = 0
var nextMonth = 0
var thisMonth = 0
var thisYear = 0
var nextYear = 0
var calender = [Int]()
var currentDate = NSDate()
var monthName = ""
var preMonth = 0
var preYear = 0
var compareMonth = 0
var compareYear = 0
var datesArray = [Int]()
var monthsArray = [Int]()
var yearsArray = [Int]()
var selectionFalg = false
var indexpathArray = [Int]()
var previousDatesarray = [Int]()
@IBOutlet var collectionView: UICollectionView!
@IBOutlet var startDateInput: UILabel!
@IBOutlet var endDateLabelScreen5: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.startDateInput.text = formatDateForUI(startDate as String)
var calObject = calenderObject()
calender = calObject.calweekDayFromDate(currentDate)
compareMonth = calender[0]
compareYear = calender[2]
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func claculateNoOfDaysinGoal(startDate: String, endDate: String)-> Int{
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
let startDt:NSDate = dateFormatter.dateFromString(startDate)!
let endDt:NSDate = dateFormatter.dateFromString(endDate)!
let cal = NSCalendar.currentCalendar()
let unit:NSCalendarUnit = (NSCalendarUnit.CalendarUnitDay)
let components = cal.components(unit, fromDate: startDt, toDate: endDt, options: nil)
return components.day
}
func getMonthName(month: Int)->String{
var monthName: String?
switch month {
case 1:
monthName = "JAN"
case 2:
monthName = "FEB"
case 3:
monthName = "MAR"
case 4:
monthName = "APR"
case 5:
monthName = "MAY"
case 6:
monthName = "JUNE"
case 7:
monthName = "JULY"
case 8:
monthName = "AUG"
case 9:
monthName = "SEP"
case 10:
monthName = "OCT"
case 11:
monthName = "NOV"
case 12:
monthName = "DEC"
default:
monthName = ""
}
return monthName!
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
println("in counter")
return 35
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var calObject = calenderObject()
if(indexPath.row == 0){
calender = calObject.calweekDayFromDate(currentDate)
}
else {
calender = calObject.calculateDay(indexPath.row, stDt: currentDate )
}
var weekDay = calender[3]
var month = calender[0]
var year = calender[2]
var dateInCell = "\(calender[0])" + "/" + "\(calender[1])" + "/" + "\(calender[2])"
if(indexPath.row == 0){
monthName = self.getMonthName(month)
}
if (currentMonth == 0){
currentMonth = month
thisMonth = month
}
if(thisYear == 0){
thisYear = year
}
if(indexPath.row == 0){
switch weekDay {
case 1:
skipDays = 1
case 2:
skipDays = 2
case 3:
skipDays = 3
case 4:
skipDays = 4
case 5:
skipDays = 4
case 6:
skipDays = 6
case 7:
skipDays = 7
default:
skipDays = 0
}
}
var daysAdedforSelection = skipDays-1
if(indexPath.row < skipDays-1 ){
println("indexPath1")
println(indexPath.row)
println(dateInCell)
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("dateCell2", forIndexPath: indexPath) as! calendarDateCollection2ViewCell
cell.dateLabel.text = ""
self.datesArray.append(0)
self.monthsArray.append(0)
self.yearsArray.append(0)
cell.backgroundColor = UIColor( red: 251/255, green: 251/255, blue:251/255, alpha: 1.0 )
return cell
}
else {
println("indexPath2")
println(indexPath.row)
println(dateInCell)
var calenderDayInCell: Array<Int> = calObject.calculateDay(indexPath.row - (skipDays - 1), stDt: currentDate)
if(daysAdedforSelection == -2){
calenderDayInCell = calObject.calculateDay(indexPath.row, stDt: currentDate)
}
var dateInCell = "\(calenderDayInCell[0])" + "/" + "\(calenderDayInCell[1])" + "/" + "\(calenderDayInCell[2])"
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("dateCell2", forIndexPath: indexPath) as! calendarDateCollection2ViewCell
var day = String(calenderDayInCell[1])
self.datesArray.append(day.toInt()!)
self.monthsArray.append(calenderDayInCell[0])
self.yearsArray.append(calenderDayInCell[2])
cell.dateLabel.text = day
self.monthAndYearLabel.text = monthName + " \(thisYear)"
self.calculateDateRange(startDate as String, enddt: endDate, compareDt: dateInCell, idPathNumber: indexPath.row)
self.highlightStartDate(startDate as String, compareDt: dateInCell, idPathNumber: indexPath.row)
cell.backgroundColor = UIColor( red: 251/255, green: 251/255, blue:251/255, alpha: 1.0 )
if(self.indexpathArray.count > 0 ) {
if (contains (self.indexpathArray, indexPath.row)){
println("green")
println(dateInCell)
cell.backgroundColor = UIColor( red: 43/255, green: 178/255, blue:212/255, alpha: 1.0 )
}
}
if(contains (self.previousDatesarray, indexPath.row)){
cell.backgroundColor = UIColor( red: 255/255, green: 255/255, blue:255/255, alpha: 1.0 )
}
return cell
}
}
func highlightStartDate(stdt: String, compareDt: String, idPathNumber: Int) {
if(stdt == "" ){
//return nothing
}
else{
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
let startDt:NSDate = dateFormatter.dateFromString(stdt)!
let compDt = dateFormatter.dateFromString(compareDt)!
if startDt.compare(compDt) == NSComparisonResult.OrderedDescending
{
self.previousDatesarray.append(idPathNumber)
}
if ( startDt == compDt){
self.indexpathArray.append(idPathNumber)
}
}
}
func calculateDateRange(stdt: String , enddt: String, compareDt: String , idPathNumber: Int) -> Bool{
if(stdt == "" || enddt == ""){
return false
}
else {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
let startDt:NSDate = dateFormatter.dateFromString(stdt)!
let endDt:NSDate = dateFormatter.dateFromString(enddt)!
let compDt = dateFormatter.dateFromString(compareDt)!
if ( startDt == compDt){
self.indexpathArray.append(idPathNumber)
}
if(endDt == compDt){
println("enddt eq compare dt")
self.indexpathArray.append(idPathNumber)
}
if startDt.compare(compDt) == NSComparisonResult.OrderedAscending
{
if endDt.compare(compDt) == NSComparisonResult.OrderedDescending
{
self.indexpathArray.append(idPathNumber)
return true
}else{
return false
}
}else
{
return false
}
}
}
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
println("reusableView")
var headerView: CalendarHeader2CollectionReusableView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier:"Header2", forIndexPath:indexPath) as! CalendarHeader2CollectionReusableView
return headerView
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
var cell : UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)!
if(startDateFlag){
startDate = "\(monthsArray[indexPath.row])" + "/" + "\(datesArray[indexPath.row])" + "/" + "\(yearsArray[indexPath.row])"
endDate = ""
// self.endDateInput.text = ""
// self.selectEndDate.text = "Select End Date"
self.startDateFlag = false
self.previousDatesarray.removeAll(keepCapacity: false)
self.indexpathArray.removeAll(keepCapacity: false)
// self.nextButton.hidden = true
self.collectionView.reloadData()
// cell.backgroundColor = UIColor( red: 43/255, green: 178/255, blue:212/255, alpha: 1.0 )
}
else {
endDate = "\(monthsArray[indexPath.row])" + "/" + "\(datesArray[indexPath.row])" + "/" + "\((yearsArray[indexPath.row]))"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
let compEndDate:NSDate = dateFormatter.dateFromString(endDate)!
let compStartDate:NSDate = dateFormatter.dateFromString(self.startDate as String)!
if(compEndDate.compare(compStartDate) == NSComparisonResult.OrderedAscending){
let alertMessage = UIAlertController(title: "", message: "End Date cannot be a date prior to Start Date", preferredStyle: .Alert);
alertMessage.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self.presentViewController(alertMessage, animated: true, completion: nil)
}
else{
self.endDateLabelScreen5.text = formatDateForUI(endDate)
NSUserDefaults.standardUserDefaults().setObject(self.endDate, forKey: kendDate)
self.collectionView.reloadData()
var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("navToFinalGoalsScreen"), userInfo: nil, repeats: false)
}
}
}
func navToFinalGoalsScreen(){
println("here")
self.performSegueWithIdentifier("showGoalsScreen4", sender: self)
}
@IBAction func calendarNextButton(sender: AnyObject) {
self.previousDatesarray.removeAll(keepCapacity: false)
indexpathArray.removeAll(keepCapacity: false)
monthsArray.removeAll(keepCapacity: false)
yearsArray.removeAll(keepCapacity: false)
datesArray.removeAll(keepCapacity: false)
if(currentMonth != 12){
nextMonth = currentMonth + 1
currentMonth = 0
nextYear = thisYear
}
else {
nextMonth = 1
nextYear = thisYear + 1
currentMonth = 0
thisYear = 0
}
var date = "\(nextMonth)" + "/" + "01/" + "\(nextYear)"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
currentDate = dateFormatter.dateFromString(date)!
collectionView.reloadData()
}
@IBAction func claendarPreviousButton(sender: AnyObject) {
self.previousDatesarray.removeAll(keepCapacity: false)
indexpathArray.removeAll(keepCapacity: false)
datesArray.removeAll(keepCapacity: false)
monthsArray.removeAll(keepCapacity: false)
yearsArray.removeAll(keepCapacity: false)
if(compareMonth == currentMonth && compareYear == thisYear){
var todaysDate = NSDate()
currentDate = todaysDate
collectionView.reloadData()
}else{
if(currentMonth != 1){
preMonth = currentMonth - 1
currentMonth = 0
preYear = thisYear
}else {
preMonth = 12
preYear = thisYear - 1
thisYear = 0
currentMonth = 0
}
if(compareMonth == preMonth && compareYear == preYear){
var todaysDate = NSDate()
currentDate = todaysDate
collectionView.reloadData()
}else{
var date = "\(preMonth)" + "/" + "01/" + "\(preYear)"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
currentDate = dateFormatter.dateFromString(date)!
collectionView.reloadData()
}
}
}
func formatDateForUI(dt: String) -> String {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
let date:NSDate = dateFormatter.dateFromString(dt)!
let flags: NSCalendarUnit = (NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitYear)
let components = NSCalendar.currentCalendar().components(flags, fromDate: date)
var year = "\(components.year)"
var month = getMonthName(components.month)
var day = "\(components.day)"
var UIDateString = month + " " + day + " " + year
return UIDateString
}
func getNameOftheMonth(month:Int)-> String{
var monthName: String = ""
switch month{
case 1:
monthName = "January "
case 2:
monthName = "Febuary "
case 3:
monthName = "March "
case 4:
monthName = "April "
case 5:
monthName = "May "
case 6:
monthName = "June "
case 7:
monthName = "July "
case 8:
monthName = "August "
case 9:
monthName = "September "
case 10:
monthName = "October "
case 11:
monthName = "November "
default:
monthName = "December "
}
return monthName
}
// to remove the satus bar
override func prefersStatusBarHidden() -> Bool {
return true;
}
}