-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from aapis/backup
Backup note
- Loading branch information
Showing
17 changed files
with
577 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// | ||
// Log.swift | ||
// DLPrototype | ||
// | ||
// Created by Ryan Priebe on 2020-07-10. | ||
// Copyright © 2020 YegCollective. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct Backup: View { | ||
@State private var lastBackupDate: String = "" | ||
|
||
var category: Category | ||
|
||
var body: some View { | ||
VStack(alignment: .leading) { | ||
HStack { | ||
Text(Image(systemName: "cloud.fill")) | ||
.font(.title) | ||
Text("Backup \(category.title).log") | ||
.font(.title) | ||
} | ||
|
||
Divider() | ||
|
||
Text("Last Backup: ") + Text($lastBackupDate.wrappedValue) | ||
|
||
Button("Backup Now", action: { | ||
self.performBackup() | ||
}) | ||
.background(Color.accentColor) | ||
|
||
Spacer() | ||
} | ||
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity) | ||
.padding() | ||
.onAppear(perform: getLastBackupDate) | ||
} | ||
|
||
private func getDocumentsDirectory() -> URL { | ||
let paths = FileManager.default.urls(for: .desktopDirectory, in: .userDomainMask) | ||
|
||
return paths[0] | ||
} | ||
|
||
private func getLastBackupDate() -> Void { | ||
let log = getDocumentsDirectory().appendingPathComponent("\(category.title).log") | ||
|
||
if let logLines = try? String(contentsOf: log) { | ||
let lines = logLines.components(separatedBy: .newlines) | ||
|
||
if lines.count > 0 { | ||
$lastBackupDate.wrappedValue = lines.first! | ||
} | ||
} | ||
} | ||
|
||
private func performBackup() -> Void { | ||
let formatter = DateFormatter() | ||
formatter.dateFormat = "yyyy-MM-dd HH:mm" | ||
|
||
let date = formatter.string(from: Date()) | ||
let to = getDocumentsDirectory().appendingPathComponent("Log Backups/\(category.title)-\(date).log") | ||
let at = getDocumentsDirectory().appendingPathComponent("\(category.title).log") | ||
|
||
if let _ = try? FileManager().copyItem(at: at, to: to) { | ||
let text = "Backed up to \"Log Backups/\(date)\"" | ||
|
||
logLine(text: text) | ||
|
||
$lastBackupDate.wrappedValue = date | ||
} | ||
} | ||
|
||
private func writeToLog(output: Data) -> Void { | ||
let fileName = "\(category.title).log" | ||
let filePath = getDocumentsDirectory().appendingPathComponent(fileName) | ||
|
||
if let fileHandle = try? FileHandle(forWritingTo: filePath) { | ||
fileHandle.truncateFile(atOffset: 0) | ||
fileHandle.write(output) | ||
fileHandle.closeFile() | ||
} | ||
} | ||
|
||
private func logLine(text: String) -> Void { | ||
let formatter = DateFormatter() | ||
formatter.dateFormat = "yyyy-MM-dd HH:mm" | ||
|
||
let date = formatter.string(from: Date()) | ||
|
||
guard let line: Data = ("\n\(date) - \(text)").data(using: String.Encoding.utf8) else { return } | ||
|
||
writeToLog(output: line) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
DLPrototypeiOS/Assets.xcassets/AccentColor.colorset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"colors" : [ | ||
{ | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
DLPrototypeiOS/Assets.xcassets/AppIcon.appiconset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "iphone", | ||
"scale" : "2x", | ||
"size" : "20x20" | ||
}, | ||
{ | ||
"idiom" : "iphone", | ||
"scale" : "3x", | ||
"size" : "20x20" | ||
}, | ||
{ | ||
"idiom" : "iphone", | ||
"scale" : "2x", | ||
"size" : "29x29" | ||
}, | ||
{ | ||
"idiom" : "iphone", | ||
"scale" : "3x", | ||
"size" : "29x29" | ||
}, | ||
{ | ||
"idiom" : "iphone", | ||
"scale" : "2x", | ||
"size" : "40x40" | ||
}, | ||
{ | ||
"idiom" : "iphone", | ||
"scale" : "3x", | ||
"size" : "40x40" | ||
}, | ||
{ | ||
"idiom" : "iphone", | ||
"scale" : "2x", | ||
"size" : "60x60" | ||
}, | ||
{ | ||
"idiom" : "iphone", | ||
"scale" : "3x", | ||
"size" : "60x60" | ||
}, | ||
{ | ||
"idiom" : "ipad", | ||
"scale" : "1x", | ||
"size" : "20x20" | ||
}, | ||
{ | ||
"idiom" : "ipad", | ||
"scale" : "2x", | ||
"size" : "20x20" | ||
}, | ||
{ | ||
"idiom" : "ipad", | ||
"scale" : "1x", | ||
"size" : "29x29" | ||
}, | ||
{ | ||
"idiom" : "ipad", | ||
"scale" : "2x", | ||
"size" : "29x29" | ||
}, | ||
{ | ||
"idiom" : "ipad", | ||
"scale" : "1x", | ||
"size" : "40x40" | ||
}, | ||
{ | ||
"idiom" : "ipad", | ||
"scale" : "2x", | ||
"size" : "40x40" | ||
}, | ||
{ | ||
"idiom" : "ipad", | ||
"scale" : "1x", | ||
"size" : "76x76" | ||
}, | ||
{ | ||
"idiom" : "ipad", | ||
"scale" : "2x", | ||
"size" : "76x76" | ||
}, | ||
{ | ||
"idiom" : "ipad", | ||
"scale" : "2x", | ||
"size" : "83.5x83.5" | ||
}, | ||
{ | ||
"idiom" : "ios-marketing", | ||
"scale" : "1x", | ||
"size" : "1024x1024" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// | ||
// ContentView.swift | ||
// DLPrototypeiOS | ||
// | ||
// Created by Ryan Priebe on 2020-11-14. | ||
// Copyright © 2020 YegCollective. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
import CoreData | ||
|
||
struct ContentView: View { | ||
@Environment(\.managedObjectContext) private var viewContext | ||
|
||
@FetchRequest( | ||
sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)], | ||
animation: .default) | ||
private var items: FetchedResults<Item> | ||
|
||
var body: some View { | ||
List { | ||
ForEach(items) { item in | ||
Text("Item at \(item.timestamp!, formatter: itemFormatter)") | ||
} | ||
.onDelete(perform: deleteItems) | ||
} | ||
.toolbar { | ||
#if os(iOS) | ||
EditButton() | ||
#endif | ||
|
||
Button(action: addItem) { | ||
Label("Add Item", systemImage: "plus") | ||
} | ||
} | ||
} | ||
|
||
private func addItem() { | ||
withAnimation { | ||
let newItem = Item(context: viewContext) | ||
newItem.timestamp = Date() | ||
|
||
do { | ||
try viewContext.save() | ||
} catch { | ||
// Replace this implementation with code to handle the error appropriately. | ||
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. | ||
let nsError = error as NSError | ||
fatalError("Unresolved error \(nsError), \(nsError.userInfo)") | ||
} | ||
} | ||
} | ||
|
||
private func deleteItems(offsets: IndexSet) { | ||
withAnimation { | ||
offsets.map { items[$0] }.forEach(viewContext.delete) | ||
|
||
do { | ||
try viewContext.save() | ||
} catch { | ||
// Replace this implementation with code to handle the error appropriately. | ||
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. | ||
let nsError = error as NSError | ||
fatalError("Unresolved error \(nsError), \(nsError.userInfo)") | ||
} | ||
} | ||
} | ||
} | ||
|
||
private let itemFormatter: DateFormatter = { | ||
let formatter = DateFormatter() | ||
formatter.dateStyle = .short | ||
formatter.timeStyle = .medium | ||
return formatter | ||
}() | ||
|
||
struct ContentView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>_XCCurrentVersionName</key> | ||
<string>DLPrototypeiOS.xcdatamodel</string> | ||
</dict> | ||
</plist> |
9 changes: 9 additions & 0 deletions
9
DLPrototypeiOS/DLPrototypeiOS.xcdatamodeld/DLPrototypeiOS.xcdatamodel/contents
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="1" systemVersion="11A491" minimumToolsVersion="Automatic" sourceLanguage="Swift" usedWithCloudKit="true" userDefinedModelVersionIdentifier=""> | ||
<entity name="Item" representedClassName="Item" syncable="YES" codeGenerationType="class"> | ||
<attribute name="timestamp" optional="YES" attributeType="Date" usesScalarValueType="NO"/> | ||
</entity> | ||
<elements> | ||
<element name="Item" positionX="-63" positionY="-18" width="128" height="44"/> | ||
</elements> | ||
</model> |
Oops, something went wrong.