Skip to content
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

view abstraction of navigationTitle and modelSelect for reuse in demo #161

Merged
merged 3 commits into from
Feb 7, 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
65 changes: 65 additions & 0 deletions Demo/DemoChat/Sources/Extensions/View.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// View.swift
//
//
// Created by James J Kalafus on 2024-02-03.
//

import SwiftUI

extension View {

@inlinable public func navigationTitle(_ titleKey: LocalizedStringKey, selectedModel: Binding<String>) -> some View {
self
.navigationTitle(titleKey)
.safeAreaInset(edge: .top) {
HStack {
Text(
"Model: \(selectedModel.wrappedValue)"
)
.font(.caption)
.foregroundColor(.secondary)
Spacer()
}
.padding(.horizontal, 16)
.padding(.vertical, 8)
}
}

@inlinable public func modelSelect(selectedModel: Binding<String>, models: [String], showsModelSelectionSheet: Binding<Bool>, help: String) -> some View {
self
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
showsModelSelectionSheet.wrappedValue.toggle()
}) {
Image(systemName: "cpu")
}
}
}
.confirmationDialog(
"Select model",
isPresented: showsModelSelectionSheet,
titleVisibility: .visible,
actions: {
ForEach(models, id: \.self) { (model: String) in
Button {
selectedModel.wrappedValue = model
} label: {
Text(model)
}
}

Button("Cancel", role: .cancel) {
showsModelSelectionSheet.wrappedValue = false
}
},
message: {
Text(
"View \(help) for details"
)
.font(.caption)
}
)
}
}
50 changes: 3 additions & 47 deletions Demo/DemoChat/Sources/UI/DetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct DetailView: View {
@State private var showsModelSelectionSheet = false
@State private var selectedChatModel: Model = .gpt4_0613

private let availableChatModels: [Model] = [.gpt3_5Turbo, .gpt4_0613]
private static let availableChatModels: [Model] = [.gpt3_5Turbo, .gpt4]

let conversation: Conversation
let error: Error?
Expand Down Expand Up @@ -65,52 +65,8 @@ struct DetailView: View {

inputBar(scrollViewProxy: scrollViewProxy)
}
.navigationTitle("Chat")
.safeAreaInset(edge: .top) {
HStack {
Text(
"Model: \(selectedChatModel)"
)
.font(.caption)
.foregroundColor(.secondary)
Spacer()
}
.padding(.horizontal, 16)
.padding(.vertical, 8)
}
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
showsModelSelectionSheet.toggle()
}) {
Image(systemName: "cpu")
}
}
}
.confirmationDialog(
"Select model",
isPresented: $showsModelSelectionSheet,
titleVisibility: .visible,
actions: {
ForEach(availableChatModels, id: \.self) { model in
Button {
selectedChatModel = model
} label: {
Text(model)
}
}

Button("Cancel", role: .cancel) {
showsModelSelectionSheet = false
}
},
message: {
Text(
"View https://platform.openai.com/docs/models/overview for details"
)
.font(.caption)
}
)
.navigationTitle("Chat", selectedModel: $selectedChatModel)
.modelSelect(selectedModel: $selectedChatModel, models: Self.availableChatModels, showsModelSelectionSheet: $showsModelSelectionSheet, help: "https://platform.openai.com/docs/models/overview")
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions Demo/DemoChat/Sources/UI/TextToSpeechView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ public struct TextToSpeechView: View {
@State private var voice: AudioSpeechQuery.AudioSpeechVoice = .alloy
@State private var speed: Double = AudioSpeechQuery.Speed.normal.rawValue
@State private var responseFormat: AudioSpeechQuery.AudioSpeechResponseFormat = .mp3

@State private var showsModelSelectionSheet = false
@State private var selectedSpeechModel: String = Model.tts_1

private static let availableSpeechModels: [String] = [Model.tts_1, Model.tts_1_hd]

public init(store: SpeechStore) {
self.store = store
}
Expand Down Expand Up @@ -79,7 +83,7 @@ public struct TextToSpeechView: View {
Section {
HStack {
Button("Create Speech") {
let query = AudioSpeechQuery(model: .tts_1,
let query = AudioSpeechQuery(model: selectedSpeechModel,
input: prompt,
voice: voice,
responseFormat: responseFormat,
Expand All @@ -93,6 +97,7 @@ public struct TextToSpeechView: View {
.disabled(prompt.replacingOccurrences(of: " ", with: "").isEmpty)
Spacer()
}
.modelSelect(selectedModel: $selectedSpeechModel, models: Self.availableSpeechModels, showsModelSelectionSheet: $showsModelSelectionSheet, help: "https://platform.openai.com/docs/models/tts")
}
if !$store.audioObjects.wrappedValue.isEmpty {
Section("Click to play, swipe to save:") {
Expand Down Expand Up @@ -129,7 +134,7 @@ public struct TextToSpeechView: View {
}
.listStyle(.insetGrouped)
.scrollDismissesKeyboard(.interactively)
.navigationTitle("Create Speech")
.navigationTitle("Create Speech", selectedModel: $selectedSpeechModel)
}
}

Expand Down
Loading