Skip to content
Open

wip #183

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
1 change: 0 additions & 1 deletion TypeaheadAI/Actors/SpecialCopyActor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ actor SpecialCopyActor: CanSimulateCopy {
}
}

// Define the notification name extension somewhere in your codebase
extension Notification.Name {
static let smartCopyPerformed = Notification.Name("smartCopyPerformed")
}
6 changes: 5 additions & 1 deletion TypeaheadAI/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele
// Handle the URL
if url.host == "login-callback" {
let urlDict: [String: URL] = ["url": url]
NotificationCenter.default.post(name: Notification.Name("OAuthCallBack"), object: nil, userInfo: urlDict)
NotificationCenter.default.post(name: .oAuthCallback, object: nil, userInfo: urlDict)
}
if url.host == "onboarding-callback" {
let urlDict: [String: URL] = ["url": url]
NotificationCenter.default.post(name: .oAuthOnboardingCallback, object: nil, userInfo: urlDict)
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions TypeaheadAI/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ final class AppState: ObservableObject {
@Published var settingsManager: SettingsManager
@Published var clientManager: ClientManager
@Published var intentManager: IntentManager

var supabaseManager = SupabaseManager()
@Published var supabaseManager = SupabaseManager()

private let historyManager: HistoryManager
private let appContextManager: AppContextManager
Expand Down
54 changes: 21 additions & 33 deletions TypeaheadAI/Supabase/SupabaseManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,34 @@ import Supabase
import SwiftUI
import Foundation

class SupabaseManager {
extension Notification.Name {
static let oAuthCallback = Notification.Name("oauthCallback")
static let oAuthOnboardingCallback = Notification.Name("oauthOnboardingCallback")
}

class SupabaseManager: ObservableObject {
let client = SupabaseClient(
supabaseURL: URL(string: "https://hwkkvezmbrlrhvipbsum.supabase.co")!,
supabaseKey: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Imh3a2t2ZXptYnJscmh2aXBic3VtIiwicm9sZSI6ImFub24iLCJpYXQiOjE2OTgzNjY4NTEsImV4cCI6MjAxMzk0Mjg1MX0.aDzWW0p2uI7wsVGsu1mtfvEh4my8s9zhgVTr4r008YU")
private let callbackURL: URL = URL(string: "app.typeahead://login-callback")!
private let onboardingCallbackURL: URL = URL(string: "app.typeahead://onboarding-callback")!

// Use this as a flag for checking if the user is signed in.
@AppStorage("token3") var token: String?
@AppStorage("uuid") var uuid: String?

init() {
// Register OAuth notifications
NotificationCenter.default.addObserver(
self,
selector: #selector(self.oAuthCallback(_:)),
name: NSNotification.Name(rawValue: "OAuthCallBack"),
object: nil)
}

@MainActor
func registerWithEmail(email: String, password: String) async throws {
try await client.auth.signUp(email: email, password: password)
let session = try await client.auth.session

let user = session.user
uuid = user.id.uuidString
token = "placeholder"
}

func signinWithApple() async throws {
let url = try client.auth.getOAuthSignInURL(provider: Provider.apple, redirectTo: callbackURL)
@MainActor
func signinWithEmail(email: String, password: String) async throws {
let authResponse = try await client.auth.signIn(email: email, password: password)
let user = authResponse.user
}

func signinWithApple(onboarding: Bool = false) async throws {
let callback = onboarding ? onboardingCallbackURL : callbackURL
let url = try client.auth.getOAuthSignInURL(provider: Provider.apple, redirectTo: callback)
NSWorkspace.shared.open(url)
}

Expand All @@ -48,23 +46,13 @@ class SupabaseManager {
NSWorkspace.shared.open(url)
}

@MainActor
func signout() async throws {
uuid = nil
token = nil
try await client.auth.signOut()
}

@objc func oAuthCallback(_ notification: NSNotification){
guard let url = notification.userInfo?["url"] as? URL else { return }
Task {
do {
let session = try await client.auth.session(from: url)
let user = session.user
uuid = user.id.uuidString
token = "placeholder"
} catch {
print("### oAuthCallback error: \(error)")
}
}
func signinFromUrl(from: URL) async throws {
let session = try await client.auth.session(from: from)
let user = session.user
}
}
6 changes: 5 additions & 1 deletion TypeaheadAI/TypeaheadAIApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct TypeaheadAIApp {
static let onboardingKey = "hasOnboardedV4"

static func main() {
// UserDefaults.standard.setValue(false, forKey: onboardingKey)
UserDefaults.standard.setValue(false, forKey: onboardingKey)

if UserDefaults.standard.bool(forKey: onboardingKey) {
MacOS13AndLaterApp.main()
Expand Down Expand Up @@ -62,6 +62,7 @@ struct MacOS13AndLaterApp: App {
promptManager: appState.promptManager,
modalManager: appState.modalManager,
settingsManager: appState.settingsManager,
supabaseManager: appState.supabaseManager,
isMenuVisible: $appState.isMenuVisible
)
} label: {
Expand Down Expand Up @@ -101,6 +102,7 @@ struct MacOS13AndLaterAppWithOnboardingV2: App {
promptManager: appState.promptManager,
modalManager: appState.modalManager,
settingsManager: appState.settingsManager,
supabaseManager: appState.supabaseManager,
isMenuVisible: $appState.isMenuVisible
)
} label: {
Expand All @@ -117,13 +119,15 @@ struct CommonMenuView: View {
@ObservedObject var promptManager: PromptManager
@ObservedObject var modalManager: ModalManager
@ObservedObject var settingsManager: SettingsManager
@ObservedObject var supabaseManager: SupabaseManager
@Binding var isMenuVisible: Bool

var body: some View {
MenuView(
promptManager: promptManager,
modalManager: modalManager,
settingsManager: settingsManager,
supabaseManager: supabaseManager,
isMenuVisible: $isMenuVisible
)
.environment(\.managedObjectContext, persistenceController.container.viewContext)
Expand Down
14 changes: 8 additions & 6 deletions TypeaheadAI/Views/Menu/MenuView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@ struct MenuView: View {
@ObservedObject var promptManager: PromptManager
@ObservedObject var modalManager: ModalManager
@ObservedObject var settingsManager: SettingsManager

private let supabaseManager = SupabaseManager()
@ObservedObject var supabaseManager: SupabaseManager

@Binding var isMenuVisible: Bool
@Environment(\.managedObjectContext) private var viewContext
@Environment(\.colorScheme) private var colorScheme

@AppStorage("token3") var token: String?
@AppStorage("settingsTab") var settingsTab: String?
@AppStorage("selectedModel") private var selectedModelURL: URL?

@State private var isLoggedIn: Bool = false
@State private var currentPreset: String = ""
@State private var isEditingID: UUID?
@FocusState private var isTextFieldFocused: Bool
Expand Down Expand Up @@ -103,10 +102,12 @@ struct MenuView: View {
isMenuVisible = false
}

if token != nil {
if isLoggedIn {
MenuButtonView(title: "Sign out") {
token = nil
isMenuVisible = false
Task {
try await supabaseManager.signout()
isMenuVisible = false
}
}
} else {
MenuButtonView(title: "Sign in") {
Expand Down Expand Up @@ -156,6 +157,7 @@ struct MenuView_Previews: PreviewProvider {
promptManager: promptManager,
modalManager: modalManager,
settingsManager: SettingsManager(context: context),
supabaseManager: SupabaseManager(),
isMenuVisible: $isMenuVisible
)
.environment(\.managedObjectContext, context)
Expand Down
2 changes: 1 addition & 1 deletion TypeaheadAI/Views/Onboarding/LoggedOutOnboardingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct LoggedOutOnboardingView: View {
AccountOptionButton(label: "Sign-in with Apple", width: 250) {
Task {
do {
try await supabaseManager.signinWithApple()
try await supabaseManager.signinWithApple(onboarding: true)
} catch {
failedToRegisterReason = error.localizedDescription
failedToSignIn = true
Expand Down
12 changes: 10 additions & 2 deletions TypeaheadAI/Views/Onboarding/OnboardingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct OnboardingView: View {
var modalManager: ModalManager
var intentManager: IntentManager

@State private var isLoggedIn: Bool = false
@State private var step: Int = 1
private let totalSteps: Int = 7

Expand All @@ -30,8 +31,8 @@ struct OnboardingView: View {
}

var body: some View {
VStack {
if let _ = supabaseManager.uuid {
Group {
if isLoggedIn {
VStack {
panel

Expand All @@ -44,6 +45,13 @@ struct OnboardingView: View {
LoggedOutOnboardingView(
supabaseManager: supabaseManager
)
.onReceive(NotificationCenter.default.publisher(for: .oAuthOnboardingCallback), perform: { notification in
guard let url = notification.userInfo?["url"] as? URL else { return }
Task {
try await supabaseManager.signinFromUrl(from: url)
isLoggedIn = true
}
})
}
}
.frame(width: 500, height: 550)
Expand Down
8 changes: 2 additions & 6 deletions TypeaheadAI/Views/Settings/AccountView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ import Supabase
import AuthenticationServices

struct AccountView: View {
// Use this as a flag for checking if the user is signed in.
@AppStorage("token3") var token: String?
@AppStorage("uuid") var uuid: String?

@Environment(\.colorScheme) var colorScheme
var supabaseManager: SupabaseManager
@ObservedObject var supabaseManager: SupabaseManager

var body: some View {
VStack(alignment: .leading) {
Expand Down Expand Up @@ -56,7 +52,7 @@ struct AccountView: View {
.frame(maxWidth: .infinity)

HStack {
Text("User ID: \(uuid ?? "<none>")")
Text("User ID: \(supabaseManager.uuid ?? "<none>")")
.font(.footnote)
.foregroundStyle(.secondary)
.padding(10)
Expand Down
9 changes: 1 addition & 8 deletions TypeaheadAI/Views/Settings/LoggedOutAccountView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import SwiftUI

struct LoggedOutAccountView: View {
// Use this as a flag for checking if the user is signed in.
@AppStorage("token3") var token: String?
@AppStorage("uuid") var uuid: String?

@Environment(\.colorScheme) var colorScheme

@State private var email: String = ""
Expand Down Expand Up @@ -93,11 +90,7 @@ struct LoggedOutAccountView: View {
Button {
Task {
do {
let authResponse = try await supabaseManager.client.auth.signIn(email: email, password: password)
let user = authResponse.user
uuid = user.id.uuidString
token = "placeholder"
let _ = try await supabaseManager.client.auth.session
_ = try await supabaseManager.signinWithEmail(email: email, password: password)
email = ""
password = ""
} catch {
Expand Down