mirror of
https://github.com/1SecondEveryday/MemoryTree.git
synced 2026-03-25 08:55:47 +00:00
110 lines
2.8 KiB
Swift
110 lines
2.8 KiB
Swift
//
|
|
// CloudView.swift
|
|
// MemoryTree
|
|
//
|
|
// Created by Work on 2023-02-05.
|
|
//
|
|
|
|
import Boutique
|
|
import SwiftUI
|
|
|
|
struct CloudView: View {
|
|
// @Stored(in: .entriesStore) var entries
|
|
@Binding var entries: [Entry] {
|
|
didSet {
|
|
countedWords = countWords()
|
|
}
|
|
}
|
|
|
|
@State var countedWords: [String: Int] = [:]
|
|
|
|
var words: [String] {
|
|
Array(countedWords.keys)
|
|
}
|
|
|
|
@Environment(\.dismiss) var dismiss
|
|
|
|
var body: some View {
|
|
VStack {
|
|
ForEach(words, id: \.self) { word in
|
|
let count = countedWords[word, default: 1]
|
|
Text(word)
|
|
.font(fontForCount(count))
|
|
.bold(count > 2)
|
|
}
|
|
}
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
.edgesIgnoringSafeArea(.bottom)
|
|
.background(Color.blue)
|
|
.foregroundColor(.white)
|
|
.toolbar {
|
|
ToolbarItem(placement: .destructiveAction) {
|
|
Button {
|
|
// Task {
|
|
// try await $entries.removeAll()
|
|
// }
|
|
entries.removeAll()
|
|
dismiss()
|
|
} label: {
|
|
Image(systemName: "flame")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func fontForCount(_ count: Int) -> Font {
|
|
switch count {
|
|
case 0, 1: return .body
|
|
case 2: return .title3
|
|
case 3: return .title2
|
|
case 4: return .title
|
|
default: return .largeTitle
|
|
}
|
|
}
|
|
|
|
func countWords() -> [String: Int] {
|
|
let words = entries
|
|
.map(\.text)
|
|
.flatMap { $0.split(separator: " ") }
|
|
.map(String.init)
|
|
return words.reduce(into: [:]) { partialResult, word in
|
|
partialResult[word, default: 0] += 1
|
|
}
|
|
}
|
|
}
|
|
|
|
struct CloudView_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
Group {
|
|
NavigationStack {
|
|
CloudView(entries: .constant([]))
|
|
}
|
|
.previewDisplayName("Empty")
|
|
|
|
NavigationStack {
|
|
CloudView(entries: .constant([]), countedWords: [
|
|
"Coffee" : 2,
|
|
"Trees" : 1,
|
|
"Mountains" : 1,
|
|
])
|
|
}
|
|
.previewDisplayName("Few entries")
|
|
|
|
NavigationStack {
|
|
CloudView(entries: .constant([]), countedWords: [
|
|
"Coffee" : 4,
|
|
"Trees" : 2,
|
|
"Mountains" : 3,
|
|
"Burritos" : 1,
|
|
"Home" : 5,
|
|
"My Cat": 2,
|
|
"Love": 3,
|
|
"Clean water": 2,
|
|
"Health": 3,
|
|
])
|
|
}
|
|
.previewDisplayName("Many entries")
|
|
}
|
|
.tint(.white)
|
|
}
|
|
}
|