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

View File

@ -17,7 +17,7 @@ struct Provider: TimelineProvider {
func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) { func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) {
let entry: SimpleEntry let entry: SimpleEntry
if (context.isPreview) { 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 { } else {
entry = SimpleEntry(date: Date(), marketData: emptyMarketData) entry = SimpleEntry(date: Date(), marketData: emptyMarketData)
} }
@ -78,7 +78,7 @@ struct WalletInformationWidget: Widget {
struct WalletInformationWidget_Previews: PreviewProvider { struct WalletInformationWidget_Previews: PreviewProvider {
static var previews: some View { 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)) .previewContext(WidgetPreviewContext(family: .systemSmall))
} }
} }

View File

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

View File

@ -16,6 +16,7 @@ enum UserDefaultsGroupKey: String {
case ElectrumSettingsSSLPort = "electrum_ssl_port" case ElectrumSettingsSSLPort = "electrum_ssl_port"
case AllWalletsBalance = "WidgetCommunicationAllWalletsSatoshiBalance" case AllWalletsBalance = "WidgetCommunicationAllWalletsSatoshiBalance"
case AllWalletsLatestTransactionTime = "WidgetCommunicationAllWalletsLatestTransactionTime" case AllWalletsLatestTransactionTime = "WidgetCommunicationAllWalletsLatestTransactionTime"
case LatestTransactionIsUnconfirmed = "\"WidgetCommunicationLatestTransactionIsUnconfirmed\""
} }
struct UserDefaultsElectrumSettings { struct UserDefaultsElectrumSettings {
@ -24,6 +25,11 @@ struct UserDefaultsElectrumSettings {
let sslPort: Int32? let sslPort: Int32?
} }
struct LatestTransaction {
let isUnconfirmed: Bool?
let epochValue: Int?
}
let DefaultElectrumPeers = [UserDefaultsElectrumSettings(host: "electrum1.bluewallet.io", port: 50001, sslPort: 443), let DefaultElectrumPeers = [UserDefaultsElectrumSettings(host: "electrum1.bluewallet.io", port: 50001, sslPort: 443),
UserDefaultsElectrumSettings(host: "electrum2.bluewallet.io", port: 50001, sslPort: 443), UserDefaultsElectrumSettings(host: "electrum2.bluewallet.io", port: 50001, sslPort: 443),
UserDefaultsElectrumSettings(host: "electrum3.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 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 { guard let allWalletsTransactionTime = suite?.string(forKey: UserDefaultsGroupKey.AllWalletsLatestTransactionTime.rawValue) else {
return 0 return LatestTransaction(isUnconfirmed: false, epochValue: 0)
} }
return Int(allWalletsTransactionTime) ?? 0 if allWalletsTransactionTime == UserDefaultsGroupKey.LatestTransactionIsUnconfirmed.rawValue {
return LatestTransaction(isUnconfirmed: true, epochValue: 0)
} else {
return LatestTransaction(isUnconfirmed: false, epochValue: Int(allWalletsTransactionTime))
}
} }
} }

View File

@ -22,10 +22,15 @@ struct WalletInformationView: View {
return amount return amount
} }
var formattedLatestTransactionTime: String { var formattedLatestTransactionTime: String {
if allWalletsBalance.latestTransactionTime == 0 { if allWalletsBalance.latestTransactionTime.isUnconfirmed == true {
return "never" 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() let dateFormatter = RelativeDateTimeFormatter()
dateFormatter.locale = Locale(identifier: Locale.current.identifier) dateFormatter.locale = Locale(identifier: Locale.current.identifier)
dateFormatter.dateTimeStyle = .numeric dateFormatter.dateTimeStyle = .numeric
@ -53,7 +58,7 @@ struct WalletInformationView: View {
struct WalletInformationView_Previews: PreviewProvider { struct WalletInformationView_Previews: PreviewProvider {
static var previews: some View { 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)) .previewContext(WidgetPreviewContext(family: .systemSmall))
} }
} }

View File

@ -17,7 +17,7 @@ struct Provider: TimelineProvider {
func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) { func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) {
let entry: SimpleEntry let entry: SimpleEntry
if (context.isPreview) { 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 { } else {
entry = SimpleEntry(date: Date(), marketData: emptyMarketData) entry = SimpleEntry(date: Date(), marketData: emptyMarketData)
} }
@ -107,9 +107,9 @@ struct WalletInformationAndMarketWidget: Widget {
struct WalletInformationAndMarketWidget_Previews: PreviewProvider { struct WalletInformationAndMarketWidget_Previews: PreviewProvider {
static var previews: some View { 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)) .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)) .previewContext(WidgetPreviewContext(family: .systemLarge))
} }
} }