Files
SideStore/SideStoreApp/Sources/SideWidgetKit/ComplicationView.swift

127 lines
5.1 KiB
Swift
Raw Normal View History

2022-08-17 15:33:13 -05:00
//
// ComplicationView.swift
// AltStore
//
// Created by Riley Testut on 7/7/22.
// Copyright © 2022 Riley Testut. All rights reserved.
//
import SwiftUI
import WidgetKit
@available(iOS 16, *)
2023-03-01 00:48:36 -05:00
extension ComplicationView {
enum Style {
case text
case icon
}
}
2023-03-10 20:43:59 -05:00
@available(iOS 16, iOSApplicationExtension 16, *)
2023-03-01 00:48:36 -05:00
struct ComplicationView: View {
2022-08-17 15:33:13 -05:00
let entry: AppEntry
let style: Style
2023-03-01 00:48:36 -05:00
2022-08-17 15:33:13 -05:00
var body: some View {
let refreshedDate = self.entry.app?.refreshedDate ?? .now
let expirationDate = self.entry.app?.expirationDate ?? .now
2023-03-01 00:48:36 -05:00
2022-08-17 15:33:13 -05:00
let totalDays = expirationDate.numberOfCalendarDays(since: refreshedDate)
let daysRemaining = expirationDate.numberOfCalendarDays(since: self.entry.date)
2023-03-01 00:48:36 -05:00
2022-08-17 15:33:13 -05:00
let progress = Double(daysRemaining) / Double(totalDays)
2023-03-01 00:48:36 -05:00
Gauge(value: progress) {
2023-03-01 00:48:36 -05:00
if daysRemaining < 0 {
Text("Expired")
.font(.system(size: 10, weight: .bold))
2023-03-01 00:48:36 -05:00
} else {
switch self.style {
case .text:
VStack(spacing: -1) {
let fontSize = daysRemaining > 99 ? 18.0 : 20.0
Text("\(daysRemaining)")
.font(.system(size: fontSize, weight: .bold, design: .rounded))
2023-03-01 00:48:36 -05:00
Text(daysRemaining == 1 ? "DAY" : "DAYS")
.font(.caption)
}
.fixedSize()
.offset(y: -1)
2023-03-01 00:48:36 -05:00
case .icon:
ZStack {
// Destination
Image("SmallIcon")
.resizable()
.aspectRatio(1.0, contentMode: .fill)
.scaleEffect(x: 0.8, y: 0.8)
2023-03-01 00:48:36 -05:00
// Source
(
daysRemaining > 7 ?
2023-03-01 00:48:36 -05:00
Text("7+")
.font(.system(size: 18, weight: .bold, design: .rounded))
.kerning(-2) :
2023-03-01 00:48:36 -05:00
Text("\(daysRemaining)")
.font(.system(size: 20, weight: .bold, design: .rounded))
2023-03-01 00:48:36 -05:00
)
.foregroundColor(Color.black)
.blendMode(.destinationOut) // Clip text out of image.
}
2022-08-17 15:33:13 -05:00
}
}
}
.gaugeStyle(.accessoryCircularCapacity)
2022-08-17 15:33:13 -05:00
.unredacted()
}
}
@available(iOS 16, *)
struct ComplicationView_Previews: PreviewProvider {
static var previews: some View {
let shortRefreshedDate = Calendar.current.date(byAdding: .day, value: -2, to: Date()) ?? Date()
let shortExpirationDate = Calendar.current.date(byAdding: .day, value: 7, to: shortRefreshedDate) ?? Date()
2023-03-01 00:48:36 -05:00
2022-08-17 15:33:13 -05:00
let longRefreshedDate = Calendar.current.date(byAdding: .day, value: -100, to: Date()) ?? Date()
let longExpirationDate = Calendar.current.date(byAdding: .day, value: 365, to: longRefreshedDate) ?? Date()
2023-03-01 00:48:36 -05:00
2022-08-17 15:33:13 -05:00
let expiredDate = shortExpirationDate.addingTimeInterval(1 * 60 * 60 * 24)
2023-03-01 00:48:36 -05:00
2022-08-17 15:33:13 -05:00
let weekAltstore = AppSnapshot(name: "AltStore",
2023-03-01 00:48:36 -05:00
bundleIdentifier: Bundle.Info.appbundleIdentifier,
expirationDate: shortExpirationDate,
refreshedDate: shortRefreshedDate,
tintColor: .altPrimary,
icon: UIImage(named: "AltStore"))
2022-08-17 15:33:13 -05:00
let yearAltstore = AppSnapshot(name: "AltStore",
2023-03-01 00:48:36 -05:00
bundleIdentifier: Bundle.Info.appbundleIdentifier,
expirationDate: longExpirationDate,
refreshedDate: longRefreshedDate,
tintColor: .altPrimary,
icon: UIImage(named: "AltStore"))
2022-08-17 15:33:13 -05:00
return Group {
ComplicationView(entry: AppEntry(date: Date(), app: weekAltstore), style: .icon)
2022-08-17 15:33:13 -05:00
.previewContext(WidgetPreviewContext(family: .accessoryCircular))
2023-03-01 00:48:36 -05:00
ComplicationView(entry: AppEntry(date: expiredDate, app: weekAltstore), style: .icon)
2022-08-17 15:33:13 -05:00
.previewContext(WidgetPreviewContext(family: .accessoryCircular))
2023-03-01 00:48:36 -05:00
ComplicationView(entry: AppEntry(date: longRefreshedDate, app: yearAltstore), style: .icon)
2022-08-17 15:33:13 -05:00
.previewContext(WidgetPreviewContext(family: .accessoryCircular))
2023-03-01 00:48:36 -05:00
ComplicationView(entry: AppEntry(date: Date(), app: weekAltstore), style: .text)
.previewContext(WidgetPreviewContext(family: .accessoryCircular))
2023-03-01 00:48:36 -05:00
ComplicationView(entry: AppEntry(date: expiredDate, app: weekAltstore), style: .text)
.previewContext(WidgetPreviewContext(family: .accessoryCircular))
2023-03-01 00:48:36 -05:00
ComplicationView(entry: AppEntry(date: longRefreshedDate, app: yearAltstore), style: .text)
2023-03-01 00:48:36 -05:00
.previewContext(WidgetPreviewContext(family: .accessoryCircular))
2022-08-17 15:33:13 -05:00
}
}
}