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 #6841: Add custom filter list urls
- Loading branch information
Showing
16 changed files
with
859 additions
and
18 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
111 changes: 111 additions & 0 deletions
111
Sources/Brave/Frontend/Settings/FilterListAddURLView.swift
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,111 @@ | ||
// Copyright 2023 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 SwiftUI | ||
import Strings | ||
import DesignSystem | ||
import BraveUI | ||
|
||
struct FilterListAddURLView: View { | ||
@ObservedObject private var customFilterListStorage = CustomFilterListStorage.shared | ||
@Environment(\.presentationMode) @Binding private var presentationMode | ||
@State private var newURLInput: String = "" | ||
@State private var errorMessage: String? | ||
|
||
private var textField: some View { | ||
TextField(Strings.filterListsEnterFilterListURL, text: $newURLInput) | ||
.onChange(of: newURLInput) { newValue in | ||
errorMessage = nil | ||
} | ||
.keyboardType(.URL) | ||
.textContentType(.URL) | ||
.autocapitalization(.none) | ||
.autocorrectionDisabled() | ||
} | ||
|
||
var body: some View { | ||
NavigationView { | ||
List { | ||
Section(content: { | ||
VStack(alignment: .leading) { | ||
textField | ||
.submitLabel(SubmitLabel.done) | ||
|
||
if let errorMessage = errorMessage { | ||
Text(errorMessage) | ||
.font(.caption) | ||
.foregroundColor(.red) | ||
} | ||
} | ||
}, header: { | ||
Text(Strings.customFilterListURL) | ||
}, footer: { | ||
VStack(alignment: .leading, spacing: 8) { | ||
Text(Strings.addCustomFilterListDescription) | ||
Text(LocalizedStringKey(Strings.addCustomFilterListWarning)) | ||
}.padding(.top, 8) | ||
}) | ||
} | ||
.listBackgroundColor(Color(UIColor.braveGroupedBackground)) | ||
.navigationTitle(Strings.customFilterList) | ||
.navigationBarTitleDisplayMode(.inline) | ||
.toolbar { | ||
ToolbarItemGroup(placement: .confirmationAction) { | ||
Button(Strings.filterListsAdd) { | ||
handleOnSubmit() | ||
}.disabled(newURLInput.isEmpty) | ||
} | ||
|
||
ToolbarItemGroup(placement: .cancellationAction) { | ||
Button(Strings.CancelString) { | ||
presentationMode.dismiss() | ||
} | ||
} | ||
} | ||
}.frame(idealWidth: 400, idealHeight: 400) | ||
} | ||
|
||
private func handleOnSubmit() { | ||
guard !newURLInput.isEmpty else { return } | ||
guard let url = URL(string: newURLInput) else { | ||
self.errorMessage = Strings.filterListAddInvalidURLError | ||
return | ||
} | ||
guard url.scheme == "https" else { | ||
self.errorMessage = Strings.filterListAddOnlyHTTPSAllowedError | ||
return | ||
} | ||
guard !customFilterListStorage.filterListsURLs.contains(where: { filterListURL in | ||
return filterListURL.setting.externalURL == url | ||
}) else { | ||
// Don't allow duplicates | ||
self.presentationMode.dismiss() | ||
return | ||
} | ||
|
||
Task { | ||
let customURL = FilterListCustomURL( | ||
externalURL: url, isEnabled: true, | ||
inMemory: !customFilterListStorage.persistChanges | ||
) | ||
|
||
customFilterListStorage.filterListsURLs.append(customURL) | ||
|
||
await FilterListCustomURLDownloader.shared.startFetching( | ||
filterListCustomURL: customURL | ||
) | ||
|
||
self.presentationMode.dismiss() | ||
} | ||
} | ||
} | ||
|
||
#if DEBUG | ||
struct FilterListAddURLView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
FilterListAddURLView() | ||
} | ||
} | ||
#endif |
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,55 @@ | ||
// Copyright 2023 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 Data | ||
|
||
@MainActor class CustomFilterListStorage: ObservableObject { | ||
static let shared = CustomFilterListStorage(persistChanges: true) | ||
/// Wether or not to store the data into disk or into memory | ||
let persistChanges: Bool | ||
/// A list of filter list URLs and their enabled statuses | ||
@Published var filterListsURLs: [FilterListCustomURL] | ||
|
||
init(persistChanges: Bool) { | ||
self.persistChanges = persistChanges | ||
self.filterListsURLs = [] | ||
} | ||
|
||
func loadCachedFilterLists() { | ||
let settings = CustomFilterListSetting.loadAllSettings(fromMemory: !persistChanges) | ||
|
||
self.filterListsURLs = settings.map { setting in | ||
let resource = setting.resource | ||
let date = try? resource.creationDate() | ||
|
||
if let date = date { | ||
return FilterListCustomURL(setting: setting, downloadStatus: .downloaded(date)) | ||
} else { | ||
return FilterListCustomURL(setting: setting, downloadStatus: .pending) | ||
} | ||
} | ||
} | ||
|
||
func update(filterListId id: ObjectIdentifier, with result: Result<Date, Error>) { | ||
guard let index = filterListsURLs.firstIndex(where: { $0.id == id }) else { | ||
return | ||
} | ||
|
||
switch result { | ||
case .failure(let error): | ||
#if DEBUG | ||
let externalURL = filterListsURLs[index].setting.externalURL.absoluteString | ||
ContentBlockerManager.log.error( | ||
"Failed to download resource \(externalURL): \(String(describing: error))" | ||
) | ||
#endif | ||
|
||
filterListsURLs[index].downloadStatus = .failure | ||
case .success(let date): | ||
filterListsURLs[index].downloadStatus = .downloaded(date) | ||
} | ||
} | ||
} |
Oops, something went wrong.