Skip to content

Instantly share code, notes, and snippets.

@rock3m
Last active July 19, 2022 20:00
Show Gist options
  • Save rock3m/65de1cae06fb74b0cbda8386f8bb8866 to your computer and use it in GitHub Desktop.
Save rock3m/65de1cae06fb74b0cbda8386f8bb8866 to your computer and use it in GitHub Desktop.
Re-render View
struct WorkHistoryView: View {
@FetchRequest(sortDescriptors: [SortDescriptor(\.createdAt, order: .reverse)])
private var historyEntries: FetchedResults<WorkHistoryEntry>
@State var refreshCounter = 0 // Use this state var to trigger the re-rendering of the list
var body: some View {
List {
ForEach(historyEntries) { historyEntry in
WorkHistoryEntryView(historyEntry: historyEntry, refreshCounter: refreshCounter)
}
}.navigationTitle("history_title")
.refreshable {
refreshCounter += 1
}
}
}
struct WorkHistoryEntryView: View {
var historyEntry: WorkHistoryEntry
var refreshCounter: Int // this is the value being passed from the parent view
let formatter = RelativeDateTimeFormatter()
var body: some View {
VStack {
HStack {
if let worker = historyEntry.historyEntryToWorker,
let workerName = worker.name,
let workerEmoji = worker.emoji{
Text("\(workerEmoji) \(workerName)")
.foregroundColor(Color.themeSecondary)
.font(.title2.weight(.semibold))
}
Spacer()
if let date = historyEntry.createdAt {
Text(formatter.localizedString(for: date, relativeTo: Date.now))
.foregroundColor(Color.themeSecondary)
.font(.footnote)
}
}
switch historyEntry.type {
case HistoryEntryType.earn.rawValue:
WorkHistoryRow(historyEntry: historyEntry)
case HistoryEntryType.withdraw.rawValue:
WithdrawHistoryRow(historyEntry: historyEntry)
default:
Text("...")
}
}.padding([.top, .bottom], 10)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment