Skip to content

Commit

Permalink
fix: auth record encoding
Browse files Browse the repository at this point in the history
Also:
* update to remote EventSource package
* Add HasLogger
  • Loading branch information
briannadoubt committed Sep 19, 2024
1 parent b6ef17f commit f2a4c20
Show file tree
Hide file tree
Showing 37 changed files with 255 additions and 810 deletions.
11 changes: 10 additions & 1 deletion .swiftpm/configuration/Package.resolved
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
{
"originHash" : "357e26f937dbe05b0cb89785de035d46ab83da449ac48eb5f488dd1a9f6de072",
"originHash" : "230c615f1f87047f3e8f427da26a1cc18e86b46756118b65cfa0c3e9192698d8",
"pins" : [
{
"identity" : "eventsource",
"kind" : "remoteSourceControl",
"location" : "https://github.com/briannadoubt/EventSource.git",
"state" : {
"revision" : "67e4d952e895d848142e4b16af3b96ab1b2a09b7",
"version" : "0.1.0"
}
},
{
"identity" : "keychainaccess",
"kind" : "remoteSourceControl",
Expand Down
11 changes: 10 additions & 1 deletion Package.resolved
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
{
"originHash" : "357e26f937dbe05b0cb89785de035d46ab83da449ac48eb5f488dd1a9f6de072",
"originHash" : "230c615f1f87047f3e8f427da26a1cc18e86b46756118b65cfa0c3e9192698d8",
"pins" : [
{
"identity" : "eventsource",
"kind" : "remoteSourceControl",
"location" : "https://github.com/briannadoubt/EventSource.git",
"state" : {
"revision" : "67e4d952e895d848142e4b16af3b96ab1b2a09b7",
"version" : "0.1.0"
}
},
{
"identity" : "keychainaccess",
"kind" : "remoteSourceControl",
Expand Down
26 changes: 16 additions & 10 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import CompilerPluginSupport
let package = Package(
name: "PocketBase",
platforms: [
.macOS(.v15),
.iOS(.v18),
.tvOS(.v18),
.watchOS(.v11),
.macCatalyst(.v18),
.visionOS(.v2),
.macOS(.v14),
.iOS(.v17),
.tvOS(.v17),
.watchOS(.v10),
.macCatalyst(.v17),
.visionOS(.v1),
],
products: [
.library(
Expand All @@ -22,12 +22,17 @@ let package = Package(
name: "PocketBaseUI",
targets: ["PocketBaseUI"]
),
.library(
name: "DataBase",
targets: ["DataBase"]
),
// MARK: WIP
// .library(
// name: "DataBase",
// targets: ["DataBase"]
// ),
],
dependencies: [
.package(
url: "https://github.com/briannadoubt/EventSource.git",
.upToNextMinor(from: "0.1.0")
),
.package(
url: "https://github.com/apple/swift-http-types.git",
.upToNextMajor(from: "1.0.0")
Expand Down Expand Up @@ -58,6 +63,7 @@ let package = Package(
name: "PocketBase",
dependencies: [
"PocketBaseMacros",
.product(name: "EventSource", package: "EventSource"),
.product(name: "KeychainAccess", package: "KeychainAccess"),
.product(name: "HTTPTypes", package: "swift-http-types"),
.product(name: "AsyncAlgorithms", package: "swift-async-algorithms"),
Expand Down
2 changes: 2 additions & 0 deletions PocketBaseDemo/PocketBaseDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = auto;
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator macosx xros xrsimulator";
SUPPORTS_MACCATALYST = NO;
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 6.0;
TARGETED_DEVICE_FAMILY = "1,2,7";
Expand Down Expand Up @@ -477,6 +478,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = auto;
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator macosx xros xrsimulator";
SUPPORTS_MACCATALYST = NO;
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 6.0;
TARGETED_DEVICE_FAMILY = "1,2,7";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
{
"originHash" : "418bba4f88620fdb3be0f429a8fc5e1de6e4a1fd499808b8a7746587a081de92",
"pins" : [
{
"identity" : "eventsource",
"kind" : "remoteSourceControl",
"location" : "https://github.com/briannadoubt/EventSource.git",
"state" : {
"revision" : "67e4d952e895d848142e4b16af3b96ab1b2a09b7",
"version" : "0.1.0"
}
},
{
"identity" : "keychainaccess",
"kind" : "remoteSourceControl",
Expand Down
85 changes: 19 additions & 66 deletions PocketBaseDemo/PocketBaseDemo/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import os
struct ContentView: View {
@Environment(\.pocketbase) private var pocketbase

@RealtimeQuery<Rawr>(
sort: [.init(\.field)]
) private var rawrs
@RealtimeQuery<Rawr>(sort: [.init(\.field)]) private var rawrs

static let logger = Logger(
subsystem: "PocketBaseDemo",
Expand All @@ -34,85 +32,40 @@ struct ContentView: View {
}
.navigationTitle("Rawrs")
.toolbar {
Button("Logout", role: .destructive) {
pocketbase.collection(User.self).logout()
}
Button("New", systemImage: "plus") {
Task {
do {
try await pocketbase.collection(Rawr.self).create(Rawr(field: "", owner: ""))
} catch {
Self.logger.error("Failed to create record with error \(error)")
}
}
}
Button("Logout", role: .destructive, action: logout)
Button("New", systemImage: "plus", action: new)
}
}
.task {
await $rawrs.start()
}
}

func delete(_ index: IndexSet) {
let rawrs = index.map { self.rawrs[$0] }
func logout() {
Task {
for rawr in rawrs {
do {
try await pocketbase.collection(Rawr.self).delete(rawr)
} catch {
Self.logger.error("Failed to deleted record with error \(error)")
}
}
await pocketbase.collection(User.self).logout()
}
}
}

struct RawrView: View {
@Environment(\.pocketbase) private var pocketbase

private let rawr: Rawr

@State private var isPresentingEditAlert: Bool = false
@State private var editText: String = ""

static let logger = Logger(
subsystem: "PocketBaseDemo",
category: "ContentView"
)

init(rawr: Rawr) {
self.rawr = rawr
}

var body: some View {
Button(rawr.field) {
editText = rawr.field
isPresentingEditAlert = true
}
.foregroundStyle(.primary)
.alert("Update Rawr", isPresented: $isPresentingEditAlert) {
TextField("Update Rawr", text: $editText)
.onSubmit {
save()
}
Button("Cancel", role: .cancel) {
isPresentingEditAlert = false
}
Button("Save") {
save()
func new() {
Task {
do {
try await pocketbase.collection(Rawr.self).create(Rawr(field: ""))
} catch {
Self.logger.error("Failed to create record with error \(error)")
}
}
}

private func save() {
var rawr = self.rawr
rawr.field = editText
func delete(_ index: IndexSet) {
let rawrs = index.map { self.rawrs[$0] }
Task {
do {
try await pocketbase.collection(Rawr.self).update(rawr)
isPresentingEditAlert = false
} catch {
Self.logger.error("Failed to update rawr with error: \(error)")
for rawr in rawrs {
do {
try await pocketbase.collection(Rawr.self).delete(rawr)
} catch {
Self.logger.error("Failed to deleted record with error \(error)")
}
}
}
}
Expand Down
15 changes: 1 addition & 14 deletions PocketBaseDemo/PocketBaseDemo/Models.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,9 @@
import PocketBase

@AuthCollection("users")
struct User {
init(username: String, email: String?) {
self.username = username
self.email = email
}
}

@BaseCollection("posts")
struct Post {
var title: String
var body: String
@Relation var author: User?
}
struct User {}

@BaseCollection("rawrs")
struct Rawr {
var field: String
@Relation var owner: User?
}
6 changes: 6 additions & 0 deletions PocketBaseDemo/PocketBaseDemo/PocketBaseDemo.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.developer.icloud-container-identifiers</key>
<array/>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
<key>keychain-access-groups</key>
<array>
<string>$(AppIdentifierPrefix)com.briannadoubt.PocketBaseDemo</string>
</array>
</dict>
</plist>
61 changes: 61 additions & 0 deletions PocketBaseDemo/PocketBaseDemo/RawrView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// RawrView.swift
// PocketBaseDemo
//
// Created by Brianna Zamora on 9/18/24.
//

import PocketBase
import SwiftUI
import os

struct RawrView: View {
@Environment(\.pocketbase) private var pocketbase

private let rawr: Rawr

@State private var isPresentingEditAlert: Bool = false
@State private var editText: String = ""

static let logger = Logger(
subsystem: "PocketBaseDemo",
category: "RawrView"
)

init(rawr: Rawr) {
self.rawr = rawr
}

var body: some View {
Button(rawr.field) {
editText = rawr.field
isPresentingEditAlert = true
}
.foregroundStyle(.primary)
.alert("Update Rawr", isPresented: $isPresentingEditAlert) {
TextField("Update Rawr", text: $editText)
.onSubmit {
save()
}
Button("Cancel", role: .cancel) {
isPresentingEditAlert = false
}
Button("Save") {
save()
}
}
}

private func save() {
var rawr = self.rawr
rawr.field = editText
Task {
do {
try await pocketbase.collection(Rawr.self).update(rawr)
isPresentingEditAlert = false
} catch {
Self.logger.error("Failed to update rawr with error: \(error)")
}
}
}
}
Loading

0 comments on commit f2a4c20

Please sign in to comment.