Skip to content

Commit

Permalink
Improvements reload (#3092)
Browse files Browse the repository at this point in the history
* ThreadSafeDictionary

Signed-off-by: Marino Faggiana <[email protected]>

---------

Signed-off-by: Marino Faggiana <[email protected]>
  • Loading branch information
Marino Faggiana authored Oct 7, 2024
1 parent b492e06 commit 3c1b092
Show file tree
Hide file tree
Showing 15 changed files with 127 additions and 79 deletions.
49 changes: 32 additions & 17 deletions Brand/NCBrand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,13 @@ class NCBrandColor: NSObject {
userColors = generateColors()
}

func settingThemingColor(account: String) {
@discardableResult
func settingThemingColor(account: String) -> Bool {
let darker: CGFloat = 30 // %
let lighter: CGFloat = 30 // %
var colorThemingColor: UIColor?
var colorThemingColorElement: UIColor?
var colorThemingColorText: UIColor?

if NCBrandOptions.shared.use_themingColor {
let themingColor = NCCapabilities.shared.getCapabilities(account: account).capabilityThemingColor
Expand All @@ -233,67 +237,78 @@ class NCBrandColor: NSObject {
// THEMING COLOR
if themingColor.first == "#" {
if let color = UIColor(hex: themingColor) {
self.themingColor[account] = color
colorThemingColor = color
} else {
self.themingColor[account] = customer
colorThemingColor = customer
}
} else {
self.themingColor[account] = customer
colorThemingColor = customer
}

// THEMING COLOR ELEMENT (control isTooLight / isTooDark)
if themingColorElement.first == "#" {
if let color = UIColor(hex: themingColorElement) {
if color.isTooLight() {
if let color = color.darker(by: darker) {
self.themingColorElement[account] = color
colorThemingColorElement = color
}
} else if color.isTooDark() {
if let color = color.lighter(by: lighter) {
self.themingColorElement[account] = color
colorThemingColorElement = color
}
} else {
self.themingColorElement[account] = color
colorThemingColorElement = color
}
} else {
self.themingColorElement[account] = customer
colorThemingColorElement = customer
}
} else {
self.themingColorElement[account] = customer
colorThemingColorElement = customer
}

// THEMING COLOR TEXT
if themingColorText.first == "#" {
if let color = UIColor(hex: themingColorText) {
self.themingColorText[account] = color
colorThemingColorText = color
} else {
self.themingColorText[account] = .white
colorThemingColorText = .white
}
} else {
self.themingColorText[account] = .white
colorThemingColorText = .white
}

} else {

// THEMING COLOR
self.themingColor[account] = customer
colorThemingColor = customer

// THEMING COLOR ELEMENT (control isTooLight / isTooDark)
if self.customer.isTooLight() {
if let color = customer.darker(by: darker) {
self.themingColorElement[account] = color
colorThemingColorElement = color
}
} else if customer.isTooDark() {
if let color = customer.lighter(by: lighter) {
self.themingColorElement[account] = color
colorThemingColorElement = color
}
} else {
self.themingColorElement[account] = customer
colorThemingColorElement = customer
}

// THEMING COLOR TEXT
self.themingColorText[account] = customerText
colorThemingColorText = customerText
}

if self.themingColor[account] != colorThemingColor || self.themingColorElement[account] != colorThemingColorElement || self.themingColorText[account] != colorThemingColorText {

self.themingColor[account] = colorThemingColor
self.themingColorElement[account] = colorThemingColorElement
self.themingColorText[account] = colorThemingColorText

return true
}

return false
}

public func getTheming(account: String?) -> UIColor {
Expand Down
1 change: 1 addition & 0 deletions iOSClient/Favorites/NCFavorite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class NCFavorite: NCCollectionViewCommon {
}
self.reloadDataSource()
}
self.refreshControl.endRefreshing()
}
}
}
27 changes: 18 additions & 9 deletions iOSClient/Files/NCFiles.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import UIKit
import NextcloudKit
import RealmSwift

