Skip to content
This repository has been archived by the owner on Mar 19, 2023. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/Ragyal-ImageCaching' into Truste…
Browse files Browse the repository at this point in the history
…t-Chat
  • Loading branch information
Trustet committed Jun 13, 2019
2 parents 25c8776 + b27eb93 commit 9c39a87
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 109 deletions.
12 changes: 12 additions & 0 deletions PulsePartner.xcodeproj/project.pbxproj
Expand Up @@ -35,6 +35,7 @@
BCAE20F02271F9EC00898553 /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = BCAE20ED2271F9EC00898553 /* GoogleService-Info.plist */; };
BCAE20F12271F9EC00898553 /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = BCAE20ED2271F9EC00898553 /* GoogleService-Info.plist */; };
BCAE20F22271F9EC00898553 /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = BCAE20ED2271F9EC00898553 /* GoogleService-Info.plist */; };
BCE8227422B2492E00FF1B01 /* ImageManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = BCE8227322B2492E00FF1B01 /* ImageManager.swift */; };
BCECF1342256241D00AFA163 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = BCECF1332256241D00AFA163 /* AppDelegate.swift */; };
BCECF1362256241D00AFA163 /* LoginViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BCECF1352256241D00AFA163 /* LoginViewController.swift */; };
BCECF1392256241D00AFA163 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = BCECF1372256241D00AFA163 /* Main.storyboard */; };
Expand Down Expand Up @@ -148,6 +149,7 @@
BC4B20CC22844C0600F7B1FF /* UITextField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UITextField.swift; sourceTree = "<group>"; };
BC4B20CE2284603D00F7B1FF /* PreferencesViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesViewController.swift; sourceTree = "<group>"; };
BCAE20ED2271F9EC00898553 /* GoogleService-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "GoogleService-Info.plist"; sourceTree = "<group>"; };
BCE8227322B2492E00FF1B01 /* ImageManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageManager.swift; sourceTree = "<group>"; };
BCECF1302256241D00AFA163 /* PulsePartner.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = PulsePartner.app; sourceTree = BUILT_PRODUCTS_DIR; };
BCECF1332256241D00AFA163 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
BCECF1352256241D00AFA163 /* LoginViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -297,6 +299,14 @@
path = Location;
sourceTree = "<group>";
};
BCE8227522B2493400FF1B01 /* Image */ = {
isa = PBXGroup;
children = (
BCE8227322B2492E00FF1B01 /* ImageManager.swift */,
);
path = Image;
sourceTree = "<group>";
};
BCECF1272256241D00AFA163 = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -389,6 +399,7 @@
BCFF5FF522737CC7005848CE /* Model */ = {
isa = PBXGroup;
children = (
BCE8227522B2493400FF1B01 /* Image */,
3E02EA9122A92A7700464C93 /* Chat */,
BC31BB1122AC6F56008DD4DF /* Firebase */,
3E02EA8E228EF0A700464C93 /* Match */,
Expand Down Expand Up @@ -802,6 +813,7 @@
3E14BBA3227C7DB700714E2E /* LocationManager.swift in Sources */,
3EF79E6C22612B5800AC1C45 /* PermissionsViewController.swift in Sources */,
BCECF1362256241D00AFA163 /* LoginViewController.swift in Sources */,
BCE8227422B2492E00FF1B01 /* ImageManager.swift in Sources */,
3E02EA9322A92A8900464C93 /* ChatManager.swift in Sources */,
3ED600AA22B1AAF000091788 /* Chat.xcdatamodeld in Sources */,
BC42FE3322AD128F0093B763 /* MatchData.swift in Sources */,
Expand Down
4 changes: 2 additions & 2 deletions PulsePartner/Controller/MainViewController.swift
Expand Up @@ -24,11 +24,11 @@ class MainViewController: UIViewController {
updateImage(user: user)
}
UserManager.sharedInstance.addObserver(self)

self.tableView.delegate = self
self.tableView.dataSource = self
MatchManager.sharedInstance.addObserver(self)

let img = UIImage()
self.navigationController?.navigationBar.shadowImage = img
self.navigationController?.navigationBar.setBackgroundImage(img, for: UIBarMetrics.default)
Expand Down
136 changes: 31 additions & 105 deletions PulsePartner/Controller/RegisterFormViewController.swift
Expand Up @@ -8,10 +8,8 @@

import UIKit
import CropViewController
import Photos

class RegisterFormViewController: UIViewController,
UINavigationControllerDelegate, UIImagePickerControllerDelegate, CropViewControllerDelegate {
class RegisterFormViewController: UIViewController {

@IBOutlet weak var pictureButton: UIButton!
@IBOutlet weak var emailInput: UITextField!
Expand All @@ -31,107 +29,16 @@ UINavigationControllerDelegate, UIImagePickerControllerDelegate, CropViewControl
super.viewDidLoad()

// Do any additional setup after loading the view.
self.imagePicker = UIImagePickerController()
self.imagePicker.delegate = self

self.hideKeyboardWhenTappedAround()
}

@IBAction func setFitnessLevel(_ sender: UISlider) {
fitnessLevelLabel.text = String(Int(sender.value))
fitnessLevel = Int(sender.value)
}

@IBAction func onPictureButtonClick(_ sender: Any) {
let alertController = UIAlertController(title: nil,
message: "Where do you want to select the picture?",
preferredStyle: .actionSheet)

let cameraAction = UIAlertAction(title: "Camera", style: .default, handler: { (alert: UIAlertAction!) -> Void in
if !UIImagePickerController.isSourceTypeAvailable(.camera) {

let alertController = UIAlertController.init(title: nil,
message: "Device has no camera.",
preferredStyle: .alert)

let okAction = UIAlertAction.init(title: "Alright", style: .default, handler: {(_: UIAlertAction!) in
})

alertController.addAction(okAction)
self.present(alertController,
animated: true,
completion: nil)
} else {
self.imagePicker.sourceType = .camera

self.present(self.imagePicker,
animated: true,
completion: nil)
}
})

let libraryAction = UIAlertAction(title: "Library", style: .default, handler: { (_: UIAlertAction!) -> Void in
let photoAuthorizationStatus = PHPhotoLibrary.authorizationStatus()
switch photoAuthorizationStatus {
case .notDetermined:
PHPhotoLibrary.requestAuthorization({ (newStatus) in
if newStatus != PHAuthorizationStatus.authorized {
print("User has denied the permission.")
return
}
})
print("It is not determined until now")
case .restricted:
print("User do not have access to photo album.")
return
case .denied:
print("User has denied the permission.")
return
case .authorized:
print("Access is granted by user")
@unknown default:
print("Unknown state.")
return
}
self.imagePicker.sourceType = .savedPhotosAlbum

self.present(self.imagePicker, animated: true, completion: nil)
})

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: { (_: UIAlertAction!) -> Void in
// Do something here upon cancellation.
})

alertController.addAction(cameraAction)
alertController.addAction(libraryAction)
alertController.addAction(cancelAction)

self.present(alertController, animated: true, completion: nil)
}

func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
picker.dismiss(animated: true, completion: nil)
guard let image: UIImage = info[.originalImage] as? UIImage else {
return
}
presentCropViewController(withImage: image)
}

func presentCropViewController(withImage image: UIImage) {
let cropViewController = CropViewController(croppingStyle: .circular, image: image)
cropViewController.delegate = self
self.present(cropViewController, animated: true, completion: nil)
ImageManager.handleImageUpload(self)
}

@objc func cropViewController(_ cropViewController: CropViewController,
didCropToCircularImage image: UIImage,
withRect cropRect: CGRect,
angle: Int) {
// 'image' is the newly cropped, circular version of the original image
cropViewController.dismiss(animated: true, completion: nil)
pictureButton.setImage(image, for: .normal)
self.image = image
@IBAction func setFitnessLevel(_ sender: UISlider) {
fitnessLevelLabel.text = String(Int(sender.value))
fitnessLevel = Int(sender.value)
}

@IBAction func onRegisterButtonClick(_ sender: UIButton) {
Expand Down Expand Up @@ -166,6 +73,7 @@ UINavigationControllerDelegate, UIImagePickerControllerDelegate, CropViewControl
guard let email = emailInput.text, email.isValidEmail else {
// emailInput.layer.borderColor = UIColor.red.cgColor
// emailInput.layer.borderWidth = 1.0
print("Email: Ungültige Email")
return
}
guard let password = passwordInput.text else {
Expand All @@ -190,15 +98,33 @@ UINavigationControllerDelegate, UIImagePickerControllerDelegate, CropViewControl
preferences: genderSettings.preferences)
completion(registerData)
}
}

/*
// MARK: - Navigation
extension RegisterFormViewController: UINavigationControllerDelegate, UIImagePickerControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
picker.dismiss(animated: true, completion: nil)
guard let image: UIImage = info[.originalImage] as? UIImage else {
return
}
presentCropViewController(withImage: image)
}

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
func presentCropViewController(withImage image: UIImage) {
let cropViewController = CropViewController(croppingStyle: .circular, image: image)
cropViewController.delegate = self
self.present(cropViewController, animated: true, completion: nil)
}
*/
}

extension RegisterFormViewController: CropViewControllerDelegate {
@objc func cropViewController(_ cropViewController: CropViewController,
didCropToCircularImage image: UIImage,
withRect cropRect: CGRect,
angle: Int) {
// 'image' is the newly cropped, circular version of the original image
cropViewController.dismiss(animated: true, completion: nil)
self.pictureButton.setImage(image, for: .normal)
self.image = image
}
}
79 changes: 77 additions & 2 deletions PulsePartner/Model/Image/ImageManager.swift
Expand Up @@ -6,9 +6,84 @@
// Copyright © 2019 PulsePartner. All rights reserved.
//

import Foundation
import UIKit
import Photos
import CropViewController

class ImageManager {
static let sharedInstance = ImageManager()
static func handleImageUpload(_ sender: RegisterFormViewController) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = sender

let alertController = UIAlertController(title: nil,
message: "Where do you want to select the picture?",
preferredStyle: .actionSheet)

let cameraAction = UIAlertAction(title: "Camera", style: .default) { (alert: UIAlertAction!) -> Void in
if !UIImagePickerController.isSourceTypeAvailable(.camera) {

let alertController = UIAlertController.init(title: nil,
message: "Device has no camera.",
preferredStyle: .alert)

let okAction = UIAlertAction.init(title: "Alright", style: .default, handler: {(_: UIAlertAction!) in
})

alertController.addAction(okAction)
sender.present(alertController,
animated: true,
completion: nil)
} else {
imagePicker.sourceType = .camera

sender.present(imagePicker,
animated: true,
completion: nil)
}
}

let libraryAction = UIAlertAction(title: "Library", style: .default) { (_: UIAlertAction!) -> Void in
if requestLibraryAuthentication() {
imagePicker.sourceType = .savedPhotosAlbum
sender.present(imagePicker, animated: true, completion: nil)
}
}

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_: UIAlertAction!) -> Void in
// Do something here upon cancellation.
}

alertController.addAction(cameraAction)
alertController.addAction(libraryAction)
alertController.addAction(cancelAction)

sender.present(alertController, animated: true, completion: nil)
}

static private func requestLibraryAuthentication() -> Bool {
let photoAuthorizationStatus = PHPhotoLibrary.authorizationStatus()
switch photoAuthorizationStatus {
case .notDetermined:
print("It is not determined until now. Requesting ...")
PHPhotoLibrary.requestAuthorization({ (newStatus) in
if newStatus != PHAuthorizationStatus.authorized {
print("User has denied the permission.")
return
}
})
return requestLibraryAuthentication()
case .restricted:
print("User do not have access to photo album.")
return false
case .denied:
print("User has denied the permission.")
return false
case .authorized:
print("Access is granted by user")
return true
@unknown default:
print("Unknown state.")
return false
}
}
}

0 comments on commit 9c39a87

Please sign in to comment.