Skip to content

Commit

Permalink
Layout (#3090)
Browse files Browse the repository at this point in the history

Signed-off-by: Marino Faggiana <marino@marinofaggiana.com>
  • Loading branch information
marinofaggiana authored Oct 6, 2024
1 parent 04d0836 commit eeca88b
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 57 deletions.
2 changes: 1 addition & 1 deletion iOSClient/Files/NCFiles.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class NCFiles: NCCollectionViewCommon {
self.richWorkspaceText = tableDirectory?.richWorkspace

if reloadDataSource {
(self.collectionView.collectionViewLayout as? NCMediaLayout)?.invalidate()
// (self.collectionView.collectionViewLayout as? NCMediaLayout)?.invalidate()
self.reloadDataSource()
} else if self.dataSource.isEmpty() {
self.collectionView.reloadData()
Expand Down
138 changes: 93 additions & 45 deletions iOSClient/Main/Collection Common/NCCollectionViewCommon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
NotificationCenter.default.addObserver(self, selector: #selector(closeRichWorkspaceWebView), name: NSNotification.Name(rawValue: global.notificationCenterCloseRichWorkspaceWebView), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(changeStatusFolderE2EE(_:)), name: NSNotification.Name(rawValue: global.notificationCenterChangeStatusFolderE2EE), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(reloadAvatar(_:)), name: NSNotification.Name(rawValue: global.notificationCenterReloadAvatar), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(changeLayout(_:)), name: NSNotification.Name(rawValue: global.notificationCenterChangeLayout), object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: global.notificationCenterDeleteFile), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(moveFile(_:)), name: NSNotification.Name(rawValue: global.notificationCenterMoveFile), object: nil)
Expand Down Expand Up @@ -275,6 +276,7 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: global.notificationCenterCloseRichWorkspaceWebView), object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: global.notificationCenterChangeStatusFolderE2EE), object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: global.notificationCenterReloadAvatar), object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: global.notificationCenterChangeLayout), object: nil)

NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: global.notificationCenterDeleteFile), object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: global.notificationCenterMoveFile), object: nil)
Expand Down Expand Up @@ -349,6 +351,43 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
collectionView.reloadData()
}

@objc func changeLayout(_ notification: NSNotification) {
guard let userInfo = notification.userInfo as NSDictionary?,
let account = userInfo["account"] as? String,
let serverUrl = userInfo["serverUrl"] as? String,
let layoutForView = userInfo["layoutForView"] as? NCDBLayoutForView,
account == session.account,
serverUrl == self.serverUrl
else { return }

if self.layoutForView?.layout == layoutForView.layout {
self.layoutForView = self.database.setLayoutForView(layoutForView: layoutForView)
self.reloadDataSource()
return
}

self.layoutForView = self.database.setLayoutForView(layoutForView: layoutForView)
layoutForView.layout = layoutForView.layout
self.layoutType = layoutForView.layout

self.collectionView.reloadData()

switch layoutForView.layout {
case global.layoutList:
self.collectionView.setCollectionViewLayout(self.listLayout, animated: true)
case global.layoutGrid:
self.collectionView.setCollectionViewLayout(self.gridLayout, animated: true)
case global.layoutPhotoSquare, global.layoutPhotoRatio:
self.collectionView.setCollectionViewLayout(self.mediaLayout, animated: true)
default:
break
}

self.collectionView.collectionViewLayout.invalidateLayout()

self.setNavigationRightItems()
}

