Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
Allow TaxonomyRequests to succeed when creating URLs to their JSON re…
Browse files Browse the repository at this point in the history
…sources
  • Loading branch information
jncosideout committed Aug 31, 2020
1 parent 6a6d4c0 commit 210e186
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
9 changes: 5 additions & 4 deletions Sources/Models/PersistenceManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ protocol PersistenceManagerProtocol {
func save(tagLine: Tagline)
func tagLine() -> Tagline?
var additivesIsEmpty: Bool { get }

func save(labels: [Label])
func label(forCode: String) -> Label?
var labelsIsEmpty: Bool { get }

// Offline
func save(offlineProducts: [RealmOfflineProduct])
Expand Down Expand Up @@ -372,15 +373,15 @@ class PersistenceManager: PersistenceManagerProtocol {
saveOrUpdate(objects: labels)
log.info("Saved \(labels.count) labels in taxonomy database")
}

func label(forCode code: String) -> Label? {
return getRealm().object(ofType: Label.self, forPrimaryKey: code)
}

var labelsIsEmpty: Bool {
getRealm().objects(Label.self).isEmpty
}

// Offline Products
func save(offlineProducts: [RealmOfflineProduct]) {
saveOrUpdate(objects: offlineProducts)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Network/TaxonomiesParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ struct TaxonomiesParser: TaxonomiesParserProtocol {
})
return ingredientsAnalysisConfig
}

func parseLabels(data: [String: Any]) -> [Label] {
let labels = data.compactMap({ (labelCode: String, value: Any) -> Label? in
let tags = parseTags(value: value)
Expand Down
2 changes: 2 additions & 0 deletions Sources/Network/TaxonomiesRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ struct TaxonomiesRequest: URLRequestConvertible {
return route.rawValue + "/data/" + Endpoint.get
case (.post, _):
return Endpoint.post + route.rawValue
case (.get, _):
return Endpoint.get + "/data/" + route.rawValue
default:
return ""
}
Expand Down
9 changes: 5 additions & 4 deletions Sources/Network/TaxonomiesService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ enum TaxonomiesRoute: String {
case getVitamins = "taxonomies/vitamins.json"
case getMinerals = "taxonomies/minerals.json"
case getNucleotides = "taxonomies/nucleotides.json"
case getInvalidBarcodes = "invalid-barcodes.json"
case getInvalidBarcodes = "taxonomies/invalid-barcodes.json"
case getLabels = "taxonomies/labels.json"
}

Expand Down Expand Up @@ -357,7 +357,7 @@ class TaxonomiesService: TaxonomiesApi {
callback(false)
}
}

fileprivate func refreshLabels(_ callback: @escaping (_: Bool) -> Void) {
do {
let request = try TaxonomiesRequest(route: .getLabels, requestType: .get).asURLRequest()
Expand Down Expand Up @@ -402,7 +402,8 @@ class TaxonomiesService: TaxonomiesApi {
persistenceManager.nucleotidesIsEmpty ||
// persistenceManager.otherNutritionalSubstancesIsEmpty ||
persistenceManager.ingredientsAnalysisIsEmpty ||
persistenceManager.ingredientsAnalysisConfigIsEmpty
persistenceManager.ingredientsAnalysisConfigIsEmpty ||
persistenceManager.labelsIsEmpty
if shouldDownload {
downloadTaxonomies()
} else {
Expand Down Expand Up @@ -480,7 +481,7 @@ class TaxonomiesService: TaxonomiesApi {
allSuccess = allSuccess && success
group.leave()
}

group.enter()
self.refreshLabels({ (success) in
allSuccess = allSuccess && success
Expand Down

0 comments on commit 210e186

Please sign in to comment.