wonders/ios/WonderousWidget/WonderWidgetViewComponents.swift

50 lines
2.0 KiB
Swift
Raw Normal View History

import Foundation
import SwiftUI
// Loads a default image from the flutter assets bundle,
2023-10-23 18:06:39 -06:00
// or displays a base64 encoded image that has been saved from the flutter application
struct BgImage : View {
2023-11-02 09:58:07 -06:00
var entry: WonderousTimelineEntry
var body: some View {
var uiImage:UIImage?;
2023-11-02 09:58:07 -06:00
// If there is no saved imageData, use the default bg image
if(entry.imageData.isEmpty){
2023-10-23 18:06:39 -06:00
let defaultImage = flutterAssetBundle.appending(path: "/assets/images/widget/background-empty.jpg").path();
uiImage = UIImage(contentsOfFile: defaultImage);
2023-11-02 09:58:07 -06:00
}
// Load a base64 encoded image that has been written by the flutter app
else {
uiImage = UIImage(data: Data(base64Encoded: entry.imageData)!)
}
if(uiImage != nil){
2023-10-23 18:06:39 -06:00
// Use geometry reader to prevent the image from pushing the other content out of the widgets bounds (https://stackoverflow.com/questions/57593552/swiftui-prevent-image-from-expanding-view-rect-outside-of-screen-bounds)
let image = GeometryReader { geometry in
Image(uiImage: uiImage!)
.resizable()
.aspectRatio(contentMode: .fill)
.edgesIgnoringSafeArea(.all) // Ignore the safe area
.frame(maxWidth: geometry.size.width, maxHeight: geometry.size.height)
}
return AnyView(image)
}
2023-10-23 18:06:39 -06:00
debugPrint("The image file could not be loaded")
return AnyView(EmptyView())
}
}
struct GaugeProgressStyle: ProgressViewStyle {
func makeBody(configuration: Configuration) -> some View {
let fractionCompleted = configuration.fractionCompleted ?? 0
return ZStack {
Circle()
2023-10-23 21:40:55 -06:00
.stroke(Color.body, style: StrokeStyle(lineWidth: 2))
Circle()
.trim(from: 0, to: fractionCompleted)
2023-10-23 21:40:55 -06:00
.stroke(Color.accent, style: StrokeStyle(lineWidth: 4, lineCap: .round))
.rotationEffect(.degrees(90))
}
}
}