2022-11-23 22:34:02 +01:00
|
|
|
//
|
|
|
|
|
// AppPillButton.swift
|
|
|
|
|
// SideStore
|
|
|
|
|
//
|
|
|
|
|
// Created by Fabian Thies on 20.11.22.
|
|
|
|
|
// Copyright © 2022 Fabian Thies. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
import AltStoreCore
|
|
|
|
|
|
|
|
|
|
struct AppPillButton: View {
|
|
|
|
|
|
|
|
|
|
@ObservedObject
|
|
|
|
|
var appManager = AppManager.shared.publisher
|
|
|
|
|
|
|
|
|
|
let app: AppProtocol
|
|
|
|
|
var showRemainingDays = false
|
|
|
|
|
|
|
|
|
|
var storeApp: StoreApp? {
|
|
|
|
|
(app as? StoreApp) ?? (app as? InstalledApp)?.storeApp
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var installedApp: InstalledApp? {
|
|
|
|
|
(app as? InstalledApp) ?? (app as? StoreApp)?.installedApp
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var progress: Progress? {
|
|
|
|
|
appManager.refreshProgress[app.bundleIdentifier] ?? appManager.installationProgress[app.bundleIdentifier]
|
|
|
|
|
}
|
|
|
|
|
// let progress = {
|
|
|
|
|
// let progress = Progress(totalUnitCount: 100)
|
|
|
|
|
// progress.completedUnitCount = 20
|
|
|
|
|
// return progress
|
|
|
|
|
// }()
|
|
|
|
|
|
|
|
|
|
var buttonText: String {
|
|
|
|
|
// guard progress == nil else {
|
|
|
|
|
// return ""
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
if let installedApp {
|
|
|
|
|
if self.showRemainingDays {
|
|
|
|
|
return DateFormatterHelper.string(forExpirationDate: installedApp.expirationDate)
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-16 21:23:16 +01:00
|
|
|
return L10n.AppPillButton.open
|
2022-11-23 22:34:02 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-16 21:23:16 +01:00
|
|
|
return L10n.AppPillButton.free
|
2022-11-23 22:34:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var body: some View {
|
2023-01-31 22:37:37 +01:00
|
|
|
SwiftUI.Button(action: handleButton) {
|
2022-11-23 22:34:02 +01:00
|
|
|
Text(buttonText.uppercased())
|
|
|
|
|
.bold()
|
2023-01-31 22:37:37 +01:00
|
|
|
}
|
2022-11-23 22:34:02 +01:00
|
|
|
.buttonStyle(PillButtonStyle(tintColor: storeApp?.tintColor ?? .black, progress: progress))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func handleButton() {
|
|
|
|
|
if let installedApp {
|
2022-12-21 17:45:44 +01:00
|
|
|
if showRemainingDays {
|
|
|
|
|
self.refreshApp(installedApp)
|
|
|
|
|
} else {
|
|
|
|
|
self.openApp(installedApp)
|
|
|
|
|
}
|
2022-11-23 22:34:02 +01:00
|
|
|
} else if let storeApp {
|
|
|
|
|
self.installApp(storeApp)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func openApp(_ installedApp: InstalledApp) {
|
|
|
|
|
UIApplication.shared.open(installedApp.openAppURL)
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-21 17:45:44 +01:00
|
|
|
func refreshApp(_ installedApp: InstalledApp) {
|
2023-02-19 14:25:13 +01:00
|
|
|
AppManager.shared.refresh([installedApp], presentingViewController: nil)
|
2022-12-21 17:45:44 +01:00
|
|
|
}
|
|
|
|
|
|
2022-11-23 22:34:02 +01:00
|
|
|
func installApp(_ storeApp: StoreApp) {
|
|
|
|
|
let previousProgress = AppManager.shared.installationProgress(for: storeApp)
|
|
|
|
|
guard previousProgress == nil else {
|
|
|
|
|
previousProgress?.cancel()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let _ = AppManager.shared.install(storeApp, presentingViewController: UIApplication.shared.keyWindow?.rootViewController) { result in
|
|
|
|
|
|
|
|
|
|
switch result {
|
|
|
|
|
case let .success(installedApp):
|
|
|
|
|
print("Installed app: \(installedApp.bundleIdentifier)")
|
|
|
|
|
|
|
|
|
|
case let .failure(error):
|
|
|
|
|
print("Failed to install app: \(error.localizedDescription)")
|
|
|
|
|
NotificationManager.shared.reportError(error: error)
|
|
|
|
|
AppManager.shared.installationProgress(for: storeApp)?.cancel()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-19 14:30:21 +01:00
|
|
|
struct AppPillButton_Previews: PreviewProvider {
|
|
|
|
|
|
|
|
|
|
static let context = DatabaseManager.shared.viewContext
|
|
|
|
|
static let app = StoreApp.makeAltStoreApp(in: context)
|
|
|
|
|
static let installedApp = InstalledApp.fetchAltStore(in: context)
|
|
|
|
|
|
|
|
|
|
static var previews: some View {
|
|
|
|
|
VStack {
|
|
|
|
|
self.preview(for: app)
|
|
|
|
|
|
|
|
|
|
self.preview(for: installedApp!)
|
|
|
|
|
|
|
|
|
|
self.preview(for: installedApp!, showRemainingDays: true)
|
|
|
|
|
}
|
|
|
|
|
.padding()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ViewBuilder
|
|
|
|
|
static func preview(for app: AppProtocol, showRemainingDays: Bool = false) -> some View {
|
|
|
|
|
HintView(backgroundColor: Color(UIColor.secondarySystemBackground)) {
|
|
|
|
|
HStack {
|
|
|
|
|
AppIconView(iconUrl: self.app.iconURL)
|
|
|
|
|
|
|
|
|
|
VStack(alignment: .leading) {
|
|
|
|
|
Text(app is StoreApp ? "Store App" : "Installed App")
|
|
|
|
|
.bold()
|
|
|
|
|
Text(
|
|
|
|
|
app is StoreApp ?
|
|
|
|
|
"Can be installed" :
|
|
|
|
|
showRemainingDays ? "Can be refreshed" : "Can be opened"
|
|
|
|
|
)
|
|
|
|
|
.font(.callout)
|
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
|
|
AppPillButton(app: app, showRemainingDays: showRemainingDays)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|