Moves theme separation code to ArticleThemeManager

This cherry picks a change that is in ArticleThemeManager for macOS so that the theme pickers are using the same code to separate themes.
This commit is contained in:
Stuart Breckenridge
2023-02-09 17:29:48 +08:00
parent 69e20479b6
commit 7fd85f5ead
2 changed files with 75 additions and 19 deletions

View File

@@ -16,29 +16,24 @@ struct ArticleThemeManagerView: View {
@State private var showImportConfirmationAlert: (ArticleTheme?, Bool) = (nil, false)
@State private var showImportErrorAlert: (Error?, Bool) = (nil, false)
@State private var showImportSuccessAlert: Bool = false
@State private var installedFirstPartyThemes: [ArticleTheme] = []
@State private var installedThirdPartyThemes: [ArticleTheme] = []
var body: some View {
Form {
Section(header: Text("Built-in Themes", comment: "Section header for installed themes"), footer: Text("These themes cannot be deleted.", comment: "Section footer for installed themes.")) {
articleThemeRow(try! themeManager.articleThemeWithThemeName("Default"))
ForEach(0..<installedFirstPartyThemes.count, id: \.self) { i in
articleThemeRow(installedFirstPartyThemes[i])
articleThemeRow(try! themeManager.articleThemeWithThemeName(AppDefaults.defaultThemeName))
ForEach(0..<themeManager.themesByDeveloper().0.count, id: \.self) { i in
articleThemeRow(themeManager.themesByDeveloper().0[i])
}
}
Section(header: Text("Other Themes", comment: "Section header for third party themes")) {
ForEach(0..<installedThirdPartyThemes.count, id: \.self) { i in
articleThemeRow(installedThirdPartyThemes[i])
ForEach(0..<themeManager.themesByDeveloper().1.count, id: \.self) { i in
articleThemeRow(themeManager.themesByDeveloper().1[i])
}
}
}
.navigationTitle(Text("Article Themes", comment: "Navigation bar title for Article Themes"))
.task {
updateThemesArrays()
}
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button {
@@ -142,9 +137,6 @@ struct ArticleThemeManagerView: View {
actions: { }, message: {
Text("\(showImportErrorAlert.0?.localizedDescription ?? "")")
})
.onReceive(themeManager.objectWillChange) { _ in
updateThemesArrays()
}
}
func articleThemeRow(_ theme: ArticleTheme) -> some View {
@@ -181,12 +173,6 @@ struct ArticleThemeManagerView: View {
}
}
}
private func updateThemesArrays() {
installedFirstPartyThemes = themeManager.themeNames.map({ try? themeManager.articleThemeWithThemeName($0) }).compactMap({ $0 }).filter({ $0.isAppTheme }).sorted(by: { $0.name < $1.name })
installedThirdPartyThemes = themeManager.themeNames.map({ try? themeManager.articleThemeWithThemeName($0) }).compactMap({ $0 }).filter({ !$0.isAppTheme }).sorted(by: { $0.name < $1.name })
}
}
struct ArticleThemeImporterView_Previews: PreviewProvider {