Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 28c5001
Author: Milen Pivchev <[email protected]>
Date:   Fri Oct 20 13:08:44 2023 +0200

    WIP

    Signed-off-by: Milen Pivchev <[email protected]>

commit a67c7a0
Merge: b42a1c8 510bdb4
Author: Milen Pivchev <[email protected]>
Date:   Fri Oct 20 13:05:10 2023 +0200

    Merge branch 'develop' of https://github.com/nextcloud/ios into media-view-performance-optimizations

    Signed-off-by: Milen Pivchev <[email protected]>

    # Conflicts:
    #	iOSClient/Media/NCMediaCache.swift

commit 510bdb4
Author: Marino Faggiana <[email protected]>
Date:   Fri Oct 20 12:17:49 2023 +0200

    lint

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

commit 6a560e3
Author: Marino Faggiana <[email protected]>
Date:   Fri Oct 20 12:17:11 2023 +0200

    improvements

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

commit a232a02
Author: Marino Faggiana <[email protected]>
Date:   Fri Oct 20 12:04:24 2023 +0200

    lint

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

commit 74d5302
Author: Marino Faggiana <[email protected]>
Date:   Fri Oct 20 12:01:26 2023 +0200

    lint

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

commit 3de2089
Author: Marino Faggiana <[email protected]>
Date:   Fri Oct 20 11:41:07 2023 +0200

    lint

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

commit 01b7014
Author: Nextcloud bot <[email protected]>
Date:   Fri Oct 20 02:42:09 2023 +0000

    Fix(l10n): Update translations from Transifex

    Signed-off-by: Nextcloud bot <[email protected]>

commit b42a1c8
Author: Milen Pivchev <[email protected]>
Date:   Thu Oct 19 18:49:44 2023 +0200

    WIP

    Signed-off-by: Milen Pivchev <[email protected]>

commit 073c390
Merge: 2475daa 928a088
Author: Milen Pivchev <[email protected]>
Date:   Thu Oct 19 18:18:43 2023 +0200

    Merge branch '2399-aspect-ratio-in-media-view' into media-view-performance-optimizations

    Signed-off-by: Milen Pivchev <[email protected]>

    # Conflicts:
    #	Nextcloud.xcodeproj/project.pbxproj

commit 472b560
Author: Marino Faggiana <[email protected]>
Date:   Thu Oct 19 17:43:49 2023 +0200

    update NextcloudNit

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

commit 2475daa
Merge: 6012f3b 4ca1fbe
Author: Milen Pivchev <[email protected]>
Date:   Thu Oct 19 14:33:34 2023 +0200

    Merge branch '2399-aspect-ratio-in-media-view' of https://github.com/nextcloud/ios into media-view-performance-optimizations

commit 6012f3b
Merge: f55c8fb 9747ad8
Author: Milen Pivchev <[email protected]>
Date:   Thu Oct 19 12:28:17 2023 +0200

    Merge branch '2399-aspect-ratio-in-media-view' of https://github.com/nextcloud/ios into media-view-performance-optimizations

commit f55c8fb
Author: Milen Pivchev <[email protected]>
Date:   Thu Oct 19 12:24:03 2023 +0200

    WIP

    Signed-off-by: Milen Pivchev <[email protected]>

commit 236c98b
Author: Marino Faggiana <[email protected]>
Date:   Tue Oct 17 16:42:52 2023 +0200

    verify etag

commit 7b2eab2
Author: Marino Faggiana <[email protected]>
Date:   Tue Oct 17 12:28:22 2023 +0200

    Update NCMediaManager.swift

commit 1cbe26b
Author: Marino Faggiana <[email protected]>
Date:   Tue Oct 17 12:23:01 2023 +0200

    optimized

commit 6267266
Author: Marino Faggiana <[email protected]>
Date:   Tue Oct 17 11:24:59 2023 +0200

    Update NCMediaManager.swift

