Skip to content

Commit

Permalink
Persist w3w (even though we don't use it yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshinejr committed Jan 23, 2021
1 parent bee8400 commit 6ccf202
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
14 changes: 11 additions & 3 deletions Tree Tracker/Models/AirtableTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ struct AirtableTree: Decodable {
let species: String
let notes: String?
let coordinates: String?
let what3words: String?
let imageMd5: String?
let createDate: Date?
let uploadDate: Date?
Expand All @@ -18,19 +19,21 @@ struct AirtableTree: Decodable {
case species = "Species"
case notes = "Notes"
case coordinates = "Coordinates"
case what3words = "What3Words"
case image = "Image"
case imageMd5 = "ImageSignature"
case createDate = "CreatedDate"
case uploadDate = "UploadedDate"
case fields
}

init(id: Int, supervisor: String, species: String, notes: String?, coordinates: String?, imageUrl: String?, thumbnailUrl: String?, imageMd5: String?, uploadDate: Date?, createDate: Date?) {
init(id: Int, supervisor: String, species: String, notes: String?, coordinates: String?, what3words: String?, imageUrl: String?, thumbnailUrl: String?, imageMd5: String?, uploadDate: Date?, createDate: Date?) {
self.id = id
self.supervisor = supervisor
self.species = species
self.notes = notes
self.coordinates = coordinates
self.what3words = what3words
self.imageUrl = imageUrl
self.thumbnailUrl = thumbnailUrl
self.imageMd5 = imageMd5
Expand All @@ -47,6 +50,7 @@ struct AirtableTree: Decodable {
species = try container.decode(String.self, forKey: .species)
notes = try container.decodeIfPresent(String.self, forKey: .notes)
coordinates = try container.decodeIfPresent(String.self, forKey: .coordinates)
what3words = try container.decodeIfPresent(String.self, forKey: .what3words)
imageMd5 = try container.decodeIfPresent(String.self, forKey: .imageMd5)
uploadDate = try container.decodeIfPresent(Date.self, forKey: .uploadDate)
createDate = try container.decodeIfPresent(Date.self, forKey: .createDate)
Expand All @@ -63,7 +67,7 @@ struct AirtableTree: Decodable {
}

func toRemoteTree() -> RemoteTree {
return RemoteTree(id: id, supervisor: supervisor, species: species, notes: notes, coordinates: coordinates, imageUrl: imageUrl, thumbnailUrl: thumbnailUrl, imageMd5: imageMd5, createDate: createDate, uploadDate: uploadDate)
return RemoteTree(id: id, supervisor: supervisor, species: species, notes: notes, coordinates: coordinates, what3words: what3words, imageUrl: imageUrl, thumbnailUrl: thumbnailUrl, imageMd5: imageMd5, createDate: createDate, uploadDate: uploadDate)
}
}

Expand Down Expand Up @@ -131,6 +135,7 @@ struct AirtableTreeEncodable: Encodable {
let species: String
let notes: String?
let coordinates: String?
let what3words: String?
let imageMd5: String?
let createDate: Date?
let uploadDate: Date?
Expand All @@ -141,18 +146,20 @@ struct AirtableTreeEncodable: Encodable {
case species = "Species"
case notes = "Notes"
case coordinates = "Coordinates"
case what3words = "What3Words"
case image = "Image"
case imageMd5 = "ImageSignature"
case createDate = "CreatedDate"
case uploadDate = "UploadedDate"
case fields
}

init(supervisor: String, species: String, notes: String?, coordinates: String?, imageUrl: String?, imageMd5: String?, uploadDate: Date?, createDate: Date?) {
init(supervisor: String, species: String, notes: String?, coordinates: String?, what3words: String?, imageUrl: String?, imageMd5: String?, uploadDate: Date?, createDate: Date?) {
self.supervisor = supervisor
self.species = species
self.notes = notes
self.coordinates = coordinates
self.what3words = what3words
self.imageUrl = imageUrl
self.imageMd5 = imageMd5
self.uploadDate = uploadDate
Expand All @@ -167,6 +174,7 @@ struct AirtableTreeEncodable: Encodable {
try container.encode(species, forKey: .species)
try container.encode(notes, forKey: .notes)
try container.encode(coordinates, forKey: .coordinates)
try container.encode(what3words, forKey: .what3words)
try container.encode(imageMd5, forKey: .imageMd5)
try container.encode(imageUrl.map { AirtableImage(url: $0, thumbnailUrl: nil) }, forKey: .image)
try container.encode(uploadDate, forKey: .uploadDate)
Expand Down
4 changes: 3 additions & 1 deletion Tree Tracker/Models/LocalTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ struct LocalTree: Codable, FetchableRecord, PersistableRecord, TableRecord {
let phImageId: String
var createDate: Date?
var supervisor: String
var what3words: String?
var species: String
var notes: String?
var coordinates: String?
Expand All @@ -14,13 +15,14 @@ struct LocalTree: Codable, FetchableRecord, PersistableRecord, TableRecord {
case phImageId
case createDate
case supervisor
case what3words
case species
case notes
case coordinates
case imageMd5
}

func toAirtableTree(imageUrl: String) -> AirtableTreeEncodable {
return AirtableTreeEncodable(supervisor: supervisor, species: species, notes: notes, coordinates: coordinates, imageUrl: imageUrl, imageMd5: imageMd5, uploadDate: Date(), createDate: createDate)
return AirtableTreeEncodable(supervisor: supervisor, species: species, notes: notes, coordinates: coordinates, what3words: what3words, imageUrl: imageUrl, imageMd5: imageMd5, uploadDate: Date(), createDate: createDate)
}
}
2 changes: 2 additions & 0 deletions Tree Tracker/Models/RemoteTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ struct RemoteTree: Codable, FetchableRecord, PersistableRecord, TableRecord {
let species: String
let notes: String?
var coordinates: String?
let what3words: String?
var imageUrl: String?
var thumbnailUrl: String?
let imageMd5: String?
Expand All @@ -19,6 +20,7 @@ struct RemoteTree: Codable, FetchableRecord, PersistableRecord, TableRecord {
case species
case notes
case coordinates
case what3words
case imageUrl
case thumbnailUrl
case imageMd5
Expand Down
2 changes: 1 addition & 1 deletion Tree Tracker/Screens/Details/AddLocalTreeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ final class AddLocalTreeViewModel: TreeDetailsViewModel {
}

private func save(asset: PHAsset, coordinates: String, species: String, supervisor: String, notes: String) {
let tree = LocalTree(phImageId: asset.localIdentifier, createDate: asset.creationDate, supervisor: supervisor, species: species, notes: notes, coordinates: coordinates, imageMd5: nil)
let tree = LocalTree(phImageId: asset.localIdentifier, createDate: asset.creationDate, supervisor: supervisor, what3words: nil, species: species, notes: notes, coordinates: coordinates, imageMd5: nil)
defaults[.species] = species
defaults[.supervisor] = supervisor
database.save([tree])
Expand Down
2 changes: 2 additions & 0 deletions Tree Tracker/Services/Database.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ final class Database {
table.column(RemoteTree.CodingKeys.species.stringValue, .text)
table.column(RemoteTree.CodingKeys.notes.stringValue, .text)
table.column(RemoteTree.CodingKeys.coordinates.stringValue, .text)
table.column(RemoteTree.CodingKeys.what3words.stringValue, .text)
table.column(RemoteTree.CodingKeys.imageUrl.stringValue, .text)
table.column(RemoteTree.CodingKeys.thumbnailUrl.stringValue, .text)
table.column(RemoteTree.CodingKeys.imageMd5.stringValue, .text)
Expand All @@ -41,6 +42,7 @@ final class Database {
table.column(LocalTree.CodingKeys.species.stringValue, .text)
table.column(LocalTree.CodingKeys.notes.stringValue, .text)
table.column(LocalTree.CodingKeys.coordinates.stringValue, .text)
table.column(LocalTree.CodingKeys.what3words.stringValue, .text)
table.column(LocalTree.CodingKeys.imageMd5.stringValue, .text)

table.primaryKey([LocalTree.CodingKeys.phImageId.stringValue])
Expand Down

0 comments on commit 6ccf202

Please sign in to comment.