Skip to content

Enable macOS support for demo #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Demo/PowerSyncExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"PowerSyncExample/Preview Content\"";
DEVELOPMENT_TEAM = ZGT7463CVJ;
DEVELOPMENT_TEAM = W284TED275;
ENABLE_PREVIEWS = YES;
ENABLE_USER_SCRIPT_SANDBOXING = NO;
GENERATE_INFOPLIST_FILE = YES;
Expand All @@ -728,6 +728,10 @@
OTHER_LDFLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = com.powersync.PowerSyncSwiftExample;
PRODUCT_NAME = "$(TARGET_NAME)";
REGISTER_APP_GROUPS = YES;
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator macosx";
SUPPORTS_MACCATALYST = NO;
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_OBJC_BRIDGING_HEADER = "PowerSyncExample/PowerSyncExample-Bridging-Header.h";
"SWIFT_OBJC_BRIDGING_HEADER[arch=*]" = "";
Expand All @@ -747,7 +751,7 @@
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"PowerSyncExample/Preview Content\"";
DEVELOPMENT_TEAM = ZGT7463CVJ;
DEVELOPMENT_TEAM = W284TED275;
ENABLE_PREVIEWS = YES;
ENABLE_USER_SCRIPT_SANDBOXING = NO;
GENERATE_INFOPLIST_FILE = YES;
Expand All @@ -765,6 +769,10 @@
OTHER_LDFLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = com.powersync.PowerSyncSwiftExample;
PRODUCT_NAME = "$(TARGET_NAME)";
REGISTER_APP_GROUPS = YES;
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator macosx";
SUPPORTS_MACCATALYST = NO;
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_OBJC_BRIDGING_HEADER = "PowerSyncExample/PowerSyncExample-Bridging-Header.h";
SWIFT_VERSION = 5.0;
Expand Down
8 changes: 8 additions & 0 deletions Demo/PowerSyncExample/Components/TodoListRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ struct TodoListRow: View {
let capturePhotoTapped: () -> Void
let selectPhotoTapped: () -> Void

#if os(iOS)
@State private var image: UIImage? = nil
#endif

var body: some View {
HStack {
Text(todo.description)
#if os(iOS)
Group {
if let image = image {
Image(uiImage: image)
Expand All @@ -34,6 +37,7 @@ struct TodoListRow: View {
EmptyView()
}
}
#endif
Spacer()
VStack {
if todo.photoId == nil {
Expand Down Expand Up @@ -69,14 +73,17 @@ struct TodoListRow: View {
}
.buttonStyle(.plain)
}.onChange(of: todo.photoId) { _, newPhotoId in
#if os(iOS)
if newPhotoId == nil {
// Clear the image when photoId becomes nil
image = nil
}
#endif
}
}
}

#if os(iOS)
private func loadImage() async {
guard let urlString = todo.photoUri else { return }
let url = URL(fileURLWithPath: urlString)
Expand All @@ -92,6 +99,7 @@ struct TodoListRow: View {
print("Error loading image from disk:", error)
}
}
#endif
}

#Preview {
Expand Down
25 changes: 25 additions & 0 deletions Demo/PowerSyncExample/Components/TodoListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ struct TodoListView: View {
@State private var newTodo: NewTodo?
@State private var editing: Bool = false

#if os(iOS)
// Called when a photo has been captured. Individual widgets should register the listener
@State private var onMediaSelect: ((_: Data) async throws -> Void)?
@State private var pickMediaType: UIImagePickerController.SourceType = .camera
@State private var showMediaPicker = false
@State private var isCameraAvailable: Bool = false
#endif

var body: some View {
List {
Expand All @@ -33,6 +35,7 @@ struct TodoListView: View {
}

ForEach(todos) { todo in
#if os(iOS)
TodoListRow(
todo: todo,
isCameraAvailable: isCameraAvailable,
Expand Down Expand Up @@ -68,6 +71,20 @@ struct TodoListView: View {
pickMediaType = .photoLibrary
showMediaPicker = true
}
#else
TodoListRow(
todo: todo,
isCameraAvailable: false,
completeTapped: {
Task {
await toggleCompletion(of: todo)
}
},
deletePhotoTapped: {},
capturePhotoTapped: {},
selectPhotoTapped: {},
)
#endif
}
.onDelete { indexSet in
Task {
Expand All @@ -80,12 +97,14 @@ struct TodoListView: View {
}
}
}
#if os(iOS)
.sheet(isPresented: $showMediaPicker) {
CameraView(
onMediaSelect: $onMediaSelect,
mediaType: $pickMediaType
)
}
#endif
.animation(.default, value: todos)
.navigationTitle("Todos")
.toolbar {
Expand All @@ -111,9 +130,11 @@ struct TodoListView: View {
}
}
}
#if os(iOS)
.onAppear {
checkCameraAvailability()
}
#endif
.task {
await system.watchTodos(listId) { tds in
withAnimation {
Expand Down Expand Up @@ -144,6 +165,7 @@ struct TodoListView: View {
}
}

#if os(iOS)
/// Registers a callback which saves a photo for the specified Todo item if media is sucessfully loaded.
func registerMediaCallback(todo: Todo) {
// Register a callback for successful image capture
Expand Down Expand Up @@ -181,6 +203,7 @@ struct TodoListView: View {
isCameraAvailable = UIImagePickerController.isSourceTypeAvailable(.camera)
#endif
}
#endif
}

#Preview {
Expand All @@ -191,6 +214,7 @@ struct TodoListView: View {
}
}

#if os(iOS)
struct CameraView: UIViewControllerRepresentable {
@Binding var onMediaSelect: ((_: Data) async throws -> Void)?
@Binding var mediaType: UIImagePickerController.SourceType
Expand Down Expand Up @@ -244,3 +268,4 @@ struct CameraView: UIViewControllerRepresentable {
}
}
}
#endif
8 changes: 6 additions & 2 deletions Demo/PowerSyncExample/Screens/SignInScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@ struct SignInScreen: View {
Form {
Section {
TextField("Email", text: $email)
.keyboardType(.emailAddress)
.textContentType(.emailAddress)
.autocorrectionDisabled()
#if os(iOS)
.keyboardType(.emailAddress)
.textInputAutocapitalization(.never)

#endif

SecureField("Password", text: $password)
.textContentType(.password)
.autocorrectionDisabled()
#if os(iOS)
.textInputAutocapitalization(.never)
#endif
}

Section {
Expand Down
6 changes: 5 additions & 1 deletion Demo/PowerSyncExample/Screens/SignUpScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ struct SignUpScreen: View {
Form {
Section {
TextField("Email", text: $email)
.keyboardType(.emailAddress)
.textContentType(.emailAddress)
.autocorrectionDisabled()
#if os(ios)
.autocorrectionDisabled()
.textInputAutocapitalization(.never)
#endif

SecureField("Password", text: $password)
.textContentType(.password)
.autocorrectionDisabled()
#if os(ios)
.textInputAutocapitalization(.never)
#endif
}

Section {
Expand Down