Skip to content

Commit

Permalink
Add UI and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marcprux committed Jan 7, 2024
1 parent d051f29 commit 1feff5a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 23 deletions.
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ let package = Package(
.product(name: "SkipFoundation", package: "skip-foundation"),
.product(name: "SkipModel", package: "skip-model"),
.product(name: "SkipFirebaseFirestore", package: "skip-firebase"),
.product(name: "SkipFirebaseMessaging", package: "skip-firebase"),
.product(name: "SkipFirebaseAuth", package: "skip-firebase")
//.product(name: "SkipFirebaseMessaging", package: "skip-firebase"),
//.product(name: "SkipFirebaseAuth", package: "skip-firebase")
], resources: [.process("Resources")], plugins: [.plugin(name: "skipstone", package: "skip")]),
.testTarget(name: "FireSideModelTests", dependencies: [
"FireSideModel",
Expand Down
15 changes: 8 additions & 7 deletions Sources/FireSide/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,7 @@ struct JoinChatView : View {
}
} label: {
ZStack {
if chatKey.isEmpty {
Text("New Chat", bundle: .module)
} else {
Text("Join Chat", bundle: .module)
}
chatKey.isEmpty ? Text("New Chat", bundle: .module) : Text("Join Chat", bundle: .module)
}
.font(.largeTitle)
.frame(width: 250.0, height: 180.0)
Expand All @@ -84,19 +80,24 @@ struct JoinChatView : View {
.disabled(chatKey.isEmpty == false && chatKey.count != 8)


Text(lastError ?? "")
.foregroundStyle(Color.red)
.opacity(lastError == nil ? 0.0 : 1.0)

Spacer()

TextField(text: $chatKey) {
Text("Chat Key", bundle: .module)
}

#if SKIP || os(iOS)
.keyboardType(.decimalPad)
.keyboardType(.numberPad)
.textFieldStyle(.roundedBorder)
#endif
.padding()
}
}
.frame(maxHeight: .infinity)
//.frame(maxHeight: .infinity)
}

private func joinChat() async {
Expand Down
31 changes: 29 additions & 2 deletions Sources/FireSideModel/FireSideModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,34 @@ public actor FireSideStore {

public init(bundleURL: URL? = nil) throws {
// TODO: use the bundleURL to load an offline bundle for testing
//let options = FirebaseOptions(googleAppID: "", gcmSenderID: "")
FirebaseApp.configure()
self.firestore = Firestore.firestore()
}

/// Create a custom Firestore with the given name
public init(options: [String: String]) throws {
guard let appId = options["GOOGLE_APP_ID"],
let senderId = options["GCM_SENDER_ID"] else {
throw InvalidConfigurationError(errorDescription: "configuration options are missing required attributes")
}
let opts = FirebaseOptions(googleAppID: appId, gcmSenderID: senderId)
if let apiKey = options["API_KEY"] {
opts.apiKey = apiKey
}
if let projectID = options["PROJECT_ID"] {
opts.projectID = projectID
}
if let storageBucket = options["STORAGE_BUCKET"] {
opts.storageBucket = storageBucket
}
// if let bundleID = options["BUNDLE_ID"] {
// opts.bundleID = bundleID
// }

FirebaseApp.configure(options: opts)
self.firestore = Firestore.firestore()
}

@MainActor public func joinChat(chatKey: String) async throws {
logger.info("joinChat: \(chatKey)")
}
Expand All @@ -39,7 +62,7 @@ public actor FireSideStore {
let dbname = "(default)"

let cref = firestore.collection("messages")

//
let snapshot = try await cref.getDocuments()
for document in snapshot.documents {
logger.log("read cref: \(document.documentID) => \(document.data())")
Expand All @@ -54,4 +77,8 @@ public actor FireSideStore {
"c": "message content"
])
}

public struct InvalidConfigurationError : LocalizedError {
public var errorDescription: String?
}
}
29 changes: 17 additions & 12 deletions Tests/FireSideModelTests/FireSideModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@ let logger: Logger = Logger(subsystem: "FireSideModel", category: "Tests")

@available(macOS 13, *)
final class FireSideModelTests: XCTestCase {
func testFireSideModel() throws {
logger.log("running testFireSideModel")
XCTAssertEqual(1 + 2, 3, "basic test")

// load the TestData.json file from the Resources folder and decode it into a struct
let resourceURL: URL = try XCTUnwrap(Bundle.module.url(forResource: "TestData", withExtension: "json"))
let testData = try JSONDecoder().decode(TestData.self, from: Data(contentsOf: resourceURL))
XCTAssertEqual("FireSideModel", testData.testModuleName)
// values from Darwin/GoogleService-Info.plist
static let store = try! FireSideStore(options: [
"API_KEY": "AIzaSyCjhtnQ4GE010ED8hRMaGZjpdApSk43z1I",
"GCM_SENDER_ID": "1058155430593",
//"BUNDLE_ID": "skip.fireside.App",
"PROJECT_ID": "skip-fireside",
"STORAGE_BUCKET": "skip-fireside.appspot.com",
"GOOGLE_APP_ID": "1:1058155430593:ios:d3a7a76d92b20132370a40",
])

func testFireSideStore() throws {
let _ = Self.store
}
}

struct TestData : Codable, Hashable {
var testModuleName: String
}
func testFireSideModel() async throws {
let chatKey = try await Self.store.startNewChat()

}
}

0 comments on commit 1feff5a

Please sign in to comment.