Skip to content

Instantly share code, notes, and snippets.

@amixpal
Created August 26, 2019 16:36
Show Gist options
  • Save amixpal/faf843358909190c4ccc02d9bad45c8a to your computer and use it in GitHub Desktop.
Save amixpal/faf843358909190c4ccc02d9bad45c8a to your computer and use it in GitHub Desktop.
ChooseTime and Date
//
// WeeklyChooseTimeAndDateViewController.swift
// Fitme
//
// Created by paly on 8/7/19.
// Copyright © 2019 RedEye. All rights reserved.
//
import Foundation
import UIKit
import RappleProgressHUD
class WeeklyChooseTimeAndDateViewController: UIViewController
{
var dayArray: [Bool] = [true,true,true,false,false,false,false]
var selectedDaysInWeek: [Int] = []
var count:Int = 3
var slotSelected:String = ""
var cellMarginSize = 8.0
@IBOutlet weak var threeWeekLabel: UILabel!
@IBOutlet weak var threeWeekView: UIView!
@IBOutlet weak var twoWeekLabel: UILabel!
@IBOutlet weak var twoWeekView: UIView!
@IBOutlet weak var oneWeeklabel: UILabel!
@IBOutlet weak var oneWeekView: UIView!
@IBOutlet weak var startDateView: UIView!
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var startDayLabel: UILabel!
@IBOutlet weak var monthLabel: UILabel!
@IBOutlet weak var endDateView: UIView!
@IBOutlet weak var endDateLabel: UILabel!
@IBOutlet weak var endMonthLabel: UILabel!
@IBOutlet weak var endDayLabel: UILabel!
@IBOutlet weak var mondayView: UIView!
@IBOutlet weak var mondayImage: UIImageView!
@IBOutlet weak var mondayLabel: UILabel!
@IBOutlet weak var tuesdayView: UIView!
@IBOutlet weak var tuesdayImage: UIImageView!
@IBOutlet weak var tuesdayLabel: UILabel!
@IBOutlet weak var wednesdayLabel: UILabel!
@IBOutlet weak var wednesdayImage: UIImageView!
@IBOutlet weak var wednesdayView: UIView!
@IBOutlet weak var thursdayView: UIView!
@IBOutlet weak var thursdayImage: UIImageView!
@IBOutlet weak var thursdayLabel: UILabel!
@IBOutlet weak var fridayView: UIView!
@IBOutlet weak var fridayImage: UIImageView!
@IBOutlet weak var fridayLabel: UILabel!
@IBOutlet weak var saturdayView: UIView!
@IBOutlet weak var saturdayImage: UIImageView!
@IBOutlet weak var saturdayLabel: UILabel!
@IBOutlet weak var sundayView: UIView!
@IBOutlet weak var sundayImage: UIImageView!
@IBOutlet weak var sundayLabel: UILabel!
@IBOutlet weak var continueBookingButton: UIButton!
@IBOutlet weak var showLocationView: UIView!
@IBOutlet weak var showLocationLabel: UILabel!
@IBOutlet weak var editLabel: UILabel!
@IBOutlet weak var availableTimeSlotCollectionView: UICollectionView!
private var contentWidth: CGFloat = 0
private var contentHeight: CGFloat = 0
var selectedDate: Int64!
var endDate: Int64!
var trainerId: Int64!
var address: String!
var lat: Double!
var lng: Double!
var numberOfWeek: Int!
var trainerSlotInfo: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
initUI()
initializeView()
}
func initCollevtionView() {
self.availableTimeSlotCollectionView.delegate = self
self.availableTimeSlotCollectionView.dataSource = self
self.availableTimeSlotCollectionView.register(UINib(nibName: "TimeSlotsCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "TimeSlotsCollectionViewCell")
}
func initUI() {
self.oneWeekView.backgroundColor = CustomColors().greenColor
self.oneWeeklabel.textColor = .white
self.numberOfWeek = 1
self.endDate = Utils.getDateInMilliSeconds(add: 7, startDate: Date(millisecondsSince1970: Int64(self.selectedDate)))
self.bindData()
editLabel.addTapGestureRecognizer {
let storyboard = UIStoryboard(name: viewControllerState.TAB_BAR, bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "SelectLocationForSubscriptionViewController") as! SelectLocationForSubscriptionViewController
controller.trainerId = self.trainerId
controller.selectedDate = self.selectedDate
self.navigationController?.pushViewController(controller, animated: true)
}
}
func initializeView()
{
continueBookingButton.layer.cornerRadius = 8
continueBookingButton.layer.masksToBounds = true
oneWeekView.layer.cornerRadius = 8
oneWeekView.layer.masksToBounds = true
twoWeekView.layer.cornerRadius = 8
twoWeekView.layer.masksToBounds = true
threeWeekView.layer.cornerRadius = 8
threeWeekView.layer.masksToBounds = true
mondayImage.image = UIImage(named: "Oval Copy 2")
mondayLabel.textColor = UIColor.white
tuesdayImage.image = UIImage(named: "Oval Copy 2")
tuesdayLabel.textColor = UIColor.white
wednesdayImage.image = UIImage(named: "Oval Copy 2")
wednesdayLabel.textColor = UIColor.white
oneWeekView.addTapGestureRecognizer {
self.oneWeekView.backgroundColor = CustomColors().greenColor
self.oneWeeklabel.textColor = .white
self.twoWeekView.backgroundColor = UIColor(red:0.97, green:0.97, blue:0.97, alpha:1.0)
self.twoWeekLabel.textColor = UIColor(red:0.71, green:0.71, blue:0.71, alpha:1.0)
self.threeWeekView.backgroundColor = UIColor(red:0.97, green:0.97, blue:0.97, alpha:1.0)
self.threeWeekLabel.textColor = UIColor(red:0.71, green:0.71, blue:0.71, alpha:1.0)
self.numberOfWeek = 1
self.endDate = Utils.getDateInMilliSeconds(add: 7, startDate: Date(millisecondsSince1970: Int64(self.selectedDate)))
self.bindData()
}
twoWeekView.addTapGestureRecognizer {
self.twoWeekView.backgroundColor = CustomColors().greenColor
self.twoWeekLabel.textColor = .white
self.oneWeekView.backgroundColor = UIColor(red:0.97, green:0.97, blue:0.97, alpha:1.0)
self.oneWeeklabel.textColor = UIColor(red:0.71, green:0.71, blue:0.71, alpha:1.0)
self.threeWeekView.backgroundColor = UIColor(red:0.97, green:0.97, blue:0.97, alpha:1.0)
self.threeWeekLabel.textColor = UIColor(red:0.71, green:0.71, blue:0.71, alpha:1.0)
self.numberOfWeek = 2
self.endDate = Utils.getDateInMilliSeconds(add: 14, startDate: Date(millisecondsSince1970: Int64(self.selectedDate)))
self.bindData()
}
threeWeekView.addTapGestureRecognizer {
self.threeWeekView.backgroundColor = CustomColors().greenColor
self.threeWeekLabel.textColor = .white
self.twoWeekView.backgroundColor = UIColor(red:0.97, green:0.97, blue:0.97, alpha:1.0)
self.twoWeekLabel.textColor = UIColor(red:0.71, green:0.71, blue:0.71, alpha:1.0)
self.oneWeekView.backgroundColor = UIColor(red:0.97, green:0.97, blue:0.97, alpha:1.0)
self.oneWeeklabel.textColor = UIColor(red:0.71, green:0.71, blue:0.71, alpha:1.0)
self.numberOfWeek = 3
self.endDate = Utils.getDateInMilliSeconds(add: 21, startDate: Date(millisecondsSince1970: Int64(self.selectedDate)))
self.bindData()
}
setupDaysTap(view: mondayView, label: mondayLabel, imageView: mondayImage, index: 0)
setupDaysTap(view: tuesdayView, label: tuesdayLabel, imageView: tuesdayImage, index: 1)
setupDaysTap(view: wednesdayView, label: wednesdayLabel, imageView: wednesdayImage, index: 2)
setupDaysTap(view: thursdayView, label: thursdayLabel, imageView: thursdayImage, index: 3)
setupDaysTap(view: fridayView, label: fridayLabel, imageView: fridayImage, index: 4)
setupDaysTap(view: saturdayView, label: saturdayLabel, imageView: saturdayImage, index: 5)
setupDaysTap(view: sundayView, label: sundayLabel, imageView: sundayImage, index: 6)
}
}
extension WeeklyChooseTimeAndDateViewController
{
func setupDaysTap(view: UIView, label: UILabel, imageView: UIImageView, index: Int)
{
view.addTapGestureRecognizer{
self.tapGestureForDay(view: view, label: label, imageView: imageView, index: index)
}
}
func tapGestureForDay(view: UIView, label: UILabel, imageView: UIImageView, index: Int)
{
//deselect view
if (dayArray[index] == true && count > 3)
{
imageView.image = UIImage(named: "Oval Copy 7")
label.textColor = UIColor(hexString: "B5B6B6")
dayArray[index] = false
count -= 1
}
else if (dayArray[index] == false)
{
//select view
imageView.image = UIImage(named: "Oval Copy 2")
label.textColor = UIColor.white
dayArray[index] = true
count += 1
}
if count >= 3 {
self.bindData()
}
}
}
extension WeeklyChooseTimeAndDateViewController: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = availableTimeSlotCollectionView.dequeueReusableCell(withReuseIdentifier: "TimeSlotsCollectionViewCell", for: indexPath) as! TimeSlotsCollectionViewCell
let trainerSlot = self.trainerSlotInfo[indexPath.item]
cell.timeLabel.text = trainerSlot
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 5
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return trainerSlotInfo.count
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = ((collectionView.frame.width - 50) / 3)
return CGSize(width: width,
height: 34)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
let cell = collectionView.cellForItem(at: indexPath)! as! TimeSlotsCollectionViewCell
cell.view.layer.backgroundColor = UIColor(hexString: "20d38f").cgColor
cell.timeLabel.textColor = UIColor(displayP3Red: 255, green: 255, blue: 255, alpha: 1.0)
slotSelected = cell.timeLabel.text!
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath){
let cell = collectionView.cellForItem(at: indexPath)! as! TimeSlotsCollectionViewCell
cell.view.layer.backgroundColor = UIColor(hexString: "f8f8f8").cgColor
cell.timeLabel.textColor = UIColor(hexString: "b5b6b6")
}
}
extension WeeklyChooseTimeAndDateViewController {
func bindData() {
self.dateLabel.text = Utils.sharedInstance.getDateFromDate(serverTime: Date(millisecondsSince1970: Int64(self.selectedDate)))
self.monthLabel.text = Utils.sharedInstance.getMonthFromDate(serverTime: Date(millisecondsSince1970: Int64(self.selectedDate)))
self.startDayLabel.text = Utils.sharedInstance.getWeekFromDate(serverTime: Date(millisecondsSince1970: Int64(self.selectedDate)))
self.showLocationLabel.text = address
self.endDateLabel.text = Utils.sharedInstance.getDateFromDate(serverTime: Date(millisecondsSince1970: Int64(self.endDate)))
self.endMonthLabel.text = Utils.sharedInstance.getMonthFromDate(serverTime: Date(millisecondsSince1970: Int64(self.endDate)))
self.endDayLabel.text = Utils.sharedInstance.getWeekFromDate(serverTime: Date(millisecondsSince1970: Int64(self.endDate)))
getSlots()
}
func getSlots() {
let clientId = UserDefaults.standard.value(forKey: ApiParametersConstant.USER_ID) as? Int64
var index: Int = 1
for value in dayArray {
if value {
selectedDaysInWeek.append(index)
}
index = index + 1
}
NetworkManager.makeRequest(HttpRouter.getTrainerSlotsForProgram(trainerId: trainerId, startDate: selectedDate, endDate: endDate, clientId: clientId!, weeklyNumber: selectedDaysInWeek), message: processingBar.PROCESSING_MESSAGE, showProgress: false)
.onSuccess { (response: SlotsResponse) in
self.trainerSlotInfo = response.data
self.selectedDaysInWeek.removeAll()
if(self.trainerSlotInfo != nil) {
self.initCollevtionView()
}
}
.onFailure { error in
print(error)
}.onComplete { _ in
RappleActivityIndicatorView.stopAnimation()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment