// // NCRecent.swift // Nextcloud // // Created by Marino Faggiana on 29/09/2020. // Copyright © 2020 Marino Faggiana. All rights reserved. // // Author Marino Faggiana // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // import UIKit import NextcloudKit class NCRecent: NCCollectionViewCommon { required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) titleCurrentFolder = NSLocalizedString("_recent_", comment: "") layoutKey = NCGlobal.shared.layoutViewRecent enableSearchBar = false headerRichWorkspaceDisable = true emptyImageName = "clock.arrow.circlepath" emptyTitle = "_files_no_files_" emptyDescription = "" } // MARK: - View Life Cycle override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) reloadDataSource() } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) getServerData() } // MARK: - DataSource override func reloadDataSource() { var metadatas: [tableMetadata] = [] if let results = self.database.getResultsMetadatas(predicate: NSPredicate(format: "account == %@ AND fileName != '.'", session.account), sortedByKeyPath: "date", ascending: false) { metadatas = Array(results.freeze()) } layoutForView?.sort = "date" layoutForView?.ascending = false layoutForView?.directoryOnTop = false self.dataSource = NCCollectionViewDataSource(metadatas: metadatas, layoutForView: layoutForView) super.reloadDataSource() } override func getServerData() { let requestBodyRecent = """ %@ infinity %@ /Users/marinofaggiana/Developer/ios/iOSClient/Assistant 100 """ let dateFormatter = DateFormatter() dateFormatter.locale = Locale(identifier: "en_US_POSIX") dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ" let lessDateString = dateFormatter.string(from: Date()) let requestBody = String(format: requestBodyRecent, "/files/" + session.userId, lessDateString) NextcloudKit.shared.searchBodyRequest(serverUrl: session.urlBase, requestBody: requestBody, showHiddenFiles: NCKeychain().showHiddenFiles, account: session.account) { task in self.dataSourceTask = task if self.dataSource.isEmpty() { self.collectionView.reloadData() } } completion: { _, files, _, error in if error == .success, let files { self.database.convertFilesToMetadatas(files, useFirstAsMetadataFolder: false) { _, metadatas in // Add metadatas self.database.addMetadatas(metadatas) self.reloadDataSource() } } self.refreshControl.endRefreshing() } } }