metatext-app-ios-iphone-ipad/Views/SwiftUI/ListsView.swift

71 lines
2.2 KiB
Swift
Raw Normal View History

2020-08-29 05:50:58 +02:00
// Copyright © 2020 Metabolist. All rights reserved.
2021-01-21 09:45:09 +01:00
import Mastodon
2020-08-29 05:50:58 +02:00
import SwiftUI
2020-09-01 09:33:49 +02:00
import ViewModels
2020-08-29 05:50:58 +02:00
struct ListsView: View {
@StateObject var viewModel: ListsViewModel
2020-09-10 00:48:56 +02:00
@EnvironmentObject var rootViewModel: RootViewModel
2020-08-29 05:50:58 +02:00
@State private var newListTitle = ""
var body: some View {
Form {
Section {
TextField("lists.new-list-title", text: $newListTitle)
.disabled(viewModel.creatingList)
if viewModel.creatingList {
ProgressView()
.frame(maxWidth: .infinity, alignment: .center)
} else {
Button {
viewModel.createList(title: newListTitle)
} label: {
Label("add", systemImage: "plus.circle")
}
2020-12-03 23:32:15 +01:00
.disabled(newListTitle.isEmpty)
2020-08-29 05:50:58 +02:00
}
}
Section {
ForEach(viewModel.lists) { list in
2021-01-21 09:45:09 +01:00
Button {
rootViewModel.navigationViewModel?.navigate(timeline: .list(list))
} label: {
Text(list.title)
.foregroundColor(.primary)
2020-08-29 05:50:58 +02:00
}
}
.onDelete {
guard let index = $0.first else { return }
viewModel.delete(list: viewModel.lists[index])
}
}
}
.navigationTitle(Text("secondary-navigation.lists"))
.toolbar {
ToolbarItem(placement: ToolbarItemPlacement.navigationBarTrailing) {
EditButton()
}
}
.alertItem($viewModel.alertItem)
.onAppear(perform: viewModel.refreshLists)
.onReceive(viewModel.$creatingList) {
if !$0 {
newListTitle = ""
}
}
}
}
#if DEBUG
2020-09-01 09:33:49 +02:00
import PreviewViewModels
2020-08-29 05:50:58 +02:00
struct ListsView_Previews: PreviewProvider {
static var previews: some View {
2021-01-26 01:06:35 +01:00
ListsView(viewModel: .init(identityContext: .preview))
2022-11-08 23:04:34 +01:00
.environmentObject(NavigationViewModel(identityContext: .preview, environment: .preview))
2020-08-29 05:50:58 +02:00
}
}
#endif