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
31 changed files
with
1,150 additions
and
645 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
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
101 changes: 101 additions & 0 deletions
101
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,101 @@ | ||
// 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 | ||
@SwiftUI.Environment(\.presentationMode) private var presentationMode: Binding<PresentationMode> | ||
@State private var newURLInput: String = "" | ||
|
||
private var textField: some View { | ||
TextField(Strings.filterListsEnterFilterListURL, text: $newURLInput) | ||
.keyboardType(.URL) | ||
.textContentType(.URL) | ||
.autocapitalization(.none) | ||
} | ||
|
||
var body: some View { | ||
NavigationView { | ||
List { | ||
Section(content: { | ||
VStack { | ||
textField | ||
.submitLabel(SubmitLabel.done) | ||
}.listRowBackground(Color(.secondaryBraveGroupedBackground)) | ||
}, header: { | ||
Text(Strings.customFilterListURL) | ||
.textCase(.uppercase) | ||
}, footer: { | ||
VStack(alignment: .leading, spacing: 8) { | ||
Text(Strings.addCustomFilterListDescription) | ||
Text(Strings.addCustomFilterListWarning).bold() + Text(Strings.addCustomFilterListWarning2) | ||
}.padding(.top, 16) | ||
}) | ||
} | ||
.listBackgroundColor(Color(UIColor.braveGroupedBackground)) | ||
.navigationTitle(Strings.customFilterList) | ||
.navigationBarTitleDisplayMode(.inline) | ||
.toolbar { | ||
ToolbarItemGroup(placement: .primaryAction) { | ||
Button(Strings.filterListsAdd) { | ||
handleOnSubmit() | ||
}.disabled(newURLInput.isEmpty) | ||
} | ||
|
||
ToolbarItemGroup(placement: .cancellationAction) { | ||
Button(Strings.CancelString) { | ||
presentationMode.wrappedValue.dismiss() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private func handleOnSubmit() { | ||
guard !newURLInput.isEmpty else { return } | ||
guard let url = URL(string: newURLInput) else { | ||
// Show invalid URL error | ||
return | ||
} | ||
guard url.scheme == "https" else { | ||
// Show invalid scheme error | ||
return | ||
} | ||
guard !customFilterListStorage.filterListsURLs.contains(where: { filterListURL in | ||
return filterListURL.setting.externalURL == url | ||
}) else { | ||
// Don't allow duplicates | ||
self.presentationMode.wrappedValue.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.wrappedValue.dismiss() | ||
} | ||
} | ||
} | ||
|
||
#if DEBUG | ||
struct FilterListAddURLView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
FilterListAddURLView() | ||
} | ||
} | ||
#endif |
Oops, something went wrong.