2020-07-13 01:35:00 +00:00
|
|
|
//
|
|
|
|
// ContentView.swift
|
|
|
|
// Gemini
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 7/12/20.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
2020-07-15 02:13:09 +00:00
|
|
|
import GeminiFormat
|
|
|
|
import GeminiRenderer
|
2020-07-15 02:53:47 +00:00
|
|
|
import GeminiProtocol
|
2020-07-13 01:35:00 +00:00
|
|
|
|
|
|
|
struct ContentView: View {
|
2020-07-15 02:53:47 +00:00
|
|
|
let url: URL
|
|
|
|
@State var task: GeminiDataTask?
|
|
|
|
@State var document: Document?
|
|
|
|
@State var errorMessage: String?
|
2020-07-15 02:13:09 +00:00
|
|
|
|
2020-07-13 01:35:00 +00:00
|
|
|
var body: some View {
|
2020-07-15 02:53:47 +00:00
|
|
|
mainView
|
2020-07-15 02:13:09 +00:00
|
|
|
.frame(minWidth: 480, maxWidth: .infinity, minHeight: 300, maxHeight: .infinity)
|
2020-07-13 01:35:00 +00:00
|
|
|
}
|
2020-07-15 02:53:47 +00:00
|
|
|
|
|
|
|
@ViewBuilder
|
|
|
|
private var mainView: some View {
|
|
|
|
if let document = document {
|
|
|
|
DocumentView(document: document)
|
|
|
|
} else if let errorMessage = errorMessage {
|
|
|
|
VStack {
|
|
|
|
Text("An error occurred")
|
|
|
|
.font(.headline)
|
|
|
|
Text(errorMessage)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
loadingView
|
|
|
|
.onAppear(perform: self.loadDocument)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder
|
|
|
|
private var loadingView: some View {
|
|
|
|
if #available(macOS 10.16, iOS 14.0, *) {
|
|
|
|
ProgressView("Loading...")
|
|
|
|
} else {
|
|
|
|
Text("Loading...")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func loadDocument() {
|
|
|
|
task = try! GeminiDataTask(url: url, completion: { (response) in
|
|
|
|
switch response {
|
|
|
|
case let .failure(error):
|
|
|
|
self.errorMessage = error.localizedDescription
|
|
|
|
case let .success(response):
|
|
|
|
guard let text = response.bodyText else {
|
|
|
|
self.errorMessage = "Response had no body text"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
self.document = GeminiParser.parse(text: text, baseURL: url)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
task!.resume()
|
|
|
|
}
|
2020-07-13 01:35:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ContentView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2020-07-15 02:53:47 +00:00
|
|
|
ContentView(url: URL(string: "gemini://gemini.circumlunar.space/")!)
|
2020-07-13 01:35:00 +00:00
|
|
|
}
|
|
|
|
}
|