First version

This commit is contained in:
Christoph Hagen
2024-10-14 19:22:32 +02:00
parent 7c812de089
commit 0989f06d87
51 changed files with 2477 additions and 234 deletions

View File

@ -0,0 +1,49 @@
import SwiftUI
struct DatePickerView: View {
@ObservedObject
var post: Post
@Binding var showDatePicker: Bool
var body: some View {
NavigationView {
VStack {
HStack(alignment: .top) {
VStack {
Text("Start date")
.font(.headline)
.padding(.vertical, 3)
DatePicker("", selection: $post.startDate, displayedComponents: .date)
.datePickerStyle(GraphicalDatePickerStyle())
.labelsHidden()
.padding()
}
VStack {
Toggle("End date", isOn: $post.hasEndDate)
.toggleStyle(.switch)
.font(.headline)
DatePicker("Select a date", selection: $post.startDate, displayedComponents: .date)
.datePickerStyle(GraphicalDatePickerStyle())
.labelsHidden()
.padding()
.disabled(!post.hasEndDate)
}
}
Button("Done") {
showDatePicker = false
}
Spacer()
}
.navigationTitle("Pick a Date")
.padding()
}
}
}
#Preview {
DatePickerView(post: .mock, showDatePicker: .constant(true))
}

View File

@ -0,0 +1,83 @@
import SwiftUI
import SFSafeSymbols
private struct NavigationIcon: View {
let symbol: SFSymbol
let edge: Edge.Set
var body: some View {
SwiftUI.Image(systemSymbol: symbol)
.resizable()
.aspectRatio(contentMode: .fit)
.padding(5)
.padding(edge, 2)
.fontWeight(.light)
.foregroundStyle(.white)
.frame(width: 30, height: 30)
.background(Color.black.opacity(0.6).clipShape(Circle()))
}
}
struct PostImageGalleryView: View {
let images: [Image]
@State private var currentIndex = 0
var body: some View {
ZStack {
images[currentIndex]
.resizable()
.scaledToFit()
if images.count > 1 {
HStack {
Button(action: previous) {
NavigationIcon(symbol: .chevronLeft, edge: .trailing)
}
.buttonStyle(.plain)
.padding()
Spacer()
Button(action: next) {
NavigationIcon(symbol: .chevronRight, edge: .leading)
}
.buttonStyle(.plain)
.padding()
}
VStack {
Spacer()
HStack(spacing: 8) {
ForEach(0..<images.count, id: \.self) { index in
Circle()
.fill(index == currentIndex ? Color.white : Color.gray) // Change color based on current index
.frame(width: 10, height: 10)
}
}
.padding(.bottom, 10)
}
}
}
}
private func previous() {
if currentIndex > 0 {
currentIndex -= 1
} else {
currentIndex = images.count - 1
}
}
private func next() {
if currentIndex < images.count - 1 {
currentIndex += 1
} else {
currentIndex = 0 // Wrap to first image
}
}
}
#Preview(traits: .fixedLayout(width: 300, height: 300)) {
PostImageGalleryView(images: MockImage.images.map { $0.imageToDisplay })
}

View File

@ -0,0 +1,75 @@
import SwiftUI
private struct CenteredPost<Content>: View where Content: View {
let content: Content
init(@ViewBuilder content: () -> Content) {
self.content = content()
}
var body: some View {
HorizontalCenter {
content
}
.listRowBackground(PostList.background)
}
}
struct PostList: View {
static let background = Color(r: 2, g: 15, b: 26)
@Binding
var posts: [Post]
var body: some View {
List {
if posts.isEmpty {
CenteredPost {
Text("No posts yet.")
.padding()
}
.listRowSeparator(.hidden)
}
CenteredPost {
Button(action: addNewPost) {
Text("Add post")
}
.padding()
.listRowSeparator(.hidden)
}
ForEach(posts) { post in
CenteredPost {
PostView(post: post)
.frame(maxWidth: 600)
}
.listRowSeparator(.hidden)
.listRowInsets(.init(top: 0, leading: 0, bottom: 30, trailing: 0))
}
}
.listStyle(.plain)
.background(PostList.background)
.scrollContentBackground(.hidden)
}
private func addNewPost() {
let largestId = posts.map { $0.id }.max() ?? 0
let post = Post(
id: largestId + 1,
isDraft: true,
startDate: .now,
endDate: nil,
title: .init(en: "Title", de: "Titel"),
text: .init(en: "Text", de: "Text"),
tags: [],
images: [])
posts.insert(post, at: 0)
}
}
#Preview {
PostList(posts: .constant([.mock, .fullMock]))
}

