From 1e23809359ecd7cca23f5e286bf9448215bb3521 Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Tue, 23 May 2023 18:49:14 +0200 Subject: [PATCH] Improve history view performance --- Shared/Home/HistoryView.swift | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/Shared/Home/HistoryView.swift b/Shared/Home/HistoryView.swift index ad2726aa..26efb06d 100644 --- a/Shared/Home/HistoryView.swift +++ b/Shared/Home/HistoryView.swift @@ -8,6 +8,12 @@ struct HistoryView: View { @ObservedObject private var player = PlayerModel.shared + @State private var visibleWatches = [Watch]() + + init(limit: Int = 10) { + self.limit = limit + } + var body: some View { LazyVStack { if visibleWatches.isEmpty { @@ -30,14 +36,13 @@ struct HistoryView: View { } } .animation(nil, value: visibleWatches) - .onAppear { - visibleWatches - .forEach(player.loadHistoryVideoDetails) - } + .onAppear(perform: reloadVisibleWatches) + .onChange(of: player.currentVideo) { _ in reloadVisibleWatches() } } - private var visibleWatches: [Watch] { - Array(watches.filter { $0.videoID != player.currentVideo?.videoID }.prefix(limit)) + func reloadVisibleWatches() { + visibleWatches = Array(watches.filter { $0.videoID != player.currentVideo?.videoID }.prefix(limit)) + visibleWatches.forEach(player.loadHistoryVideoDetails) } }