From 4a26ef2839a86ecc6cd3ae118691c7671f09765e Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Thu, 10 Nov 2022 21:46:37 +0100 Subject: [PATCH] Fix clear history, add clear cache --- Model/CacheModel.swift | 5 ++++ Shared/Settings/HistorySettings.swift | 38 +++++++++++++-------------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/Model/CacheModel.swift b/Model/CacheModel.swift index af9f5d99..8b8d4c92 100644 --- a/Model/CacheModel.swift +++ b/Model/CacheModel.swift @@ -22,4 +22,9 @@ struct CacheModel { let jsonTransformer = Transformer(toData: toData, fromData: fromData) videoStorage = try? Storage(diskConfig: videoStorageConfig, memoryConfig: videoStorageMemoryConfig, transformer: jsonTransformer) } + + func removeAll() { + try? videoStorage?.removeAll() + try? urlBookmarksStorage?.removeAll() + } } diff --git a/Shared/Settings/HistorySettings.swift b/Shared/Settings/HistorySettings.swift index 7990722b..a7673e9c 100644 --- a/Shared/Settings/HistorySettings.swift +++ b/Shared/Settings/HistorySettings.swift @@ -4,9 +4,8 @@ import SwiftUI struct HistorySettings: View { static let watchedThresholds = [50, 60, 70, 80, 90, 95, 100] - @State private var presentingClearHistoryConfirmation = false - @EnvironmentObject private var player + @EnvironmentObject private var settings @Default(.saveRecents) private var saveRecents @Default(.saveLastPlayed) private var saveLastPlayed @@ -143,25 +142,26 @@ struct HistorySettings: View { } private var clearHistoryButton: some View { - Button("Clear History") { - presentingClearHistoryConfirmation = true - } - .alert(isPresented: $presentingClearHistoryConfirmation) { - Alert( - title: Text( - "Are you sure you want to clear history of watched videos?" - ), - message: Text( - "This cannot be reverted. You might need to switch between views or restart the app to see changes." - ), - primaryButton: .destructive(Text("Clear All")) { - player.removeAllWatches() - presentingClearHistoryConfirmation = false - }, - secondaryButton: .cancel() + Button { + settings.presentAlert( + Alert( + title: Text( + "Are you sure you want to clear history of watched videos?" + ), + message: Text( + "This cannot be reverted. You might need to switch between views or restart the app to see changes." + ), + primaryButton: .destructive(Text("Clear All")) { + player.removeAllWatches() + CacheModel.shared.removeAll() + }, + secondaryButton: .cancel() + ) ) + } label: { + Text("Clear History") + .foregroundColor(.red) } - .foregroundColor(.red) } }