Skip to content

Commit

Permalink
Improve handling of Bitrise token editing (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
lfroms authored Dec 3, 2024
1 parent 4844ce6 commit 66c205a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
14 changes: 12 additions & 2 deletions Tophat/Utilities/CachingApplicationDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,18 @@ actor CachingApplicationDownloader: ApplicationDownloading {
return application
}

downloads[source] = Download(container: container, task: task)
return try await task.value
let download = Download(container: container, task: task)
downloads[source] = download

do {
return try await task.value
} catch {
// If the download failed, do not cache it.
try? await download.container.delete()
downloads[source] = nil

throw error
}
}

/// Deletes all cached data that was tracked by this instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import SwiftUI
import TophatKit

struct SettingsView: View {
@SecureStorage(Constants.keychainPersonalAccessTokenKey) var personalAccessToken: String?

@State private var enteredPersonalAccessToken: String = ""
@SecureStorage(Constants.keychainPersonalAccessTokenKey) var storedPersonalAccessToken: String?
@State private var enteredPersonalAccessToken = ""

var body: some View {
Form {
Expand All @@ -26,10 +25,10 @@ struct SettingsView: View {
}
.formStyle(.grouped)
.onAppear {
enteredPersonalAccessToken = personalAccessToken ?? ""
enteredPersonalAccessToken = storedPersonalAccessToken ?? ""
}
.onDisappear {
personalAccessToken = enteredPersonalAccessToken
.onChange(of: enteredPersonalAccessToken, initial: false) { oldValue, newValue in
storedPersonalAccessToken = newValue.isEmpty ? nil : newValue
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,27 @@
//

import SwiftUI
import Observation
import SimpleKeychain

@propertyWrapper
public struct SecureStorage: DynamicProperty {
struct SecureStorage {
private let key: String
private let keychain = SimpleKeychain()

@State private var value: String?

public init(_ key: String) {
self.key = key
self._value = State(initialValue: try? keychain.string(forKey: key))
}

public var wrappedValue: String? {
get { value }
get {
try? keychain.string(forKey: key)
}
nonmutating set {
if let newValue {
try? keychain.set(newValue, forKey: key)
} else {
try? keychain.deleteItem(forKey: key)
}

value = newValue
}
}

public var projectedValue: Binding<String?> {
Binding {
wrappedValue
} set: { newValue in
wrappedValue = newValue
}
}
}

0 comments on commit 66c205a

Please sign in to comment.