Skip to content

Commit

Permalink
add delete animation
Browse files Browse the repository at this point in the history
  • Loading branch information
truongminhthang committed Nov 1, 2017
1 parent 34f4a23 commit 3d309ee
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 119 deletions.
27 changes: 13 additions & 14 deletions Cleaner/Base.lproj/SortFileTableVC.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
<outlet property="photoImageView" destination="FXQ-xq-Spa" id="ec2-0a-Asd"/>
<outlet property="sizeLabel" destination="rmb-Mx-3Zd" id="hCM-SU-4Tu"/>
<outlet property="typeAssetLabel" destination="hrn-xQ-pbd" id="1iY-Mw-yiB"/>
<segue destination="yl4-oS-S53" kind="show" id="YOq-AQ-vGz"/>
<segue destination="yl4-oS-S53" kind="show" identifier="show photo details" id="YOq-AQ-vGz"/>
</connections>
</tableViewCell>
</prototypes>
Expand Down Expand Up @@ -257,14 +257,13 @@
<color key="textColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="252" text="" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="v5J-iO-IIK">
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="DW2-qv-kt8">
<rect key="frame" x="0.0" y="30" width="359" height="30"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="HFG-bi-w36"/>
<constraint firstAttribute="height" constant="30" id="m8x-xb-9Jp"/>
</constraints>
<viewLayoutGuide key="safeArea" id="pCE-r9-fUx"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<fontDescription key="fontDescription" name=".AppleSystemUIFont" family=".AppleSystemUIFont" pointSize="17"/>
<color key="textColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<nil key="highlightedColor"/>
</label>
Expand Down Expand Up @@ -300,7 +299,7 @@
</constraints>
<state key="normal" title="DELETE!"/>
<connections>
<action selector="deleteButton:" destination="yl4-oS-S53" eventType="touchUpInside" id="pwT-YX-juS"/>
<segue destination="xXg-If-bGx" kind="unwind" unwindAction="unwindToDeleteAssetWithSender:" id="EZk-bg-Z2Y"/>
</connections>
</button>
</subviews>
Expand All @@ -319,10 +318,11 @@
<outlet property="creationDayLabel" destination="wkA-JG-zXp" id="tL9-j5-ota"/>
<outlet property="detailImageView" destination="Afc-lh-uQe" id="5oa-eA-tgA"/>
<outlet property="scrollView" destination="tsS-SO-EC9" id="tVE-UR-AF1"/>
<outlet property="sizeLabel" destination="v5J-iO-IIK" id="0AJ-BU-gCF"/>
<outlet property="sizeLabel" destination="DW2-qv-kt8" id="sPs-ba-0Fh"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="HB9-Y9-CBC" userLabel="First Responder" sceneMemberID="firstResponder"/>
<exit id="xXg-If-bGx" userLabel="Exit" sceneMemberID="exit"/>
</objects>
<point key="canvasLocation" x="2042" y="479"/>
</scene>
Expand Down Expand Up @@ -360,14 +360,13 @@
<color key="textColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="252" text="" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="RuD-YG-4h5">
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="D6L-2z-w7N">
<rect key="frame" x="0.0" y="30" width="359" height="30"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="SK3-dG-rTA"/>
<constraint firstAttribute="height" constant="30" id="kOF-lr-4uC"/>
</constraints>
<viewLayoutGuide key="safeArea" id="UTn-PD-wjC"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<fontDescription key="fontDescription" name=".AppleSystemUIFont" family=".AppleSystemUIFont" pointSize="17"/>
<color key="textColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<nil key="highlightedColor"/>
</label>
Expand Down Expand Up @@ -432,8 +431,7 @@
</constraints>
<state key="normal" title="DELETE!"/>
<connections>
<action selector="deleteButton:" destination="zvs-Mj-ncP" eventType="touchUpInside" id="THY-kL-hHq"/>
<action selector="deleteButton:" destination="yl4-oS-S53" eventType="touchUpInside" id="kev-KM-scz"/>
<segue destination="Vev-qt-Zvv" kind="unwind" unwindAction="unwindToDeleteAssetWithSender:" id="ZOI-Zc-I7B"/>
</connections>
</button>
</subviews>
Expand All @@ -451,12 +449,13 @@
<connections>
<outlet property="creationDayLabel" destination="E42-Bz-eFY" id="Lgq-6r-Cp7"/>
<outlet property="playButton" destination="I8s-jt-gDe" id="3Pw-JH-Ysx"/>
<outlet property="sizeLabel" destination="RuD-YG-4h5" id="cWi-nO-fYw"/>
<outlet property="sizeLabel" destination="D6L-2z-w7N" id="a9l-AF-geq"/>
<outlet property="stopCorverButton" destination="p3v-pj-B52" id="k3l-8f-Gli"/>
<outlet property="videoContainer" destination="alo-TY-XWB" id="8ao-W6-6hj"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="pdQ-PD-5hH" userLabel="First Responder" sceneMemberID="firstResponder"/>
<exit id="Vev-qt-Zvv" userLabel="Exit" sceneMemberID="exit"/>
<navigationItem title="Title" id="GH4-Ix-G0h">
<barButtonItem key="rightBarButtonItem" title="Item" id="Hfx-0v-vex">
<connections>
Expand Down
9 changes: 4 additions & 5 deletions Cleaner/CleanerAsset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class CleanerAsset: Equatable {
}
}
}

