From 5ca53d19994b1af9ab8dbddf049e49c3e7dc1e96 Mon Sep 17 00:00:00 2001 From: "atsushi.yoshimoto" Date: Wed, 2 Mar 2022 11:42:37 +0900 Subject: [PATCH 1/4] Fixed fetch PHAsset only once. --- NohanaImagePicker/PhotoKitAlbumList.swift | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/NohanaImagePicker/PhotoKitAlbumList.swift b/NohanaImagePicker/PhotoKitAlbumList.swift index bca1843..68ae330 100644 --- a/NohanaImagePicker/PhotoKitAlbumList.swift +++ b/NohanaImagePicker/PhotoKitAlbumList.swift @@ -60,8 +60,9 @@ public class PhotoKitAlbumList: ItemList { for fetchResult in albumListFetchResult { fetchResult.enumerateObjects({ (album, index, stop) in if self.assetCollectionSubtypes.contains(album.assetCollectionSubtype) || isAssetCollectionSubtypeAny { - if self.shouldShowEmptyAlbum || PHAsset.fetchAssets(in: album, options: PhotoKitAssetList.fetchOptions(self.mediaType)).count != 0 { - tmpAlbumList.append(PhotoKitAssetList(album: album, mediaType: self.mediaType, ascending: self.ascending)) + let assets = PhotoKitAssetList(album: album, mediaType: self.mediaType, ascending: self.ascending) + if self.shouldShowEmptyAlbum || assets.count > 0 { + tmpAlbumList.append(assets) } } }) From dbde640ee1b7de80e9f91757c72f789f07d66343 Mon Sep 17 00:00:00 2001 From: "atsushi.yoshimoto" Date: Mon, 7 Mar 2022 12:43:24 +0900 Subject: [PATCH 2/4] Fixed params. --- NohanaImagePicker/RootViewController.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NohanaImagePicker/RootViewController.swift b/NohanaImagePicker/RootViewController.swift index 197f008..55478a6 100644 --- a/NohanaImagePicker/RootViewController.swift +++ b/NohanaImagePicker/RootViewController.swift @@ -130,8 +130,8 @@ class RootViewController: UIViewController { private func showRecentPhotos() { albumList = PhotoKitAlbumList( - assetCollectionTypes: [.smartAlbum, .album], - assetCollectionSubtypes: nohanaImagePickerController.assetCollectionSubtypes, + assetCollectionTypes: [.smartAlbum], + assetCollectionSubtypes: [.smartAlbumUserLibrary], mediaType: nohanaImagePickerController.mediaType, shouldShowEmptyAlbum: nohanaImagePickerController.shouldShowEmptyAlbum, ascending: !nohanaImagePickerController.canPickDateSection, From c1573663eb7917594430e5b62e15e23fe761327d Mon Sep 17 00:00:00 2001 From: "atsushi.yoshimoto" Date: Mon, 7 Mar 2022 12:38:33 +0900 Subject: [PATCH 3/4] Fixed create instance of AlbumListViewControler only once. --- NohanaImagePicker/RootViewController.swift | 38 ++++++++++++---------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/NohanaImagePicker/RootViewController.swift b/NohanaImagePicker/RootViewController.swift index 55478a6..bd5a529 100644 --- a/NohanaImagePicker/RootViewController.swift +++ b/NohanaImagePicker/RootViewController.swift @@ -28,6 +28,26 @@ class RootViewController: UIViewController { }() private var currentChildViewController: UIViewController? private var albumList: PhotoKitAlbumList! + private lazy var albumListViewController: AlbumListViewController = { + let storyboard = UIStoryboard(name: "AlbumList", bundle: nohanaImagePickerController.assetBundle) + guard let albumListViewController = storyboard.instantiateInitialViewController(creator: { corder in + AlbumListViewController(coder: corder, nohanaImagePickerController: self.nohanaImagePickerController) + }) else { + fatalError("albumListViewController init failed.") + } + albumListViewController.photoKitAlbumList = PhotoKitAlbumList(assetCollectionTypes: [.smartAlbum, .album], + assetCollectionSubtypes: nohanaImagePickerController.assetCollectionSubtypes, + mediaType: nohanaImagePickerController.mediaType, + shouldShowEmptyAlbum: nohanaImagePickerController.shouldShowMoment, + ascending: !nohanaImagePickerController.canPickDateSection, + handler: { [weak albumListViewController] in + DispatchQueue.main.async { + albumListViewController?.isLoading = false + albumListViewController?.tableView.reloadData() + } + }) + return albumListViewController + }() init?(coder: NSCoder, nohanaImagePickerController: NohanaImagePickerController) { self.nohanaImagePickerController = nohanaImagePickerController @@ -94,25 +114,7 @@ class RootViewController: UIViewController { } private func showAlbumList() { - let storyboard = UIStoryboard(name: "AlbumList", bundle: nohanaImagePickerController.assetBundle) - guard let albumListViewController = storyboard.instantiateInitialViewController(creator: { corder in - AlbumListViewController(coder: corder, nohanaImagePickerController: self.nohanaImagePickerController) - }) else { - fatalError("albumListViewController init failed.") - } let navigationController = UINavigationController(rootViewController: albumListViewController) - - albumListViewController.photoKitAlbumList = PhotoKitAlbumList(assetCollectionTypes: [.smartAlbum, .album], - assetCollectionSubtypes: nohanaImagePickerController.assetCollectionSubtypes, - mediaType: nohanaImagePickerController.mediaType, - shouldShowEmptyAlbum: nohanaImagePickerController.shouldShowMoment, - ascending: !nohanaImagePickerController.canPickDateSection, - handler: { [weak albumListViewController] in - DispatchQueue.main.async { - albumListViewController?.isLoading = false - albumListViewController?.tableView.reloadData() - } - }) albumListViewController.delegate = self navigationController.presentationController?.delegate = self let appearance = navigationBarAppearance(nohanaImagePickerController) From 44aea7ebe22b292cca11b91d10c333a3fe0bea89 Mon Sep 17 00:00:00 2001 From: "atsushi.yoshimoto" Date: Mon, 7 Mar 2022 18:03:07 +0900 Subject: [PATCH 4/4] Fixed run with indexset in fenumerateObjects. --- NohanaImagePicker.xcodeproj/project.pbxproj | 4 ++++ NohanaImagePicker/Array+Ex.swift | 25 +++++++++++++++++++++ NohanaImagePicker/PhotoKitAlbumList.swift | 22 ++++++++---------- 3 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 NohanaImagePicker/Array+Ex.swift diff --git a/NohanaImagePicker.xcodeproj/project.pbxproj b/NohanaImagePicker.xcodeproj/project.pbxproj index b78f0ee..93471c5 100644 --- a/NohanaImagePicker.xcodeproj/project.pbxproj +++ b/NohanaImagePicker.xcodeproj/project.pbxproj @@ -8,6 +8,7 @@ /* Begin PBXBuildFile section */ 3569CAA91EC1918E000C41C0 /* NohanaImagePicker.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 3569CAA61EC1918E000C41C0 /* NohanaImagePicker.xcassets */; }; + 6A0117BE27D6011500B868F9 /* Array+Ex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A0117BD27D6011500B868F9 /* Array+Ex.swift */; }; 6A434AE72775631C009526D3 /* UIViewController+Ex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A434AE62775631C009526D3 /* UIViewController+Ex.swift */; }; 6A8047FF276C3D77000F3B28 /* AlbumList.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 6A8047FE276C3D77000F3B28 /* AlbumList.storyboard */; }; 6A804801276C3E80000F3B28 /* Moment.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 6A804800276C3E80000F3B28 /* Moment.storyboard */; }; @@ -89,6 +90,7 @@ 23D1CD93207CEB1200F8115E /* de */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = de; path = de.lproj/NohanaImagePicker.strings; sourceTree = ""; }; 3569CAA61EC1918E000C41C0 /* NohanaImagePicker.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = NohanaImagePicker.xcassets; sourceTree = ""; }; 3590F1F51EC1A79400F32E06 /* ru */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ru; path = ru.lproj/NohanaImagePicker.strings; sourceTree = ""; }; + 6A0117BD27D6011500B868F9 /* Array+Ex.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Array+Ex.swift"; sourceTree = ""; }; 6A434AE62775631C009526D3 /* UIViewController+Ex.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIViewController+Ex.swift"; sourceTree = ""; }; 6A8047FE276C3D77000F3B28 /* AlbumList.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = AlbumList.storyboard; sourceTree = ""; }; 6A804800276C3E80000F3B28 /* Moment.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = Moment.storyboard; sourceTree = ""; }; @@ -177,6 +179,7 @@ children = ( 6AD1607E2770B23D00A8B066 /* UIApplication+Ex.swift */, 6A434AE62775631C009526D3 /* UIViewController+Ex.swift */, + 6A0117BD27D6011500B868F9 /* Array+Ex.swift */, ); name = Extension; sourceTree = ""; @@ -465,6 +468,7 @@ 6AD1607F2770B23D00A8B066 /* UIApplication+Ex.swift in Sources */, F25C10C21C8ED9BF007453C3 /* MomentViewController.swift in Sources */, F25C69881CA23A0A005935D6 /* MomentCell.swift in Sources */, + 6A0117BE27D6011500B868F9 /* Array+Ex.swift in Sources */, F2FE1F781C901D9400FDBE7B /* MomentSectionHeaderView.swift in Sources */, F1A26CD12738E88700433E9F /* AssetDateSection.swift in Sources */, F23554281C69D5DB00796DCA /* NohanaImagePickerController.swift in Sources */, diff --git a/NohanaImagePicker/Array+Ex.swift b/NohanaImagePicker/Array+Ex.swift new file mode 100644 index 0000000..9768e89 --- /dev/null +++ b/NohanaImagePicker/Array+Ex.swift @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2016 nohana, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import Foundation + +extension Array { + func chunked(by chunkSize: Int) -> [[Element]] { + return stride(from: 0, to: self.count, by: chunkSize).map { + Array(self[$0.. 0 { - tmpAlbumList.append(assets) + Array(0.. 0 { + self.albumList.append(assets) + } } } - }) - } - self.albumList = tmpAlbumList - - if let handler = handler { - handler() + handler?() + } } } }