Commit 56b30e96 authored by Paul Bird's avatar Paul Bird

Initial commit

parent 8ca47e11
//
// Location.swift
// MRoomDemo1
//
// Created by Paul Bird on 22/09/2016.
// Copyright © 2016 essensys. All rights reserved.
//
import Foundation
struct Location {
var locationId: String
var locationName: String
var meetingRooms: [(productId: String, roomName: String)]
}
//
// MeetingRoom.swift
// MRoomDemo1
//
// Created by Paul Bird on 20/09/2016.
// Copyright © 2016 essensys. All rights reserved.
//
import Foundation
struct MeetingRoom {
var locationId: String
var productId: String
var roomName: String
init() {
locationId = ""
productId = ""
roomName = ""
}
}
//
// TimeSegments.swift
// MRoomDemo1
//
// Created by Paul Bird on 20/09/2016.
// Copyright © 2016 essensys. All rights reserved.
//
import Foundation
struct TimeSegments {
var timeSegmentSizeMinutes: Int
var segments: [TimeSegment] = []
var managedHoursStart: Int
var managedHoursFinished: Int
init(timeSegmentSizeMinutes: Int, managedHoursStart: Int, managedHoursFinished: Int) {
segments = []
self.timeSegmentSizeMinutes = timeSegmentSizeMinutes
self.managedHoursStart = managedHoursStart
self.managedHoursFinished = managedHoursFinished
self.populateSegmentsForManagedHours()
}
func currentTimeSegmentIndex() -> Int? {
let segmentIndex = (hoursSinceStart() * timeSegmentsPerHour()) + minuteSegmentsSinceStart()
if segmentIndex >= 0 && segmentIndex < (numberOfManagedHours() * timeSegmentsPerHour()) {
return segmentIndex
} else {
return nil
}
}
fileprivate func hoursSinceStart() -> Int {
let currentHour = Date().hour()
return currentHour - managedHoursStart
}
fileprivate func lastSegmentIndex() -> Int {
return segments.count - 1
}
fileprivate func minuteSegmentsSinceStart() -> Int {
let currentMinute = Date().minute()
return currentMinute / timeSegmentSizeMinutes
}
func numberOfManagedHours() -> Int {
return managedHoursFinished - managedHoursStart
}
func numberOfManagedMinutes() -> Int {
return 60 * (managedHoursFinished - managedHoursStart)
}
fileprivate mutating func populateSegmentsForManagedHours() {
let numberOfManagedHours = managedHoursFinished - managedHoursStart
let segmentsPerHour = 60 / timeSegmentSizeMinutes
let numberOfSegments = numberOfManagedHours * segmentsPerHour
for _ in 1...numberOfSegments {
segments += [TimeSegment(occupied: false)]
}
}
func indexForNextFreeSegment() -> Int? {
guard let ctsi = currentTimeSegmentIndex() else { return nil }
let startSequenceIndex = ctsi + 1
for segment in startSequenceIndex...lastSegmentIndex() {
if segments[segment].occupied == false {
return segment
}
}
return nil
}
func indexForNextOccupiedSegment() -> Int? {
guard let ctsi = currentTimeSegmentIndex() else { return nil }
let startSequenceIndex = ctsi + 1
guard lastSegmentIndex() > startSequenceIndex else { return nil }
for segment in startSequenceIndex...lastSegmentIndex() {
if segments[segment].occupied == true {
return segment
}
}
return nil
}
func indexForTime(hour: Int, minute: Int) -> Int? {
let hoursSinceStart = hour - managedHoursStart
let minuteSegments = minute / timeSegmentSizeMinutes
let segmentIndex = (hoursSinceStart * timeSegmentsPerHour()) + minuteSegments
if segmentIndex >= 0 && segmentIndex <= lastSegmentIndex() {
return segmentIndex
}
return nil
}
func indexForEndTime(hour: Int, minute: Int) -> Int? {
let hoursSinceStart = hour - managedHoursStart
let minuteSegments = minute / timeSegmentSizeMinutes
var segmentIndex = (hoursSinceStart * timeSegmentsPerHour()) + minuteSegments
//If meeting is set to end a segment we don't want to roll into the next time segment
if minute % timeSegmentSizeMinutes == 0 {
segmentIndex = segmentIndex - 1
}
if segmentIndex >= 0 && segmentIndex <= lastSegmentIndex() {
return segmentIndex
}
return nil
}
func minutesUntilNextFreeTimeSegment() -> Int? {
guard let nextFree = startTimeDateForNextFreeSegment() else { return nil }
return nextFree.minutesFrom(Date())
}
mutating func refreshFromBookings(_ bookings: [RoomBooking]) {
setAllTimeSegmentsToOccupiedFalse()
for booking in bookings {
let _ = setOccupied(fromHour: booking.startTime.hour(),
fromMin: booking.startTime.minute(),
duration: booking.durationInMinutes)
}
}
func secondsUntilNextFreeTimeSegment() -> Int? {
guard let nextFree = startTimeDateForNextFreeSegment() else { return nil }
return nextFree.secondsFrom(Date())
}
mutating func setAllTimeSegmentsToOccupiedFalse() {
for index in 0..<segments.count {
segments[index].occupied = false
}
}
mutating func setOccupied(fromHour: Int, fromMin: Int, duration: Int) -> Bool {
var toHour = 0
var toMin = 0
let hoursToAdd = Int((fromMin+duration)/60)
toHour = fromHour + hoursToAdd
toMin = (fromMin + duration) % 60
let result = setOccupied(fromHour: fromHour, fromMin: fromMin, toHour: toHour, toMin: toMin)
return result
}
mutating func setOccupied(fromHour: Int, fromMin: Int, toHour: Int, toMin: Int) -> Bool {
if fromHour >= managedHoursFinished || toHour > managedHoursFinished {
return false
}
guard let startIndex = indexForTime(hour: fromHour, minute: fromMin) else {
return false
}
guard let endIndex = indexForEndTime(hour: toHour, minute: toMin) else {
return false
}
for index in startIndex...endIndex {
segments[index].occupied = true
}
return true
}
func startTimeForIndex(_ segmentIndex: Int) -> Date {
let startDate = Date().startOfDay
let managedHoursStartMinutes = managedHoursStart * 60
let minutesToAdd = segmentIndex * timeSegmentSizeMinutes
let totalMinutesToAdd = managedHoursStartMinutes + minutesToAdd
let datetimeForIndex = startDate.addingTimeInterval(Double(totalMinutesToAdd * 60))
return datetimeForIndex
}
func startTimeDateForNextFreeSegment() -> Date? {
guard let index = indexForNextFreeSegment() else { return nil}
return startTimeForIndex(index)
}
func startTimeDateForNextBooking() -> Date? {
guard let index = indexForNextOccupiedSegment() else { return nil}
return startTimeForIndex(index)
}
fileprivate func timeSegmentsPerHour() -> Int {
return 60 / timeSegmentSizeMinutes
}
}
......@@ -18,95 +18,29 @@ class UserDefaultsHelper {
if defaults.string(forKey: "accessToken") == nil {
setAccessToken("")
}
setLoginFailed(false)
}
//MARK:- Getters
class func accessToken() -> String {
let accessToken = defaults.string(forKey: "accessToken")
return accessToken ?? ""
}
class func calendarsUpdated() -> Bool {
let calendarsUpdated = defaults.bool(forKey: "calendarsUpdated")
return calendarsUpdated
}
class func locationsUpdated() -> Bool {
let locationsUpdated = defaults.bool(forKey: "locationsUpdated")
return locationsUpdated
}
class func loginFailed() -> Bool {
let loginFailed = defaults.bool(forKey: "loginFailed")
return loginFailed
}
class func productsUpdated() -> Bool {
let productsUpdated = defaults.bool(forKey: "productsUpdated")
return productsUpdated
}
class func refreshToken() -> String {
let refreshToken = defaults.string(forKey: "refreshToken") ?? ""
return refreshToken
}
class func username() -> String {
let username = defaults.string(forKey: "username") ?? ""
return username
}
//MARK:- Setters
class func setAccessToken(_ accessToken: String) {
defaults.set(accessToken, forKey: "accessToken")
defaults.synchronize()
}
class func setCalendarsUpdated(_ calendarsUpdated: Bool) {
defaults.set(calendarsUpdated, forKey: "calendarsUpdated")
defaults.synchronize()
}
class func setLocationsUpdated(_ locationsUpdated: Bool) {
defaults.set(locationsUpdated, forKey: "locationsUpdated")
defaults.synchronize()
}
class func setLoginFailed(_ loginFailed: Bool) {
defaults.set(loginFailed, forKey: "loginFailed")
defaults.synchronize()
}
class func setProductsUpdated(_ productsUpdated: Bool) {
defaults.set(productsUpdated, forKey: "productsUpdated")
defaults.synchronize()
}
class func setRefreshToken(_ refreshToken: String) {
defaults.set(refreshToken, forKey: "refreshToken")
defaults.synchronize()
}
class func setUsername(_ username: String) {
defaults.set(username, forKey: "username")
defaults.synchronize()
}
}
......@@ -151,8 +151,10 @@ class operateAPIUtilities {
request.httpMethod = "POST"
// Need to Add client Secret
var postString = "grant_type=password&client_id=trustedclient"
postString += "&client_secret=8bf2d32e-f624-49c5-b6a9-38c2fc413858"
postString += "&client_secret="
postString += "&scope=hubapi&username=\(username)&password=\(password)"
request.httpBody = postString.data(using: String.Encoding.utf8)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment