Last active
March 23, 2020 08:15
-
-
Save ahmedAlmasri/d4deba1378b6fa34f920bc5217f06a4c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// BaseUploadOperation.swift | |
// UHive | |
// | |
// Created by Ahmad AlMasri on 6/27/19. | |
// Copyright © 2019 Genie9. All rights reserved. | |
// | |
import Foundation | |
class BaseOperation: Operation { | |
// MARK:- Declarations | |
private var pauseCondition:NSCondition! | |
var isStarted = false | |
var paused:Bool = false | |
var collectionCompleted:NSCondition | |
private var _executing = false { | |
willSet { | |
willChangeValue(forKey: "isExecuting") | |
} | |
didSet { | |
didChangeValue(forKey: "isExecuting") | |
} | |
} | |
private var _finished = false { | |
willSet { | |
willChangeValue(forKey: "isFinished") | |
} | |
didSet { | |
didChangeValue(forKey: "isFinished") | |
} | |
} | |
override var isExecuting: Bool { | |
return _executing | |
} | |
override var isFinished: Bool { | |
return _finished | |
} | |
// MARK:- Inits | |
override init() { | |
pauseCondition = NSCondition() | |
collectionCompleted = NSCondition() | |
} | |
// MARK:- Main Transactions | |
override func cancel() { | |
super.cancel() | |
} | |
func pause() { | |
lockCondition(true) | |
} | |
func resume() { | |
lockCondition(false) | |
} | |
func waitIfPaused() { | |
self.pauseCondition.lock() | |
while (paused) { | |
self.pauseCondition.wait() | |
} | |
self.pauseCondition.unlock() | |
} | |
func executing(_ executing:Bool){ | |
_executing = executing | |
} | |
func finish(_ finished:Bool){ | |
_finished = finished | |
} | |
// MARK:- Local Functionality | |
private func lockCondition(_ pause:Bool){ | |
self.pauseCondition.lock() | |
self.paused = pause | |
self.pauseCondition.signal() | |
self.pauseCondition.unlock() | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PhotoOperation:TestOP { | |
var albumName = "" | |
var photoAssets = PHFetchResult<PHAsset>() | |
var assetCollection: PHAssetCollection? | |
var albumFound = false | |
let disQ = DispatchGroup() | |
override func main() { | |
guard isCancelled == false else { | |
finish(true) | |
return | |
} | |
// executing(true) | |
//getSyncPhotos() | |
// disQ.enter() | |
//req() | |
// getSyncPhotos() | |
// DispatchQueue.global().async { | |
// for i in 0..<1000000{ | |
// print(i) | |
// | |
// | |
// } | |
// | |
// self.disQ.leave() | |
// } | |
// | |
// disQ.enter() | |
// DispatchQueue.global().async { | |
// for i in 0..<100{ | |
// print("AAAAAAAAAA \(i)") | |
// | |
// } | |
// | |
// self.disQ.leave() | |
// } | |
// | |
// disQ.notify(queue: DispatchQueue.main) { | |
// print("call disQ") | |
// self.executing(false) | |
// self.finish(true) | |
// | |
// } | |
for i in 0...10 { | |
print("Fetching Photo \(i)" ) | |
self.pausedBlock() | |
guard isCancelled == false else { | |
finish(true) | |
return | |
} | |
sleep(1) | |
} | |
executing(false) | |
finish(true) | |
} | |
// | |
// } | |
func req(){ | |
PHPhotoLibrary.requestAuthorization { (status) in | |
switch status { | |
case .authorized: | |
print("Good to proceed") | |
// let fetchOptions = PHFetchOptions() | |
// let allPhotos = PHAsset.fetchAssets(with: .image, options: fetchOptions) | |
// print("Found \(allPhotos.count) images") | |
self.getSyncPhotos() | |
case .denied, .restricted: | |
print("Not allowed") | |
case .notDetermined: | |
print("Not determined yet") | |
} | |
} | |
} | |
func getSyncPhotos() | |
{ | |
self.albumName = String(1) | |
//let fetchOptions = PHFetchOptions() | |
// fetchOptions.predicate = NSPredicate(format: "title = %@", self.albumName) | |
let collection:PHFetchResult = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .albumRegular, options: nil) | |
if let _:AnyObject = collection.firstObject{ | |
//found the album | |
self.assetCollection = collection.firstObject | |
self.albumFound = true | |
} | |
else { albumFound = false } | |
_ = collection.count | |
let options = PHFetchOptions() | |
options.predicate = NSPredicate(format: "mediaType == %d || mediaType == %d", | |
PHAssetMediaType.image.rawValue, | |
PHAssetMediaType.video.rawValue) | |
self.photoAssets = PHAsset.fetchAssets(in: self.assetCollection!, options: options) | |
let imageManager = PHCachingImageManager() | |
self.photoAssets.enumerateObjects{(object: AnyObject!, | |
count: Int, | |
stop: UnsafeMutablePointer<ObjCBool>) in | |
if object is PHAsset{ | |
let asset = object as! PHAsset | |
let imageSize = CGSize(width: asset.pixelWidth, | |
height: asset.pixelHeight) | |
/* For faster performance, and maybe degraded image */ | |
let options = PHImageRequestOptions() | |
options.deliveryMode = .fastFormat | |
// options.isSynchronous = true | |
imageManager.requestImage(for: asset, | |
targetSize: imageSize, | |
contentMode: .aspectFill, | |
options: options, | |
resultHandler: { | |
image, info in | |
// self.photo = image! | |
/* The image is now available to us */ | |
// self.getSyncPhotos(self.photo) | |
print("load image ...") | |
self.pausedBlock() | |
// | |
}) | |
} | |
} | |
//print("Finsh Operation ......... ") | |
} | |
} | |
class ContactOperation:TestOP { | |
var albumName = "" | |
var photoAssets = PHFetchResult<PHAsset>() | |
var assetCollection: PHAssetCollection? | |
var albumFound = false | |
let disQ = DispatchGroup() | |
override func main() { | |
for i in 0...10 { | |
print("Fetching Contact \(i)" ) | |
self.pausedBlock() | |
guard isCancelled == false else { | |
finish(true) | |
return | |
} | |
sleep(1) | |
} | |
executing(false) | |
finish(true) | |
} | |
// | |
// } | |
func req(){ | |
PHPhotoLibrary.requestAuthorization { (status) in | |
switch status { | |
case .authorized: | |
print("Good to proceed") | |
// let fetchOptions = PHFetchOptions() | |
// let allPhotos = PHAsset.fetchAssets(with: .image, options: fetchOptions) | |
// print("Found \(allPhotos.count) images") | |
self.getSyncPhotos() | |
case .denied, .restricted: | |
print("Not allowed") | |
case .notDetermined: | |
print("Not determined yet") | |
} | |
} | |
} | |
func getSyncPhotos() | |
{ | |
self.albumName = String(1) | |
//let fetchOptions = PHFetchOptions() | |
// fetchOptions.predicate = NSPredicate(format: "title = %@", self.albumName) | |
let collection:PHFetchResult = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .albumRegular, options: nil) | |
if let _:AnyObject = collection.firstObject{ | |
//found the album | |
self.assetCollection = collection.firstObject | |
self.albumFound = true | |
} | |
else { albumFound = false } | |
_ = collection.count | |
let options = PHFetchOptions() | |
options.predicate = NSPredicate(format: "mediaType == %d || mediaType == %d", | |
PHAssetMediaType.image.rawValue, | |
PHAssetMediaType.video.rawValue) | |
self.photoAssets = PHAsset.fetchAssets(in: self.assetCollection!, options: options) | |
let imageManager = PHCachingImageManager() | |
self.photoAssets.enumerateObjects{(object: AnyObject!, | |
count: Int, | |
stop: UnsafeMutablePointer<ObjCBool>) in | |
if object is PHAsset{ | |
let asset = object as! PHAsset | |
let imageSize = CGSize(width: asset.pixelWidth, | |
height: asset.pixelHeight) | |
/* For faster performance, and maybe degraded image */ | |
let options = PHImageRequestOptions() | |
options.deliveryMode = .fastFormat | |
// options.isSynchronous = true | |
imageManager.requestImage(for: asset, | |
targetSize: imageSize, | |
contentMode: .aspectFill, | |
options: options, | |
resultHandler: { | |
image, info in | |
// self.photo = image! | |
/* The image is now available to us */ | |
// self.getSyncPhotos(self.photo) | |
print("load image ...") | |
self.pausedBlock() | |
// | |
}) | |
} | |
} | |
//print("Finsh Operation ......... ") | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import Photos | |
class PhotoOperation2:Operation { | |
private var _finished = false { | |
willSet { | |
willChangeValue(forKey: "isFinished") | |
} | |
didSet { | |
didChangeValue(forKey: "isFinished") | |
} | |
} | |
private var _executing = false { | |
willSet { | |
willChangeValue(forKey: "isExecuting") | |
} | |
didSet { | |
didChangeValue(forKey: "isExecuting") | |
} | |
} | |
func executing(_ executing: Bool) { | |
_executing = executing | |
} | |
func finish(_ finished: Bool) { | |
_finished = finished | |
} | |
var albumName = "" | |
var photoAssets = PHFetchResult<PHAsset>() | |
var assetCollection: PHAssetCollection? | |
var albumFound = false | |
override func main() { | |
guard isCancelled == false else { | |
finish(true) | |
return | |
} | |
executing(true) | |
req() | |
} | |
func req(){ | |
PHPhotoLibrary.requestAuthorization { (status) in | |
switch status { | |
case .authorized: | |
print("Good to proceed") | |
// let fetchOptions = PHFetchOptions() | |
// let allPhotos = PHAsset.fetchAssets(with: .image, options: fetchOptions) | |
// print("Found \(allPhotos.count) images") | |
self.getSyncPhotos() | |
case .denied, .restricted: | |
print("Not allowed") | |
case .notDetermined: | |
print("Not determined yet") | |
} | |
} | |
} | |
func getSyncPhotos() | |
{ | |
print("start 22222") | |
self.albumName = String(1) | |
//let fetchOptions = PHFetchOptions() | |
// fetchOptions.predicate = NSPredicate(format: "title = %@", self.albumName) | |
let collection:PHFetchResult = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: nil) | |
if let _:AnyObject = collection.firstObject{ | |
//found the album | |
self.assetCollection = collection.firstObject | |
self.albumFound = true | |
} | |
else { albumFound = false } | |
_ = collection.count | |
self.photoAssets = PHAsset.fetchAssets(in: self.assetCollection!, options: nil) | |
let imageManager = PHCachingImageManager() | |
self.photoAssets.enumerateObjects{(object: AnyObject!, | |
count: Int, | |
stop: UnsafeMutablePointer<ObjCBool>) in | |
if object is PHAsset{ | |
let asset = object as! PHAsset | |
let imageSize = CGSize(width: asset.pixelWidth, | |
height: asset.pixelHeight) | |
/* For faster performance, and maybe degraded image */ | |
let options = PHImageRequestOptions() | |
options.deliveryMode = .fastFormat | |
// options.isSynchronous = true | |
imageManager.requestImage(for: asset, | |
targetSize: imageSize, | |
contentMode: .aspectFill, | |
options: options, | |
resultHandler: { | |
image, info in | |
// self.photo = image! | |
/* The image is now available to us */ | |
// self.getSyncPhotos(self.photo) | |
print("load image ... 222222222 >>>>>>> :)") | |
// sleep(5) | |
}) | |
} | |
} | |
self.executing(false) | |
self.finish(true) | |
print("Finsh Operation ......... 22222222222 ") | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController: UIViewController { | |
var photoOperation = PhotoOperation() | |
var photoOperation2 = PhotoOperation2() | |
private let operationQueue: OperationQueue = OperationQueue() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
//https://web.facebook.com/profile.php?id=100002845497398 | |
let url : URL! | |
if (UIApplication.shared.canOpenURL(URL(string:"fb://")!)) { | |
//FIXME: Url is not working ✋ | |
let pageName = "104958162837" | |
// tried with fb://page?name=%@ not worked | |
let urlString = String.init(format: "fb://profile/%@",pageName) | |
url = URL.init(string: urlString) | |
} else { | |
url = URL.init(string: "https://www.facebook.com/104958162837") | |
} | |
if #available(iOS 10.0, *) { | |
UIApplication.shared.open(url!, completionHandler: nil) | |
} else { | |
UIApplication.shared.openURL(url!) | |
} | |
} | |
@IBAction func stop(_ sender:UIButton){ | |
photoOperation.pause() | |
} | |
@IBAction func stRT(_ sender:UIButton){ | |
photoOperation.resume() | |
} | |
@IBAction func startOps(_ sender: Any) { | |
DispatchQueue.global().async { | |
self.photoOperation.completionBlock = { | |
print("end Photos operation Completed ") | |
} | |
let contactOP = ContactOperation() | |
// contactOP.addDependency(photoOperation) | |
contactOP.completionBlock = { | |
print("Contacts finished") | |
} | |
self.operationQueue.addOperations([self.photoOperation,contactOP], waitUntilFinished: true) | |
DispatchQueue.main.async { | |
print("Both Ops Finished") | |
} | |
} | |
print ("Ops Started") | |
} | |
@IBAction func cancelTapped(_ sender: Any) { | |
operationQueue.cancelAllOperations() | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment