Skip to content

Commit

Permalink
Add Import/Eport/Delete collection (New strings added #68 )
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimillian committed May 14, 2020
1 parent db7693e commit 861b7a4
Show file tree
Hide file tree
Showing 8 changed files with 355 additions and 93 deletions.
4 changes: 4 additions & 0 deletions ACHNBrowserUI/ACHNBrowserUI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
69875F59246557D000B1D46C /* UserListDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69875F58246557D000B1D46C /* UserListDetailView.swift */; };
69875F5B24655AF000B1D46C /* UserListDetailViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69875F5A24655AF000B1D46C /* UserListDetailViewModel.swift */; };
69933E0D246D1D6100528B51 /* items_zh.json in Resources */ = {isa = PBXBuildFile; fileRef = 69933E0C246D1D6100528B51 /* items_zh.json */; };
69933E0F246D476E00528B51 /* DocumentPickerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69933E0E246D476E00528B51 /* DocumentPickerView.swift */; };
699425D724688C200089C21B /* clothing in Resources */ = {isa = PBXBuildFile; fileRef = 699425D624688C200089C21B /* clothing */; };
699425EC246927C50089C21B /* items_es.json in Resources */ = {isa = PBXBuildFile; fileRef = 699425E5246927C40089C21B /* items_es.json */; };
699425EE246927C50089C21B /* items_ru.json in Resources */ = {isa = PBXBuildFile; fileRef = 699425E7246927C40089C21B /* items_ru.json */; };
Expand Down Expand Up @@ -409,6 +410,7 @@
69875F5A24655AF000B1D46C /* UserListDetailViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserListDetailViewModel.swift; sourceTree = "<group>"; };
69933E0B246D1CDD00528B51 /* ZH_TW */ = {isa = PBXFileReference; fileEncoding = 10; lastKnownFileType = text.plist.strings; name = ZH_TW; path = ZH_TW.lproj/Localizable.strings; sourceTree = "<group>"; };
69933E0C246D1D6100528B51 /* items_zh.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = items_zh.json; sourceTree = "<group>"; };
69933E0E246D476E00528B51 /* DocumentPickerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DocumentPickerView.swift; sourceTree = "<group>"; };
699425D624688C200089C21B /* clothing */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = clothing; sourceTree = "<group>"; };
699425E5246927C40089C21B /* items_es.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = items_es.json; sourceTree = "<group>"; };
699425E7246927C40089C21B /* items_ru.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = items_ru.json; sourceTree = "<group>"; };
Expand Down Expand Up @@ -702,6 +704,7 @@
children = (
693E72A6245EBB1200CD26F4 /* SafariView.swift */,
693E72A4245EBAF900CD26F4 /* ActivityControllerView.swift */,
69933E0E246D476E00528B51 /* DocumentPickerView.swift */,
);
path = representables;
sourceTree = "<group>";
Expand Down Expand Up @@ -1303,6 +1306,7 @@
68BA5826244A511700B53811 /* CategoryDetailView.swift in Sources */,
697E6400244AD66E008FB710 /* SettingsView.swift in Sources */,
68BA582C244B695F00B53811 /* IslandDetailViewModel.swift in Sources */,
69933E0F246D476E00528B51 /* DocumentPickerView.swift in Sources */,
024363D92465EA15006A37A0 /* TodayTurnipSection.swift in Sources */,
69875F5B24655AF000B1D46C /* UserListDetailViewModel.swift in Sources */,
69CA5F01244F1AD300B275B6 /* SectionHeaderView.swift in Sources */,
Expand Down
62 changes: 62 additions & 0 deletions ACHNBrowserUI/ACHNBrowserUI/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeIconFiles</key>
<array>
<string>round-lime@2x.png</string>
</array>
<key>CFBundleTypeName</key>
<string>AC Helper user collection</string>
<key>LSItemContentTypes</key>
<array>
<string>public.data</string>
<string>com.thomasricouard.ACNH.achelper</string>
</array>
</dict>
</array>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIcons</key>
Expand Down Expand Up @@ -399,6 +415,8 @@
<string>public.app-category.reference</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
Expand Down Expand Up @@ -444,5 +462,49 @@
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeDescription</key>
<string>User collection</string>
<key>UTTypeConformsTo</key>
<array>
<string>public.data</string>
</array>
<key>UTTypeIconFiles</key>
<array>
<string>round-lime@2x.png</string>
</array>
<key>UTTypeIdentifier</key>
<string>com.thomasricouard.ACNH.achelper</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>achelper</string>
</dict>
</dict>
</array>
<key>UTImportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeDescription</key>
<string>User collection</string>
<key>UTTypeConformsTo</key>
<array>
<string>public.data</string>
</array>
<key>UTTypeIconFiles</key>
<array>
<string>round-lime@2x.png</string>
</array>
<key>UTTypeIdentifier</key>
<string>com.thomasricouard.ACNH.achelper</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>achelper</string>
</dict>
</dict>
</array>
</dict>
</plist>
Binary file modified ACHNBrowserUI/ACHNBrowserUI/fr.lproj/Localizable.strings
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,7 @@ public class UserCollection: ObservableObject {
in: .userDomainMask,
appropriateFor: nil,
create: false).appendingPathComponent("collection")
if let data = try? Data(contentsOf: filePath) {
decoder.dataDecodingStrategy = .base64
do {
let savedData = try decoder.decode(SavedData.self, from: data)
self.items = savedData.items
self.villagers = savedData.villagers
self.critters = savedData.critters
self.lists = savedData.lists ?? []
} catch {
try? FileManager.default.removeItem(at: filePath)
}
}
_ = loadCollection(file: filePath)
} catch let error {
fatalError(error.localizedDescription)
}
Expand Down Expand Up @@ -111,7 +100,7 @@ public class UserCollection: ObservableObject {
save()
}