commit 7ecc1b4
Author: Marino Faggiana <[email protected]>
Date:   Tue Oct 17 11:22:34 2023 +0200

    Update AppDelegate.swift

commit 062c224
Author: Marino Faggiana <[email protected]>
Date:   Tue Oct 17 11:15:32 2023 +0200

    Update NCMediaManager.swift

commit 7357d6a
Author: Marino Faggiana <[email protected]>
Date:   Tue Oct 17 11:12:24 2023 +0200

    Update NCMediaManager.swift

commit 2a866d6
Author: Marino Faggiana <[email protected]>
Date:   Tue Oct 17 11:10:43 2023 +0200

    Update NCMediaManager.swift

commit 3212e92
Author: Marino Faggiana <[email protected]>
Date:   Tue Oct 17 11:09:07 2023 +0200

    Update NCMediaManager.swift

commit d493847
Author: Marino Faggiana <[email protected]>
Date:   Tue Oct 17 11:03:36 2023 +0200

    Update NCMediaManager.swift

commit e9a5d97
Author: Marino Faggiana <[email protected]>
Date:   Tue Oct 17 11:02:11 2023 +0200

    Update NCMediaManager.swift

commit e73c0af
Author: Marino Faggiana <[email protected]>
Date:   Tue Oct 17 10:58:25 2023 +0200

    Update AppDelegate.swift

commit eee1b89
Author: Marino Faggiana <[email protected]>
Date:   Tue Oct 17 10:55:28 2023 +0200

    Update NCMediaManager.swift

commit 753841f
Author: Marino Faggiana <[email protected]>
Date:   Tue Oct 17 10:52:25 2023 +0200

    Update NCMediaManager.swift

commit 7b94537
Author: Marino Faggiana <[email protected]>
Date:   Tue Oct 17 10:50:13 2023 +0200

    coding

commit b7bb1fe
Author: Marino Faggiana <[email protected]>
Date:   Mon Oct 16 18:13:11 2023 +0200

    test

    test

commit 873df2d
Author: Milen Pivchev <[email protected]>
Date:   Fri Oct 13 19:30:38 2023 +0200

    More optimizations

    Signed-off-by: Milen Pivchev <[email protected]>

commit fcf9f5a
Author: Milen Pivchev <[email protected]>
Date:   Fri Oct 13 18:56:48 2023 +0200

    Replace LazyVStack with List

    Signed-off-by: Milen Pivchev <[email protected]>

commit 449641c
Author: Milen Pivchev <[email protected]>
Date:   Fri Oct 13 18:21:29 2023 +0200

    Fix some rows not updating on rotate/zoom

    Signed-off-by: Milen Pivchev <[email protected]>

commit 7091d2e
Author: Milen Pivchev <[email protected]>
Date:   Fri Oct 13 18:14:39 2023 +0200

    WIP

    Signed-off-by: Milen Pivchev <[email protected]>

commit bc571bc
Author: Milen Pivchev <[email protected]>
Date:   Fri Oct 13 12:12:04 2023 +0200

    Refactor

    Signed-off-by: Milen Pivchev <[email protected]>

commit 7172b43
Author: Milen Pivchev <[email protected]>
Date:   Fri Oct 13 10:48:32 2023 +0200

    WIP

    Signed-off-by: Milen Pivchev <[email protected]>

commit 3f97177
Author: Milen Pivchev <[email protected]>
Date:   Fri Oct 13 10:46:13 2023 +0200

    WIP

    Signed-off-by: Milen Pivchev <[email protected]>

commit 6cd03fc
Author: Milen Pivchev <[email protected]>
Date:   Thu Oct 12 18:57:26 2023 +0200

    Fix scroll offset bug

    Signed-off-by: Milen Pivchev <[email protected]>

commit adc5670
Author: Milen Pivchev <[email protected]>
Date:   Thu Oct 12 17:43:13 2023 +0200

    WIP

    Signed-off-by: Milen Pivchev <[email protected]>

