Skip to content

docs: Slack Clone Example #235

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

Merged
merged 2 commits into from
Feb 5, 2024
Merged
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
36 changes: 24 additions & 12 deletions Examples/Examples.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@
796298992AEBBA77000AA957 /* MFAFlow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 796298982AEBBA77000AA957 /* MFAFlow.swift */; };
7962989D2AEBC6F9000AA957 /* SVGView in Frameworks */ = {isa = PBXBuildFile; productRef = 7962989C2AEBC6F9000AA957 /* SVGView */; };
79719ECE2ADF26C400737804 /* Supabase in Frameworks */ = {isa = PBXBuildFile; productRef = 79719ECD2ADF26C400737804 /* Supabase */; };
797D664A2B46A1D8007592ED /* Store.swift in Sources */ = {isa = PBXBuildFile; fileRef = 797D66492B46A1D8007592ED /* Store.swift */; };
797D664A2B46A1D8007592ED /* Dependencies.swift in Sources */ = {isa = PBXBuildFile; fileRef = 797D66492B46A1D8007592ED /* Dependencies.swift */; };
7993B8A92B3C673A009B610B /* AuthView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7993B8A82B3C673A009B610B /* AuthView.swift */; };
7993B8AB2B3C67E0009B610B /* Toast.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7993B8AA2B3C67E0009B610B /* Toast.swift */; };
79AF047F2B2CE207008761AD /* AuthWithEmailAndPassword.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79AF047E2B2CE207008761AD /* AuthWithEmailAndPassword.swift */; };
79AF04812B2CE261008761AD /* AuthView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79AF04802B2CE261008761AD /* AuthView.swift */; };
79AF04842B2CE408008761AD /* AuthWithMagicLink.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79AF04832B2CE408008761AD /* AuthWithMagicLink.swift */; };
79AF04862B2CE586008761AD /* Debug.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79AF04852B2CE586008761AD /* Debug.swift */; };
79B8F4242B5FED7C0000E839 /* IdentifiedCollections in Frameworks */ = {isa = PBXBuildFile; productRef = 79B8F4232B5FED7C0000E839 /* IdentifiedCollections */; };
79B8F4262B602F640000E839 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79B8F4252B602F640000E839 /* Logger.swift */; };
79BD76772B59C3E300CA3D68 /* UserStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79BD76762B59C3E300CA3D68 /* UserStore.swift */; };
79BD76792B59C53900CA3D68 /* ChannelsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79BD76782B59C53900CA3D68 /* ChannelsViewModel.swift */; };
79BD767B2B59C61300CA3D68 /* MessagesViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79BD767A2B59C61300CA3D68 /* MessagesViewModel.swift */; };
79BD76792B59C53900CA3D68 /* ChannelStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79BD76782B59C53900CA3D68 /* ChannelStore.swift */; };
79BD767B2B59C61300CA3D68 /* MessageStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79BD767A2B59C61300CA3D68 /* MessageStore.swift */; };
79D884CA2B3C18830009EA4A /* SlackCloneApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79D884C92B3C18830009EA4A /* SlackCloneApp.swift */; };
79D884CC2B3C18830009EA4A /* AppView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79D884CB2B3C18830009EA4A /* AppView.swift */; };
79D884CE2B3C18840009EA4A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 79D884CD2B3C18840009EA4A /* Assets.xcassets */; };
Expand Down Expand Up @@ -80,17 +82,18 @@
795640692955AFBD0088A06F /* ErrorText.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ErrorText.swift; sourceTree = "<group>"; };
796298982AEBBA77000AA957 /* MFAFlow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MFAFlow.swift; sourceTree = "<group>"; };
7962989A2AEBBD9F000AA957 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
797D66492B46A1D8007592ED /* Store.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Store.swift; sourceTree = "<group>"; };
797D66492B46A1D8007592ED /* Dependencies.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Dependencies.swift; sourceTree = "<group>"; };
7993B8A82B3C673A009B610B /* AuthView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthView.swift; sourceTree = "<group>"; };
7993B8AA2B3C67E0009B610B /* Toast.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Toast.swift; sourceTree = "<group>"; };
7993B8AC2B3C97B6009B610B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
79AF047E2B2CE207008761AD /* AuthWithEmailAndPassword.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthWithEmailAndPassword.swift; sourceTree = "<group>"; };
79AF04802B2CE261008761AD /* AuthView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthView.swift; sourceTree = "<group>"; };
79AF04832B2CE408008761AD /* AuthWithMagicLink.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthWithMagicLink.swift; sourceTree = "<group>"; };
79AF04852B2CE586008761AD /* Debug.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Debug.swift; sourceTree = "<group>"; };
79B8F4252B602F640000E839 /* Logger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = "<group>"; };
79BD76762B59C3E300CA3D68 /* UserStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserStore.swift; sourceTree = "<group>"; };
79BD76782B59C53900CA3D68 /* ChannelsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelsViewModel.swift; sourceTree = "<group>"; };
79BD767A2B59C61300CA3D68 /* MessagesViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessagesViewModel.swift; sourceTree = "<group>"; };
79BD76782B59C53900CA3D68 /* ChannelStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelStore.swift; sourceTree = "<group>"; };
79BD767A2B59C61300CA3D68 /* MessageStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageStore.swift; sourceTree = "<group>"; };
79D884C72B3C18830009EA4A /* SlackClone.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SlackClone.app; sourceTree = BUILT_PRODUCTS_DIR; };
79D884C92B3C18830009EA4A /* SlackCloneApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SlackCloneApp.swift; sourceTree = "<group>"; };
79D884CB2B3C18830009EA4A /* AppView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -132,6 +135,7 @@
buildActionMask = 2147483647;
files = (
79D884D92B3C18E90009EA4A /* Supabase in Frameworks */,
79B8F4242B5FED7C0000E839 /* IdentifiedCollections in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -234,10 +238,11 @@
79D884DC2B3C19320009EA4A /* MessagesView.swift */,
7993B8A82B3C673A009B610B /* AuthView.swift */,
7993B8AA2B3C67E0009B610B /* Toast.swift */,
797D66492B46A1D8007592ED /* Store.swift */,
797D66492B46A1D8007592ED /* Dependencies.swift */,
79BD76762B59C3E300CA3D68 /* UserStore.swift */,
79BD76782B59C53900CA3D68 /* ChannelsViewModel.swift */,
79BD767A2B59C61300CA3D68 /* MessagesViewModel.swift */,
79BD76782B59C53900CA3D68 /* ChannelStore.swift */,
79BD767A2B59C61300CA3D68 /* MessageStore.swift */,
79B8F4252B602F640000E839 /* Logger.swift */,
);
path = SlackClone;
sourceTree = "<group>";
Expand Down Expand Up @@ -318,6 +323,7 @@
name = SlackClone;
packageProductDependencies = (
79D884D82B3C18E90009EA4A /* Supabase */,
79B8F4232B5FED7C0000E839 /* IdentifiedCollections */,
);
productName = SlackClone;
productReference = 79D884C72B3C18830009EA4A /* SlackClone.app */;
Expand Down Expand Up @@ -453,10 +459,11 @@
7993B8A92B3C673A009B610B /* AuthView.swift in Sources */,
7993B8AB2B3C67E0009B610B /* Toast.swift in Sources */,
79D884DD2B3C19320009EA4A /* MessagesView.swift in Sources */,
79BD76792B59C53900CA3D68 /* ChannelsViewModel.swift in Sources */,
797D664A2B46A1D8007592ED /* Store.swift in Sources */,
79BD76792B59C53900CA3D68 /* ChannelStore.swift in Sources */,
797D664A2B46A1D8007592ED /* Dependencies.swift in Sources */,
79D884DB2B3C191F0009EA4A /* ChannelListView.swift in Sources */,
79BD767B2B59C61300CA3D68 /* MessagesViewModel.swift in Sources */,
79BD767B2B59C61300CA3D68 /* MessageStore.swift in Sources */,
79B8F4262B602F640000E839 /* Logger.swift in Sources */,
79D884CC2B3C18830009EA4A /* AppView.swift in Sources */,
79BD76772B59C3E300CA3D68 /* UserStore.swift in Sources */,
79D884D72B3C18DB0009EA4A /* Supabase.swift in Sources */,
Expand Down Expand Up @@ -911,6 +918,11 @@
isa = XCSwiftPackageProductDependency;
productName = Supabase;
};
79B8F4232B5FED7C0000E839 /* IdentifiedCollections */ = {
isa = XCSwiftPackageProductDependency;
package = 7956406E2955B5190088A06F /* XCRemoteSwiftPackageReference "swift-identified-collections" */;
productName = IdentifiedCollections;
};
79D884D82B3C18E90009EA4A /* Supabase */ = {
isa = XCSwiftPackageProductDependency;
productName = Supabase;
Expand Down
34 changes: 33 additions & 1 deletion Examples/SlackClone/ChannelListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,25 @@ import SwiftUI

@MainActor
struct ChannelListView: View {
let store = Store.shared.channel
@Bindable var store = Dependencies.shared.channel
@Binding var channel: Channel?

@State private var addChannelPresented = false
@State private var newChannelName = ""

var body: some View {
List(store.channels, selection: $channel) { channel in
NavigationLink(channel.slug, value: channel)
}
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button("Add Channel") {
addChannelPresented = true
}
.popover(isPresented: $addChannelPresented) {
addChannelView
}
}
ToolbarItem {
Button("Log out") {
Task {
Expand All @@ -25,5 +36,26 @@ struct ChannelListView: View {
}
}
}
.toast(state: $store.toast)
}

private var addChannelView: some View {
Form {
Section {
TextField("New channel name", text: $newChannelName)
}

Section {
Button("Add") {
Task {
await store.addChannel(newChannelName)
addChannelPresented = false
}
}
}
}
#if os(macOS)
.padding()
#endif
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// ChannelsViewModel.swift
// ChannelStore.swift
// SlackClone
//
// Created by Guilherme Souza on 18/01/24.
Expand All @@ -8,20 +8,19 @@
import Foundation
import Supabase

protocol ChannelsStore: AnyObject {
func fetchChannel(id: Channel.ID) async throws -> Channel
}

@MainActor
@Observable
final class ChannelsViewModel: ChannelsStore {
final class ChannelStore {
static let shared = ChannelStore()

private(set) var channels: [Channel] = []
var toast: ToastState?

weak var messages: MessagesStore!
var messages: MessageStore { Dependencies.shared.messages }

init() {
private init() {
Task {
channels = try await fetchChannels()
channels = await fetchChannels()

let channel = await supabase.realtimeV2.channel("public:channels")

Expand All @@ -44,6 +43,20 @@ final class ChannelsViewModel: ChannelsStore {
}
}

func addChannel(_ name: String) async {
do {
let userId = try await supabase.auth.session.user.id
let channel = AddChannel(slug: name, createdBy: userId)
try await supabase.database
.from("channels")
.insert(channel)
.execute()
} catch {
dump(error)
toast = .init(status: .error, title: "Error", description: error.localizedDescription)
}
}

func fetchChannel(id: Channel.ID) async throws -> Channel {
if let channel = channels.first(where: { $0.id == id }) {
return channel
Expand All @@ -65,6 +78,7 @@ final class ChannelsViewModel: ChannelsStore {
channels.append(channel)
} catch {
dump(error)
toast = .init(status: .error, title: "Error", description: error.localizedDescription)
}
}

Expand All @@ -74,7 +88,13 @@ final class ChannelsViewModel: ChannelsStore {
messages.removeMessages(for: id)
}

private func fetchChannels() async throws -> [Channel] {
try await supabase.database.from("channels").select().execute().value
private func fetchChannels() async -> [Channel] {
do {
return try await supabase.database.from("channels").select().execute().value
} catch {
dump(error)
toast = .init(status: .error, title: "Error", description: error.localizedDescription)
return []
}
}
}
52 changes: 52 additions & 0 deletions Examples/SlackClone/Dependencies.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// Dependencies.swift
// SlackClone
//
// Created by Guilherme Souza on 04/01/24.
//

import Foundation
import Supabase

class Dependencies {
static let shared = Dependencies()

let channel = ChannelStore.shared
let users = UserStore.shared
let messages = MessageStore.shared
}

struct User: Codable, Identifiable, Hashable {
var id: UUID
var username: String
}

struct AddChannel: Encodable {
var slug: String
var createdBy: UUID
}

struct Channel: Identifiable, Codable, Hashable {
var id: Int
var slug: String
var insertedAt: Date
}

struct Message: Identifiable, Codable, Hashable {
var id: Int
var insertedAt: Date
var message: String
var user: User
var channel: Channel
}

struct NewMessage: Codable {
var message: String
var userId: UUID
let channelId: Int
}

struct UserPresence: Codable, Hashable {
var userId: UUID
var onlineAt: Date
}
39 changes: 39 additions & 0 deletions Examples/SlackClone/Logger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// Logger.swift
// SlackClone
//
// Created by Guilherme Souza on 23/01/24.
//

import Foundation
import OSLog
import Supabase

extension Logger {
static let main = Self(subsystem: "com.supabase.SlackClone", category: "app")
}

final class SupabaseLoggerImpl: SupabaseLogger, @unchecked Sendable {
private let lock = NSLock()
private var loggers: [String: Logger] = [:]

func log(message: SupabaseLogMessage) {
lock.withLock {
if loggers[message.system] == nil {
loggers[message.system] = Logger(
subsystem: "com.supabase.SlackClone.supabase-swift",
category: message.system
)
}

let logger = loggers[message.system]!

switch message.level {
case .debug: logger.debug("\(message)")
case .error: logger.error("\(message)")
case .verbose: logger.info("\(message)")
case .warning: logger.notice("\(message)")
}
}
}
}
Loading