// MARK: - Saving
// MARK: - Import / Export
private func save() {
do {
let savedData = SavedData(items: items, villagers: villagers, critters: critters, lists: lists)
Expand All @@ -122,4 +111,72 @@ public class UserCollection: ObservableObject {
}
encoder.dataEncodingStrategy = .base64
}

private func loadCollection(file: URL) -> Bool {
if let data = try? Data(contentsOf: file) {
decoder.dataDecodingStrategy = .base64
do {
let savedData = try decoder.decode(SavedData.self, from: data)
self.items = savedData.items
self.villagers = savedData.villagers
self.critters = savedData.critters
self.lists = savedData.lists ?? []
return true
} catch {
return false
}
}
return false
}

public func deleteCollection() -> Bool {
do {
try FileManager.default.removeItem(at: filePath)
self.items = []
self.villagers = []
self.critters = []
self.lists = []
save()
return true
} catch {
return false
}
}

public func generateExportURL() -> URL? {
do {
var sharedURL = try FileManager.default.url(for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: false)
sharedURL.appendPathComponent("exported-collection")
sharedURL.appendPathExtension("achelper")
try? FileManager.default.removeItem(at: sharedURL)
try FileManager.default.copyItem(at: filePath, to: sharedURL)
return sharedURL
} catch let error {
print(error.localizedDescription)
return nil
}
}

public func sizeOfArchivedState() -> String {
do {
let resources = try filePath.resourceValues(forKeys:[.fileSizeKey])
let formatter = ByteCountFormatter()
formatter.allowedUnits = .useKB
formatter.countStyle = .file
return formatter.string(fromByteCount: Int64(resources.fileSize ?? 0))
} catch {
return "0"
}
}

public func processImportedFile(url: URL) -> Bool {
let success = loadCollection(file: url)
if success {
save()
}
return success
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// DocumentPickerView.swift
// ACHNBrowserUI
//
// Created by Thomas Ricouard on 14/05/2020.
// Copyright © 2020 Thomas Ricouard. All rights reserved.
//

import Foundation
import SwiftUI

#if !os(tvOS)
public final class DocumentPickerView: NSObject, UIViewControllerRepresentable, UIDocumentPickerDelegate {
public let url: URL?
public let mode: UIDocumentPickerMode

@Binding var importedFile: URL?

public init(url: URL?, mode: UIDocumentPickerMode, importedFile: Binding<URL?>) {
self.url = url
self.mode = mode
self._importedFile = importedFile
}

public class Coordinator: NSObject, UIDocumentPickerDelegate {
var parent: DocumentPickerView

init(_ parent: DocumentPickerView) {
self.parent = parent
}

public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
if let file = urls.first {
parent.importedFile = file
}
}
}

public func makeCoordinator() -> Coordinator {
Coordinator(self)
}

public func makeUIViewController(context: UIViewControllerRepresentableContext<DocumentPickerView>) -> UIDocumentPickerViewController {
if let url = url, mode == .exportToService {
let picker = UIDocumentPickerViewController(url: url, in: mode)
return picker
} else {
let picker = UIDocumentPickerViewController(documentTypes: ["com.thomasricouard.ACNH.achelper"],
in: .import)
picker.delegate = context.coordinator
return picker
}
}

public func updateUIViewController(_ uiViewController: UIDocumentPickerViewController,
context: UIViewControllerRepresentableContext<DocumentPickerView>) {

}
}
#endif

Loading

0 comments on commit 861b7a4

Please sign in to comment.