commit 15ed955
Author: Milen Pivchev <[email protected]>
Date:   Wed Oct 11 18:04:33 2023 +0200

    WIP

    Signed-off-by: Milen Pivchev <[email protected]>

commit 1106224
Author: Milen Pivchev <[email protected]>
Date:   Wed Oct 11 14:43:38 2023 +0200

    Fix compile

    Signed-off-by: Milen Pivchev <[email protected]>

commit f668bdb
Merge: 5ac583b ae6b884
Author: Milen Pivchev <[email protected]>
Date:   Wed Oct 11 12:36:11 2023 +0200

    Merge branch '2399-aspect-ratio-in-media-view' of https://github.com/nextcloud/ios into media-view-performance-optimizations

commit 5ac583b
Author: Milen Pivchev <[email protected]>
Date:   Wed Oct 11 12:10:37 2023 +0200

    WIP

    Signed-off-by: Milen Pivchev <[email protected]>

commit 0105110
Author: Milen Pivchev <[email protected]>
Date:   Wed Oct 11 12:08:14 2023 +0200

    WIP

    Signed-off-by: Milen Pivchev <[email protected]>

commit 40011df
Author: Milen Pivchev <[email protected]>
Date:   Wed Oct 11 12:06:54 2023 +0200

    WIP

    Signed-off-by: Milen Pivchev <[email protected]>

commit 7f948e1
Merge: 2d8cfb7 b8b08f4
Author: Milen Pivchev <[email protected]>
Date:   Wed Oct 11 11:34:47 2023 +0200

    Merge branch 'media-view-performance-optimizations' of https://github.com/nextcloud/ios into media-view-performance-optimizations

commit 2d8cfb7
Author: Milen Pivchev <[email protected]>
Date:   Wed Oct 11 11:34:33 2023 +0200

    WIP

    Signed-off-by: Milen Pivchev <[email protected]>

commit b8b08f4
Merge: 60de2d9 01f2c31
Author: Marino Faggiana <[email protected]>
Date:   Wed Oct 11 10:36:43 2023 +0200

    Merge branch 'media-view-performance-optimizations' of https://github.com/nextcloud/ios into media-view-performance-optimizations

commit 01f2c31
Author: Milen Pivchev <[email protected]>
Date:   Tue Oct 10 18:55:22 2023 +0200

    WIP

    Signed-off-by: Milen Pivchev <[email protected]>

commit b0f4b46
Author: Milen Pivchev <[email protected]>
Date:   Tue Oct 10 18:52:34 2023 +0200

    WIP

    Signed-off-by: Milen Pivchev <[email protected]>

commit 134c91e
Author: Milen Pivchev <[email protected]>
Date:   Tue Oct 10 18:05:08 2023 +0200

    WIP

    Signed-off-by: Milen Pivchev <[email protected]>

commit fce4423
Author: Milen Pivchev <[email protected]>
Date:   Tue Oct 10 17:31:32 2023 +0200

    WIP

    Signed-off-by: Milen Pivchev <[email protected]>

commit bdb5f4f
Author: Milen Pivchev <[email protected]>
Date:   Tue Oct 10 16:56:04 2023 +0200

    WIP

    Signed-off-by: Milen Pivchev <[email protected]>

commit ffa930a
Author: Milen Pivchev <[email protected]>
Date:   Tue Oct 10 14:15:50 2023 +0200

    WIP

    Signed-off-by: Milen Pivchev <[email protected]>

commit 60de2d9
Author: Marino Faggiana <[email protected]>
Date:   Tue Oct 10 11:13:54 2023 +0200

    test

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

commit 179993a
Author: Milen Pivchev <[email protected]>
Date:   Mon Oct 9 19:37:35 2023 +0200

    WIP

    Signed-off-by: Milen Pivchev <[email protected]>

