|
| 1 | +import SwiftUI |
| 2 | +import MobileCoreServices |
| 3 | +import UniformTypeIdentifiers |
| 4 | +import WriteFreely |
| 5 | + |
| 6 | +enum WFActionExtensionError: Error { |
| 7 | + case userCancelledRequest |
| 8 | + case couldNotParseInputItems |
| 9 | +} |
| 10 | + |
| 11 | +struct ContentView: View { |
| 12 | + @Environment(\.extensionContext) private var extensionContext: NSExtensionContext! |
| 13 | + @Environment(\.managedObjectContext) private var managedObjectContext |
| 14 | + |
| 15 | + @AppStorage(WFDefaults.defaultFontIntegerKey, store: UserDefaults.shared) var fontIndex: Int = 0 |
| 16 | + |
| 17 | + @FetchRequest( |
| 18 | + entity: WFACollection.entity(), |
| 19 | + sortDescriptors: [NSSortDescriptor(keyPath: \WFACollection.title, ascending: true)] |
| 20 | + ) var collections: FetchedResults<WFACollection> |
| 21 | + |
| 22 | + @State private var draftTitle: String = "" |
| 23 | + @State private var draftText: String = "" |
| 24 | + @State private var isShowingAlert: Bool = false |
| 25 | + @State private var selectedBlog: WFACollection? |
| 26 | + |
| 27 | + private var draftsCollectionName: String { |
| 28 | + guard UserDefaults.shared.string(forKey: WFDefaults.serverStringKey) == "https://write.as" else { |
| 29 | + return "Drafts" |
| 30 | + } |
| 31 | + return "Anonymous" |
| 32 | + } |
| 33 | + |
| 34 | + private var controls: some View { |
| 35 | + HStack { |
| 36 | + Group { |
| 37 | + Button( |
| 38 | + action: { extensionContext.cancelRequest(withError: WFActionExtensionError.userCancelledRequest) }, |
| 39 | + label: { Image(systemName: "xmark.circle").imageScale(.large) } |
| 40 | + ) |
| 41 | + .accessibilityLabel(Text("Cancel")) |
| 42 | + Spacer() |
| 43 | + Button( |
| 44 | + action: { |
| 45 | + savePostToCollection(collection: selectedBlog, title: draftTitle, body: draftText) |
| 46 | + extensionContext.completeRequest(returningItems: nil, completionHandler: nil) |
| 47 | + }, |
| 48 | + label: { Image(systemName: "square.and.arrow.down").imageScale(.large) } |
| 49 | + ) |
| 50 | + .accessibilityLabel(Text("Create new draft")) |
| 51 | + } |
| 52 | + .padding() |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + var body: some View { |
| 57 | + VStack { |
| 58 | + controls |
| 59 | + Form { |
| 60 | + Section(header: Text("Title")) { |
| 61 | + switch fontIndex { |
| 62 | + case 1: |
| 63 | + TextField("Draft Title", text: $draftTitle).font(.custom("OpenSans-Regular", size: 26)) |
| 64 | + case 2: |
| 65 | + TextField("Draft Title", text: $draftTitle).font(.custom("Hack-Regular", size: 26)) |
| 66 | + default: |
| 67 | + TextField("Draft Title", text: $draftTitle).font(.custom("Lora", size: 26)) |
| 68 | + } |
| 69 | + } |
| 70 | + Section(header: Text("Content")) { |
| 71 | + switch fontIndex { |
| 72 | + case 1: |
| 73 | + TextEditor(text: $draftText).font(.custom("OpenSans-Regular", size: 17)) |
| 74 | + case 2: |
| 75 | + TextEditor(text: $draftText).font(.custom("Hack-Regular", size: 17)) |
| 76 | + default: |
| 77 | + TextEditor(text: $draftText).font(.custom("Lora", size: 17)) |
| 78 | + } |
| 79 | + } |
| 80 | + Section(header: Text("Save To")) { |
| 81 | + Button(action: { |
| 82 | + self.selectedBlog = nil |
| 83 | + }, label: { |
| 84 | + HStack { |
| 85 | + Text(draftsCollectionName) |
| 86 | + .foregroundColor(selectedBlog == nil ? .primary : .secondary) |
| 87 | + Spacer() |
| 88 | + if selectedBlog == nil { |
| 89 | + Image(systemName: "checkmark") |
| 90 | + } |
| 91 | + } |
| 92 | + }) |
| 93 | + ForEach(collections, id: \.self) { collection in |
| 94 | + Button(action: { |
| 95 | + self.selectedBlog = collection |
| 96 | + }, label: { |
| 97 | + HStack { |
| 98 | + Text(collection.title) |
| 99 | + .foregroundColor(selectedBlog == collection ? .primary : .secondary) |
| 100 | + Spacer() |
| 101 | + if selectedBlog == collection { |
| 102 | + Image(systemName: "checkmark") |
| 103 | + } |
| 104 | + } |
| 105 | + }) |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + .padding(.bottom, 24) |
| 110 | + } |
| 111 | + .alert(isPresented: $isShowingAlert, content: { |
| 112 | + Alert( |
| 113 | + title: Text("Something Went Wrong"), |
| 114 | + message: Text("WriteFreely can't create a draft with the data received."), |
| 115 | + dismissButton: .default(Text("OK"), action: { |
| 116 | + extensionContext.cancelRequest(withError: WFActionExtensionError.couldNotParseInputItems) |
| 117 | + })) |
| 118 | + }) |
| 119 | + .onAppear { |
| 120 | + do { |
| 121 | + try getPageDataFromExtensionContext() |
| 122 | + } catch { |
| 123 | + self.isShowingAlert = true |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private func savePostToCollection(collection: WFACollection?, title: String, body: String) { |
| 129 | + let post = WFAPost(context: managedObjectContext) |
| 130 | + post.createdDate = Date() |
| 131 | + post.title = title |
| 132 | + post.body = body |
| 133 | + post.status = PostStatus.local.rawValue |
| 134 | + post.collectionAlias = collection?.alias |
| 135 | + switch fontIndex { |
| 136 | + case 1: |
| 137 | + post.appearance = "sans" |
| 138 | + case 2: |
| 139 | + post.appearance = "wrap" |
| 140 | + default: |
| 141 | + post.appearance = "serif" |
| 142 | + } |
| 143 | + if let languageCode = Locale.current.languageCode { |
| 144 | + post.language = languageCode |
| 145 | + post.rtl = Locale.characterDirection(forLanguage: languageCode) == .rightToLeft |
| 146 | + } |
| 147 | + LocalStorageManager.standard.saveContext() |
| 148 | + } |
| 149 | + |
| 150 | + private func getPageDataFromExtensionContext() throws { |
| 151 | + if let inputItem = extensionContext.inputItems.first as? NSExtensionItem { |
| 152 | + if let itemProvider = inputItem.attachments?.first { |
| 153 | + |
| 154 | + let typeIdentifier: String |
| 155 | + |
| 156 | + if #available(iOS 15, *) { |
| 157 | + typeIdentifier = UTType.propertyList.identifier |
| 158 | + } else { |
| 159 | + typeIdentifier = kUTTypePropertyList as String |
| 160 | + } |
| 161 | + |
| 162 | + itemProvider.loadItem(forTypeIdentifier: typeIdentifier) { (dict, error) in |
| 163 | + if let error = error { |
| 164 | + print("⚠️", error) |
| 165 | + self.isShowingAlert = true |
| 166 | + } |
| 167 | + |
| 168 | + guard let itemDict = dict as? NSDictionary else { |
| 169 | + return |
| 170 | + } |
| 171 | + guard let jsValues = itemDict[NSExtensionJavaScriptPreprocessingResultsKey] as? NSDictionary else { |
| 172 | + return |
| 173 | + } |
| 174 | + |
| 175 | + let pageTitle = jsValues["title"] as? String ?? "" |
| 176 | + let pageURL = jsValues["URL"] as? String ?? "" |
| 177 | + let pageSelectedText = jsValues["selection"] as? String ?? "" |
| 178 | + |
| 179 | + if pageSelectedText.isEmpty { |
| 180 | + // If there's no selected text, create a Markdown link to the webpage. |
| 181 | + self.draftText = "[\(pageTitle)](\(pageURL))" |
| 182 | + } else { |
| 183 | + // If there is selected text, create a Markdown blockquote with the selection |
| 184 | + // and add a Markdown link to the webpage. |
| 185 | + self.draftText = """ |
| 186 | + > \(pageSelectedText) |
| 187 | +
|
| 188 | + Via: [\(pageTitle)](\(pageURL)) |
| 189 | + """ |
| 190 | + } |
| 191 | + } |
| 192 | + } else { |
| 193 | + throw WFActionExtensionError.couldNotParseInputItems |
| 194 | + } |
| 195 | + } else { |
| 196 | + throw WFActionExtensionError.couldNotParseInputItems |
| 197 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments