Add Settings and Settings Edit Functionality

This commit is contained in:
Sebastian Lenzlinger 2023-07-11 17:59:14 +02:00
parent e1c606fdda
commit 379d3c8a0e
5 changed files with 87 additions and 12 deletions

View File

@ -10,7 +10,7 @@ import CoreBluetooth
struct ContentView: View {
@State var currentView = 0
@EnvironmentObject var dataStore: DataStore
@StateObject var dataStore = DataStore()
var body: some View {
VStack {
@ -24,6 +24,7 @@ struct ContentView: View {
case 2:
SettingsView()
.environmentObject(dataStore)
.navigationTitle("Settings")
default:
FeedListView(feeds: [])
.environmentObject(dataStore)

View File

@ -18,4 +18,6 @@ class DataStore: ObservableObject {
self.friends = friends
self.feeds = feeds
}
}

View File

@ -9,11 +9,10 @@ import SwiftUI
@main
struct RippleChatApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(DataStore())
}
}
}

View File

@ -9,14 +9,36 @@ import SwiftUI
struct SettingsEditView: View {
@EnvironmentObject var dataStore: DataStore
@State private var newFeedID = ""
var body: some View {
Form {
Section(header: Text("Personal Feed ID")) {
//TextField("FeedID", text: $scrum.title)
//Label(dataStore.personalID, systemImage: "person.crop.circle")
HStack {
TextField(dataStore.personalID, text: $dataStore.personalID)
}
}
Section(header: Text("Friends")) {
ForEach(dataStore.friends) { friend in
Label(friend, systemImage: "person")
}
.onDelete {indices in
dataStore.friends.remove(atOffsets: indices)
}
HStack {
TextField("New Feed", text: $newFeedID)
Button(action: {
withAnimation {
let feedid = newFeedID
dataStore.friends.append(feedid)
newFeedID = ""
}
}) {
Image(systemName: "plus.circle.fill")
}
.disabled(newFeedID.isEmpty)
}
}
}
}
@ -25,5 +47,6 @@ struct SettingsEditView: View {
struct SettingsEditView_Previews: PreviewProvider {
static var previews: some View {
SettingsEditView()
.environmentObject(DataStore())
}
}

View File

@ -8,22 +8,72 @@
import SwiftUI
struct SettingsView: View {
@State private var newEntry: String = ""
@EnvironmentObject var dataStore: DataStore
@State private var isPresentingEditView = false
var body: some View {
List {
HStack {
Spacer()
Button("Edit") {
isPresentingEditView = true
}
}
Section(header: Text("Personal Feed ID")) {
Text("Your FeedID is: \(dataStore.personalID)")
Label(dataStore.personalID, systemImage: "person.crop.circle")
}
Section(header: Text("Friends")) {
ForEach(dataStore.friends) { friend in
Label(friend, systemImage: "person")
}
}
}
.navigationTitle("Settings")
.sheet(isPresented: $isPresentingEditView) {
NavigationStack {
SettingsEditView()
.navigationTitle("Settings")
.toolbar {
ToolbarItem(placement: .cancellationAction){
Button("Cancel") {
isPresentingEditView = false
}
}
ToolbarItem(placement: .confirmationAction) {
Button("Done") {
isPresentingEditView = false
}
}
}
}
}
}
}
}
}
struct SettingsView_Previews: PreviewProvider {
static var friends = [
"BOS",
"ALI",
"CYN"
]
static var previews: some View {
SettingsView()
.environmentObject(DataStore(personalID: "BOB"))
.environmentObject(DataStore(personalID: "BOB", friends: friends))
.navigationTitle("Settings")
}
}
extension String: Identifiable {
public typealias ID = Int
public var id: Int {
return hash
}
}