-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientViewModel.swift
69 lines (55 loc) · 2.26 KB
/
ClientViewModel.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// ClientViewModel.swift
// HyperClient
//
// Created by Niko Ma on 3/29/22.
//
import SwiftUI
enum UploadOptions: Identifiable {
case ipfs, cloud
var id: Self { self }
}
class ClientViewModel: ObservableObject {
@Published var showProgressView = false
@Published var rawData: [ResponsePair]?
@Published var history: [ResponsePair]?
@Published var pickedImage = [UIImage]()
@Published var uploadOption:UploadOptions = .ipfs
@MainActor
public func fetchData() async {
logger.info("Fetching data...")
rawData = await HyperClient.shared.fetchData()
}
@MainActor func fetchHistory(id: UInt32) async {
history = await HyperClient.shared.fetchHistory(id: id)
}
@MainActor
public func submitData(name: String, experimentTime: Date, author: String, email: String, institution: String,
environment:String, parameters:String, details: String, offset: Int32) async {
// convert Date to timestamp String
let experimentTimeString = Double(experimentTime.timeIntervalSince1970)
var attachmentURL = ""
func completion(url: String?) {
if let url = url {
attachmentURL += url
attachmentURL += " "
print("attachmentURL is \(attachmentURL)")
}
}
if pickedImage.count > 0 {
switch uploadOption {
case .ipfs:
for image in pickedImage {
await HyperClient.shared.uploadIPFS(data: image.jpegData(compressionQuality: 1)!, completion: completion)
}
case .cloud:
for image in pickedImage {
await HyperClient.shared.uploadCloud(data: image.jpegData(compressionQuality: 1)!, completion: completion)
}
}
pickedImage.removeAll()
}
let entry = Entry(id: 1, name: name, experiment_time: experimentTimeString, author: author, email: email, institution: institution, environment: environment, parameters: parameters, details: details, attachment: attachmentURL, hash: "", offset: offset, timestamp: 0)
HyperClient.shared.insertData(data: entry)
}
}