Skip to content

Commit

Permalink
Cancellation install or update app in hub
Browse files Browse the repository at this point in the history
  • Loading branch information
Programistich committed Mar 20, 2024
1 parent 81094cd commit 64b5002
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 31 deletions.
76 changes: 60 additions & 16 deletions Flipper/Packages/Core/Sources/Applications/Applications.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class Applications: ObservableObject {
public typealias Application = Catalog.Application

private var categories: [Category] = []
private var taskStorage: [Application.ID: Task<Void, Never>] = [:]

public enum InstalledStatus {
case loading
Expand Down Expand Up @@ -117,30 +118,40 @@ public class Applications: ObservableObject {
}

public func install(_ application: Application) async {
do {
statuses[application.id] = .installing(0)
try await _install(application) { progress in
Task {
statuses[application.id] = .installing(progress)
taskStorage[application.id]?.cancel()

taskStorage[application.id] = Task {
do {
statuses[application.id] = .installing(0)
try await _install(application) { progress in
Task {
statuses[application.id] = .installing(progress)
}
}
statuses[application.id] = .installed
} catch {
logger.error("install app: \(error)")
}
statuses[application.id] = .installed
} catch {
logger.error("install app: \(error)")
taskStorage[application.id] = nil
}
}

public func update(_ application: Application) async {
do {
statuses[application.id] = .updating(0)
try await _install(application) { progress in
Task {
statuses[application.id] = .updating(progress)
taskStorage[application.id]?.cancel()

taskStorage[application.id] = Task {
do {
statuses[application.id] = .updating(0)
try await _install(application) { progress in
Task {
statuses[application.id] = .updating(progress)
}
}
statuses[application.id] = .installed
} catch {
logger.error("update app: \(error)")
}
statuses[application.id] = .installed
} catch {
logger.error("update app: \(error)")
taskStorage[application.id] = nil
}
}

Expand All @@ -163,6 +174,31 @@ public class Applications: ObservableObject {
}
}

public func cancel(_ id: Application.ID) async {
guard
let status = statuses[id],
let task = taskStorage[id]
else { return }

switch status {
case .updating(_), .installing(_):
task.cancel()
_ = await task.result
default:
return
}

switch status {
case .updating(_):
statuses[id] = .outdated
case .installing(_):
installed.removeAll { $0.id == id }
statuses[id] = .notInstalled
default:
return
}
}

public enum OpenAppStatus {
case success
case busy
Expand Down Expand Up @@ -402,6 +438,14 @@ public class Applications: ObservableObject {
case .checking: return 8
}
}

public var hasCancelOpportunity: Bool {
switch self {
case .installing(_): return true
case .updating(_): return true
default: return false
}
}
}

public var hasOpenAppSupport: Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ extension AppView {

var body: some View {
HStack(alignment: .center, spacing: 12) {
if canDelete {
if status.hasCancelOpportunity {
CancelProgressAppButton {
cancel()
}
.frame(width: 46, height: 46)
} else if canDelete {
DeleteAppButton {
confirmDelete = true
}
Expand Down Expand Up @@ -122,6 +127,12 @@ extension AppView {
}
}

func cancel() {
Task {
await model.cancel(application.id)
}
}

func openApp() {
Task {
await model.openApp(by: application.id) { result in
Expand Down
35 changes: 24 additions & 11 deletions Flipper/Packages/UI/Sources/Hub/Apps/Components/AppRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,24 @@ struct AppRow: View {
.disabled(!isBuildReady)

if isInstalled {
DeleteAppButton {
showConfirmDelete = true
}
.frame(width: 34, height: 34)
.alert(isPresented: $showConfirmDelete) {
ConfirmDeleteAppAlert(
isPresented: $showConfirmDelete,
application: application,
category: model.category(for: application)
) {
delete()
if status.hasCancelOpportunity {
CancelProgressAppButton {
cancel()
}
.frame(width: 34, height: 34)
} else {
DeleteAppButton {
showConfirmDelete = true
}
.frame(width: 34, height: 34)
.alert(isPresented: $showConfirmDelete) {
ConfirmDeleteAppAlert(
isPresented: $showConfirmDelete,
application: application,
category: model.category(for: application)
) {
delete()
}
}
}
}
Expand All @@ -69,6 +76,12 @@ struct AppRow: View {
}
}

func cancel() {
Task {
await model.cancel(application.id)
}
}

struct AppRowActionButton: View {
@EnvironmentObject var model: Applications
@EnvironmentObject var device: Device
Expand Down
23 changes: 20 additions & 3 deletions Flipper/Packages/UI/Sources/Hub/Apps/Components/Buttons.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,16 @@ struct UpdateAllAppButton: View {
}
}

struct DeleteAppButton: View {
var action: () -> Void
private struct ActionAppButton: View {
let image: String
let action: () -> Void

var body: some View {
Button {
action()
} label: {
GeometryReader { proxy in
Image("AppDelete")
Image(image)
.resizable()
.frame(
width: proxy.size.width,
Expand All @@ -70,6 +71,22 @@ struct DeleteAppButton: View {
}
}

struct DeleteAppButton: View {
var action: () -> Void

var body: some View {
ActionAppButton(image: "AppDelete", action: action)
}
}

struct CancelProgressAppButton: View {
var action: () -> Void

var body: some View {
ActionAppButton(image: "AppCancel", action: action)
}
}

struct InstallAppButton: View {
var action: () -> Void

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"filename" : "AppCancel.svg",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

0 comments on commit 64b5002

Please sign in to comment.