-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add pending payment notifications #828
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ovitrif
wants to merge
19
commits into
feat/send-pending-ui
Choose a base branch
from
feat/send-pending-ui-notif
base: feat/send-pending-ui
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+777
−203
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
002f554
feat: add pending payment query
ovitrif 06773dd
feat: add pending resolution command
ovitrif 734c366
feat: notify pending payment resolution
ovitrif b7055ee
refactor: extract pending payment repo
ovitrif 717b8a4
refactor: use new guard conditions for watchUntil
ovitrif f34f18c
test: pending payments logic
ovitrif 1b72226
fix: non-service notifications icon
ovitrif af2fb39
fix: pending payment error notification text
ovitrif 6343887
refactor: shared notification model and repo state
ovitrif 78c8a51
fix: remove pending error sheet message
ovitrif 59a688d
refactor: remove redundant resolution wrapper
ovitrif c50fea5
refactor: simplify
ovitrif 1b71bb5
refactor: use context ext prop
ovitrif 5f96938
chore: update /pr ai command description
ovitrif 27a378b
fix: active hash clearing bug and review issues
ovitrif becd509
refactor: extract PendingPaymentNotification
ovitrif 8de49b3
refactor: cleanup vm test
ovitrif be29098
fix: resolve pr review comments
ovitrif 6ceb732
chore: update ai rule for docs
ovitrif File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
27 changes: 27 additions & 0 deletions
27
app/src/main/java/to/bitkit/domain/commands/NotifyPendingPaymentResolved.kt
This file contains hidden or 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,27 @@ | ||
| package to.bitkit.domain.commands | ||
|
|
||
| import org.lightningdevkit.ldknode.Event | ||
| import to.bitkit.models.NotificationDetails | ||
|
|
||
| sealed interface NotifyPendingPaymentResolved { | ||
|
|
||
| sealed interface Command : NotifyPendingPaymentResolved { | ||
| val paymentHash: String | ||
|
|
||
| data class Success(override val paymentHash: String) : Command | ||
| data class Failure(override val paymentHash: String) : Command | ||
|
|
||
| companion object { | ||
ovitrif marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| fun from(event: Event): Command? = when (event) { | ||
| is Event.PaymentSuccessful -> Success(event.paymentHash) | ||
| is Event.PaymentFailed -> event.paymentHash?.let { Failure(it) } | ||
| else -> null | ||
| } | ||
| } | ||
| } | ||
|
|
||
| sealed interface Result : NotifyPendingPaymentResolved { | ||
| data class ShowNotification(val notification: NotificationDetails) : Result | ||
| data object Skip : Result | ||
| } | ||
| } | ||
44 changes: 44 additions & 0 deletions
44
app/src/main/java/to/bitkit/domain/commands/NotifyPendingPaymentResolvedHandler.kt
This file contains hidden or 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,44 @@ | ||
| package to.bitkit.domain.commands | ||
|
|
||
| import android.content.Context | ||
| import dagger.hilt.android.qualifiers.ApplicationContext | ||
| import kotlinx.coroutines.CoroutineDispatcher | ||
| import kotlinx.coroutines.withContext | ||
| import to.bitkit.di.IoDispatcher | ||
| import to.bitkit.repositories.PendingPaymentNotification | ||
| import to.bitkit.repositories.PendingPaymentRepo | ||
| import to.bitkit.utils.Logger | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| @Singleton | ||
| class NotifyPendingPaymentResolvedHandler @Inject constructor( | ||
| @ApplicationContext private val context: Context, | ||
| @IoDispatcher private val ioDispatcher: CoroutineDispatcher, | ||
| private val pendingPaymentRepo: PendingPaymentRepo, | ||
| ) { | ||
| companion object { | ||
| const val TAG = "NotifyPendingPaymentResolvedHandler" | ||
| } | ||
|
|
||
| suspend operator fun invoke( | ||
| command: NotifyPendingPaymentResolved.Command, | ||
| ): Result<NotifyPendingPaymentResolved.Result> = withContext(ioDispatcher) { | ||
| runCatching { | ||
| if (!pendingPaymentRepo.isPending(command.paymentHash)) { | ||
ovitrif marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return@runCatching NotifyPendingPaymentResolved.Result.Skip | ||
| } | ||
| val notification = buildNotificationContent(command) | ||
| NotifyPendingPaymentResolved.Result.ShowNotification(notification) | ||
| }.onFailure { | ||
| Logger.error("Failed to process pending payment notification", it, context = TAG) | ||
| } | ||
| } | ||
|
|
||
| private fun buildNotificationContent( | ||
| command: NotifyPendingPaymentResolved.Command, | ||
| ) = when (command) { | ||
| is NotifyPendingPaymentResolved.Command.Success -> PendingPaymentNotification.success(context) | ||
| is NotifyPendingPaymentResolved.Command.Failure -> PendingPaymentNotification.error(context) | ||
| } | ||
| } | ||
This file contains hidden or 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
64 changes: 64 additions & 0 deletions
64
app/src/main/java/to/bitkit/repositories/PendingPaymentRepo.kt
This file contains hidden or 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,64 @@ | ||
| package to.bitkit.repositories | ||
|
|
||
| import android.content.Context | ||
| import kotlinx.coroutines.flow.MutableSharedFlow | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.asSharedFlow | ||
| import kotlinx.coroutines.flow.asStateFlow | ||
| import kotlinx.coroutines.flow.update | ||
| import to.bitkit.R | ||
| import to.bitkit.models.NotificationDetails | ||
| import to.bitkit.utils.AppError | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| @Singleton | ||
| class PendingPaymentRepo @Inject constructor() { | ||
|
|
||
| private val _state = MutableStateFlow(PendingPaymentsState()) | ||
| val state = _state.asStateFlow() | ||
|
|
||
| private val _resolution = MutableSharedFlow<PendingPaymentResolution>(extraBufferCapacity = 1) | ||
| val resolution = _resolution.asSharedFlow() | ||
|
|
||
| fun track(paymentHash: String) { | ||
| _state.update { it.copy(pendingPayments = it.pendingPayments + paymentHash) } | ||
| } | ||
|
|
||
| fun isPending(hash: String): Boolean = _state.value.pendingPayments.contains(hash) | ||
|
|
||
| fun resolve(resolution: PendingPaymentResolution) { | ||
| _state.update { it.copy(pendingPayments = it.pendingPayments - resolution.paymentHash) } | ||
| _resolution.tryEmit(resolution) | ||
| } | ||
|
|
||
| fun setActiveHash(hash: String?) = _state.update { it.copy(activeHash = hash) } | ||
|
|
||
| fun isActive(hash: String): Boolean = _state.value.activeHash == hash | ||
| } | ||
|
|
||
| data class PendingPaymentsState( | ||
| val pendingPayments: Set<String> = emptySet(), | ||
| val activeHash: String? = null, | ||
| ) | ||
|
|
||
| class PaymentPendingException(val paymentHash: String) : AppError("Payment pending") | ||
|
|
||
| sealed interface PendingPaymentResolution { | ||
| val paymentHash: String | ||
|
|
||
| data class Success(override val paymentHash: String) : PendingPaymentResolution | ||
| data class Failure(override val paymentHash: String) : PendingPaymentResolution | ||
| } | ||
ovitrif marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| object PendingPaymentNotification { | ||
| fun success(context: Context) = NotificationDetails( | ||
| title = context.getString(R.string.wallet__toast_payment_sent_title), | ||
| body = context.getString(R.string.wallet__toast_payment_sent_description), | ||
| ) | ||
|
|
||
| fun error(context: Context) = NotificationDetails( | ||
| title = context.getString(R.string.wallet__toast_payment_failed_title), | ||
| body = context.getString(R.string.wallet__toast_payment_failed_description), | ||
| ) | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.