@objc func reloadDataSource(_ notification: NSNotification) {
if let userInfo = notification.userInfo as NSDictionary?,
let serverUrl = userInfo["serverUrl"] as? String {
Expand Down Expand Up @@ -653,65 +692,54 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
func createMenuActions() -> [UIMenuElement] {
guard let layoutForView = database.getLayoutForView(account: session.account, key: layoutKey, serverUrl: serverUrl) else { return [] }

func saveLayout(_ layoutForView: NCDBLayoutForView) {
database.setLayoutForView(layoutForView: layoutForView)

(self.collectionView.collectionViewLayout as? NCMediaLayout)?.invalidate()
self.collectionView.reloadData()

setNavigationRightItems()
}

let select = UIAction(title: NSLocalizedString("_select_", comment: ""),
image: utility.loadImage(named: "checkmark.circle"),
attributes: (self.dataSource.isEmpty() || NCNetworking.shared.isOffline) ? .disabled : []) { _ in
self.setEditMode(true)
}

let list = UIAction(title: NSLocalizedString("_list_", comment: ""), image: utility.loadImage(named: "list.bullet"), state: layoutForView.layout == global.layoutList ? .on : .off) { _ in
layoutForView.layout = self.global.layoutList
self.layoutForView = self.database.setLayoutForView(layoutForView: layoutForView)
self.layoutType = self.global.layoutList

self.collectionView.reloadData()
self.collectionView.setCollectionViewLayout(self.listLayout, animated: true)
layoutForView.layout = self.global.layoutList

self.setNavigationRightItems()
NotificationCenter.default.postOnMainThread(name: self.global.notificationCenterChangeLayout,
object: nil,
userInfo: ["account": self.session.account,
"serverUrl": self.serverUrl,
"layoutForView": layoutForView])
}

let grid = UIAction(title: NSLocalizedString("_icons_", comment: ""), image: utility.loadImage(named: "square.grid.2x2"), state: layoutForView.layout == global.layoutGrid ? .on : .off) { _ in
layoutForView.layout = self.global.layoutGrid
self.layoutForView = self.database.setLayoutForView(layoutForView: layoutForView)
self.layoutType = self.global.layoutGrid

self.collectionView.reloadData()
self.collectionView.setCollectionViewLayout(self.gridLayout, animated: true)
layoutForView.layout = self.global.layoutGrid

self.setNavigationRightItems()
NotificationCenter.default.postOnMainThread(name: self.global.notificationCenterChangeLayout,
object: nil,
userInfo: ["account": self.session.account,
"serverUrl": self.serverUrl,
"layoutForView": layoutForView])
}

let mediaSquare = UIAction(title: NSLocalizedString("_media_square_", comment: ""), image: utility.loadImage(named: "square.grid.3x3"), state: layoutForView.layout == global.layoutPhotoSquare ? .on : .off) { _ in
layoutForView.layout = self.global.layoutPhotoSquare
self.layoutForView = self.database.setLayoutForView(layoutForView: layoutForView)
self.layoutType = self.global.layoutPhotoSquare

(self.collectionView.collectionViewLayout as? NCMediaLayout)?.invalidate()
self.collectionView.reloadData()
self.collectionView.setCollectionViewLayout(self.mediaLayout, animated: true)
layoutForView.layout = self.global.layoutPhotoSquare

self.setNavigationRightItems()
NotificationCenter.default.postOnMainThread(name: self.global.notificationCenterChangeLayout,
object: nil,
userInfo: ["account": self.session.account,
"serverUrl": self.serverUrl,
"layoutForView": layoutForView])
}

let mediaRatio = UIAction(title: NSLocalizedString("_media_ratio_", comment: ""), image: utility.loadImage(named: "rectangle.grid.3x2"), state: layoutForView.layout == self.global.layoutPhotoRatio ? .on : .off) { _ in
layoutForView.layout = self.global.layoutPhotoRatio
self.layoutForView = self.database.setLayoutForView(layoutForView: layoutForView)
self.layoutType = self.global.layoutPhotoRatio

(self.collectionView.collectionViewLayout as? NCMediaLayout)?.invalidate()
self.collectionView.reloadData()
self.collectionView.setCollectionViewLayout(self.mediaLayout, animated: true)
layoutForView.layout = self.global.layoutPhotoRatio

self.setNavigationRightItems()
NotificationCenter.default.postOnMainThread(name: self.global.notificationCenterChangeLayout,
object: nil,
userInfo: ["account": self.session.account,
"serverUrl": self.serverUrl,
"layoutForView": layoutForView])
}

let viewStyleSubmenu = UIMenu(title: "", options: .displayInline, children: [list, grid, mediaSquare, mediaRatio])
Expand All @@ -723,55 +751,75 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
let isSize = layoutForView.sort == "size"

let byName = UIAction(title: NSLocalizedString("_name_", comment: ""), image: isName ? ascendingChevronImage : nil, state: isName ? .on : .off) { _ in

if isName { // repeated press
layoutForView.ascending = !layoutForView.ascending
}

layoutForView.sort = "fileName"
saveLayout(layoutForView)

NotificationCenter.default.postOnMainThread(name: self.global.notificationCenterChangeLayout,
object: nil,
userInfo: ["account": self.session.account,
"serverUrl": self.serverUrl,
"layoutForView": layoutForView])
}

let byNewest = UIAction(title: NSLocalizedString("_date_", comment: ""), image: isDate ? ascendingChevronImage : nil, state: isDate ? .on : .off) { _ in

if isDate { // repeated press
layoutForView.ascending = !layoutForView.ascending
}

layoutForView.sort = "date"
saveLayout(layoutForView)

NotificationCenter.default.postOnMainThread(name: self.global.notificationCenterChangeLayout,
object: nil,
userInfo: ["account": self.session.account,
"serverUrl": self.serverUrl,
"layoutForView": layoutForView])
}

let byLargest = UIAction(title: NSLocalizedString("_size_", comment: ""), image: isSize ? ascendingChevronImage : nil, state: isSize ? .on : .off) { _ in

if isSize { // repeated press
layoutForView.ascending = !layoutForView.ascending
}

layoutForView.sort = "size"
saveLayout(layoutForView)

NotificationCenter.default.postOnMainThread(name: self.global.notificationCenterChangeLayout,
object: nil,
userInfo: ["account": self.session.account,
"serverUrl": self.serverUrl,
"layoutForView": layoutForView])
}

let sortSubmenu = UIMenu(title: NSLocalizedString("_order_by_", comment: ""), options: .displayInline, children: [byName, byNewest, byLargest])

let foldersOnTop = UIAction(title: NSLocalizedString("_directory_on_top_no_", comment: ""), image: utility.loadImage(named: "folder"), state: layoutForView.directoryOnTop ? .on : .off) { _ in

layoutForView.directoryOnTop = !layoutForView.directoryOnTop
saveLayout(layoutForView)

NotificationCenter.default.postOnMainThread(name: self.global.notificationCenterChangeLayout,
object: nil,
userInfo: ["account": self.session.account,
"serverUrl": self.serverUrl,
"layoutForView": layoutForView])
}

let personalFilesOnly = NCKeychain().getPersonalFilesOnly(account: session.account)
let personalFilesOnlyAction = UIAction(title: NSLocalizedString("_personal_files_only_", comment: ""), image: utility.loadImage(named: "folder.badge.person.crop", colors: NCBrandColor.shared.iconImageMultiColors), state: personalFilesOnly ? .on : .off) { _ in

NCKeychain().setPersonalFilesOnly(account: self.session.account, value: !personalFilesOnly)

(self.collectionView.collectionViewLayout as? NCMediaLayout)?.invalidate()
self.reloadDataSource()
self.setNavigationRightItems()
}

let showDescriptionKeychain = NCKeychain().showDescription
let showDescription = UIAction(title: NSLocalizedString("_show_description_", comment: ""), image: utility.loadImage(named: "list.dash.header.rectangle"), attributes: richWorkspaceText == nil ? .disabled : [], state: showDescriptionKeychain && richWorkspaceText != nil ? .on : .off) { _ in

NCKeychain().showDescription = !showDescriptionKeychain

(self.collectionView.collectionViewLayout as? NCMediaLayout)?.invalidate()
self.collectionView.reloadData()

self.setNavigationRightItems()
}
showDescription.subtitle = richWorkspaceText == nil ? NSLocalizedString("_no_description_available_", comment: "") : ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ extension NCCollectionViewCommon {
self.currentScale = 1.0

UIView.transition(with: self.collectionView, duration: 0.20, options: .transitionCrossDissolve) {
(self.collectionView.collectionViewLayout as? NCMediaLayout)?.invalidate()
// (self.collectionView.collectionViewLayout as? NCMediaLayout)?.invalidate()
self.collectionView.reloadData()
} completion: { _ in

Expand Down
2 changes: 1 addition & 1 deletion iOSClient/Media/NCMediaDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ extension NCMedia {
func collectionViewReloadData() {
DispatchQueue.main.async {
self.refreshControl.endRefreshing()
self.layout.invalidate()
// self.layout.invalidate()
self.collectionView.reloadData()
self.setTitleDate()
}
Expand Down
9 changes: 1 addition & 8 deletions iOSClient/Media/NCMediaLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public class NCMediaLayout: UICollectionViewLayout {
}
public var frameWidth: Float = 0
public var itemWidth: Float = 0
public var oldFrameWidth: CGFloat = 0

// MARK: - Private Properties
private weak var delegate: NCMediaLayoutDelegate? {
Expand All @@ -114,10 +113,8 @@ public class NCMediaLayout: UICollectionViewLayout {

guard let numberOfSections = collectionView?.numberOfSections,
let collectionView = collectionView,
let delegate = delegate,
collectionView.frame.size.width != oldFrameWidth else { return }
let delegate = delegate else { return }

oldFrameWidth = collectionView.frame.size.width
columnCount = delegate.getColumnCount()
(delegate as? NCMedia)?.buildMediaPhotoVideo(columnCount: columnCount)

Expand Down Expand Up @@ -328,8 +325,4 @@ extension NCMediaLayout {
invalidateLayout()
}
}

func invalidate() {
oldFrameWidth = 0
}
}
2 changes: 1 addition & 1 deletion iOSClient/Media/NCMediaPinchGesture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ extension NCMedia {
self.currentScale = 1.0

UIView.transition(with: self.collectionView, duration: 0.20, options: .transitionCrossDissolve) {
self.layout.invalidate()
// self.layout.invalidate()
self.collectionView.reloadData()
} completion: { _ in
self.setTitleDate()
Expand Down
1 change: 1 addition & 0 deletions iOSClient/NCGlobal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ class NCGlobal: NSObject {
let notificationCenterCloseRichWorkspaceWebView = "closeRichWorkspaceWebView"
let notificationCenterReloadAvatar = "reloadAvatar"
let notificationCenterClearCache = "clearCache"
let notificationCenterChangeLayout = "changeLayout" // userInfo: account, serverUrl, layoutForView

let notificationCenterReloadDataSource = "reloadDataSource" // userInfo: serverUrl?
let notificationCenterGetServerData = "getServerData" // userInfo: serverUrl?
Expand Down

0 comments on commit eeca88b

Please sign in to comment.