-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Created
PostedTransactionCache
- Loading branch information
Showing
3 changed files
with
92 additions
and
10 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
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,58 @@ | ||
// | ||
// Copyright RevenueCat Inc. All Rights Reserved. | ||
// | ||
// Licensed under the MIT License (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://opensource.org/licenses/MIT | ||
// | ||
// PostedTransactionCache.swift | ||
// | ||
// Created by Nacho Soto on 7/27/23. | ||
|
||
import Foundation | ||
|
||
protocol PostedTransactionCacheType { | ||
|
||
func savePostedTransaction(_ transaction: StoreTransactionType) | ||
func hasPostedTransaction(_ transaction: StoreTransactionType) -> Bool | ||
|
||
} | ||
|
||
// TODO: tests | ||
// TODO: integrate | ||
|
||
final class PostedTransactionCache: PostedTransactionCacheType { | ||
|
||
private typealias StoredTransactions = Set<String> | ||
|
||
private let deviceCache: DeviceCache | ||
|
||
init(deviceCache: DeviceCache) { | ||
self.deviceCache = deviceCache | ||
} | ||
|
||
func savePostedTransaction(_ transaction: StoreTransactionType) { | ||
self.deviceCache.update(key: CacheKey.transactions, | ||
default: Set<String>()) { transactions in | ||
transactions.insert(transaction.transactionIdentifier) | ||
} | ||
} | ||
|
||
func hasPostedTransaction(_ transaction: StoreTransactionType) -> Bool { | ||
let transactions: StoredTransactions = self.deviceCache.value(for: CacheKey.transactions) ?? [] | ||
return transactions.contains(transaction.transactionIdentifier) | ||
} | ||
|
||
} | ||
|
||
private extension PostedTransactionCache { | ||
|
||
enum CacheKey: String, DeviceCacheKeyType { | ||
|
||
case transactions = "com.revenuecat.cached_transaction_identifier" | ||
|
||
} | ||
|
||
} |