Skip to content

Commit b1dabea

Browse files
committed
refactor: use activity factories w. default values
1 parent 6192d82 commit b1dabea

File tree

9 files changed

+142
-212
lines changed

9 files changed

+142
-212
lines changed

app/src/main/java/to/bitkit/ext/Activities.kt

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package to.bitkit.ext
22

33
import com.synonym.bitkitcore.Activity
4+
import com.synonym.bitkitcore.LightningActivity
5+
import com.synonym.bitkitcore.OnchainActivity
46
import com.synonym.bitkitcore.PaymentState
57
import com.synonym.bitkitcore.PaymentType
68

@@ -70,3 +72,75 @@ fun Activity.Onchain.boostType() = when (this.v1.txType) {
7072
}
7173

7274
enum class BoostType { RBF, CPFP }
75+
76+
@Suppress("LongParameterList")
77+
fun LightningActivity.Companion.create(
78+
id: String,
79+
txType: PaymentType,
80+
status: PaymentState,
81+
value: ULong,
82+
invoice: String,
83+
timestamp: ULong,
84+
fee: ULong = 0u,
85+
message: String = "",
86+
preimage: String? = null,
87+
createdAt: ULong? = timestamp,
88+
updatedAt: ULong? = createdAt,
89+
seenAt: ULong? = null,
90+
) = LightningActivity(
91+
id = id,
92+
txType = txType,
93+
status = status,
94+
value = value,
95+
fee = fee,
96+
invoice = invoice,
97+
message = message,
98+
timestamp = timestamp,
99+
preimage = preimage,
100+
createdAt = createdAt,
101+
updatedAt = updatedAt,
102+
seenAt = seenAt,
103+
)
104+
105+
@Suppress("LongParameterList")
106+
fun OnchainActivity.Companion.create(
107+
id: String,
108+
txType: PaymentType,
109+
txId: String,
110+
value: ULong,
111+
fee: ULong,
112+
address: String,
113+
timestamp: ULong,
114+
confirmed: Boolean = false,
115+
feeRate: ULong = 1u,
116+
isBoosted: Boolean = false,
117+
boostTxIds: List<String> = emptyList(),
118+
isTransfer: Boolean = false,
119+
doesExist: Boolean = true,
120+
confirmTimestamp: ULong? = null,
121+
channelId: String? = null,
122+
transferTxId: String? = null,
123+
createdAt: ULong? = timestamp,
124+
updatedAt: ULong? = createdAt,
125+
seenAt: ULong? = null,
126+
) = OnchainActivity(
127+
id = id,
128+
txType = txType,
129+
txId = txId,
130+
value = value,
131+
fee = fee,
132+
feeRate = feeRate,
133+
address = address,
134+
confirmed = confirmed,
135+
timestamp = timestamp,
136+
isBoosted = isBoosted,
137+
boostTxIds = boostTxIds,
138+
isTransfer = isTransfer,
139+
doesExist = doesExist,
140+
confirmTimestamp = confirmTimestamp,
141+
channelId = channelId,
142+
transferTxId = transferTxId,
143+
createdAt = createdAt,
144+
updatedAt = updatedAt,
145+
seenAt = seenAt,
146+
)

