create contentview view model
add background to content view make contentview list split by year / month test data will add 120 days instead of 25 closed #35 closed #36
This commit is contained in:
@@ -15,12 +15,11 @@ struct ContentView: View {
|
||||
@State private var showingSheet = false
|
||||
@State private var showTodayInput = true
|
||||
|
||||
@FetchRequest(
|
||||
sortDescriptors: [NSSortDescriptor(keyPath: \MoodEntry.forDate, ascending: false)],
|
||||
animation: .spring())
|
||||
private var items: FetchedResults<MoodEntry>
|
||||
|
||||
init(){ }
|
||||
@ObservedObject var viewModel = ContentModeViewModel()
|
||||
|
||||
init(){
|
||||
UITabBar.appearance().backgroundColor = UIColor.systemBackground
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
TabView {
|
||||
@@ -38,7 +37,6 @@ struct ContentView: View {
|
||||
.tabItem {
|
||||
Label("Stats", systemImage: "chart.line.uptrend.xyaxis")
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,10 +47,14 @@ struct ContentView: View {
|
||||
showingSheet.toggle()
|
||||
}, label: {
|
||||
Image(systemName: "gear")
|
||||
.foregroundColor(Color(UIColor.systemGray))
|
||||
.foregroundColor(Color(UIColor.darkGray))
|
||||
.font(.system(size: 20))
|
||||
}).sheet(isPresented: $showingSheet) {
|
||||
SettingsView()
|
||||
SettingsView(editedDataClosure: {
|
||||
withAnimation{
|
||||
viewModel.updateData()
|
||||
}
|
||||
})
|
||||
}.padding(.trailing)
|
||||
}
|
||||
}
|
||||
@@ -61,98 +63,125 @@ struct ContentView: View {
|
||||
let weekday = Calendar.current.component(.weekday, from: date)
|
||||
let calendar = Calendar.current
|
||||
let dayIndex = ((weekday - 1) + (calendar.firstWeekday - 1)) % 7
|
||||
return calendar.shortWeekdaySymbols[dayIndex]
|
||||
return calendar.weekdaySymbols[dayIndex]
|
||||
}
|
||||
|
||||
private func monthName(fromMonthInt: Int) -> String {
|
||||
let monthName = DateFormatter().monthSymbols[fromMonthInt-1]
|
||||
return monthName
|
||||
}
|
||||
|
||||
private func dayFormat(fromDate date: Date) -> String {
|
||||
let components = Calendar.current.dateComponents([.day], from: date)
|
||||
let day = components.day!
|
||||
|
||||
let formatter = NumberFormatter()
|
||||
formatter.numberStyle = .ordinal
|
||||
return formatter.string(from: NSNumber(integerLiteral: day)) ?? ""
|
||||
}
|
||||
|
||||
private var listView: some View {
|
||||
List {
|
||||
ForEach(items) { item in
|
||||
HStack {
|
||||
item.mood.icon
|
||||
.resizable()
|
||||
.aspectRatio(contentMode: .fit)
|
||||
.frame(width: 40, height: 40, alignment: .center)
|
||||
.foregroundColor(item.mood.color)
|
||||
VStack {
|
||||
HStack {
|
||||
Text(weekdayName(fromDate:item.forDate!))
|
||||
.font(.title3)
|
||||
.foregroundColor(Color(UIColor.label))
|
||||
.frame(maxWidth: 40, alignment: .leading)
|
||||
Text(" - ")
|
||||
.padding([.leading, .trailing], -10)
|
||||
Text(item.forDate ?? Date(), style: .date)
|
||||
.font(.title3)
|
||||
.foregroundColor(Color(UIColor.label))
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
}
|
||||
Text("\(item.moodString)")
|
||||
.font(.body)
|
||||
.foregroundColor(Color(UIColor.systemGray))
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
}
|
||||
VStack {
|
||||
List {
|
||||
// for reach year
|
||||
ForEach(viewModel.grouped.sorted(by: {
|
||||
$0.key > $1.key
|
||||
}), id: \.key) { year, months in
|
||||
|
||||
// for reach month
|
||||
ForEach(months.sorted(by: {
|
||||
$0.key > $1.key
|
||||
}), id: \.key) { month, entries in
|
||||
Section(header:
|
||||
HStack{
|
||||
Text(monthName(fromMonthInt: month))
|
||||
.font(.title2)
|
||||
.foregroundColor(Color(UIColor.label))
|
||||
Text(String(year))
|
||||
.font(.title2)
|
||||
.foregroundColor(Color(UIColor.label))
|
||||
}) {
|
||||
// for reach all entries
|
||||
ForEach(entries.sorted(by: {
|
||||
$0.forDate! > $1.forDate!
|
||||
}), id: \.self) { entry in
|
||||
entryListView(entry: entry)
|
||||
}.onDelete(perform: { offsets in
|
||||
withAnimation {
|
||||
viewModel.delete(offsets: offsets, inMonth: month, inYear: year)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.onDelete(perform: deleteItems)
|
||||
.background(Color.clear.ignoresSafeArea())
|
||||
.onAppear {
|
||||
// Set the default to clear
|
||||
UITableView.appearance().backgroundColor = .clear
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func entryListView(entry: MoodEntry) -> some View {
|
||||
HStack {
|
||||
entry.mood.icon
|
||||
.resizable()
|
||||
.aspectRatio(contentMode: .fit)
|
||||
.frame(width: 40, height: 40, alignment: .center)
|
||||
.foregroundColor(entry.mood.color)
|
||||
|
||||
VStack {
|
||||
HStack {
|
||||
Text(weekdayName(fromDate:entry.forDate!))
|
||||
.font(.title3)
|
||||
.foregroundColor(Color(UIColor.label))
|
||||
Text(" - ")
|
||||
.padding([.leading, .trailing], -10)
|
||||
Text(dayFormat(fromDate:entry.forDate!))
|
||||
.font(.title3)
|
||||
.foregroundColor(Color(UIColor.label))
|
||||
|
||||
Spacer()
|
||||
}
|
||||
.multilineTextAlignment(.leading)
|
||||
|
||||
Text("\(entry.moodString)")
|
||||
.font(.body)
|
||||
.foregroundColor(Color(UIColor.systemGray))
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var mainView: some View {
|
||||
VStack{
|
||||
settingsButtonView
|
||||
if shouldShowTodayInput() {
|
||||
AddMoodHeaderView()
|
||||
.frame(minHeight: 85, maxHeight: 180)
|
||||
.frame(minWidth: 0, maxWidth: .infinity)
|
||||
} else {
|
||||
HeaderStatsView(fakeData: false, backDays: 30)
|
||||
.frame(minHeight: 85, maxHeight: 180)
|
||||
// should match backDays above
|
||||
Text("Past \(30) days")
|
||||
.font(.body)
|
||||
.foregroundColor(Color(UIColor.systemGray))
|
||||
.frame(maxWidth: .infinity, alignment: .center)
|
||||
ZStack {
|
||||
BGView()
|
||||
VStack{
|
||||
settingsButtonView
|
||||
if viewModel.shouldShowTodayInput() {
|
||||
AddMoodHeaderView(addItemClosure: { (mood, date) in
|
||||
withAnimation {
|
||||
viewModel.add(mood: mood, forDate: date)
|
||||
}
|
||||
})
|
||||
.frame(minHeight: 85, maxHeight: 180)
|
||||
.frame(minWidth: 0, maxWidth: .infinity)
|
||||
} else {
|
||||
HeaderStatsView(fakeData: false, backDays: 30)
|
||||
.frame(minHeight: 85, maxHeight: 180)
|
||||
// should match backDays above
|
||||
Text("Past \(30) days")
|
||||
.font(.body)
|
||||
.foregroundColor(Color(UIColor.systemGray))
|
||||
.frame(maxWidth: .infinity, alignment: .center)
|
||||
}
|
||||
listView
|
||||
.padding(.bottom)
|
||||
}
|
||||
listView
|
||||
.padding(.top, 50)
|
||||
}
|
||||
}
|
||||
|
||||
private func deleteItems(offsets: IndexSet) {
|
||||
withAnimation {
|
||||
offsets.map { items[$0] }.forEach(viewContext.delete)
|
||||
|
||||
do {
|
||||
try viewContext.save()
|
||||
} catch {
|
||||
// Replace this implementation with code to handle the error appropriately.
|
||||
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
|
||||
let nsError = error as NSError
|
||||
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func shouldShowTodayInput() -> Bool {
|
||||
let fetchRequest = NSFetchRequest<MoodEntry>(entityName: "MoodEntry")
|
||||
|
||||
var calendar = Calendar.current
|
||||
calendar.timeZone = NSTimeZone.local
|
||||
|
||||
// Get today's beginning & end
|
||||
let dateFrom = calendar.startOfDay(for: Date()) // eg. 2016-10-10 00:00:00
|
||||
let dateTo = calendar.date(byAdding: .day, value: 1, to: dateFrom)!
|
||||
// Note: Times are printed in UTC. Depending on where you live it won't print 00:00:00 but it will work with UTC times which can be converted to local time
|
||||
|
||||
// Set predicate as date being today's date
|
||||
let fromPredicate = NSPredicate(format: "%@ <= %K", dateFrom as NSDate, #keyPath(MoodEntry.forDate))
|
||||
let toPredicate = NSPredicate(format: "%K < %@", #keyPath(MoodEntry.forDate), dateTo as NSDate)
|
||||
let datePredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [fromPredicate, toPredicate])
|
||||
fetchRequest.predicate = datePredicate
|
||||
let entries = try! self.viewContext.count(for: fetchRequest)
|
||||
|
||||
return entries == 0
|
||||
}
|
||||
}
|
||||
|
||||
private let itemFormatter: DateFormatter = {
|
||||
|
||||
Reference in New Issue
Block a user