Skip to content

Instantly share code, notes, and snippets.

@amixpal
Created November 11, 2019 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amixpal/d0ec94ccd8c20d0fd9a31a80e7ed4351 to your computer and use it in GitHub Desktop.
Save amixpal/d0ec94ccd8c20d0fd9a31a80e7ed4351 to your computer and use it in GitHub Desktop.
//
// DashboardViewController.swift
// Client
//
// Created by Heeral on 7/9/19.
// Copyright © 2019 heeral. All rights reserved.
//
import Foundation
import UIKit
import RappleProgressHUD
class DashboardViewController: UIViewController
{
@IBOutlet weak var titleView: UIView!
@IBOutlet weak var upcomingAppointmentView: UIView!
@IBOutlet weak var popularProgramView: UIView!
@IBOutlet weak var popularTrainerView: UIView!
@IBOutlet weak var upComingAppointmentCollectionView: UICollectionView!
@IBOutlet weak var exerciseCollectionView: UICollectionView!
@IBOutlet weak var popularTrainersCollectionView: UICollectionView!
@IBOutlet weak var popularProgramsCollectionView: UICollectionView!
@IBOutlet weak var showAllView: UIView!
@IBOutlet weak var popularVideosProgramView: UIView!
@IBOutlet weak var popularEBookView: UIView!
@IBOutlet weak var popularVideosCollectionView: UICollectionView!
@IBOutlet weak var popularEBookCollectionView: UICollectionView!
var dashBoard: [DashboardResponseData]! = []
var popularTrainer: [PopularTrainers]! = []
var popularPrograms: [PopularProgram]! = []
var popularVideosProgram: [PopularProgram] = []
var popularEBooksProgram: [PopularProgram] = []
var popularTitles: [PopularWorkout]! = []
var upcomingSession: [UpcomingAppointmentForTrainerProfile]! = []
private var contentWidth: CGFloat = 0
private var contentHeight: CGFloat = 0
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.navigationController?.navigationBar.clipsToBounds = false
self.navigationController?.navigationBar.shadowImage = UIColor(red: 215/255, green: 215/255, blue: 215/255, alpha: 1.0).image(CGSize(width: self.view.frame.width, height: 1))
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.navigationBar.prefersLargeTitles = false
self.navigationController?.setNavigationBarHidden(true, animated: false)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.getDashBoardInfo()
self.navigationController?.setNavigationBarHidden(false , animated: false)
showAllView.addTapGestureRecognizer {
self.openUpcomingSessionFullView()
}
let button = UIButton(type: .custom)
button.setTitleColor(UIColor(red:0.28, green:0.28, blue:0.28, alpha:1.0), for: .normal)
let name: String = (UserDefaults.standard.value(forKey: ApiParametersConstant.USER_NAME) as? String)!
let fullName = name.components(separatedBy: " ")
button.setTitle("Welcome " + fullName[0] + " !", for: .normal)
button.titleLabel?.font = UIFont(name: "Gilroy-Bold", size: 22)
button.frame = CGRect.init(x: 0, y: 0, width: 30, height: 30)
let barButton = UIBarButtonItem(customView: button)
self.navigationItem.leftBarButtonItem = barButton
let searchButton = UIButton(type: .custom)
searchButton.setImage(UIImage(named: "ic_search_new"), for: .normal)
searchButton.addTarget(self, action: #selector(searchButton(_:)), for: .touchUpInside)
searchButton.frame = CGRect.init(x: 0, y: 0, width: 30, height: 30)
let searchBarButton = UIBarButtonItem(customView: searchButton)
navigationItem.rightBarButtonItems = [searchBarButton]
}
@objc private func searchButton(_ sender: Any) {
let storyboard = UIStoryboard(name: viewControllerState.TAB_BAR, bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "SearchViewController") as! SearchViewController
self.navigationController?.pushViewController(controller, animated: true)
}
func setupExerciseCollectionView()
{
self.exerciseCollectionView.delegate = self
self.exerciseCollectionView.dataSource = self
self.exerciseCollectionView.register(UINib(nibName: "ExerciseCellCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "ExerciseCellCollectionViewCell")
DispatchQueue.main.async{self.exerciseCollectionView.reloadData()}
exerciseCollectionView.contentInset.left = 24
setUpGridView(exerciseCollectionView, width: 108, height: 64, spacing: 8)
}
func setupUpcomingCollectionView()
{
self.upComingAppointmentCollectionView.delegate = self
self.upComingAppointmentCollectionView.dataSource = self
self.upComingAppointmentCollectionView.register(UINib(nibName: "UpComingAppointmentCell", bundle: nil), forCellWithReuseIdentifier: "UpComingAppointmentCell")
upComingAppointmentCollectionView.contentInset.left = 24
self.setUpGridView(self.upComingAppointmentCollectionView, width: 327, height: 156, spacing: 12)
}
func openUpcomingSessionFullView(){
let chatsVc : UpcomingSessionViewController = UIStoryboard(name: viewControllerState.TAB_BAR, bundle: nil).instantiateViewController(withIdentifier: "UpcomingSessionViewController") as! UpcomingSessionViewController
chatsVc.upcomingSessionList = self.upcomingSession
self.navigationController?.pushViewController(chatsVc, animated: true)
}
func setupPopularTrainersCollectionView()
{
self.popularTrainersCollectionView.delegate = self
self.popularTrainersCollectionView.dataSource = self
self.popularTrainersCollectionView.register(UINib(nibName: nibParameter.POPULAR_TRAINER_LIST, bundle: nil), forCellWithReuseIdentifier: nibParameter.POPULAR_TRAINER_LIST)
popularTrainersCollectionView.contentInset.left = 24
setUpGridView(popularTrainersCollectionView, width: 146, height: 222, spacing: 12)
}
func setuppopularProgramsCollectionView()
{
self.popularProgramsCollectionView.delegate = self
self.popularProgramsCollectionView.dataSource = self
self.popularProgramsCollectionView.register(UINib(nibName: "PopularProgramsCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "PopularProgramsCollectionViewCell")
DispatchQueue.main.async{self.popularProgramsCollectionView.reloadData()}
popularProgramsCollectionView.contentInset.left = 24
setUpGridView(popularProgramsCollectionView, width: 327, height: 156, spacing: 12)
}
func setuppopularEBookCollectionView()
{
self.popularEBookCollectionView.delegate = self
self.popularEBookCollectionView.dataSource = self
self.popularEBookCollectionView.register(UINib(nibName: "PopularProgramsCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "PopularProgramsCollectionViewCell")
DispatchQueue.main.async{self.popularEBookCollectionView.reloadData()}
popularEBookCollectionView.contentInset.left = 24
setUpGridView(popularEBookCollectionView, width: 327, height: 156, spacing: 12)
}
func setupPopularVideosView() {
self.popularVideosCollectionView.delegate = self
self.popularVideosCollectionView.dataSource = self
self.popularVideosCollectionView.register(UINib(nibName: "PopularProgramsCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "PopularProgramsCollectionViewCell")
DispatchQueue.main.async{self.popularVideosCollectionView.reloadData()}
popularVideosCollectionView.contentInset.left = 24
setUpGridView(popularVideosCollectionView, width: 327, height: 156, spacing: 12)
}
func setUpGridView(_ collectionView:UICollectionView, width:Int, height:Int, spacing: CGFloat){
let flow = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
flow.minimumInteritemSpacing = spacing
flow.scrollDirection = .horizontal
flow.itemSize = CGSize.init(width: width, height: height)
}
}
extension DashboardViewController: UICollectionViewDelegate, UICollectionViewDataSource
{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
print (collectionView)
print (popularTitles.count)
switch (collectionView)
{
case upComingAppointmentCollectionView:
return upcomingSession.count
case exerciseCollectionView:
return popularTitles.count
case popularTrainersCollectionView:
return self.popularTrainer.count
case popularProgramsCollectionView:
return self.popularPrograms.count
case popularVideosCollectionView:
return self.popularVideosProgram.count
case popularEBookCollectionView:
return self.popularEBooksProgram.count
default:
return 0
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == popularTrainersCollectionView {
let cell = popularTrainersCollectionView.dequeueReusableCell(withReuseIdentifier: nibParameter.POPULAR_TRAINER_LIST, for: indexPath) as! PopularTrainerCollectionViewCell
let trainer = self.popularTrainer[indexPath.item]
cell.trainerNameLabel.text = trainer.trainerName
cell.imageView.sd_setImage(with: URL(string: trainer.tarinerProfilePictureUrl != nil ? trainer.tarinerProfilePictureUrl! : ""), placeholderImage: UIImage.init(named: "ic_camera_placeholder"), options: .handleCookies, completed: nil)
return cell
} else if collectionView == popularProgramsCollectionView {
let cell = popularProgramsCollectionView.dequeueReusableCell(withReuseIdentifier: nibParameter.POPULAR_PROGRAM_LIST, for: indexPath) as! PopularProgramsCollectionViewCell
let popularProgram = self.popularPrograms[indexPath.item]
cell.workOutNameLabel.text = popularProgram.programName
cell.numberOfTrainersLabel.text = popularProgram.trainerName
cell.imageView.sd_setImage(with: URL(string: popularProgram.imageUrl != nil ? popularProgram.imageUrl! : ""), placeholderImage: UIImage.init(named: "ic_camera_placeholder"), options: .handleCookies, completed: nil)
if popularProgram.typeOfProgram == 0 {
cell.statusView.isHidden = true
cell.numberOfWeekLabel.text = popularProgram.numberOfWeeks
if popularProgram.isNutritionAvail {
cell.statusLabel.text = "Nutrition Included"
let yourImage: UIImage = UIImage(named: "ic_meal_plan")!
cell.statusImage.image = yourImage
}
}
return cell
} else if collectionView == popularEBookCollectionView {
let cell = popularEBookCollectionView.dequeueReusableCell(withReuseIdentifier: nibParameter.POPULAR_PROGRAM_LIST, for: indexPath) as! PopularProgramsCollectionViewCell
let popularProgram = self.popularEBooksProgram[indexPath.item]
cell.workOutNameLabel.text = popularProgram.programName
cell.numberOfTrainersLabel.text = popularProgram.trainerName
cell.imageView.sd_setImage(with: URL(string: popularProgram.imageUrl != nil ? popularProgram.imageUrl! : ""), placeholderImage: UIImage.init(named: "ic_camera_placeholder"), options: .handleCookies, completed: nil)
if popularProgram.typeOfProgram == 1 {
cell.statusView.isHidden = false
cell.statusImage.isHidden = false
cell.statusLabel.text = "PDF"
let yourImage: UIImage = UIImage(named: "ic_offline_pdf")!
cell.statusImage.image = yourImage
if popularProgram.isSaved {
cell.numberOfWeekLabel.text = "Saved"
} else {
cell.numberOfWeekLabel.isHidden = true
}
}
} else if collectionView == popularVideosCollectionView {
let cell = popularVideosCollectionView.dequeueReusableCell(withReuseIdentifier: nibParameter.POPULAR_PROGRAM_LIST, for: indexPath) as! PopularProgramsCollectionViewCell
let popularProgram = self.popularVideosProgram[indexPath.item]
cell.workOutNameLabel.text = popularProgram.programName
cell.numberOfTrainersLabel.text = popularProgram.trainerName
cell.imageView.sd_setImage(with: URL(string: popularProgram.imageUrl != nil ? popularProgram.imageUrl! : ""), placeholderImage: UIImage.init(named: "ic_camera_placeholder"), options: .handleCookies, completed: nil)
if popularProgram.typeOfProgram == 2 {
cell.statusView.isHidden = false
cell.statusLabel.text = "Meal Plan"
let yourImage: UIImage = UIImage(named: "ic_meal_plan")!
cell.statusImage.image = yourImage
if popularProgram.isSaved {
cell.numberOfWeekLabel.text = "Saved"
} else {
cell.numberOfWeekLabel.isHidden = true
}
}
return cell
} else if collectionView == exerciseCollectionView {
let cell = exerciseCollectionView.dequeueReusableCell(withReuseIdentifier: nibParameter.TITLE_LIST, for: indexPath) as! ExerciseCellCollectionViewCell
let titleInfo = self.popularTitles[indexPath.item]
cell.exerciseNameLabel.text = titleInfo.workoutName
if let imageUrl = titleInfo.imageUrl {
cell.set(imageUrl: imageUrl)
}
return cell
} else if collectionView == upComingAppointmentCollectionView {
let cell = upComingAppointmentCollectionView.dequeueReusableCell(withReuseIdentifier: "UpComingAppointmentCell", for: indexPath) as! UpComingAppointmentCell
let appointment = self.upcomingSession[indexPath.item]
let bookingTime = Date(millisecondsSince1970: Int64(appointment.bookingTime))
let bookingDateFormat = Utils.sharedInstance.getDayFromDate(serverTime: bookingTime)
let timeFormat = Utils.sharedInstance.getTimeFromDate(serverTime: bookingTime)
cell.classNameLabel.text = appointment.trainingName
cell.personNameLabel.text = "with " + appointment.clientName + " - " + bookingDateFormat + " , " + timeFormat
cell.backgroundImage.sd_setImage(with: URL(string: appointment.imageUrl != nil ? appointment.imageUrl! : ""), placeholderImage: UIImage.init(named: "ic_camera_placeholder"), options: .handleCookies, completed: nil)
if appointment.status == 1 {
cell.timeLabel.text = "Upcoming"
} else if appointment.status == 2 {
cell.timeLabel.text = "OnGoing"
} else if appointment.status == 5 {
cell.timeLabel.text = "OnGoing"
} else if appointment.status == 6 {
cell.timeLabel.text = "Saved"
}
return cell
}
return UICollectionViewCell()
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == self.popularProgramsCollectionView {
let data = self.popularPrograms[indexPath.item]
let storyboard = UIStoryboard(name: viewControllerState.TAB_BAR, bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "ProgramDetailInfoViewController") as! ProgramDetailInfoViewController
controller.programId = data.programId
self.navigationController?.pushViewController(controller, animated: true)
}
if collectionView == self.popularVideosCollectionView{
let data = self.popularVideosProgram[indexPath.item]
let storyboard = UIStoryboard(name: viewControllerState.TAB_BAR, bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "OfflineProgramViewController") as! OfflineProgramViewController
controller.programId = data.programId
self.navigationController?.pushViewController(controller, animated: true)
}
if collectionView == self.popularEBookCollectionView {
let data = self.popularEBooksProgram[indexPath.item]
let storyboard = UIStoryboard(name: viewControllerState.TAB_BAR, bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "OfflineProgramViewController") as! OfflineProgramViewController
controller.programId = data.programId
self.navigationController?.pushViewController(controller, animated: true)
}
if collectionView == self.popularTrainersCollectionView {
let data = self.popularTrainer[indexPath.item]
let storyboard = UIStoryboard(name: viewControllerState.TAB_BAR, bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "TrainerProfileViewController") as! TrainerProfileViewController
controller.trainerId = data.trainerId
self.navigationController?.pushViewController(controller, animated: true)
}
if collectionView == self.upComingAppointmentCollectionView {
let data = self.upcomingSession[indexPath.item]
if data.status == 1 {
let storyboard = UIStoryboard(name: viewControllerState.NOTIFICATION_TAB, bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "UpcomingAppointmentDetailViewController") as! UpcomingAppointmentDetailViewController
controller.appointmentId = data.appointmentId
self.navigationController?.pushViewController(controller, animated: true)
} else if data.status == 5 {
let storyboard = UIStoryboard(name: viewControllerState.NOTIFICATION_TAB, bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "YourTainerIsArrivingViewController") as! YourTainerIsArrivingViewController
controller.appointmentId = data.appointmentId.string
controller.workoutName = data.trainingName
controller.trainerId = data.trainerId.string
controller.trainerProfilePictureUrl = data.trainerProfilePictureUrl
controller.address = data.address
controller.trainerName = data.clientName
controller.notificationTrainerLat = String(data.trainerLatitude)
controller.notificationTrainerLong = String(data.trainerLongitude)
self.navigationController?.pushViewController(controller, animated: true)
} else if data.status == 2 {
let storyboard = UIStoryboard(name:
viewControllerState.NOTIFICATION_TAB, bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "NotificationOnGoingWorkoutViewController") as! NotificationOnGoingWorkoutViewController
controller.appointmentId = data.appointmentId.string
controller.trainerId = data.trainerId.string
controller.startTime = data.bookingTime.string
controller.workoutImageData = data.imageUrl
controller.workoutNameDetail = data.trainingName
controller.trainerName = data.clientName
self.navigationController?.pushViewController(controller, animated: true)
} else if data.status == 6 {
let storyboard = UIStoryboard(name: viewControllerState.TAB_BAR, bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "PdfViewController") as! PdfViewController
controller.pdfUrl = data.pdfurl
self.navigationController?.pushViewController(controller, animated: true)
}
}
if collectionView == self.exerciseCollectionView {
let data = self.popularTitles[indexPath.item]
let storyboard = UIStoryboard(name:
viewControllerState.TAB_BAR, bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "SearchViewController") as! SearchViewController
controller.isComingFromDashboard = true
controller.searchContentFromDashboard = data.workoutName
self.navigationController?.pushViewController(controller, animated: true)
}
}
}
extension DashboardViewController
{
func getDashBoardInfo(){
let userIdInfo = UserDefaults.standard.value(forKey: ApiParametersConstant.USER_ID)
let currentDate = Utils.sharedInstance.getTodayDateOnly()
NetworkManager.makeRequest(HttpRouter.getDashboardInfo(userId: userIdInfo as! Int64 , currentDate: currentDate), message: processingBar.PROCESSING_MESSAGE, showProgress: false)
.onSuccess { (response: DashboardResponse) in
if response.data.popularTrainer != nil && response.data.popularTrainer.count > 0{
self.popularTrainer = response.data.popularTrainer
self.setupPopularTrainersCollectionView()
} else {
self.popularTrainerView.isHidden = true
}
if response.data.popularPrograms != nil && response.data.popularPrograms.count > 0{
self.popularPrograms = response.data.popularPrograms
self.setuppopularProgramsCollectionView()
} else {
self.popularProgramView.isHidden = true
}
if response.data.popularMealPlan != nil && response.data.popularMealPlan.count > 0 {
self.popularEBooksProgram = response.data.popularMealPlan
self.setuppopularEBookCollectionView()
} else {
self.popularEBookView.isHidden = true
}
if response.data.popularVideos != nil && response.data.popularVideos.count > 0 {
self.popularVideosProgram = response.data.popularVideos
self.setupPopularVideosView()
} else {
self.popularVideosProgramView.isHidden = true
}
if response.data.PopularTitles != nil && response.data.PopularTitles.count > 0{
self.popularTitles = response.data.PopularTitles
self.setupExerciseCollectionView()
} else {
self.titleView.isHidden = true
}
if response.data.upcomingEvents != nil && response.data.upcomingEvents.count > 0
{
self.upcomingSession = response.data.upcomingEvents
self.setupUpcomingCollectionView()
} else {
self.upcomingAppointmentView.isHidden = true
}
}
.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