Skip to content

Commit

Permalink
better settings, conversations,
Browse files Browse the repository at this point in the history
  • Loading branch information
ctate committed May 22, 2024
1 parent 105d7eb commit eb287db
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 63 deletions.
4 changes: 4 additions & 0 deletions platforms/apple/Crystal.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
D72B02822BF512A300D5FEA4 /* CustomTextEditor.swift in Sources */ = {isa = PBXBuildFile; fileRef = D72B02812BF512A300D5FEA4 /* CustomTextEditor.swift */; };
D76D3B102BF5D8CA0017F2BA /* ConversationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = D76D3B0F2BF5D8CA0017F2BA /* ConversationManager.swift */; };
D7B415792BFD8EF800D2C131 /* UserDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7B415782BFD8EF800D2C131 /* UserDefaults.swift */; };
D7C9D2A82BF2FCC500697DDE /* CrystalApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7C9D2A72BF2FCC500697DDE /* CrystalApp.swift */; };
D7C9D2AA2BF2FCC500697DDE /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7C9D2A92BF2FCC500697DDE /* ContentView.swift */; };
D7C9D2AC2BF2FCC500697DDE /* Item.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7C9D2AB2BF2FCC500697DDE /* Item.swift */; };
Expand Down Expand Up @@ -87,6 +88,7 @@
/* Begin PBXFileReference section */
D72B02812BF512A300D5FEA4 /* CustomTextEditor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomTextEditor.swift; sourceTree = "<group>"; };
D76D3B0F2BF5D8CA0017F2BA /* ConversationManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConversationManager.swift; sourceTree = "<group>"; };
D7B415782BFD8EF800D2C131 /* UserDefaults.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserDefaults.swift; sourceTree = "<group>"; };
D7C9D2A42BF2FCC500697DDE /* Crystal.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Crystal.app; sourceTree = BUILT_PRODUCTS_DIR; };
D7C9D2A72BF2FCC500697DDE /* CrystalApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CrystalApp.swift; sourceTree = "<group>"; };
D7C9D2A92BF2FCC500697DDE /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -206,6 +208,7 @@
D7C9D32E2BF345BC00697DDE /* PlainButtonStyle.swift */,
D7C9D2A92BF2FCC500697DDE /* ContentView.swift */,
D7C9D3362BF3A48100697DDE /* Notification.swift */,
D7B415782BFD8EF800D2C131 /* UserDefaults.swift */,
D76D3B0F2BF5D8CA0017F2BA /* ConversationManager.swift */,
D7C9D3402BF3F20C00697DDE /* AdaptiveHeightView.swift */,
D7C9D3262BF31A6A00697DDE /* Globals.swift */,
Expand Down Expand Up @@ -464,6 +467,7 @@
D7C9D3202BF3172200697DDE /* PromptModelsView.swift in Sources */,
D7C9D2E92BF3165300697DDE /* OpenAiApi.swift in Sources */,
D7C9D2E62BF3165300697DDE /* GroqApi.swift in Sources */,
D7B415792BFD8EF800D2C131 /* UserDefaults.swift in Sources */,
D7C9D31E2BF3172200697DDE /* DocumentPicker.swift in Sources */,
D7C9D3212BF3172200697DDE /* SettingsView.swift in Sources */,
D7C9D3022BF3167400697DDE /* ChatView.swift in Sources */,
Expand Down
1 change: 0 additions & 1 deletion platforms/apple/Crystal/Cards/TextCard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ struct TextCard: View {
var body: some View {
AdaptiveHeightView {
Text(text)
.font(.system(size: 18))
.padding(.horizontal, 20)
.foregroundColor(.white)
.textSelection(.enabled)
Expand Down
1 change: 0 additions & 1 deletion platforms/apple/Crystal/Chat/ChatToolbarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ struct ChatToolbarView: ToolbarContent {
.resizable()
.scaledToFit()
.frame(width: 16, height: 16)
.forceDarkMode()
Text(selectedModelId)
}
}
Expand Down
91 changes: 76 additions & 15 deletions platforms/apple/Crystal/Chat/ChatView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,21 @@ struct ChatContentView: View {
.padding(.bottom, 40)
} else {
VStack(alignment: .trailing) {
#if os(iOS)
TextField("Say something...", text: $userInput, axis: .vertical)
.onSubmit {
viewModel.sendMessage(
userInput,
conversationManager: conversationManager,
modelContext: modelContext
)
userInput = ""
}
.textFieldStyle(.roundedBorder)
.padding()
#else
CustomTextEditor(text: $userInput)
#endif
}
}

Expand Down Expand Up @@ -198,19 +212,15 @@ struct ChatContentView: View {
}

struct ChatView: View {
#if os(iOS)
init() {
let appearance = UINavigationBarAppearance()
appearance.configureWithTransparentBackground()
appearance.backgroundColor = .clear
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().compactAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
}
#endif

@State private var isSidebarVisible: Bool = false
private let sidebarWidth: CGFloat = 300
@State private var dragOffset: CGFloat = 0
private var sidebarWidth: CGFloat {
#if os(macOS)
return 300
#else
return UIScreen.main.bounds.width * 0.75
#endif
}

var body: some View {
#if os(macOS)
Expand All @@ -230,14 +240,59 @@ struct ChatView: View {
.frame(maxWidth: .infinity, maxHeight: .infinity)

#else
NavigationView {
ChatContentView()
GeometryReader { geometry in
HStack(alignment: .top, spacing: 0) {
SidebarView(sidebarAction: {
withAnimation {
isSidebarVisible = false
dragOffset = 0
}
})
.frame(width: sidebarWidth)

NavigationView {
ChatContentView(sidebarAction: {
withAnimation {
isSidebarVisible.toggle()
if isSidebarVisible {
dragOffset = sidebarWidth
} else {
dragOffset = 0
}
}
})
}
.frame(width: geometry.size.width)
.background(Color.white)
}
.frame(width: geometry.size.width + sidebarWidth)
.offset(x: -sidebarWidth + dragOffset)
.animation(.interactiveSpring(), value: dragOffset)
.gesture(
DragGesture().onChanged { gesture in
let newOffset = gesture.translation.width + (isSidebarVisible ? sidebarWidth : 0)
if newOffset > 0 {
dragOffset = min(newOffset, sidebarWidth)
}
}
.onEnded { gesture in
let threshold = sidebarWidth / 2
if dragOffset > threshold {
isSidebarVisible = true
} else {
isSidebarVisible = false
}
dragOffset = isSidebarVisible ? sidebarWidth : 0
}
)
}
#endif
}
}

struct SidebarView: View {
var sidebarAction: (() -> Void)? = {}

@Environment(\.modelContext) private var modelContext
@EnvironmentObject var conversationManager: ConversationManager
@Query private var conversations: [Conversation]
Expand All @@ -249,9 +304,15 @@ struct SidebarView: View {
ForEach(conversations.sorted { $0.createdAt > $1.createdAt }, id: \.self) { conversation in
Button(action: {
conversationManager.selectedConversation = conversation
if sidebarAction != nil {
sidebarAction!()
}
}) {
Text("\(conversation.messages.sorted { $0.timestamp > $1.timestamp }.first?.text ?? "(no text)")")
.frame(maxWidth: .infinity, alignment: .leading)
.foregroundColor(conversationManager.selectedConversation == conversation ? .white : .black)
.lineLimit(1)
.truncationMode(.tail)
.padding()
.contentShape(Rectangle())
}
Expand All @@ -274,7 +335,7 @@ struct SidebarView: View {
}),
secondaryButton: .default(Text("Cancel"))
)

}
}
}
Expand Down
6 changes: 4 additions & 2 deletions platforms/apple/Crystal/CrystalApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct CrystalApp: App {
fatalError("Could not create ModelContainer: \(error)")
}
}()

var body: some Scene {
#if os(macOS)
Window("Crystal", id: "main-window") {
Expand All @@ -32,7 +32,9 @@ struct CrystalApp: App {
#else
WindowGroup("Crystal", id: "main-window") {
ContentView()
.preferredColorScheme(.dark)
.environment(\.font, Font.custom(UserDefaults.standard.string(forKey: UserDefaults.Keys.font) ?? "San Francisco", size: 14))
// .forceDarkMode()
// .preferredColorScheme(.dark)
}
.environmentObject(ConversationManager())
.modelContainer(sharedModelContainer)
Expand Down
28 changes: 14 additions & 14 deletions platforms/apple/Crystal/ForceDarkMode.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import SwiftUI

struct ForceDarkMode: ViewModifier {
func body(content: Content) -> some View {
content
.environment(\.colorScheme, .dark)
}
}

extension View {
func forceDarkMode() -> some View {
self.modifier(ForceDarkMode())
}
}
//import SwiftUI
//
//struct ForceDarkMode: ViewModifier {
// func body(content: Content) -> some View {
// content
// .environment(\.colorScheme, .dark)
// }
//}
//
//extension View {
// func forceDarkMode() -> some View {
// self.modifier(ForceDarkMode())
// }
//}
Loading

0 comments on commit eb287db

Please sign in to comment.