class NCFiles: NCCollectionViewCommon {
internal var isRoot: Bool = true
Expand Down Expand Up @@ -77,6 +78,7 @@ class NCFiles: NCCollectionViewCommon {
self.titleCurrentFolder = self.getNavigationTitle()
self.setNavigationLeftItems()

self.dataSource.removeAll()
self.reloadDataSource()
self.getServerData()
}
Expand Down Expand Up @@ -120,6 +122,7 @@ class NCFiles: NCCollectionViewCommon {
override func reloadDataSource() {
var predicate = self.defaultPredicate
let predicateDirectory = NSPredicate(format: "account == %@ AND serverUrl == %@", session.account, self.serverUrl)
let dataSourceMetadatas = self.dataSource.getMetadatas()

if NCKeychain().getPersonalFilesOnly(account: session.account) {
predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND (ownerId == %@ || ownerId == '') AND mountType == '' AND NOT (status IN %@)", session.account, self.serverUrl, session.userId, global.metadataStatusHideInView)
Expand All @@ -131,7 +134,16 @@ class NCFiles: NCCollectionViewCommon {
let results = self.database.getResultsMetadatasPredicate(predicate, layoutForView: layoutForView)
self.dataSource = NCCollectionViewDataSource(results: results, layoutForView: layoutForView)

super.reloadDataSource()
if let results {
let metadatas = Array(results.freeze())
self.dataSource.updateMetadataIndexPath(metadatas: metadatas, dataSourceMetadatas: dataSourceMetadatas) { updated in
if updated || self.isNumberOfItemsInAllSectionsNull {
super.reloadDataSource()
}
}
} else {
super.reloadDataSource()
}
}

override func getServerData() {
Expand Down Expand Up @@ -168,16 +180,13 @@ class NCFiles: NCCollectionViewCommon {
}

self.richWorkspaceText = tableDirectory?.richWorkspace
}

if reloadDataSource {
// (self.collectionView.collectionViewLayout as? NCMediaLayout)?.invalidate()
self.reloadDataSource()
} else if self.dataSource.isEmpty() {
self.collectionView.reloadData()
}
} else if self.dataSource.isEmpty() {
self.collectionView.reloadData()
if reloadDataSource || self.isNumberOfItemsInAllSectionsNull {
self.reloadDataSource()
}

self.refreshControl.endRefreshing()
}
}
}
Expand Down
1 change: 1 addition & 0 deletions iOSClient/Groupfolders/NCGroupfolders.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class NCGroupfolders: NCCollectionViewCommon {
self.reloadDataSource()
}
}
self.refreshControl.endRefreshing()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ extension NCCollectionViewCommon: UICollectionViewDelegate {
} else {
let image = utility.getImage(ocId: metadata.ocId, etag: metadata.etag, ext: NCGlobal.shared.previewExt1024)
if !metadata.isDirectoryE2EE, (metadata.isImage || metadata.isAudioOrVideo) {

Check warning on line 64 in iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDelegate.swift

View workflow job for this annotation

GitHub Actions / Lint

Control Statement Violation: `if`, `for`, `guard`, `switch`, `while`, and `catch` statements shouldn't unnecessarily wrap their conditionals or arguments in parentheses (control_statement)
let metadatas = self.dataSource.getResultMetadatas()
let metadatas = self.dataSource.getMetadatas()
let ocIds = metadatas.filter { $0.classFile == NKCommon.TypeClassFile.image.rawValue ||
$0.classFile == NKCommon.TypeClassFile.video.rawValue ||
$0.classFile == NKCommon.TypeClassFile.audio.rawValue }.map(\.ocId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ import NextcloudKit

extension NCCollectionViewCommon: NCCollectionViewCommonSelectTabBarDelegate {
func selectAll() {
if !fileSelect.isEmpty, self.dataSource.getResultMetadatas().count == fileSelect.count {
if !fileSelect.isEmpty, self.dataSource.getMetadatas().count == fileSelect.count {
fileSelect = []
} else {
fileSelect = self.dataSource.getResultMetadatas().compactMap({ $0.ocId })
fileSelect = self.dataSource.getMetadatas().compactMap({ $0.ocId })
}
tabBarSelect.update(fileSelect: fileSelect, metadatas: getSelectedMetadatas(), userId: session.userId)
collectionView.reloadData()
self.reloadDataSource()
}

func delete() {
Expand Down Expand Up @@ -135,6 +135,6 @@ extension NCCollectionViewCommon: NCCollectionViewCommonSelectTabBarDelegate {
navigationController?.interactivePopGestureRecognizer?.isEnabled = !editMode
navigationItem.hidesBackButton = editMode
searchController(enabled: !editMode)
collectionView.reloadData()
self.reloadDataSource()
}
}
45 changes: 24 additions & 21 deletions iOSClient/Main/Collection Common/NCCollectionViewCommon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
return predicate
}

var isNumberOfItemsInAllSectionsNull: Bool {
var totalItems = 0
for section in 0..<self.collectionView.numberOfSections {
totalItems += self.collectionView.numberOfItems(inSection: section)
}
return totalItems == 0
}

// MARK: - View Life Cycle

override func viewDidLoad() {
Expand Down Expand Up @@ -172,6 +180,7 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
collectionView.refreshControl = refreshControl
refreshControl.action(for: .valueChanged) { _ in
self.database.cleanEtagDirectory(serverUrl: self.serverUrl, account: self.session.account)
self.dataSource.removeAll()
self.getServerData()
}

Expand Down Expand Up @@ -348,7 +357,7 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
}

@objc func changeTheming(_ notification: NSNotification) {
collectionView.reloadData()
self.reloadDataSource()
}

@objc func changeLayout(_ notification: NSNotification) {
Expand All @@ -370,7 +379,7 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
layoutForView.layout = layoutForView.layout
self.layoutType = layoutForView.layout

self.collectionView.reloadData()
self.reloadDataSource()

switch layoutForView.layout {
case global.layoutList:
Expand Down Expand Up @@ -819,7 +828,7 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS

NCKeychain().showDescription = !showDescriptionKeychain

self.collectionView.reloadData()
self.reloadDataSource()
self.setNavigationRightItems()
}
showDescription.subtitle = richWorkspaceText == nil ? NSLocalizedString("_no_description_available_", comment: "") : ""
Expand Down Expand Up @@ -901,7 +910,7 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
isSearchingMode = true
self.providers?.removeAll()
self.dataSource.removeAll()
self.collectionView.reloadData()
self.reloadDataSource()
// TIP
dismissTip()
}
Expand Down Expand Up @@ -1025,17 +1034,11 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
@objc func reloadDataSource() {
guard !session.account.isEmpty, !self.isSearchingMode else { return }

/*
let animator = UIViewPropertyAnimator(duration: 0.3, curve: .easeInOut) {
self.collectionView?.collectionViewLayout.invalidateLayout()

}
animator.startAnimation()
*/

self.collectionView?.reloadData()
self.refreshControl.endRefreshing()
self.setNavigationRightItems()
UIView.transition(with: self.collectionView,
duration: 0.20,
options: .transitionCrossDissolve,
animations: { self.collectionView.reloadData() },
completion: nil)
}

func getServerData() {
Expand All @@ -1050,12 +1053,12 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS

self.dataSource.removeAll()
self.refreshControl.beginRefreshing()
self.collectionView.reloadData()
self.reloadDataSource()

if NCCapabilities.shared.getCapabilities(account: session.account).capabilityServerVersionMajor >= global.nextcloudVersion20 {
NCNetworking.shared.unifiedSearchFiles(literal: literalSearch, account: session.account) { task in
self.dataSourceTask = task
self.collectionView.reloadData()
self.reloadDataSource()
} providers: { _, searchProviders in
self.providers = searchProviders
self.searchResults = []
Expand All @@ -1065,16 +1068,16 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
NCNetworking.shared.unifiedSearchQueue.addOperation(NCCollectionViewUnifiedSearch(collectionViewCommon: self, metadatas: metadatas, searchResult: searchResult))
} completion: { _, _ in
self.refreshControl.endRefreshing()
self.collectionView.reloadData()
self.reloadDataSource()
}
} else {
NCNetworking.shared.searchFiles(literal: literalSearch, account: session.account) { task in
self.dataSourceTask = task
self.collectionView.reloadData()
self.reloadDataSource()
} completion: { metadatas, error in
DispatchQueue.main.async {
self.refreshControl.endRefreshing()
self.collectionView.reloadData()
self.reloadDataSource()
}
guard let metadatas, error == .success, self.isSearchingMode else { return }
let ocId = metadatas.map { $0.ocId }
Expand All @@ -1093,7 +1096,7 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS

NCNetworking.shared.unifiedSearchFilesProvider(id: lastSearchResult.id, term: term, limit: 5, cursor: cursor, account: session.account) { task in
self.dataSourceTask = task
self.collectionView.reloadData()
self.reloadDataSource()
} completion: { _, searchResult, metadatas, error in
if error != .success {
NCContentPresenter().showError(error: error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ extension NCCollectionViewCommon {
self.currentScale = 1.0

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

self.collectionView.reloadData()
self.collectionView.collectionViewLayout.invalidateLayout()

} completion: { _ in

if let layoutForView = self.database.getLayoutForView(account: self.session.account, key: NCGlobal.shared.layoutViewFiles, serverUrl: self.serverUrl) {
Expand Down
Loading

0 comments on commit 3c1b092

Please sign in to comment.