Skip to content
This repository was archived by the owner on Oct 24, 2025. It is now read-only.

Commit b8385c3

Browse files
committed
ios basic example
1 parent 13adec2 commit b8385c3

File tree

6 files changed

+267
-2
lines changed

6 files changed

+267
-2
lines changed

samples/ios/iosExample.xcodeproj/project.pbxproj

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
2152FB042600AC8F00CF470E /* iOSApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2152FB032600AC8F00CF470E /* iOSApp.swift */; };
1313
7555FF83242A565900829871 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7555FF82242A565900829871 /* ContentView.swift */; };
1414
DFB106835B31169EA75F74AA /* Pods_iosExample.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0AC38B36598A128984C6C459 /* Pods_iosExample.framework */; };
15+
E231C3C72B74355C00A54F92 /* DatabaseSelectorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E231C3C62B74355C00A54F92 /* DatabaseSelectorView.swift */; };
16+
E231C3C92B74359500A54F92 /* BasicPage.swift in Sources */ = {isa = PBXBuildFile; fileRef = E231C3C82B74359500A54F92 /* BasicPage.swift */; };
17+
E231C3CB2B74361800A54F92 /* BasicCRUDView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E231C3CA2B74361800A54F92 /* BasicCRUDView.swift */; };
1518
/* End PBXBuildFile section */
1619

1720
/* Begin PBXFileReference section */
@@ -24,6 +27,9 @@
2427
7555FF82242A565900829871 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
2528
7555FF8C242A565B00829871 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
2629
9D57C0B1986FECE7BF0E1B51 /* Pods-iosExample.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-iosExample.release.xcconfig"; path = "Target Support Files/Pods-iosExample/Pods-iosExample.release.xcconfig"; sourceTree = "<group>"; };
30+
E231C3C62B74355C00A54F92 /* DatabaseSelectorView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DatabaseSelectorView.swift; sourceTree = "<group>"; };
31+
E231C3C82B74359500A54F92 /* BasicPage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BasicPage.swift; sourceTree = "<group>"; };
32+
E231C3CA2B74361800A54F92 /* BasicCRUDView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BasicCRUDView.swift; sourceTree = "<group>"; };
2733
/* End PBXFileReference section */
2834

