-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathoctobase_demoApp.swift
203 lines (163 loc) · 7.08 KB
/
octobase_demoApp.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//
// octobase_demoApp.swift
// octobase-demo
//
// Created by ds on 2023/2/12.
//
import SwiftUI
import OctoBase
@main
struct octobase_demoApp: App {
var body: some Scene {
WindowGroup {
ContentView().environmentObject(
JwstWorkspace(workspace: "test"))
}
}
}
class JwstWorkspace: ObservableObject {
var workspace: Workspace
init (workspace: String) {
self.workspace = Workspace(workspace)
}
func create(block_id:String, flavor:String) -> Block {
return self.workspace.create(block_id, flavor)
}
func get(blockId: String) -> Optional<Block> {
return self.workspace.get(blockId)
}
func get_blocks_by_flavour(flavour: String) -> RustVec<OctoBase.Block> {
return self.workspace.get_blocks_by_flavour(flavour)
}
func create_block_set_prop_demo() {
print("create_block_set_prop_demo")
let block = self.create(block_id: "test", flavor: "test")
print(self.get(blockId: "test"))
print(block.id().toString());
print(block.flavour().toString())
block.set_integer("integer", 1)
block.set_float("float", 1.1)
block.set_bool("bool", true)
block.set_string("string", "blockValue")
block.set_null("null")
print(block.get_integer("integer"))
print(block.get_float("float"))
print(block.get_bool("bool"))
print(block.get_string("string")?.toString())
}
func insert_remove_children_demo() {
print("insert_remove_children_demo")
let block = self.create(block_id: "test", flavor: "test")
let b = self.create(block_id: "b", flavor: "test")
let c = self.create(block_id: "c", flavor: "test")
let d = self.create(block_id: "d", flavor: "test")
let e = self.create(block_id: "e", flavor: "test")
let f = self.create(block_id: "f", flavor: "test")
block.push_children(b)
block.insert_children_at(c, 0)
block.insert_children_before(d, "b")
block.insert_children_after(e, "b")
block.insert_children_after(f, "c")
for child in block.children() {
print(child.as_str().toString());
}
block.remove_children(d);
print("after remove:")
for child in block.children() {
print(child.as_str().toString());
}
}
func search_demo() {
print("search_demo")
let block = self.create(block_id: "search_test", flavor: "search_test_flavor")
block.set_string("title", "introduction")
block.set_string("text", "hello every one")
block.set_string("index", "this is demo")
let index_fields1 = RustVec<RustString>()
index_fields1.push(value: "title".intoRustString())
index_fields1.push(value: "text".intoRustString())
self.workspace.set_search_index(index_fields1)
print("search_index: ", terminator: "")
for field in self.workspace.get_search_index() {
print(field.as_str().toString() + " ", terminator: "");
}
print("")
let result1 = self.workspace.search("duc")
print("search_result1: ", result1.toString())
let result2 = self.workspace.search("this")
print("search_result2: ", result2.toString())
let index_fields2 = RustVec<RustString>()
index_fields2.push(value: "index".intoRustString())
self.workspace.set_search_index(index_fields2)
print("search_index: ", terminator: "")
for field in self.workspace.get_search_index() {
print(field.as_str().toString() + " ", terminator: "");
}
print("")
let result3 = self.workspace.search("this")
print("search_result3: ", result3.toString())
}
func search_blocks_demo() {
print("search_blocks_demo")
let block = self.create(block_id: "test", flavor: "test")
print(self.get_blocks_by_flavour(flavour: "test"))
}
func storage_demo() {
let fileManager = FileManager.default
if let documentDirectory = try? fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) {
let fileURL = documentDirectory.appendingPathComponent("jwst.db")
if !fileManager.fileExists(atPath: fileURL.path) {
fileManager.createFile(atPath: fileURL.path, contents: nil, attributes: nil)
}
let storage = Storage(fileURL.description.intoRustString())
print("is_offline", storage.is_offline())
print("is_connected", storage.is_connected())
print("is_finished", storage.is_finished())
print("is_error", storage.is_error())
print("get_sync_state", storage.get_sync_state().toString())
let workspace = storage.connect("test", "ws://127.0.0.1:3000/collaboration/test")
print("get_last_synced", storage.get_last_synced().map({String($0)}).joined())
print( workspace?.client_id() )
sleep(1)
let block = workspace!.create("test", "test1")
block.set_bool("bool", true)
block.set_string("str", "str4")
sleep(2)
print("get_sync_state", storage.get_sync_state().toString())
print("get_last_synced", storage.get_last_synced().map({String($0)}).joined())
let block1 = workspace!.get("test")
block1?.set_bool("bool1", true)
block1?.set_string("str1", "str3")
sleep(2)
print("get_sync_state", storage.get_sync_state().toString())
print("get_last_synced", storage.get_last_synced().map({String($0)}).joined())
let block2 = workspace!.get("test")
block2?.set_bool("bool2", false)
block2?.set_string("str2", "str2")
// let workspace = storage.connect("test", "ws://127.0.0.1:3000/collaboration/test")
// let block = workspace!.get("test")!
// print(block.get_string("str1").map({$0.toString()}))
// print(workspace!.get_blocks_by_flavour("test1").map({$0.id()}))
sleep(2)
print("get_sync_state", storage.get_sync_state().toString())
print("get_last_synced", storage.get_last_synced().map({String($0)}));
sleep(2)
print("get_sync_state", storage.get_sync_state().toString())
print("get_last_synced", storage.get_last_synced().map({String($0)}));
print(block.get_string("str1").map({$0.toString()}))
}
}
}
class JWSTStorage: ObservableObject {
var storage: Storage
var remote: String
// path: path of sqlite db
// remote: websocket server api, eg: ws://localhost:3000/collaboration
init (path: String, remote: String) {
self.storage = Storage(path)
self.remote = remote
}
func get_workspace(id: String) -> Workspace {
return self.storage.connect(id, self.remote + "/" + id)!
}
}