Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@chashmeetsingh
Last active April 13, 2018 15:24
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 chashmeetsingh/dae5cac0dde88aaba20007e2e40a619e to your computer and use it in GitHub Desktop.
Save chashmeetsingh/dae5cac0dde88aaba20007e2e40a619e to your computer and use it in GitHub Desktop.
//
// DrawerViewController.swift
// GPS Info
//
// Created by Chashmeet Singh on 16/03/18.
// Copyright © 2018 Chashmeet Singh. All rights reserved.
//
import UIKit
import KYDrawerController
import CoreData
class DrawerViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
let draweritems = ["Logout"]
let drawerIcons = ["ic_arrow_back"]
private let cellId = "cellId"
private let headerViewId = "headerViewId"
var appDelegate: AppDelegate?
var menuButton: UIButton?
override func viewDidLoad() {
super.viewDidLoad()
setupView()
appDelegate = UIApplication.shared.delegate as? AppDelegate
}
func setupView() {
collectionView?.backgroundColor = UIColor.white
collectionView?.alwaysBounceVertical = true
collectionView?.register(DrawerCollectionViewCell.self, forCellWithReuseIdentifier: cellId)
collectionView?.register(HeaderCollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerViewId)
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return draweritems.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath as IndexPath) as! DrawerCollectionViewCell
let text = draweritems[indexPath.row]
cell.backgroundColor = .white
let imageName = drawerIcons[indexPath.row]
cell.label.text = text
cell.imageView.image = UIImage(named: imageName)?.withRenderingMode(.alwaysTemplate)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width, height: 36)
}
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerViewId, for: indexPath) as! HeaderCollectionReusableView
headerView.usernameLabel.text = Client.sharedInstance.user!.username
return headerView
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: collectionView.frame.width, height: 120)
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
switch (indexPath.row) {
case self.draweritems.index(of: "Logout")!:
logout()
break
default:
break
}
}
func logout() {
let alertController = UIAlertController(title: "Are you sure?", message: nil, preferredStyle: .alert)
let logoutAction = UIAlertAction(title: NSLocalizedString("Logout", comment: ""), style: .default, handler: { (pAlert) in
Client.sharedInstance.logout()
self.deleteSOSAlerts()
self.dismiss(animated: true, completion: nil)
})
let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .default, handler: { (pAlert) in
//
})
alertController.addAction(cancelAction)
alertController.addAction(logoutAction)
self.present(alertController, animated: true, completion: nil)
}
@objc func openDrawer() {
if let drawerController = parent as? KYDrawerController {
drawerController.setDrawerState(.closed, animated: true)
}
}
func deleteSOSAlerts() {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "SOSAlert")
do {
let result = try context.fetch(request)
//alerts = (result as! [NSManagedObject])
for alert in result as! [NSManagedObject] {
context.delete(alert)
}
try context.save()
} catch {
print("Failed")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment