This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #4835: Add the ability to remove site-specific web data
- Loading branch information
1 parent
8ae3f1c
commit 7cb7b0f
Showing
4 changed files
with
287 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
// Copyright 2022 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
import Foundation | ||
import SwiftUI | ||
import WebKit | ||
import BraveUI | ||
import struct Shared.Strings | ||
|
||
struct ManageWebsiteDataView: View { | ||
@State private var isLoading: Bool = false | ||
@State private var dataRecords: [WKWebsiteDataRecord] = [] | ||
@State private var filter: String = "" | ||
@State private var selectedRecordsIds: Set<String> = [] | ||
@State private var editMode: EditMode = .inactive | ||
@Environment(\.presentationMode) @Binding private var presentationMode | ||
|
||
private var filteredRecords: [WKWebsiteDataRecord] { | ||
if filter.isEmpty { | ||
return dataRecords | ||
} | ||
return dataRecords.filter { | ||
$0.displayName.lowercased().contains(filter.lowercased()) | ||
} | ||
} | ||
|
||
private func removeRecords(_ records: [WKWebsiteDataRecord]) { | ||
let recordIds = records.map(\.id) | ||
WKWebsiteDataStore.default() | ||
.removeData( | ||
ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(), | ||
for: records | ||
) { | ||
withAnimation { | ||
if dataRecords.count == records.count { | ||
dataRecords.removeAll() | ||
} else { | ||
dataRecords.removeAll(where: { recordIds.contains($0.displayName) }) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private func removeRecordsWithIds(_ recordIds: [String]) { | ||
let records = dataRecords.filter { recordIds.contains($0.id) } | ||
removeRecords(records) | ||
} | ||
|
||
private var isEditMode: Bool { | ||
!selectedRecordsIds.isEmpty && editMode == .active | ||
} | ||
|
||
private var removeButtonTitle: String { | ||
if isEditMode { | ||
if selectedRecordsIds.count == 1 { | ||
return Strings.removeDataRecord | ||
} | ||
return String.localizedStringWithFormat(Strings.removeSelectedDataRecord, selectedRecordsIds.count) | ||
} | ||
return Strings.removeAllDataRecords | ||
} | ||
|
||
private func loadRecords() { | ||
isLoading = true | ||
WKWebsiteDataStore.default() | ||
.fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(), | ||
completionHandler: { records in | ||
withAnimation { | ||
self.dataRecords = records | ||
.filter { $0.displayName != "localhost" } | ||
.sorted(by: { $0.displayName < $1.displayName }) | ||
self.isLoading = false | ||
} | ||
}) | ||
} | ||
|
||
var body: some View { | ||
NavigationView { | ||
let visibleRecords = filteredRecords | ||
List(selection: $selectedRecordsIds) { | ||
Section { | ||
if isLoading { | ||
HStack { | ||
ProgressView() | ||
Text(Strings.loadingWebsiteData) | ||
} | ||
} else { | ||
ForEach(visibleRecords) { record in | ||
VStack(alignment: .leading, spacing: 2) { | ||
let types = Set( | ||
record.dataTypes.compactMap(localizedStringForDataRecordType) | ||
).sorted() | ||
Text(record.displayName) | ||
.foregroundColor(Color(.bravePrimary)) | ||
if !types.isEmpty { | ||
Text(ListFormatter.localizedString(byJoining: types)) | ||
.foregroundColor(Color(.braveLabel)) | ||
.font(.footnote) | ||
} | ||
} | ||
.frame(maxWidth: .infinity, alignment: .leading) | ||
.osAvailabilityModifiers { content in | ||
if #available(iOS 15.0, *) { | ||
// Better swipe gestures | ||
content | ||
.swipeActions(edge: .trailing) { | ||
Button(role: .destructive) { | ||
removeRecords([record]) | ||
} label: { | ||
Label(Strings.removeDataRecord, systemImage: "trash") | ||
} | ||
} | ||
} else { | ||
content | ||
} | ||
} | ||
} | ||
.onDelete { indexSet in | ||
let recordsToDelete = indexSet.map { visibleRecords[$0] } | ||
removeRecords(recordsToDelete) | ||
} | ||
} | ||
} | ||
.listRowBackground(Color(.braveBackground)) | ||
} | ||
.listStyle(.plain) | ||
.environment(\.editMode, $editMode) | ||
.overlay(Group { | ||
if !isLoading && visibleRecords.isEmpty { | ||
Text(Strings.noSavedWebsiteData) | ||
.font(.headline) | ||
.foregroundColor(Color(.secondaryBraveLabel)) | ||
} | ||
}) | ||
.toolbar { | ||
ToolbarItemGroup(placement: .confirmationAction) { | ||
Button(action: { | ||
presentationMode.dismiss() | ||
}) { | ||
Text(Strings.done) | ||
.foregroundColor(Color(.braveOrange)) | ||
} | ||
} | ||
ToolbarItemGroup(placement: .bottomBar) { | ||
HStack { | ||
Button(action: { | ||
withAnimation { | ||
editMode = editMode.isEditing ? .inactive : .active | ||
} | ||
}) { | ||
Text(editMode.isEditing ? Strings.done : Strings.edit) | ||
.foregroundColor(visibleRecords.isEmpty ? Color(.braveDisabled) : Color(.braveOrange)) | ||
} | ||
.disabled(visibleRecords.isEmpty) | ||
Spacer() | ||
Button(action: { | ||
if isEditMode { | ||
removeRecordsWithIds(Array(selectedRecordsIds)) | ||
selectedRecordsIds = [] | ||
} else { | ||
removeRecords(visibleRecords) | ||
} | ||
}) { | ||
Text(removeButtonTitle) | ||
.foregroundColor(visibleRecords.isEmpty ? Color(.braveDisabled) : .red) | ||
.animation(nil, value: isEditMode) | ||
} | ||
.disabled(visibleRecords.isEmpty) | ||
} | ||
} | ||
} | ||
.filterable(text: $filter) | ||
.navigationTitle(Strings.manageWebsiteDataTitle) | ||
.navigationBarTitleDisplayMode(.inline) | ||
} | ||
.navigationViewStyle(.stack) | ||
.onAppear(perform: loadRecords) | ||
} | ||
} | ||
|
||
private func localizedStringForDataRecordType(_ type: String) -> String? { | ||
switch type { | ||
case WKWebsiteDataTypeCookies: | ||
return Strings.dataRecordCookies | ||
case WKWebsiteDataTypeDiskCache, WKWebsiteDataTypeMemoryCache, WKWebsiteDataTypeFetchCache, | ||
WKWebsiteDataTypeOfflineWebApplicationCache, WKWebsiteDataTypeServiceWorkerRegistrations: | ||
return Strings.dataRecordCache | ||
case WKWebsiteDataTypeLocalStorage, WKWebsiteDataTypeSessionStorage: | ||
return Strings.dataRecordLocalStorage | ||
case WKWebsiteDataTypeWebSQLDatabases, WKWebsiteDataTypeIndexedDBDatabases: | ||
return Strings.dataRecordDatabases | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
extension WKWebsiteDataRecord: Identifiable { | ||
public var id: String { | ||
displayName | ||
} | ||
} |