Widget Extension Fixes: - Create standalone WidgetDataProvider for widget data isolation - Add WIDGET_EXTENSION compiler flag for conditional compilation - Fix DataController references in widget-shared files - Sync widget version numbers with main app (23, 1.0.2) - Add WidgetBackground color to asset catalog Warning Resolutions: - Fix UIScreen.main deprecation in BGView and SharingListView - Fix Text '+' concatenation deprecation in PurchaseButtonView and SettingsTabView - Fix exhaustive switch in BiometricAuthManager (add .none case) - Fix var to let in ExportService (3 instances) - Fix unused result warning in NoteEditorView - Fix ForEach duplicate ID warnings in MonthView and YearView Code Quality Improvements: - Wrap bypassSubscription in #if DEBUG for security - Rename StupidAssCustomWidgetObservableObject to CustomWidgetStateViewModel - Add @MainActor to IconViewModel - Replace fatalError with graceful fallback in SharedModelContainer - Add [weak self] to closures in DayViewViewModel - Add OSLog-based AppLogger for production logging - Add ImageCache with NSCache for memory efficiency - Add AccessibilityHelpers with Reduce Motion support - Create DataControllerProtocol for dependency injection - Update .gitignore with secrets exclusions 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
210 lines
7.5 KiB
Swift
210 lines
7.5 KiB
Swift
//
|
|
// MonthTotalTemplate.swift
|
|
// Feels (iOS)
|
|
//
|
|
// Created by Trey Tartt on 2/9/22.
|
|
//
|
|
|
|
import SwiftUI
|
|
|
|
struct MonthTotalTemplate: View, SharingTemplate {
|
|
static var description: String {
|
|
"This Month"
|
|
}
|
|
|
|
var isPreview: Bool
|
|
var startDate: Date
|
|
var endDate: Date
|
|
var totalEntryCount: Int = 0
|
|
|
|
private var month = Calendar.current.dateComponents([.month], from: Date()).month!
|
|
|
|
@State var showSharingTemplate = false
|
|
@StateObject private var shareImage = ShareImageStateViewModel()
|
|
|
|
@Environment(\.presentationMode) var presentationMode
|
|
@AppStorage(UserDefaultsStore.Keys.moodTint.rawValue, store: GroupUserDefaults.groupDefaults) private var moodTint: MoodTints = .Default
|
|
@AppStorage(UserDefaultsStore.Keys.theme.rawValue, store: GroupUserDefaults.groupDefaults) private var theme: Theme = .system
|
|
@AppStorage(UserDefaultsStore.Keys.textColor.rawValue, store: GroupUserDefaults.groupDefaults) private var textColor: Color = .white
|
|
|
|
private var moodMetrics = [MoodMetrics]()
|
|
private var moodEntries = [MoodEntryModel]()
|
|
|
|
let columns = [
|
|
GridItem(.flexible(minimum: 5, maximum: .infinity), alignment: .center),
|
|
GridItem(.flexible(minimum: 5, maximum: .infinity), alignment: .center),
|
|
GridItem(.flexible(minimum: 5, maximum: .infinity), alignment: .center),
|
|
GridItem(.flexible(minimum: 5, maximum: .infinity), alignment: .center),
|
|
GridItem(.flexible(minimum: 5, maximum: .infinity), alignment: .center),
|
|
GridItem(.flexible(minimum: 5, maximum: .infinity), alignment: .center),
|
|
GridItem(.flexible(minimum: 5, maximum: .infinity), alignment: .center)
|
|
]
|
|
|
|
@MainActor
|
|
init(isPreview: Bool, startDate: Date, endDate: Date, fakeData: Bool) {
|
|
self.isPreview = isPreview
|
|
self.startDate = startDate
|
|
self.endDate = endDate
|
|
|
|
var _moodEntries: [MoodEntryModel]?
|
|
|
|
if fakeData {
|
|
_moodEntries = DataController.shared.randomEntries(count: 10)
|
|
} else {
|
|
_moodEntries = DataController.shared.getData(startDate: startDate,
|
|
endDate: endDate,
|
|
includedDays: [1,2,3,4,5,6,7])
|
|
}
|
|
|
|
moodEntries = _moodEntries ?? [MoodEntryModel]()
|
|
|
|
totalEntryCount = moodEntries.count
|
|
moodMetrics = Random.createTotalPerc(fromEntries: moodEntries)
|
|
moodMetrics = moodMetrics.sorted(by: {
|
|
$0.mood.rawValue > $1.mood.rawValue
|
|
})
|
|
}
|
|
|
|
var image: UIImage {
|
|
let image = shareView.asImage(size: CGSize(width: 666, height: 1190))
|
|
return image
|
|
}
|
|
|
|
var preview: some View {
|
|
circularViews
|
|
.frame(maxWidth: .infinity, alignment: .center)
|
|
.frame(height: 100)
|
|
}
|
|
|
|
private var circularViews: some View {
|
|
HStack {
|
|
ForEach(moodMetrics, id: \.mood) { model in
|
|
ZStack {
|
|
Circle().fill(moodTint.color(forMood: model.mood))
|
|
|
|
Text("\(model.percent, specifier: "%.0f")%")
|
|
.font(.title)
|
|
.fontWeight(.bold)
|
|
.minimumScaleFactor(0.5)
|
|
.lineLimit(1)
|
|
.foregroundColor(textColor)
|
|
.padding(5)
|
|
}
|
|
.frame(maxWidth: .infinity, alignment: .center)
|
|
.frame(height: 100)
|
|
}
|
|
}
|
|
}
|
|
|
|
var shareView: some View {
|
|
VStack {
|
|
Text(String(format: String(localized: "share_view_month_moods_total_template_title"), Random.monthName(fromMonthInt: month), moodEntries.count))
|
|
.font(.title)
|
|
.foregroundColor(textColor)
|
|
.frame(maxWidth: .infinity, alignment: .center)
|
|
.padding()
|
|
|
|
HStack {
|
|
ForEach(Mood.allValues, id: \.self) { mood in
|
|
Circle()
|
|
.frame(minWidth: 10, idealWidth: 70, maxWidth: 100, minHeight: 10, idealHeight: 70, maxHeight: 100)
|
|
.aspectRatio(contentMode: .fit)
|
|
.foregroundColor(moodTint.color(forMood: mood))
|
|
.overlay(
|
|
mood.icon
|
|
.resizable()
|
|
.aspectRatio(contentMode: .fit)
|
|
.foregroundColor(textColor)
|
|
.padding(9)
|
|
)
|
|
}
|
|
}
|
|
.padding()
|
|
|
|
VStack {
|
|
LazyVGrid(columns: columns, spacing: 10) {
|
|
ForEach(moodEntries) { entry in
|
|
entry.mood.icon
|
|
.resizable()
|
|
.aspectRatio(contentMode: .fit)
|
|
.foregroundColor(moodTint.color(forMood: entry.mood))
|
|
}
|
|
}
|
|
}
|
|
.padding()
|
|
|
|
circularViews
|
|
.padding()
|
|
}
|
|
.background(Color(UIColor.secondarySystemBackground))
|
|
.cornerRadius(10)
|
|
.padding()
|
|
}
|
|
|
|
var mainView: some View {
|
|
VStack {
|
|
shareView
|
|
|
|
Spacer()
|
|
HStack(alignment: .center) {
|
|
Button(action: {
|
|
let _image = self.image
|
|
self.shareImage.showSheet = true
|
|
self.shareImage.selectedShareImage = _image
|
|
}, label: {
|
|
Text("Share")
|
|
.font(.title)
|
|
.fontWeight(.bold)
|
|
.foregroundColor(Color.white)
|
|
.padding(.top, 20)
|
|
})
|
|
.sheet(isPresented: self.$shareImage.showSheet) {
|
|
if let uiImage = self.shareImage.selectedShareImage {
|
|
ShareSheet(photo: uiImage)
|
|
}
|
|
}
|
|
.frame(maxWidth: .infinity, alignment: .center)
|
|
.background(
|
|
Color.green
|
|
)
|
|
.padding(.trailing, -5)
|
|
|
|
Button(action: {
|
|
presentationMode.wrappedValue.dismiss()
|
|
}, label: {
|
|
Text("Exit")
|
|
.font(.title)
|
|
.fontWeight(.bold)
|
|
.foregroundColor(Color.white)
|
|
.padding(.top, 20)
|
|
})
|
|
.frame(maxWidth: .infinity, alignment: .center)
|
|
.background(
|
|
Color.red
|
|
)
|
|
.padding(.leading, -5)
|
|
}
|
|
.padding([.leading, .trailing], -20)
|
|
}
|
|
}
|
|
|
|
var body: some View {
|
|
if isPreview {
|
|
shareView
|
|
.scaleEffect(2)
|
|
} else {
|
|
mainView
|
|
}
|
|
}
|
|
}
|
|
|
|
struct MonthTotalTemplate_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
MonthTotalTemplate(isPreview: true, startDate: Date().startOfMonth, endDate: Date().endOfMonth, fakeData: true)
|
|
|
|
MonthTotalTemplate(isPreview: false, startDate: Date().startOfMonth, endDate: Date().endOfMonth, fakeData: true)
|
|
|
|
MonthTotalTemplate(isPreview: false, startDate: Date().startOfMonth, endDate: Date().endOfMonth, fakeData: true).shareView
|
|
}
|
|
}
|