WIP - Sharing
This commit is contained in:
@@ -9,36 +9,192 @@ import SwiftUI
|
||||
|
||||
struct AllMoodsTotalTemplate: View, SharingTemplate {
|
||||
static var description: String {
|
||||
"AllMoodsSharingTemplate"
|
||||
"All Time Count"
|
||||
}
|
||||
|
||||
var isPreview: Bool
|
||||
|
||||
var startDate: Date
|
||||
var endDate: Date
|
||||
var totalEntryCount: Int = 0
|
||||
|
||||
@State var showSharingTemplate = false
|
||||
@Environment(\.presentationMode) var presentationMode
|
||||
|
||||
private var entries = [MoodMetrics]()
|
||||
|
||||
init(isPreview: Bool, startDate: Date, endDate: Date, fakeData: Bool) {
|
||||
self.isPreview = isPreview
|
||||
self.startDate = startDate
|
||||
self.endDate = endDate
|
||||
|
||||
var moodEntries: [MoodEntry]?
|
||||
|
||||
if fakeData {
|
||||
moodEntries = PersistenceController.shared.randomEntries(count: 10)
|
||||
} else {
|
||||
|
||||
moodEntries = PersistenceController.shared.getData(startDate:startDate,
|
||||
endDate: endDate,
|
||||
includedDays: [1,2,3,4,5,6,7])
|
||||
}
|
||||
|
||||
totalEntryCount = moodEntries?.count ?? 0
|
||||
|
||||
if let moodEntries = moodEntries {
|
||||
for (_, mood) in Mood.allValues.enumerated() {
|
||||
|
||||
let moodEntries = moodEntries.filter({
|
||||
Int($0.moodValue) == mood.rawValue
|
||||
})
|
||||
let total = moodEntries.count
|
||||
let perc = (Float(total) / Float(totalEntryCount)) * 100
|
||||
entries.append(MoodMetrics(mood: mood, total: total, percent: perc))
|
||||
}
|
||||
|
||||
entries = entries.sorted(by: {
|
||||
$0.mood.rawValue > $1.mood.rawValue
|
||||
})
|
||||
} else {
|
||||
fatalError("no data")
|
||||
}
|
||||
}
|
||||
|
||||
var image: UIImage {
|
||||
let image = shareView.snapshot()
|
||||
return image
|
||||
}
|
||||
|
||||
var preview: some View {
|
||||
Rectangle()
|
||||
.frame(width: 150, height: 50, alignment: .leading)
|
||||
circularViews
|
||||
.frame(maxWidth: .infinity, alignment: .center)
|
||||
.frame(height: 100)
|
||||
}
|
||||
|
||||
private var circularViews: some View {
|
||||
VStack {
|
||||
|
||||
HStack {
|
||||
ForEach(Mood.allValues, id: \.self) { mood in
|
||||
mood.icon
|
||||
.resizable()
|
||||
.aspectRatio(contentMode: .fit)
|
||||
.foregroundColor(mood.color)
|
||||
}
|
||||
}
|
||||
.frame(maxWidth: .infinity, alignment: .center)
|
||||
.frame(height: 100)
|
||||
|
||||
HStack {
|
||||
ForEach(entries, id: \.mood) { model in
|
||||
ZStack {
|
||||
Circle().fill(model.mood.color)
|
||||
|
||||
Text("\(model.percent, specifier: "%.0f")%")
|
||||
.font(.title)
|
||||
.fontWeight(.bold)
|
||||
.scaledToFill()
|
||||
.minimumScaleFactor(0.5)
|
||||
.lineLimit(1)
|
||||
.foregroundColor(Color(UIColor.white))
|
||||
.padding(2)
|
||||
}
|
||||
.frame(maxWidth: .infinity, alignment: .center)
|
||||
.frame(height: 100)
|
||||
}
|
||||
}
|
||||
|
||||
HStack {
|
||||
ForEach(entries, id: \.mood) { model in
|
||||
ZStack {
|
||||
Circle().fill(model.mood.color)
|
||||
|
||||
Text("\(model.total)")
|
||||
.font(.title)
|
||||
.fontWeight(.bold)
|
||||
.scaledToFill()
|
||||
.minimumScaleFactor(0.5)
|
||||
.lineLimit(1)
|
||||
.foregroundColor(Color(UIColor.white))
|
||||
.padding(2)
|
||||
}
|
||||
.frame(maxWidth: .infinity, alignment: .center)
|
||||
.frame(height: 100)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var shareView: some View {
|
||||
VStack {
|
||||
Text(String(format: String(localized: "share_view_all_moods_total_template_title"), totalEntryCount))
|
||||
.font(.title)
|
||||
.foregroundColor(Color(UIColor.label))
|
||||
.frame(maxWidth: .infinity, alignment: .center)
|
||||
.padding(.top, 1)
|
||||
|
||||
circularViews
|
||||
.padding([.trailing, .leading, .top])
|
||||
|
||||
Spacer()
|
||||
}
|
||||
}
|
||||
|
||||
var mainView: some View {
|
||||
Text("AllMoodsSharingTemplate body")
|
||||
VStack {
|
||||
shareView
|
||||
Spacer()
|
||||
HStack(alignment: .center) {
|
||||
Button(action: {
|
||||
showSharingTemplate = true
|
||||
}, label: {
|
||||
Text("Share")
|
||||
.font(.title)
|
||||
.fontWeight(.bold)
|
||||
.foregroundColor(Color.white)
|
||||
.padding(.top, 20)
|
||||
})
|
||||
.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)
|
||||
}.sheet(isPresented: $showSharingTemplate) {
|
||||
ActivityViewController(activityItems: [self.image])
|
||||
}
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
if isPreview {
|
||||
preview
|
||||
.padding([.leading, .trailing], -20)
|
||||
} else {
|
||||
mainView
|
||||
.padding([.leading, .trailing])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct AllMoodsSharingTemplate_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
AllMoodsTotalTemplate(isPreview: true, startDate: Date(), endDate: Date())
|
||||
AllMoodsTotalTemplate(isPreview: true, startDate: Date(), endDate: Date(), fakeData: true)
|
||||
|
||||
AllMoodsTotalTemplate(isPreview: false, startDate: Date(), endDate: Date())
|
||||
AllMoodsTotalTemplate(isPreview: false, startDate: Date(), endDate: Date(), fakeData: true)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user