View File

@ -0,0 +1,88 @@
import SwiftUI
struct PostView: View {
@Environment(\.language)
var language
@ObservedObject
var post: Post
@State
private var showDatePicker = false
var body: some View {
VStack(alignment: .center) {
if !post.images.isEmpty {
PostImageGalleryView(images: post.displayImages)
.aspectRatio(1.33, contentMode: .fill)
}
VStack(alignment: .leading) {
HStack(alignment: .center, spacing: 0) {
Text(post.dateText(in: language))
.font(.system(size: 19, weight: .semibold))
.onTapGesture { showDatePicker = true }
Spacer()
Toggle("Draft", isOn: $post.isDraft)
}
.foregroundStyle(Color(r: 96, g: 186, b: 255))
TextField("", text: post.title.text(for: language))
.font(.system(size: 24, weight: .bold))
.foregroundStyle(Color.white)
.textFieldStyle(.plain)
.lineLimit(2)
FlowHStack {
ForEach(post.tags, id: \.id) { tag in
TagView(tag: tag.name)
.onTapGesture {
remove(tag: tag)
}
}
Button(action: showTagList) {
SwiftUI.Image(systemSymbol: .plusCircleFill)
.resizable()
.aspectRatio(1, contentMode: .fit)
.frame(height: 18)
.foregroundColor(TagView.foreground)
.opacity(0.7)
.padding(.top, 3)
}
.buttonStyle(.plain)
}
TextEditor(text: post.text.text(for: language))
.font(.body)
.foregroundStyle(Color(r: 221, g: 221, b: 221))
.textEditorStyle(.plain)
.padding(.leading, -5)
.scrollDisabled(true)
}
.padding()
}
.background(Color(r: 4, g: 31, b: 52))
.cornerRadius(8)
.sheet(isPresented: $showDatePicker) {
DatePickerView(post: post, showDatePicker: $showDatePicker)
}
}
private func remove(tag: Tag) {
post.tags = post.tags.filter {$0.id != tag.id }
}
private func showTagList() {
}
}
#Preview(traits: .fixedLayout(width: 450, height: 600)) {
List {
PostView(post: .fullMock)
.listRowSeparator(.hidden)
.listRowBackground(Color(r: 2, g: 15, b: 26))
.environment(\.language, ContentLanguage.german)
PostView(post: .mock)
.listRowSeparator(.hidden)
.listRowBackground(Color(r: 2, g: 15, b: 26))
}
.listStyle(.plain)
}

View File

@ -0,0 +1,57 @@
import Foundation
import SwiftUI
import SFSafeSymbols
struct TagView: View {
static let background = Color(r: 9, g: 62, b: 103)
static let foreground = Color(r: 96, g: 186, b: 255)
@Environment(\.language)
var language: ContentLanguage
let tag: LocalizedText
let icon: SFSymbol
let iconSize: CGFloat
init(tag: LocalizedText, icon: SFSymbol = .xCircleFill, iconSize: CGFloat = 12.0) {
self.tag = tag
self.icon = icon
self.iconSize = iconSize
}
static var add: TagView {
.init(tag: LocalizedText(en: "Add", de: "Mehr"), icon: .plusCircleFill)
}
var body: some View {
HStack {
Text(tag.getText(for: language))
.font(.subheadline)
.padding(.leading, 2)
SwiftUI.Image(systemSymbol: icon)
.font(.system(size: iconSize, weight: .black, design: .rounded))
.opacity(0.7)
.padding(.leading, -5)
}
.foregroundColor(TagView.foreground)
.font(.caption2)
.padding(.horizontal, 8)
.padding(.vertical, 4)
.background(TagView.background)
.cornerRadius(8)
}
}
#Preview {
HStack {
TagView(tag: LocalizedText(en: "Some", de: "Etwas"))
.environment(\.language, ContentLanguage.german)
TagView(tag: LocalizedText(en: "Some", de: "Etwas"))
.environment(\.language, ContentLanguage.english)
TagView.add
}
}