commit 6620406
Author: Milen Pivchev <[email protected]>
Date:   Mon Oct 9 17:08:56 2023 +0200

    WIP

    Signed-off-by: Milen Pivchev <[email protected]>

commit 221a248
Author: Milen Pivchev <[email protected]>
Date:   Mon Oct 9 15:53:01 2023 +0200

    Add thread-safe LRU Cache

    Signed-off-by: Milen Pivchev <[email protected]>

commit d895e51
Author: Milen Pivchev <[email protected]>
Date:   Mon Oct 9 15:00:59 2023 +0200

    WIP

    Signed-off-by: Milen Pivchev <[email protected]>

commit d46aee2
Author: Milen Pivchev <[email protected]>
Date:   Fri Oct 6 13:21:25 2023 +0200

    WIP

    Signed-off-by: Milen Pivchev <[email protected]>

commit 14b29c4
Author: Milen Pivchev <[email protected]>
Date:   Fri Oct 6 13:17:16 2023 +0200

    WIP

    Signed-off-by: Milen Pivchev <[email protected]>

commit fb65918
Author: Milen Pivchev <[email protected]>
Date:   Fri Oct 6 11:08:31 2023 +0200

    WIP

    Signed-off-by: Milen Pivchev <[email protected]>

commit 7c77620
Author: Milen Pivchev <[email protected]>
Date:   Thu Oct 5 19:29:33 2023 +0200

    WIP

    Signed-off-by: Milen Pivchev <[email protected]>

commit 689f441
Author: Milen Pivchev <[email protected]>
Date:   Thu Oct 5 17:04:18 2023 +0200

    WIP

    Signed-off-by: Milen Pivchev <[email protected]>

commit fb7b1c6
Author: Milen Pivchev <[email protected]>
Date:   Thu Oct 5 15:11:05 2023 +0200

    WIP

    Signed-off-by: Milen Pivchev <[email protected]>

commit 9d4a71c
Author: Milen Pivchev <[email protected]>
Date:   Thu Oct 5 13:36:20 2023 +0200

    Change LRU cache

    Signed-off-by: Milen Pivchev <[email protected]>

commit 28d5092
Author: Milen Pivchev <[email protected]>
Date:   Wed Oct 4 17:52:35 2023 +0200

    WIP

    Signed-off-by: Milen Pivchev <[email protected]>

Signed-off-by: Milen Pivchev <[email protected]>
  • Loading branch information
mpivchev authored and marinofaggiana committed Oct 31, 2023
1 parent 8468214 commit 8b22e71
Show file tree
Hide file tree
Showing 9 changed files with 372 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ struct DeviceOrientationViewModifier: ViewModifier {
}
}

struct ViewDidFirstAppearModifier: ViewModifier {
@State private var didLoad = false
private let action: (() -> Void)?

init(perform action: (() -> Void)? = nil) {
self.action = action
}

func body(content: Content) -> some View {
content.onAppear {
if didLoad == false {
didLoad = true
action?()
}
}
}
}