2935
/* Begin PBXFrameworksBuildPhase section */
@@ -76,6 +82,7 @@
7682
7555FF7D242A565900829871 /* iosExample */ = {
7783
isa = PBXGroup;
7884
children = (
85+
E231C3C52B74354000A54F92 /* Basic */,
7986
058557BA273AAA24004C7B11 /* Assets.xcassets */,
8087
7555FF82242A565900829871 /* ContentView.swift */,
8188
7555FF8C242A565B00829871 /* Info.plist */,
@@ -85,6 +92,16 @@
8592
path = iosExample;
8693
sourceTree = "<group>";
8794
};
95+
E231C3C52B74354000A54F92 /* Basic */ = {
96+
isa = PBXGroup;
97+
children = (
98+
E231C3C82B74359500A54F92 /* BasicPage.swift */,
99+
E231C3C62B74355C00A54F92 /* DatabaseSelectorView.swift */,
100+
E231C3CA2B74361800A54F92 /* BasicCRUDView.swift */,
101+
);
102+
path = Basic;
103+
sourceTree = "<group>";
104+
};
88105
FDD7D045D71C07FF996583FB /* Frameworks */ = {
89106
isa = PBXGroup;
90107
children = (
@@ -208,7 +225,10 @@
208225
buildActionMask = 2147483647;
209226
files = (
210227
2152FB042600AC8F00CF470E /* iOSApp.swift in Sources */,
228+
E231C3C72B74355C00A54F92 /* DatabaseSelectorView.swift in Sources */,
211229
7555FF83242A565900829871 /* ContentView.swift in Sources */,
230+
E231C3CB2B74361800A54F92 /* BasicCRUDView.swift in Sources */,
231+
E231C3C92B74359500A54F92 /* BasicPage.swift in Sources */,
212232
);
213233
runOnlyForDeploymentPostprocessing = 0;
214234
};
@@ -343,6 +363,7 @@
343363
ENABLE_PREVIEWS = YES;
344364
FRAMEWORK_SEARCH_PATHS = "$(inherited)";
345365
INFOPLIST_FILE = iosExample/Info.plist;
366+
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
346367
LD_RUNPATH_SEARCH_PATHS = (
347368
"$(inherited)",
348369
"@executable_path/Frameworks",
@@ -368,6 +389,7 @@
368389
ENABLE_PREVIEWS = YES;
369390
FRAMEWORK_SEARCH_PATHS = "$(inherited)";
370391
INFOPLIST_FILE = iosExample/Info.plist;
392+
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
371393
LD_RUNPATH_SEARCH_PATHS = (
372394
"$(inherited)",
373395
"@executable_path/Frameworks",
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "1500"
4+
version = "1.7">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "7555FF7A242A565900829871"
18+
BuildableName = "iosExample.app"
19+
BlueprintName = "iosExample"
20+
ReferencedContainer = "container:iosExample.xcodeproj">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
</BuildActionEntries>
24+
</BuildAction>
25+
<TestAction
26+
buildConfiguration = "Debug"
27+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
28+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29+
shouldUseLaunchSchemeArgsEnv = "YES"
30+
shouldAutocreateTestPlan = "YES">
31+
</TestAction>
32+
<LaunchAction
33+
buildConfiguration = "Debug"
34+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
35+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
36+
launchStyle = "0"
37+
useCustomWorkingDirectory = "NO"
38+
ignoresPersistentStateOnLaunch = "NO"
39+
debugDocumentVersioning = "YES"
40+
debugServiceExtension = "internal"
41+
allowLocationSimulation = "YES">
42+
<BuildableProductRunnable
43+
runnableDebuggingMode = "0">
44+
<BuildableReference
45+
BuildableIdentifier = "primary"
46+
BlueprintIdentifier = "7555FF7A242A565900829871"
47+
BuildableName = "iosExample.app"
48+
BlueprintName = "iosExample"
49+
ReferencedContainer = "container:iosExample.xcodeproj">
50+
</BuildableReference>
51+
</BuildableProductRunnable>
52+
</LaunchAction>
53+
<ProfileAction
54+
buildConfiguration = "Release"
55+
shouldUseLaunchSchemeArgsEnv = "YES"
56+
savedToolIdentifier = ""
57+
useCustomWorkingDirectory = "NO"
58+
debugDocumentVersioning = "YES">
59+
<BuildableProductRunnable
60+
runnableDebuggingMode = "0">
61+
<BuildableReference
62+
BuildableIdentifier = "primary"
63+
BlueprintIdentifier = "7555FF7A242A565900829871"
64+
BuildableName = "iosExample.app"
65+
BlueprintName = "iosExample"
66+
ReferencedContainer = "container:iosExample.xcodeproj">
67+
</BuildableReference>
68+
</BuildableProductRunnable>
69+
</ProfileAction>
70+
<AnalyzeAction
71+
buildConfiguration = "Debug">
72+
</AnalyzeAction>
73+
<ArchiveAction
74+
buildConfiguration = "Release"
75+
revealArchiveInOrganizer = "YES">
76+
</ArchiveAction>
77+
</Scheme>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//
2+
// OperationsView.swift
3+
// iosExample
4+
//
5+
// Created by Krzysztof Borowy on 07/02/2024.
6+
// Copyright © 2024 orgName. All rights reserved.
7+
//
8+
9+
import SwiftUI
10+
import AsyncStorageSQLiteKMP
11+
12+
13+
struct BasicCRUDView: View {
14+
internal var db: AsyncStorageSQLite
15+
16+
@State var value: String?
17+
@State var timelineChanges: [String?] = []
18+
19+
init(db: AsyncStorageSQLite) {
20+
self.db = db
21+
self.value = nil
22+
}
23+
24+
func saveRandom() {
25+
let entry = Entry(key: "random", value: String(Int.random(in: 1...100)))
26+
Task {
27+
try await db.write(entry: entry)
28+
}
29+
}
30+
31+
func readRandom() {
32+
Task {
33+
let result = try await db.read(key: "random")
34+
value = result.value
35+
}
36+
}
37+
38+
func deleteRandom() {
39+
Task {
40+
try await db.remove(key: "random")
41+
}
42+
}
43+
44+
func onAppear() async {
45+
print("on appear")
46+
do {
47+
for try await updates in db.readAsFlow(keys: ["random"]) {
48+
print("updated for \(updates)")
49+
let result = try await db.read(key: "random")
50+
timelineChanges.append(result.value ?? nil)
51+
}
52+
} catch {
53+
print("error \(error)")
54+
}
55+
}
56+
57+
58+
var body: some View {
59+
VStack {
60+
HStack {
61+
Button("Save", action: saveRandom)
62+
.padding()
63+
.border(/*@START_MENU_TOKEN@*/Color.black/*@END_MENU_TOKEN@*/)
64+
Button("Read", action: readRandom)
65+
.padding()
66+
.border(/*@START_MENU_TOKEN@*/Color.black/*@END_MENU_TOKEN@*/)
67+
Button("Remove", role: .destructive, action: deleteRandom)
68+
.padding()
69+
.border(/*@START_MENU_TOKEN@*/Color.black/*@END_MENU_TOKEN@*/)
70+
}.frame(maxWidth: .infinity).padding()
71+
72+
(Text("value: ") + Text(value ?? "nil").fontWeight(.bold))
73+
VStack {
74+
Text("value changes in time:").fontWeight(.bold)
75+
ForEach(0..<timelineChanges.count, id: \.self) { index in
76+
Text("Updated to \(timelineChanges[index] ?? "nil")")
77+
}
78+
}.padding()
79+
}.task {
80+
let _ = await onAppear()
81+
}
82+
}
83+
}
84+
85+
86+
87+
#Preview {
88+
BasicCRUDView(db: AsyncStorageSQLite(name: "preview"))
89+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import SwiftUI
2+
import AsyncStorageSQLiteKMP
3+
4+
5+
struct BasicPage: View {
6+
@State var database: AsyncStorageSQLite?
7+
8+
init() {
9+
let db = AsyncStorageSQLite(name: "basic.db")
10+
self._database = State(initialValue: db)
11+
}
12+
13+
func onChangeName(name: String) {
14+
database = AsyncStorageSQLite(name: name)
15+
}
16+
17+
var body: some View {
18+
VStack {
19+
DatabaseSelector(db: self.$database, onSelect: onChangeName)
20+
if let database {
21+
VStack {
22+
Divider()
23+
BasicCRUDView(db: database)
24+
}.id(database.name)
25+
26+
}
27+
Spacer()
28+
}
29+
30+
}
31+
}
32+
33+
34+
#Preview {
35+
BasicPage()
36+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import SwiftUI
2+
import AsyncStorageSQLiteKMP
3+
4+
struct DatabaseSelector: View {
5+
@Binding private var db: AsyncStorageSQLite?
6+
private var onSelect: (String) -> Void
7+
8+
@FocusState private var focused: Bool
9+
@State private var name: String
10+
11+
init(db: Binding<AsyncStorageSQLite?> = Binding.constant(nil), onSelect: @escaping (String) -> Void) {
12+
self._db = db
13+
self.onSelect = onSelect
14+
self.name = db.wrappedValue?.name ?? ""
15+
self.focused = false
16+
}
17+
18+
19+
func finish() {
20+
onSelect(name)
21+
focused = false
22+
}
23+
24+
var body: some View {
25+
let dbName = db?.name ?? ""
26+
27+
VStack(spacing: nil) {
28+
HStack {
29+
TextField("Database name", text: $name, onCommit: finish)
30+
.padding()
31+
.border(/*@START_MENU_TOKEN@*/Color.black/*@END_MENU_TOKEN@*/, width: /*@START_MENU_TOKEN@*/1/*@END_MENU_TOKEN@*/)
32+
.focused($focused)
33+
Button("Select", action: finish)
34+
}
35+
Text("database: ") + Text(dbName).fontWeight(.bold)
36+
}.padding()
37+
}
38+
}

samples/ios/iosExample/iOSApp.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import SwiftUI
44
struct iOSApp: App {
55
var body: some Scene {
66
WindowGroup {
7-
ContentView()
7+
TabView {
8+
BasicPage()
9+
.tabItem { Label("Basic", systemImage: "figure.walk.circle").labelStyle(.titleAndIcon) }
10+
}
811
}
912
}
10-
}
13+
}

0 commit comments

Comments
 (0)