[AltWidget] Initial version

This commit is contained in:
Riley Testut
2020-09-15 13:51:29 -07:00
parent 669c6f5bf4
commit 5abf7a5a11
21 changed files with 1042 additions and 0 deletions

173
AltWidget/AltWidget.swift Normal file
View File

@@ -0,0 +1,173 @@
//
// AltWidget.swift
// AltWidget
//
// Created by Riley Testut on 6/26/20.
// Copyright © 2020 Riley Testut. All rights reserved.
//
import SwiftUI
import WidgetKit
import UIKit
import CoreData
import AltStoreCore
import AltSign
struct AppEntry: TimelineEntry
{
var date: Date
var relevance: TimelineEntryRelevance?
var app: AppSnapshot?
var isPlaceholder: Bool = false
}
struct AppSnapshot
{
var name: String
var bundleIdentifier: String
var expirationDate: Date
var refreshedDate: Date
var tintColor: UIColor?
var icon: UIImage?
}
extension AppSnapshot
{
// Declared in extension so we retain synthesized initializer.
init(installedApp: InstalledApp)
{
self.name = installedApp.name
self.bundleIdentifier = installedApp.bundleIdentifier
self.expirationDate = installedApp.expirationDate
self.refreshedDate = installedApp.refreshedDate
self.tintColor = installedApp.storeApp?.tintColor
let application = ALTApplication(fileURL: installedApp.fileURL)
self.icon = application?.icon?.resizing(toFill: CGSize(width: 180, height: 180))
}
}
struct Provider: IntentTimelineProvider
{
typealias Intent = ViewAppIntent
typealias Entry = AppEntry
func placeholder(in context: Context) -> AppEntry
{
return AppEntry(date: Date(), app: nil, isPlaceholder: true)
}
func getSnapshot(for configuration: ViewAppIntent, in context: Context, completion: @escaping (AppEntry) -> Void)
{
self.prepare { (result) in
do
{
let context = try result.get()
let snapshot = InstalledApp.fetchAltStore(in: context).map(AppSnapshot.init)
let entry = AppEntry(date: Date(), app: snapshot)
completion(entry)
}
catch
{
print("Error preparing widget snapshot:", error)
let entry = AppEntry(date: Date(), app: nil)
completion(entry)
}
}
}
func getTimeline(for configuration: ViewAppIntent, in context: Context, completion: @escaping (Timeline<AppEntry>) -> Void) {
self.prepare { (result) in
autoreleasepool {
do
{
let context = try result.get()
let installedApp: InstalledApp?
if let identifier = configuration.app?.identifier
{
let app = InstalledApp.first(satisfying: NSPredicate(format: "%K == %@", #keyPath(InstalledApp.bundleIdentifier), identifier),
in: context)
installedApp = app
}
else
{
installedApp = InstalledApp.fetchAltStore(in: context)
}
let snapshot = installedApp.map(AppSnapshot.init)
var entries: [AppEntry] = []
// Generate a timeline consisting of one entry per day.
if let snapshot = snapshot
{
let currentDate = Calendar.current.startOfDay(for: Date())
let numberOfDays = snapshot.expirationDate.numberOfCalendarDays(since: currentDate)
for dayOffset in 0 ..< min(numberOfDays, 7)
{
guard let entryDate = Calendar.current.date(byAdding: .day, value: dayOffset, to: currentDate) else { continue }
let score = Float(dayOffset + 1) / Float(numberOfDays)
let entry = AppEntry(date: entryDate, relevance: TimelineEntryRelevance(score: score), app: snapshot)
entries.append(entry)
}
}
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
}
catch
{
print("Error preparing widget timeline:", error)
let entry = AppEntry(date: Date(), app: nil)
let timeline = Timeline(entries: [entry], policy: .atEnd)
completion(timeline)
}
}
}
}
private func prepare(completion: @escaping (Result<NSManagedObjectContext, Error>) -> Void)
{
DatabaseManager.shared.start { (error) in
if let error = error
{
completion(.failure(error))
}
else
{
DatabaseManager.shared.viewContext.perform {
completion(.success(DatabaseManager.shared.viewContext))
}
}
}
}
}
@main
struct AltWidget: Widget
{
private let kind: String = "AppDetail"
public var body: some WidgetConfiguration {
return IntentConfiguration(kind: kind,
intent: ViewAppIntent.self,
provider: Provider()) { (entry) in
WidgetView(entry: entry)
}
.supportedFamilies([.systemSmall])
.configurationDisplayName("AltWidget")
.description("View remaining days until your sideloaded apps expire.")
}
}

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.application-groups</key>
<array>
<string>group.com.rileytestut.AltStore</string>
</array>
</dict>
</plist>

View File

@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@@ -0,0 +1,22 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "Group 23_120.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "Group 23_180.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

View File

@@ -0,0 +1,98 @@
{
"images" : [
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "20x20"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "20x20"
},
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "29x29"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "29x29"
},
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "40x40"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "40x40"
},
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "60x60"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "60x60"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "20x20"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "20x20"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "29x29"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "29x29"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "40x40"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "40x40"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "76x76"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "76x76"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "83.5x83.5"
},
{
"idiom" : "ios-marketing",
"scale" : "1x",
"size" : "1024x1024"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@@ -0,0 +1,12 @@
{
"images" : [
{
"filename" : "group16Copy2.pdf",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

Binary file not shown.

View File

@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@@ -0,0 +1,22 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "icon-120.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "icon-180.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.0 KiB

View File

@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

79
AltWidget/Countdown.swift Normal file
View File

@@ -0,0 +1,79 @@
//
// Countdown.swift
// AltWidgetExtension
//
// Created by Riley Testut on 7/6/20.
// Copyright © 2020 Riley Testut. All rights reserved.
//
import SwiftUI
import WidgetKit
struct Countdown: View
{
let startDate: Date?
let endDate: Date?
@Environment(\.font) private var font
private var numberOfDays: Int {
guard let date = self.endDate else { return 0 }
let numberOfDays = date.numberOfCalendarDays(since: Date())
return numberOfDays
}
private var fractionComplete: CGFloat {
guard let startDate = self.startDate, let endDate = self.endDate else { return 1.0 }
let totalNumberOfDays = endDate.numberOfCalendarDays(since: startDate)
let fractionComplete = CGFloat(self.numberOfDays) / CGFloat(totalNumberOfDays)
return fractionComplete
}
@ViewBuilder
private func overlay(progress: CGFloat) -> some View
{
let strokeStyle = StrokeStyle(lineWidth: 4.0, lineCap: .round, lineJoin: .round)
if self.numberOfDays > 9 || self.numberOfDays < 0 {
Capsule(style: .continuous)
.trim(from: 0.0, to: progress)
.stroke(style: strokeStyle)
}
else {
Circle()
.trim(from: 0.0, to: progress)
.rotation(Angle(degrees: -90), anchor: .center)
.stroke(style: strokeStyle)
}
}
var body: some View {
Text("\(numberOfDays)")
.font((font ?? .title).monospacedDigit())
.bold()
.opacity(endDate != nil ? 1 : 0)
.padding(.horizontal, 12)
.padding(.vertical, 6)
.overlay(
ZStack {
overlay(progress: 1.0)
.opacity(0.3)
overlay(progress: fractionComplete)
}
)
}
}
struct Countdown_Previews: PreviewProvider {
static var previews: some View {
let startDate = Calendar.current.date(byAdding: .day, value: -2, to: Date()) ?? Date()
Group {
Countdown(startDate: startDate, endDate: Calendar.current.date(byAdding: .day, value: 7, to: startDate))
Countdown(startDate: startDate, endDate: Calendar.current.date(byAdding: .day, value: 365, to: startDate))
}
.previewContext(WidgetPreviewContext(family: .systemSmall))
}
}

33
AltWidget/Info.plist Normal file
View File

@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>ALTAppGroups</key>
<array>
<string>group.com.rileytestut.AltStore</string>
</array>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key>
<string>AltWidget</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.widgetkit-extension</string>
</dict>
</dict>
</plist>

170
AltWidget/WidgetView.swift Normal file
View File

@@ -0,0 +1,170 @@
//
// WidgetView.swift
// AltStore
//
// Created by Riley Testut on 9/14/20.
// Copyright © 2020 Riley Testut. All rights reserved.
//
import WidgetKit
import SwiftUI
import AltStoreCore
import AltSign
import CoreData
struct WidgetView : View
{
var entry: AppEntry
var body: some View {
Group {
if let app = self.entry.app
{
let daysRemaining = app.expirationDate.numberOfCalendarDays(since: Date())
GeometryReader { (geometry) in
Group {
VStack(alignment: .leading) {
VStack(alignment: .leading, spacing: 5) {
let imageHeight = geometry.size.height * 0.45
app.icon.map {
Image(uiImage: $0)
.resizable()
.aspectRatio(CGSize(width: 1, height: 1), contentMode: .fit)
.frame(height: imageHeight)
.mask(RoundedRectangle(cornerRadius: imageHeight / 5.0, style: .continuous))
}
Text(app.name.uppercased())
.font(.system(size: 12, weight: .semibold, design: .rounded))
.foregroundColor(.white)
.lineLimit(1)
.minimumScaleFactor(0.5)
}
HStack(alignment: .bottom) {
(
Text("Expires in\n")
.font(.system(size: 13, weight: .semibold, design: .rounded))
.foregroundColor(Color.white.opacity(0.45)) +
Text(daysRemaining == 1 ? "1 day" : "\(daysRemaining) days")
.font(.system(size: 15, weight: .semibold, design: .rounded))
.foregroundColor(.white)
)
.lineLimit(2)
.lineSpacing(1.0)
.minimumScaleFactor(0.5)
Spacer()
Countdown(startDate: app.refreshedDate, endDate: app.expirationDate)
.font(.system(size: 20, weight: .semibold, design: .rounded))
.foregroundColor(Color.white)
.opacity(0.8)
.fixedSize(horizontal: true, vertical: false)
.offset(x: 5)
}
.offset(y: 5) // Offset so we don't affect layout, but still leave space between app name and Countdown.
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
.padding()
}
}
else
{
VStack {
// Put conditional inside VStack, or else an empty view will be returned
// if isPlaceholder == false, which messes up layout.
if !entry.isPlaceholder
{
Text("App Not Found")
.font(.system(.body, design: .rounded))
.fontWeight(.semibold)
.foregroundColor(Color.white.opacity(0.4))
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
.background(backgroundView(icon: entry.app?.icon, tintColor: entry.app?.tintColor))
}
}
private extension WidgetView
{
func backgroundView(icon: UIImage? = nil, tintColor: UIColor? = nil) -> some View
{
let icon = icon ?? UIImage(named: "AltStore")!
let tintColor = tintColor ?? .altPrimary
let imageHeight = 60 as CGFloat
let saturation = 1.8
let blurRadius = 5 as CGFloat
let tintOpacity = 0.45
return ZStack(alignment: .topTrailing) {
// Blurred Image
GeometryReader { geometry in
ZStack {
Image(uiImage: icon)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: imageHeight, height: imageHeight, alignment: .center)
.saturation(saturation)
.blur(radius: blurRadius, opaque: true)
.scaleEffect(geometry.size.width / imageHeight, anchor: .center)
Color(tintColor)
.opacity(tintOpacity)
}
}
Image("Badge")
.resizable()
.frame(width: 26, height: 26)
.padding()
}
}
}
struct WidgetView_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()
let longRefreshedDate = Calendar.current.date(byAdding: .day, value: -100, to: Date()) ?? Date()
let longExpirationDate = Calendar.current.date(byAdding: .day, value: 365, to: longRefreshedDate) ?? Date()
let altstore = AppSnapshot(name: "AltStore",
bundleIdentifier: "com.rileytestut.AltStore",
expirationDate: shortExpirationDate,
refreshedDate: shortRefreshedDate,
tintColor: .altPrimary,
icon: UIImage(named: "AltStore"))
let delta = AppSnapshot(name: "Delta",
bundleIdentifier: "com.rileytestut.Delta",
expirationDate: longExpirationDate,
refreshedDate: longRefreshedDate,
tintColor: .deltaPrimary,
icon: UIImage(named: "Delta"))
return Group {
WidgetView(entry: AppEntry(date: Date(), app: altstore))
.previewContext(WidgetPreviewContext(family: .systemSmall))
WidgetView(entry: AppEntry(date: Date(), app: delta))
.previewContext(WidgetPreviewContext(family: .systemSmall))
WidgetView(entry: AppEntry(date: Date(), app: nil))
.previewContext(WidgetPreviewContext(family: .systemSmall))
WidgetView(entry: AppEntry(date: Date(), app: nil, isPlaceholder: true))
.previewContext(WidgetPreviewContext(family: .systemSmall))
}
}
}