extension SwiftUI.View {
func toVC() -> UIViewController {
let vc = UIHostingController (rootView: self)
Expand All @@ -47,4 +65,8 @@ extension SwiftUI.View {
func onRotate(perform action: @escaping (UIDeviceOrientation) -> Void) -> some View {
self.modifier(DeviceOrientationViewModifier(action: action))
}

func onFirstAppear(perform action: (() -> Void)? = nil) -> some View {
modifier(ViewDidFirstAppearModifier(perform: action))
}
}
32 changes: 20 additions & 12 deletions iOSClient/Media/Cell/NCMediaCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

import SwiftUI
import VisibilityTrackingScrollView
import Shimmer
import NextcloudKit

Expand All @@ -30,7 +29,6 @@ struct NCMediaCell: View {
var body: some View {
let image = Image(uiImage: thumbnail.image)
.resizable()
.trackVisibility(id: CCUtility.getTitleSectionDate(thumbnail.metadata.date as Date) ?? "")

ZStack(alignment: .center) {
if thumbnail.isPlaceholderImage {
Expand Down Expand Up @@ -136,7 +134,7 @@ struct NCMediaCell: View {
struct NCMediaLoadingCell: View {
let itemsInRow: Int
let metadata: tableMetadata
let geometryProxy: GeometryProxy
let rowSize: CGFloat
let spacing: CGFloat

let gradient = Gradient(colors: [
Expand All @@ -146,14 +144,24 @@ struct NCMediaLoadingCell: View {
])

var body: some View {
ZStack {
Image(uiImage: UIImage())
.resizable()
.trackVisibility(id: CCUtility.getTitleSectionDate(metadata.date as Date) ?? "")// TODO: Fix spacing
.aspectRatio(1.5, contentMode: .fit)
.frame(width: (geometryProxy.size.width - spacing) / CGFloat(itemsInRow))
.redacted(reason: .placeholder)
.shimmering(gradient: gradient, bandSize: 0.7)
}
// let _ = Self._printChanges()

Image(uiImage: UIImage())
.resizable()
// .background(Color.random)
.aspectRatio(1.5, contentMode: .fit)
.frame(width: (UIScreen.main.bounds.width - spacing) / CGFloat(itemsInRow))
.redacted(reason: .placeholder)
.shimmering(gradient: gradient, bandSize: 0.7)
}
}

extension Color {
static var random: Color {
return Color(
red: .random(in: 0...1),
green: .random(in: 0...1),
blue: .random(in: 0...1)
)
}
}
109 changes: 109 additions & 0 deletions iOSClient/Media/NCMediaCache.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//
// NCMediaCache.swift
// Nextcloud
//
// Created by Marino Faggiana on 18/10/23.
// Copyright © 2021 Marino Faggiana. All rights reserved.
//
// Author Marino Faggiana <[email protected]>
//
// 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 <http://www.gnu.org/licenses/>.
//

import UIKit
import LRUCache
import NextcloudKit

@objc class NCMediaCache: NSObject {

@objc public static let shared: NCMediaCache = {
let instance = NCMediaCache()
return instance
}()

let limit: Int = 2000
private typealias ThumbnailLRUCache = LRUCache<String, UIImage>
private lazy var cache: ThumbnailLRUCache = {
return ThumbnailLRUCache(countLimit: limit)
}()

func createCache(account: String) {

let resultsMedia = NCManageDatabase.shared.getMediaOcIdEtag(account: account)
guard !resultsMedia.isEmpty,
let directory = CCUtility.getDirectoryProviderStorage() else { return }

let ext = ".preview.ico"
let manager = FileManager.default
let resourceKeys = Set<URLResourceKey>([.nameKey, .pathKey, .fileSizeKey, .creationDateKey])
struct FileInfo {
var path: URL
var ocId: String
var date: Date
}
var files: [FileInfo] = []
let startDate = Date()

if let enumerator = manager.enumerator(at: URL(fileURLWithPath: directory), includingPropertiesForKeys: [.isRegularFileKey], options: [.skipsHiddenFiles]) {
for case let fileURL as URL in enumerator where fileURL.lastPathComponent.hasSuffix(ext) {
let fileName = fileURL.lastPathComponent
let ocId = fileURL.deletingLastPathComponent().lastPathComponent
guard let resourceValues = try? fileURL.resourceValues(forKeys: resourceKeys),
let size = resourceValues.fileSize,
size > 0,
let date = resourceValues.creationDate,
let etag = resultsMedia[ocId],
fileName == etag + ext else { continue }
files.append(FileInfo(path: fileURL, ocId: ocId, date: date))
}
}

files.sort(by: { $0.date > $1.date })
if let firstDate = files.first?.date, let lastDate = files.last?.date {
print("First date: \(firstDate)")
print("Last date: \(lastDate)")
}

cache.removeAllValues()
var counter: Int = 0
for file in files {
counter += 1
if counter > limit { break }
autoreleasepool {
if let image = UIImage(contentsOfFile: file.path.path) {
cache.setValue(image, forKey: file.ocId)
}
}
}

let endDate = Date()
let diffDate = endDate.timeIntervalSinceReferenceDate - startDate.timeIntervalSinceReferenceDate
NextcloudKit.shared.nkCommonInstance.writeLog("--------- ThumbnailLRUCache image process ---------")
NextcloudKit.shared.nkCommonInstance.writeLog("Counter process: \(cache.count)")
NextcloudKit.shared.nkCommonInstance.writeLog("Time process: \(diffDate)")
NextcloudKit.shared.nkCommonInstance.writeLog("--------- ThumbnailLRUCache image process ---------")
}

func getImage(ocId: String) -> UIImage? {
return cache.value(forKey: ocId)
}

func setImage(ocId: String, image: UIImage) {
cache.setValue(image, forKey: ocId)
}

@objc func clearCache() {
cache.removeAllValues()
}
}
29 changes: 21 additions & 8 deletions iOSClient/Media/NCMediaRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,45 @@

import SwiftUI
import PreviewSnapshots
import VisibilityTrackingScrollView
import Queuer

struct NCMediaRow: View, Equatable {
static func == (lhs: NCMediaRow, rhs: NCMediaRow) -> Bool {
return lhs.metadatas == rhs.metadatas
}

struct NCMediaRow: View {
let metadatas: [tableMetadata]
let geometryProxy: GeometryProxy

@Binding var isInSelectMode: Bool
let onCellSelected: (ScaledThumbnail, Bool) -> Void
let onCellContextMenuItemSelected: (ScaledThumbnail, ContextMenuSelection) -> Void
@StateObject private var vm = NCMediaRowViewModel()
private let spacing: CGFloat = 2

var body: some View {
let _ = Self._printChanges()

Check warning on line 27 in iOSClient/Media/NCMediaRow.swift

View workflow job for this annotation

GitHub Actions / Lint

Redundant Discardable Let Violation: Prefer `_ = foo()` over `let _ = foo()` when discarding a result from a function (redundant_discardable_let)

HStack(spacing: spacing) {
if vm.rowData.scaledThumbnails.isEmpty {
ForEach(metadatas, id: \.self) { metadata in
NCMediaLoadingCell(itemsInRow: metadatas.count, metadata: metadata, geometryProxy: geometryProxy, spacing: spacing)
ForEach(metadatas, id: \.ocId) { metadata in
NCMediaLoadingCell(itemsInRow: metadatas.count, metadata: metadata, rowSize: UIScreen.main.bounds.width, spacing: spacing)
}
} else {
ForEach(vm.rowData.scaledThumbnails, id: \.self) { thumbnail in
ForEach(vm.rowData.scaledThumbnails, id: \.metadata.ocId) { thumbnail in
NCMediaCell(thumbnail: thumbnail, shrinkRatio: vm.rowData.shrinkRatio, isInSelectMode: $isInSelectMode, onSelected: onCellSelected, onContextMenuItemSelected: onCellContextMenuItemSelected, isFavorite: thumbnail.metadata.favorite)
}
}
}
.onAppear {
.onFirstAppear {
vm.configure(metadatas: metadatas)
vm.downloadThumbnails(rowWidth: UIScreen.main.bounds.width, spacing: spacing)
}
.onDisappear {
vm.cancelDownloadingThumbnails()
}
.onRotate { _ in
vm.configure(metadatas: metadatas)
vm.downloadThumbnails(rowWidth: geometryProxy.size.width, spacing: spacing)
vm.downloadThumbnails(rowWidth: UIScreen.main.bounds.width, spacing: spacing)
}
}
}
Loading

0 comments on commit 8b22e71

Please sign in to comment.