app/src/main/java/to/bitkit/services/CoreService.kt

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import to.bitkit.async.ServiceQueue
6969
import to.bitkit.data.CacheStore
7070
import to.bitkit.env.Env
7171
import to.bitkit.ext.amountSats
72+
import to.bitkit.ext.create
7273
import to.bitkit.models.toCoreNetwork
7374
import to.bitkit.utils.AppError
7475
import to.bitkit.utils.Logger
@@ -473,18 +474,16 @@ class ActivityService(
473474
status = state
474475
)
475476
} else {
476-
LightningActivity(
477+
LightningActivity.create(
477478
id = payment.id,
478479
txType = payment.direction.toPaymentType(),
479480
status = state,
480481
value = payment.amountSats ?: 0u,
481-
fee = (payment.feePaidMsat ?: 0u) / 1000u,
482482
invoice = kind.bolt11 ?: "Loading...",
483-
message = kind.description.orEmpty(),
484483
timestamp = payment.latestUpdateTimestamp,
484+
fee = (payment.feePaidMsat ?: 0u) / 1000u,
485+
message = kind.description.orEmpty(),
485486
preimage = kind.preimage,
486-
createdAt = payment.latestUpdateTimestamp,
487-
updatedAt = payment.latestUpdateTimestamp,
488487
seenAt = null, // TODO implement synonymdev/bitkit-ios#270 changes
489488
)
490489
}
@@ -598,25 +597,18 @@ class ActivityService(
598597
): OnchainActivity {
599598
val isTransfer = channelId != null
600599

601-
return OnchainActivity(
600+
return OnchainActivity.create(
602601
id = payment.id,
603602
txType = payment.direction.toPaymentType(),
604603
txId = kind.txid,
605604
value = payment.amountSats ?: 0u,
606605
fee = (payment.feePaidMsat ?: 0u) / 1000u,
607-
feeRate = 1u,
608606
address = resolvedAddress ?: "Loading...",
609-
confirmed = confirmationData.isConfirmed,
610607
timestamp = confirmationData.timestamp,
611-
isBoosted = false,
612-
boostTxIds = emptyList(),
608+
confirmed = confirmationData.isConfirmed,
613609
isTransfer = isTransfer,
614-
doesExist = true,
615610
confirmTimestamp = confirmationData.confirmedTimestamp,
616611
channelId = channelId,
617-
transferTxId = null,
618-
createdAt = confirmationData.timestamp,
619-
updatedAt = confirmationData.timestamp,
620612
seenAt = null, // TODO implement synonymdev/bitkit-ios#270 changes
621613
)
622614
}
@@ -728,44 +720,35 @@ class ActivityService(
728720
if (isLightning) {
729721
id = "test-lightning-$i"
730722
activity = Activity.Lightning(
731-
LightningActivity(
723+
LightningActivity.create(
732724
id = id,
733725
txType = txType,
734726
status = status,
735727
value = value,
736-
fee = (1..1_000).random().toULong(),
737728
invoice = "lnbc$value",
738-
message = possibleMessages.random(),
739729
timestamp = txTimestamp,
730+
fee = (1..1_000).random().toULong(),
731+
message = possibleMessages.random(),
740732
preimage = if (Random.nextBoolean()) "preimage$i" else null,
741-
createdAt = txTimestamp,
742-
updatedAt = txTimestamp,
743-
seenAt = null, // TODO implement synonymdev/bitkit-ios#270 changes
744733
)
745734
)
746735
} else {
747736
id = "test-onchain-$i"
748737
activity = Activity.Onchain(
749-
OnchainActivity(
738+
OnchainActivity.create(
750739
id = id,
751740
txType = txType,
752741
txId = "a".repeat(64), // Mock txid
753742
value = value,
754743
fee = (100..10_000).random().toULong(),
755-
feeRate = (1..100).random().toULong(),
756744
address = "bc1...$i",
757-
confirmed = Random.nextBoolean(),
758745
timestamp = txTimestamp,
746+
confirmed = Random.nextBoolean(),
747+
feeRate = (1..100).random().toULong(),
759748
isBoosted = Random.nextBoolean(),
760-
boostTxIds = emptyList(),
761749
isTransfer = Random.nextBoolean(),
762-
doesExist = true,
763750
confirmTimestamp = if (Random.nextBoolean()) txTimestamp + 3600.toULong() else null,
764751
channelId = if (Random.nextBoolean()) "channel$i" else null,
765-
transferTxId = null,
766-
createdAt = txTimestamp,
767-
updatedAt = txTimestamp,
768-
seenAt = null, // TODO implement synonymdev/bitkit-ios#270 changes
769752
)
770753
)
771754
}

app/src/main/java/to/bitkit/ui/screens/wallets/activity/ActivityDetailScreen.kt

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import com.synonym.bitkitcore.OnchainActivity
4646
import com.synonym.bitkitcore.PaymentState
4747
import com.synonym.bitkitcore.PaymentType
4848
import to.bitkit.R
49+
import to.bitkit.ext.create
4950
import to.bitkit.ext.ellipsisMiddle
5051
import to.bitkit.ext.isSent
5152
import to.bitkit.ext.isTransfer
@@ -768,19 +769,15 @@ private fun PreviewLightningSent() {
768769
AppThemeSurface {
769770
ActivityDetailContent(
770771
item = Activity.Lightning(
771-
v1 = LightningActivity(
772+
v1 = LightningActivity.create(
772773
id = "test-lightning-1",
773774
txType = PaymentType.SENT,
774775
status = PaymentState.SUCCEEDED,
775776
value = 50000UL,
776-
fee = 1UL,
777777
invoice = "lnbc...",
778-
message = "Thanks for paying at the bar. Here's my share.",
779778
timestamp = (System.currentTimeMillis() / 1000).toULong(),
780-
preimage = null,
781-
createdAt = null,
782-
updatedAt = null,
783-
seenAt = null,
779+
fee = 1UL,
780+
message = "Thanks for paying at the bar. Here's my share.",
784781
)
785782
),
786783
tags = listOf("Lunch", "Drinks"),
@@ -800,26 +797,17 @@ private fun PreviewOnchain() {
800797
AppThemeSurface {
801798
ActivityDetailContent(
802799
item = Activity.Onchain(
803-
v1 = OnchainActivity(
800+
v1 = OnchainActivity.create(
804801
id = "test-onchain-1",
805802
txType = PaymentType.RECEIVED,
806803
txId = "abc123",
807804
value = 100000UL,
808805
fee = 500UL,
809-
feeRate = 8UL,
810806
address = "bc1...",
811-
confirmed = true,
812807
timestamp = (System.currentTimeMillis() / 1000 - 3600).toULong(),
813-
isBoosted = false,
814-
boostTxIds = emptyList(),
815-
isTransfer = false,
816-
doesExist = true,
808+
confirmed = true,
809+
feeRate = 8UL,
817810
confirmTimestamp = (System.currentTimeMillis() / 1000).toULong(),
818-
channelId = null,
819-
transferTxId = null,
820-
createdAt = null,
821-
updatedAt = null,
822-
seenAt = null,
823811
)
824812
),
825813
tags = emptyList(),
@@ -842,19 +830,15 @@ private fun PreviewSheetSmallScreen() {
842830
) {
843831
ActivityDetailContent(
844832
item = Activity.Lightning(
845-
v1 = LightningActivity(
833+
v1 = LightningActivity.create(
846834
id = "test-lightning-1",
847835
txType = PaymentType.SENT,
848836
status = PaymentState.SUCCEEDED,
849837
value = 50000UL,
850-
fee = 1UL,
851838
invoice = "lnbc...",
852-
message = "Thanks for paying at the bar. Here's my share.",
853839
timestamp = (System.currentTimeMillis() / 1000).toULong(),
854-
preimage = null,
855-
createdAt = null,
856-
updatedAt = null,
857-
seenAt = null,
840+
fee = 1UL,
841+
message = "Thanks for paying at the bar. Here's my share.",
858842
)
859843
),
860844
tags = listOf("Lunch", "Drinks"),

app/src/main/java/to/bitkit/ui/screens/wallets/activity/ActivityExploreScreen.kt

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import com.synonym.bitkitcore.PaymentState
4040
import com.synonym.bitkitcore.PaymentType
4141
import org.lightningdevkit.ldknode.TransactionDetails
4242
import to.bitkit.R
43+
import to.bitkit.ext.create
4344
import to.bitkit.ext.ellipsisMiddle
4445
import to.bitkit.ext.isSent
4546
import to.bitkit.ext.rawId
@@ -328,19 +329,16 @@ private fun PreviewLightning() {
328329
AppThemeSurface {
329330
ActivityExploreContent(
330331
item = Activity.Lightning(
331-
v1 = LightningActivity(
332+
v1 = LightningActivity.create(
332333
id = "test-lightning-1",
333334
txType = PaymentType.SENT,
334335
status = PaymentState.SUCCEEDED,
335336
value = 50000UL,
336-
fee = 1UL,
337337
invoice = "lnbc...",
338-
message = "Thanks for paying at the bar. Here's my share.",
339338
timestamp = (System.currentTimeMillis() / 1000).toULong(),
339+
fee = 1UL,
340+
message = "Thanks for paying at the bar. Here's my share.",
340341
preimage = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
341-
createdAt = null,
342-
updatedAt = null,
343-
seenAt = null,
344342
),
345343
),
346344
)
@@ -353,26 +351,17 @@ private fun PreviewOnchain() {
353351
AppThemeSurface {
354352
ActivityExploreContent(
355353
item = Activity.Onchain(
356-
v1 = OnchainActivity(
354+
v1 = OnchainActivity.create(
357355
id = "test-onchain-1",
358356
txType = PaymentType.RECEIVED,
359357
txId = "abc123",
360358
value = 100000UL,
361359
fee = 500UL,
362-
feeRate = 8UL,
363360
address = "bc1...",
364-
confirmed = true,
365361
timestamp = (System.currentTimeMillis() / 1000 - 3600).toULong(),
366-
isBoosted = false,
367-
boostTxIds = emptyList(),
368-
isTransfer = false,
369-
doesExist = true,
362+
confirmed = true,
363+
feeRate = 8UL,
370364
confirmTimestamp = (System.currentTimeMillis() / 1000).toULong(),
371-
channelId = null,
372-
transferTxId = null,
373-
createdAt = null,
374-
updatedAt = null,
375-
seenAt = null,
376365
),
377366
),
378367
)

0 commit comments

Comments
 (0)