Advertisement
dartmeadow

fileexporter

Nov 3rd, 2021
1,387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 6.83 KB | None | 0 0
  1. struct JournalDocument: FileDocument {
  2.    
  3.     static var readableContentTypes: [UTType] { [.plainText] }
  4.  
  5.     var journal: String
  6.  
  7.     init(journal: String) {
  8.         self.journal = journal
  9.     }
  10.  
  11.     init(configuration: ReadConfiguration) throws {
  12.         guard let data = configuration.file.regularFileContents,
  13.               let string = String(data: data, encoding: .utf8)
  14.         else {
  15.             throw CocoaError(.fileReadCorruptFile)
  16.         }
  17.         journal = string
  18.     }
  19.  
  20.     func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
  21.         return FileWrapper(regularFileWithContents: journal.data(using: .utf8)!)
  22.     }
  23.    
  24. }
  25. struct JournalView: View {
  26.     @State private var document: JournalDocument = JournalDocument(journal: "DART Meadow Articren Journal")
  27.        
  28.     @State private var isImporting: Bool = false
  29.     @State private var isExporting: Bool = false
  30.    
  31.    
  32.     @State var diameter = Double()
  33.     @State var radicalsize = Double()//Input
  34.     @State var radicalcircumference = Double()//Input
  35.     @State var radicalarea = Double()//Input
  36.     @State var radicalvolume = Double()//Input
  37.     @State var circumferencearea = Double()//Input
  38.     @State var quartervolume = Double()//Input
  39.     @State var circumference = Double()
  40.     @State var circlearea = Double()
  41.     @State var volume = Double()
  42.     @State var spheresurface = Double()
  43.     @State var trimorbitlocationsqaured = Double()
  44.     @State var trimorbitlocationcubed = Double()
  45.     @State var orbitlocationexponentsquared = Double()
  46.     @State var orbitlocationareaexponentsqaured = Double()
  47.     @State var radicalsizeedgesq = Double()
  48.     @State var radicalsizeedgecb = Double()
  49.    
  50.    
  51.     let formatter: NumberFormatter = {
  52.             let formatter = NumberFormatter()
  53.             formatter.numberStyle = .scientific
  54.             formatter.positiveFormat = "0.###E+0"
  55.             formatter.exponentSymbol = "e"
  56.             return formatter
  57.         }()
  58.     func diameter(diameterresult: Double) -> Double { pow(sqrt(radicalsize * 3), 2) }
  59.     func circumference(circumferenceresult: Double) -> Double { pow(pow(sqrt(radicalcircumference * 3), 2), 2) }
  60.     func circlearea(circlearearesult: Double) -> Double { pow(pow(sqrt(radicalarea * 3), 2), 3) }
  61.     func volume(volumeresult: Double) -> Double { pow(pow(pow(sqrt(radicalvolume * 3), 2), 3), 2) }
  62.     func spheresurface(spheresurfaceresult: Double) -> Double{ pow(pow(pow(sqrt(quartervolume * 3), 2), 3), 2) - pow(pow(sqrt(quartervolume * 3), 2), 3) }
  63.     func trimorbitlocationsqaured(trimorbitlocationsqauredresult: Double) -> Double { (pow(2, sqrt(1 * 0.25)) + radicalsizeedgesq) - 1 }
  64.     func trimorbitlocationcubed(trimorbitlocationcubedresult: Double) -> Double {  ((2 / (cbrt(1 * 2))) + radicalsizeedgecb) - 1  }
  65.    
  66.     var body: some View{
  67.         HStack(alignment: .center) {
  68.             VStack(alignment: .center) {
  69.            
  70.                
  71.             Text("Journal")
  72.                 .font(.largeTitle)
  73.                 .bold()
  74.                 //.colorInvert()
  75.                
  76.                 .shadow(radius: 3)
  77.              
  78.             }
  79.            
  80.             HStack(alignment: .bottom) {
  81.                 VStack(alignment: .center) {
  82.                 Image("DARTEdge256")
  83.                     .shadow(radius: 3)
  84.                 }
  85.             }
  86.         }.padding(.top, 10)
  87.            
  88.             Divider()
  89.  
  90.             ScrollView(.vertical) {
  91.             VStack {
  92.                 GroupBox(label: Text("Journal Entry:").font(.callout)
  93.                                     .bold())
  94.                 {
  95.                             TextEditor(text: $document.journal)
  96.                         .frame(width: 690, height: 400, alignment: .center)
  97.                             Text("Sigcothian Squared: (Electron | Diameter):")
  98.                                 .font(.callout)
  99.                                 .bold()
  100.                                // .colorInvert()
  101.                             TextField("#", value: self.$radicalsizeedgesq, formatter: formatter)
  102.                                 //.colorInvert()
  103.                            
  104.                                 .textFieldStyle(RoundedBorderTextFieldStyle())
  105.                                 .overlay(
  106.                                         RoundedRectangle(cornerRadius: 6)
  107.                                             .stroke(Color.blue, lineWidth: 2)
  108.                                     )
  109.                                
  110.                             Text("Sigcothian Squared Unit(s): \(trimorbitlocationsqaured(trimorbitlocationsqauredresult: radicalsizeedgesq))")
  111.                                 .font(.callout)
  112.                                 .bold()
  113.                                 .frame(width: 150, height: 60, alignment: .leading)
  114.                                 //.colorInvert()
  115.                         }
  116.            
  117.                         GroupBox {
  118.                             HStack {
  119.                                 Spacer()
  120.                                
  121.                                 Button(action: { isImporting = true }, label: {
  122.                                     Text("Import")
  123.                                 })
  124.                                
  125.                                 Spacer()
  126.                                
  127.                                 Button(action: { isExporting = true }, label: {
  128.                                     Text("Export")
  129.                                 })
  130.                                
  131.                                 Spacer()
  132.                             }
  133.                         }
  134.                     }
  135.            
  136.             .padding()
  137.             .fileExporter(
  138.                   isPresented: $isExporting,
  139.                   document: document,
  140.                   contentType: .plainText,
  141.                   defaultFilename: "Articren Journal"
  142.               ) { result in
  143.                   if case .success = result {
  144.                       // Handle success.
  145.                   } else {
  146.                       // Handle failure.
  147.                   }
  148.               }
  149.               .fileImporter(
  150.                   isPresented: $isImporting,
  151.                   allowedContentTypes: [.plainText],
  152.                   allowsMultipleSelection: false
  153.               ) { result in
  154.                   do {
  155.                       guard let selectedFile: URL = try result.get().first else { return }
  156.                       guard let journal = String(data: try Data(contentsOf: selectedFile), encoding: .utf8) else { return }
  157.  
  158.                       document.journal = journal
  159.                   } catch {
  160.                       // Handle failure.
  161.                   }
  162.               }
  163.             }.frame(width: 900, height: 670, alignment: .center).padding(10)
  164.      
  165.             //.background(Color.white)
  166.        
  167.         Spacer()
  168.     }
  169.    
  170. }
  171.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement