Skip to content

Commit

Permalink
Add suffix to imported item name if exists
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyfreeman committed Oct 25, 2022
1 parent 86aa89d commit 03ba76d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
18 changes: 11 additions & 7 deletions Flipper/Packages/Core/Sources/Archive/Archive.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,19 @@ extension Archive {
deletedItems.append(item)
}

public func restore(_ item: ArchiveItem) async throws {
let newPath = try await mobileArchive.nextAvailablePath(for: item.path)
var newItem = try ArchiveItem(
filename: newPath.lastComponent ?? "",
public func copyIfExists(_ item: ArchiveItem) async throws -> ArchiveItem {
let path = try await mobileArchive.nextAvailablePath(for: item.path)
return try ArchiveItem(
filename: path.lastComponent ?? "",
properties: item.properties,
shadowCopy: item.shadowCopy)
newItem.status = status(for: newItem)
try await upsert(newItem)
try await wipe(newItem)
}

public func restore(_ item: ArchiveItem) async throws {
var item = try await copyIfExists(item)
item.status = status(for: item)
try await upsert(item)
try await wipe(item)
}

public func restoreAll() async throws {
Expand Down
7 changes: 5 additions & 2 deletions Flipper/Packages/Core/Sources/Archive/ArchiveProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,13 @@ extension ArchiveProtocol {
// format: name_{Int}.type
let parts = name.value.split(separator: "_")

let namePrefix = parts.count >= 2
var hasNumberSuffix: Bool {
parts.count >= 2 && Int(parts.last.unsafelyUnwrapped) != nil
}
let namePrefix = hasNumberSuffix
? parts.dropLast().joined(separator: "_")
: parts.joined(separator: "_")
var number = parts.count >= 2
var number = hasNumberSuffix
? Int(parts.last.unsafelyUnwrapped) ?? 1
: 1

Expand Down
3 changes: 2 additions & 1 deletion Flipper/Packages/Core/Sources/State/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ public class AppState {

private func onOpenKeyURL(_ url: URL) async throws {
let item = try await Sharing.importKey(from: url)
importQueue = [item]
let newItem = try await archive.copyIfExists(item)
importQueue = [newItem]
logger.info("key url opened")
}

Expand Down
2 changes: 1 addition & 1 deletion Flipper/Packages/UI/Sources/Archive/ArchiveViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ArchiveViewModel: ObservableObject {
@Published var showSearchView = false
@Published var hasImportedItem = false

var importedItem: ArchiveItem? {
var importedItem: ArchiveItem {
appState.importQueue.removeFirst()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class ImportViewModel: ObservableObject {
let appState: AppState = .shared
var dismissPublisher = PassthroughSubject<Void, Never>()

init(item: ArchiveItem?) {
self.item = item ?? .none
self.backup = item ?? .none
init(item: ArchiveItem) {
self.item = item
self.backup = item
recordImport()
}

Expand Down

0 comments on commit 03ba76d

Please sign in to comment.