FIX: Widget now shows Pending if latest tx is unconfirmed

This commit is contained in:
marcosrdz 2021-01-31 18:39:43 -05:00
parent 00f57695ca
commit 2e9fb9c09a
6 changed files with 37 additions and 16 deletions

View File

@ -8,6 +8,7 @@ function WidgetCommunication() {
WidgetCommunication.WidgetCommunicationAllWalletsSatoshiBalance = 'WidgetCommunicationAllWalletsSatoshiBalance';
WidgetCommunication.WidgetCommunicationAllWalletsLatestTransactionTime = 'WidgetCommunicationAllWalletsLatestTransactionTime';
WidgetCommunication.WidgetCommunicationDisplayBalanceAllowed = 'WidgetCommunicationDisplayBalanceAllowed';
WidgetCommunication.LatestTransactionIsUnconfirmed = 'WidgetCommunicationLatestTransactionIsUnconfirmed';
const { wallets, walletsInitialized, isStorageEncrypted } = useContext(BlueStorageContext);
WidgetCommunication.isBalanceDisplayAllowed = async () => {
@ -40,7 +41,11 @@ function WidgetCommunication() {
}
balance += wallet.getBalance();
if (wallet.getLatestTransactionTimeEpoch() > latestTransactionTime) {
latestTransactionTime = wallet.getLatestTransactionTimeEpoch();
if (wallet.getTransactions()[0].confirmations === 0) {
latestTransactionTime = WidgetCommunication.LatestTransactionIsUnconfirmed;
} else {
latestTransactionTime = wallet.getLatestTransactionTimeEpoch();
}
}
}
return { allWalletsBalance: balance, latestTransactionTime };

View File

@ -17,7 +17,7 @@ struct Provider: TimelineProvider {
func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) {
let entry: SimpleEntry
if (context.isPreview) {
entry = SimpleEntry(date: Date(), marketData: MarketData(nextBlock: "26", sats: "9 134", price: "$10,000", rate: 10000), allWalletsBalance: WalletData(balance: 1000000, latestTransactionTime: 1568804029000))
entry = SimpleEntry(date: Date(), marketData: MarketData(nextBlock: "26", sats: "9 134", price: "$10,000", rate: 10000), allWalletsBalance: WalletData(balance: 1000000, latestTransactionTime: LatestTransaction(isUnconfirmed: false, epochValue: 1568804029000)))
} else {
entry = SimpleEntry(date: Date(), marketData: emptyMarketData)
}
@ -78,7 +78,7 @@ struct WalletInformationWidget: Widget {
struct WalletInformationWidget_Previews: PreviewProvider {
static var previews: some View {
WalletInformationWidgetEntryView(entry: SimpleEntry(date: Date(), marketData: MarketData(nextBlock: "26", sats: "9 134", price: "$10,000", rate: Double(13000)), allWalletsBalance: WalletData(balance: 10000, latestTransactionTime: 1568804029000)))
WalletInformationWidgetEntryView(entry: SimpleEntry(date: Date(), marketData: MarketData(nextBlock: "26", sats: "9 134", price: "$10,000", rate: Double(13000)), allWalletsBalance: WalletData(balance: 10000, latestTransactionTime: LatestTransaction(isUnconfirmed: false, epochValue: 1568804029000))))
.previewContext(WidgetPreviewContext(family: .systemSmall))
}
}

View File

@ -33,7 +33,7 @@ struct MarketData:Codable {
struct WalletData {
var balance: Double
var latestTransactionTime: Int = 0
var latestTransactionTime: LatestTransaction = LatestTransaction(isUnconfirmed: false, epochValue: 0)
var formattedBalanceBTC: String {
let formatter = NumberFormatter()
formatter.numberStyle = .none
@ -52,7 +52,7 @@ struct WalletData {
let emptyMarketData = MarketData(nextBlock: "...", sats: "...", price: "...", rate: 0)
let emptyWalletData = WalletData(balance: 0, latestTransactionTime: Int(Date().timeIntervalSince1970))
let emptyWalletData = WalletData(balance: 0, latestTransactionTime: LatestTransaction(isUnconfirmed: false, epochValue: Int(Date().timeIntervalSince1970)))
enum MarketDataTimeline: String {
case Previous = "previous"

View File

@ -16,6 +16,7 @@ enum UserDefaultsGroupKey: String {
case ElectrumSettingsSSLPort = "electrum_ssl_port"
case AllWalletsBalance = "WidgetCommunicationAllWalletsSatoshiBalance"
case AllWalletsLatestTransactionTime = "WidgetCommunicationAllWalletsLatestTransactionTime"
case LatestTransactionIsUnconfirmed = "\"WidgetCommunicationLatestTransactionIsUnconfirmed\""
}
struct UserDefaultsElectrumSettings {
@ -24,6 +25,11 @@ struct UserDefaultsElectrumSettings {
let sslPort: Int32?
}
struct LatestTransaction {
let isUnconfirmed: Bool?
let epochValue: Int?
}
let DefaultElectrumPeers = [UserDefaultsElectrumSettings(host: "electrum1.bluewallet.io", port: 50001, sslPort: 443),
UserDefaultsElectrumSettings(host: "electrum2.bluewallet.io", port: 50001, sslPort: 443),
UserDefaultsElectrumSettings(host: "electrum3.bluewallet.io", port: 50001, sslPort: 443)]
@ -54,12 +60,17 @@ class UserDefaultsGroup {
return Double(allWalletsBalance) ?? 0
}
static func getAllWalletsLatestTransactionTime() -> Int {
// Int: EPOCH value, Bool: Latest transaction is unconfirmed
static func getAllWalletsLatestTransactionTime() -> LatestTransaction {
guard let allWalletsTransactionTime = suite?.string(forKey: UserDefaultsGroupKey.AllWalletsLatestTransactionTime.rawValue) else {
return 0
return LatestTransaction(isUnconfirmed: false, epochValue: 0)
}
if allWalletsTransactionTime == UserDefaultsGroupKey.LatestTransactionIsUnconfirmed.rawValue {
return LatestTransaction(isUnconfirmed: true, epochValue: 0)
} else {
return LatestTransaction(isUnconfirmed: false, epochValue: Int(allWalletsTransactionTime))
}
return Int(allWalletsTransactionTime) ?? 0
}
}

View File

@ -22,10 +22,15 @@ struct WalletInformationView: View {
return amount
}
var formattedLatestTransactionTime: String {
if allWalletsBalance.latestTransactionTime == 0 {
return "never"
if allWalletsBalance.latestTransactionTime.isUnconfirmed == true {
return "Pending..."
} else if allWalletsBalance.latestTransactionTime.epochValue == 0 {
return "Never"
}
let forDate = Date(timeIntervalSince1970: (TimeInterval(allWalletsBalance.latestTransactionTime) / 1000))
guard let epochValue = allWalletsBalance.latestTransactionTime.epochValue else {
return "Never"
}
let forDate = Date(timeIntervalSince1970: TimeInterval(epochValue / 1000))
let dateFormatter = RelativeDateTimeFormatter()
dateFormatter.locale = Locale(identifier: Locale.current.identifier)
dateFormatter.dateTimeStyle = .numeric
@ -53,7 +58,7 @@ struct WalletInformationView: View {
struct WalletInformationView_Previews: PreviewProvider {
static var previews: some View {
WalletInformationView(allWalletsBalance: WalletData(balance: 10000, latestTransactionTime: 1568804029000), marketData: MarketData(nextBlock: "26", sats: "9 134", price: "$10,000", rate: Double(13000)))
WalletInformationView(allWalletsBalance: WalletData(balance: 10000, latestTransactionTime: LatestTransaction(isUnconfirmed: false, epochValue: 1568804029000)), marketData: MarketData(nextBlock: "26", sats: "9 134", price: "$10,000", rate: Double(13000)))
.previewContext(WidgetPreviewContext(family: .systemSmall))
}
}

View File

@ -17,7 +17,7 @@ struct Provider: TimelineProvider {
func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) {
let entry: SimpleEntry
if (context.isPreview) {
entry = SimpleEntry(date: Date(), marketData: MarketData(nextBlock: "26", sats: "9 134", price: "$10,000", rate: 10000), allWalletsBalance: WalletData(balance: 1000000, latestTransactionTime: 1568804029000))
entry = SimpleEntry(date: Date(), marketData: MarketData(nextBlock: "26", sats: "9 134", price: "$10,000", rate: 10000), allWalletsBalance: WalletData(balance: 1000000, latestTransactionTime: LatestTransaction(isUnconfirmed: false, epochValue: 1568804029000)))
} else {
entry = SimpleEntry(date: Date(), marketData: emptyMarketData)
}
@ -107,9 +107,9 @@ struct WalletInformationAndMarketWidget: Widget {
struct WalletInformationAndMarketWidget_Previews: PreviewProvider {
static var previews: some View {
WalletInformationAndMarketWidgetEntryView(entry: SimpleEntry(date: Date(), marketData: MarketData(nextBlock: "26", sats: "9 134", price: "$10,000", rate: 0), allWalletsBalance: WalletData(balance: 10000, latestTransactionTime: 1568804029000)))
WalletInformationAndMarketWidgetEntryView(entry: SimpleEntry(date: Date(), marketData: MarketData(nextBlock: "26", sats: "9 134", price: "$10,000", rate: 0), allWalletsBalance: WalletData(balance: 10000, latestTransactionTime: LatestTransaction(isUnconfirmed: false, epochValue: 1568804029000))))
.previewContext(WidgetPreviewContext(family: .systemMedium))
WalletInformationAndMarketWidgetEntryView(entry: SimpleEntry(date: Date(), marketData: MarketData(nextBlock: "26", sats: "9 134", price: "$10,000", rate: 0), allWalletsBalance: WalletData(balance: 10000, latestTransactionTime: 1568804029000)))
WalletInformationAndMarketWidgetEntryView(entry: SimpleEntry(date: Date(), marketData: MarketData(nextBlock: "26", sats: "9 134", price: "$10,000", rate: 0), allWalletsBalance: WalletData(balance: 10000, latestTransactionTime: LatestTransaction(isUnconfirmed: false, epochValue: 1568804029000))))
.previewContext(WidgetPreviewContext(family: .systemLarge))
}
}