Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cancellation install or update app in hub #202

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 55 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,26 @@ public class Applications: ObservableObject {
}
}

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

guard status.hasCancelOpportunity else { return }
task.cancel()

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 +433,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
}
}