106 lines
3.3 KiB
Swift
106 lines
3.3 KiB
Swift
// AdvancedPrefsView.swift
|
|
// Tusker
|
|
//
|
|
// Created by Shadowfacts on 6/13/19.
|
|
// Copyright © 2019 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import SwiftUI
|
|
import Pachyderm
|
|
import CoreData
|
|
|
|
struct AdvancedPrefsView : View {
|
|
@ObservedObject var preferences = Preferences.shared
|
|
|
|
var body: some View {
|
|
List {
|
|
formattingSection
|
|
cachingSection
|
|
}
|
|
.listStyle(InsetGroupedListStyle())
|
|
.navigationBarTitle(Text("Advanced"))
|
|
}
|
|
|
|
var formattingFooter: some View {
|
|
Text("This option is only supported for Pleroma and Mastodon instances with formatting enabled. On all other instances, formatting symbols will remain in the unformatted plain text.").lineLimit(nil)
|
|
}
|
|
|
|
var formattingSection: some View {
|
|
Section(footer: formattingFooter) {
|
|
Picker(selection: $preferences.statusContentType, label: Text("Post Content Type")) {
|
|
ForEach(StatusContentType.allCases, id: \.self) { type in
|
|
Text(type.displayName).tag(type)
|
|
}//.navigationBarTitle("Post Content Type")
|
|
// see FB6838291
|
|
}
|
|
}
|
|
}
|
|
|
|
var cachingSection: some View {
|
|
Section(header: Text("Caching"), footer: Text("Clearing caches will restart the app.")) {
|
|
Button(action: clearCache) {
|
|
Text("Clear Mastodon Cache")
|
|
}.foregroundColor(.red)
|
|
Button(action: clearImageCaches) {
|
|
Text("Clear Image Caches")
|
|
}.foregroundColor(.red)
|
|
}
|
|
}
|
|
|
|
private func clearCache() {
|
|
for account in LocalData.shared.accounts {
|
|
let controller = MastodonController.getForAccount(account)
|
|
let container = controller.persistentContainer
|
|
do {
|
|
let statusesReq = NSBatchDeleteRequest(fetchRequest: StatusMO.fetchRequest())
|
|
try container.viewContext.execute(statusesReq)
|
|
let accountsReq = NSBatchDeleteRequest(fetchRequest: AccountMO.fetchRequest())
|
|
try container.viewContext.execute(accountsReq)
|
|
let relationshipsReq = NSBatchDeleteRequest(fetchRequest: RelationshipMO.fetchRequest())
|
|
try container.viewContext.execute(relationshipsReq)
|
|
} catch {
|
|
Logging.general.error("Error while clearing Mastodon cache: \(String(describing: error), privacy: .public)")
|
|
}
|
|
}
|
|
resetUI()
|
|
}
|
|
|
|
private func clearImageCaches() {
|
|
[
|
|
ImageCache.avatars,
|
|
ImageCache.headers,
|
|
ImageCache.attachments,
|
|
ImageCache.emojis,
|
|
].forEach {
|
|
try! $0.reset()
|
|
}
|
|
resetUI()
|
|
}
|
|
|
|
private func resetUI() {
|
|
let mostRecent = LocalData.shared.getMostRecentAccount()!
|
|
NotificationCenter.default.post(name: .activateAccount, object: nil, userInfo: ["account": mostRecent])
|
|
}
|
|
}
|
|
|
|
extension StatusContentType {
|
|
var displayName: String {
|
|
switch self {
|
|
case .plain:
|
|
return "Plain"
|
|
case .markdown:
|
|
return "Markdown"
|
|
case .html:
|
|
return "HTML"
|
|
}
|
|
}
|
|
}
|
|
|
|
#if DEBUG
|
|
struct AdvancedPrefsView_Previews : PreviewProvider {
|
|
static var previews: some View {
|
|
AdvancedPrefsView()
|
|
}
|
|
}
|
|
#endif
|