func fetchFileSize(completeBlock: (() -> Void)?) {
if asset.duration == 0 {
PHImageManager.default().requestImageData(for: asset, options: nil, resultHandler: { (data, string, orientation, dictionary) in
Expand All @@ -82,11 +83,9 @@ class CleanerAsset: Equatable {
}
}

func remove(completionHandler: ((Bool, Error?) -> Void)? = nil) {
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.deleteAssets([self.asset] as NSArray)
}, completionHandler: completionHandler)
PhotoServices.shared.removeCleanerAsset(self)
func remove(completionHandler: ((Bool, Int, Error?) -> Void)? = nil) {

PhotoServices.shared.removeCleanerAsset(self, completionHandler: completionHandler)
}

public static func ==(lhs: CleanerAsset, rhs: CleanerAsset) -> Bool {
Expand Down
32 changes: 26 additions & 6 deletions Cleaner/Services/PhotoServices.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ class PhotoServices : NSObject {
label: "com.bababibo.CleanerAsset.cleanerAssetQueue",
attributes: .concurrent)
var fetchResult : PHFetchResult<PHAsset>?
private var _displayedAssets : [CleanerAsset] = []
var isDeleting = false
private var _displayedAssets : [CleanerAsset] = [] {
didSet {
guard !isDeleting else {return}
NotificationCenter.default.post(name: NotificationName.didFinishFetchPHAsset, object: nil)
}
}
weak var changeObserver : PHPhotoLibraryChangeObserver? {
didSet {
if changeObserver != nil {
Expand All @@ -43,9 +49,21 @@ class PhotoServices : NSObject {
}
}

func removeCleanerAsset(_ cleanerAsset : CleanerAsset) {
func removeCleanerAsset(_ cleanerAsset : CleanerAsset, completionHandler: ((Bool, Int, Error?) -> Void)? = nil) {
concurrentCleanerAssetQueue.async(flags: .barrier) {
self._displayedAssets.remove(object: cleanerAsset)
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.deleteAssets([cleanerAsset.asset] as NSArray)
self.isDeleting = true
}, completionHandler: { (success, error) in
guard success else {
return
}

if let removeIndex = self._displayedAssets.remove(object: cleanerAsset) {
completionHandler?(success, removeIndex, error)
self.isDeleting = false
}
})
}

}
Expand Down Expand Up @@ -92,13 +110,11 @@ class PhotoServices : NSObject {
downloadGroup.enter()
_displayedAssets.append(CleanerAsset(asset: fetchResult!.object(at: index), completeBlock: {
downloadGroup.leave()
NotificationCenter.default.post(name: NotificationName.didFinishFetchPHAsset, object: nil)
}))
}
hideActivity()
downloadGroup.notify(queue: DispatchQueue.main) {
self._displayedAssets = self._displayedAssets.sorted(by: {$0.fileSize > $1.fileSize})
NotificationCenter.default.post(name: NotificationName.didFinishFetchPHAsset, object: nil)
self.isFetching = false

}
Expand All @@ -107,11 +123,15 @@ class PhotoServices : NSObject {

extension Array where Element: Equatable {
// Remove first collection element that is equal to the given `object`:
mutating func remove(object: Element) {
mutating func remove(object: Element) -> Int? {
if let index = index(of: object) {
remove(at: index)
return index
}
return nil
}
}




51 changes: 19 additions & 32 deletions Cleaner/ViewControllers/SortFile/DetailImageVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,43 @@
import UIKit
import Photos
import AVKit
class DetailImageVC: UIViewController, UIScrollViewDelegate {
var assetCollection: PHAssetCollection!

class DetailVC: UIViewController {
var cleanerAsset: CleanerAsset!

@IBOutlet weak var detailImageView: UIImageView!
@IBOutlet weak var sizeLabel: UILabel!
@IBOutlet weak var creationDayLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
getInfo()
}

func getInfo() {
self.sizeLabel.text = cleanerAsset.fileSize.fileSizeString
self.creationDayLabel.text = cleanerAsset.dateCreatedString

}
}
class DetailImageVC: DetailVC, UIScrollViewDelegate {


@IBOutlet weak var detailImageView: UIImageView!

@IBOutlet weak var scrollView: UIScrollView! {
didSet {
scrollView.delegate = self
scrollView.contentSize = detailImageView.frame.size
scrollView.maximumZoomScale = 6.0
scrollView.minimumZoomScale = 1.0

}
}

fileprivate var playerLayer: AVPlayerLayer!
fileprivate var playerLooper: AVPlayerLooper?
fileprivate var isPlayingHint = false
fileprivate let imageManager = PHCachingImageManager()





override func viewDidLoad() {
super.viewDidLoad()

displayImage()


}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
Expand All @@ -65,7 +71,6 @@ class DetailImageVC: UIViewController, UIScrollViewDelegate {
let options = PHImageRequestOptions()
options.deliveryMode = .highQualityFormat
options.isNetworkAccessAllowed = true

PHImageManager.default().requestImage(for: cleanerAsset.asset,
targetSize: targetSize,
contentMode: .aspectFit,
Expand All @@ -78,26 +83,8 @@ class DetailImageVC: UIViewController, UIScrollViewDelegate {
// Now that we have the image, show it.
self.detailImageView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleLeftMargin, .flexibleRightMargin, .flexibleTopMargin, .flexibleBottomMargin]
self.detailImageView.contentMode = UIViewContentMode.scaleAspectFit

self.detailImageView.image = image
})
self.sizeLabel.text = cleanerAsset.fileSize.fileSizeString
self.creationDayLabel.text = cleanerAsset.dateCreatedString

}

@IBAction func deleteButton(_ sender: UIButton) {
let completion = { (success: Bool, error: Error?) -> Void in
if success {
DispatchQueue.main.sync {
_ = self.navigationController!.popViewController(animated: true)
}
} else {
print("can't remove asset: \(String(describing: error))")
}
}

cleanerAsset.remove(completionHandler: completion)

}
}
Expand Down
Loading

0 comments on commit 3d309ee

Please sign in to comment.