Skip to content
2 changes: 1 addition & 1 deletion Sources/CommonMain/Caching/CachingManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import CommonCrypto
private var customCachePath: String?
private var cacheKey: String = ""

init(apiKey: String? = nil) {
public init(apiKey: String? = nil) {
super.init()
if let apiKey {
self.setCacheKey(apiKey)
Expand Down
28 changes: 8 additions & 20 deletions Sources/CommonMain/StickyBucket/StickyBucketService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,18 @@ import Foundation

@objc public class StickyBucketService: NSObject, StickyBucketServiceProtocol {
private let prefix: String
private let localStorage: CachingLayer?
private let localStorage = CachingManager()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we need to set cacheKey somehow?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


public init(prefix: String = "gbStickyBuckets__", localStorage: CachingLayer? = nil) {
public init(prefix: String = "gbStickyBuckets__", localStoragePath: CacheDirectory = .applicationSupport) {
self.prefix = prefix
self.localStorage = localStorage
localStorage.setSystemCacheDirectory(localStoragePath)
}

public func getAssignments(attributeName: String,
attributeValue: String,
completion: @escaping (StickyAssignmentsDocument?, Error?) -> Void) {
let key = "\(attributeName)||\(attributeValue)"

guard let localStorage = localStorage else {
completion(nil, nil)
return
}

if
let data = localStorage.getContent(fileName: prefix + key),
let jsonPetitions = try? JSONDecoder().decode(StickyAssignmentsDocument.self, from: data) {
Expand All @@ -39,12 +34,9 @@ import Foundation
completion: @escaping (Error?) -> Void) {
let key = "\(doc.attributeName)||\(doc.attributeValue)"

guard let localStorage = localStorage,
let docData = try? JSONEncoder().encode(doc) else {
completion(nil)
return
}


guard let docData = try? JSONEncoder().encode(doc) else { return }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vazarkevych I think we need to call completion(nil) in else block

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


localStorage.saveContent(fileName: prefix + key, content: docData)
completion(nil)
}
Expand All @@ -65,12 +57,8 @@ import Foundation

private func getAssignmentsSync(attributeName: String, attributeValue: String) -> StickyAssignmentsDocument? {
let key = "\(attributeName)||\(attributeValue)"

guard let localStorage = localStorage else { return nil }

if
let data = localStorage.getContent(fileName: prefix + key),
let jsonPetitions = try? JSONDecoder().decode(StickyAssignmentsDocument.self, from: data) {

if let data = localStorage.getContent(fileName: prefix + key), let jsonPetitions = try? JSONDecoder().decode(StickyAssignmentsDocument.self, from: data) {
return jsonPetitions
}

Expand Down