-
Notifications
You must be signed in to change notification settings - Fork 3
fix: handle cjit channel ready notification #787
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
8296006
chore: create channel ready handler
jvsena42 37fc97d
fix: handle channel ready
jvsena42 345fca8
refactor: implement notifyChannelReadyHandler on AppViewModel.kt
jvsena42 21f9fd0
Merge branch 'master' into fix/channelready-cjit
jvsena42 bbb9a61
Merge branch 'master' into fix/channelready-cjit
jvsena42 4994d96
Merge branch 'master' into fix/channelready-cjit
jvsena42 91cb76c
Merge remote-tracking branch 'origin/master' into fix/channelready-cjit
jvsena42 cb7669d
fix: deduplicate channel ready notification
jvsena42 4e5649e
fix: deduplicate regular sent
jvsena42 73368bf
Merge branch 'master' into fix/channelready-cjit
jvsena42 b758da9
fix: check duplicated activity notification
jvsena42 b5d5385
Merge branch 'master' into fix/channelready-cjit
jvsena42 52fdc8b
Merge branch 'master' into fix/channelready-cjit
jvsena42 b668182
refactor: unify receive notification formating logic
jvsena42 af747ed
Merge branch 'master' into fix/channelready-cjit
jvsena42 e256a08
fix: apply formatting to WakeNodeWorker.onCjitChannelReady
jvsena42 5ec67b6
chore: update journeys with rich format
jvsena42 500285d
fix: cjit notification race condition
jvsena42 73e576f
refactor: inject node service state
jvsena42 3bcc9ff
fix: return early when the app is in foreground
jvsena42 10f3322
refactor: rename foreground state class
jvsena42 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
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
26 changes: 26 additions & 0 deletions
26
app/src/main/java/to/bitkit/domain/commands/NotifyChannelReady.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,26 @@ | ||
| package to.bitkit.domain.commands | ||
|
|
||
| import org.lightningdevkit.ldknode.Event | ||
| import to.bitkit.models.NewTransactionSheetDetails | ||
| import to.bitkit.models.NotificationDetails | ||
|
|
||
| sealed interface NotifyChannelReady { | ||
|
|
||
| data class Command( | ||
| val event: Event.ChannelReady, | ||
| val includeNotification: Boolean = false, | ||
| ) : NotifyChannelReady | ||
|
|
||
| sealed interface Result : NotifyChannelReady { | ||
| data class ShowSheet( | ||
| val sheet: NewTransactionSheetDetails, | ||
| ) : Result | ||
|
|
||
| data class ShowNotification( | ||
| val sheet: NewTransactionSheetDetails, | ||
| val notification: NotificationDetails, | ||
| ) : Result | ||
|
|
||
| data object Skip : Result | ||
| } | ||
| } |
99 changes: 99 additions & 0 deletions
99
app/src/main/java/to/bitkit/domain/commands/NotifyChannelReadyHandler.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,99 @@ | ||
| package to.bitkit.domain.commands | ||
|
|
||
| import android.content.Context | ||
| import dagger.hilt.android.qualifiers.ApplicationContext | ||
| import kotlinx.coroutines.CoroutineDispatcher | ||
| import kotlinx.coroutines.flow.first | ||
| import kotlinx.coroutines.withContext | ||
| import to.bitkit.R | ||
| import to.bitkit.data.SettingsData | ||
| import to.bitkit.data.SettingsStore | ||
| import to.bitkit.di.IoDispatcher | ||
| import to.bitkit.ext.amountOnClose | ||
| import to.bitkit.models.BITCOIN_SYMBOL | ||
| import to.bitkit.models.NewTransactionSheetDetails | ||
| import to.bitkit.models.NewTransactionSheetDirection | ||
| import to.bitkit.models.NewTransactionSheetType | ||
| import to.bitkit.models.NotificationDetails | ||
| import to.bitkit.models.PrimaryDisplay | ||
| import to.bitkit.models.formatToModernDisplay | ||
| import to.bitkit.repositories.ActivityRepo | ||
| import to.bitkit.repositories.BlocktankRepo | ||
| import to.bitkit.repositories.CurrencyRepo | ||
| import to.bitkit.repositories.LightningRepo | ||
| import to.bitkit.utils.Logger | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| @Suppress("LongParameterList") | ||
| @Singleton | ||
| class NotifyChannelReadyHandler @Inject constructor( | ||
| @ApplicationContext private val context: Context, | ||
| @IoDispatcher private val ioDispatcher: CoroutineDispatcher, | ||
| private val lightningRepo: LightningRepo, | ||
| private val blocktankRepo: BlocktankRepo, | ||
| private val activityRepo: ActivityRepo, | ||
| private val currencyRepo: CurrencyRepo, | ||
| private val settingsStore: SettingsStore, | ||
| ) { | ||
| companion object { | ||
| const val TAG = "NotifyChannelReadyHandler" | ||
| } | ||
|
|
||
| suspend operator fun invoke( | ||
| command: NotifyChannelReady.Command, | ||
| ): Result<NotifyChannelReady.Result> = withContext(ioDispatcher) { | ||
| runCatching { | ||
| val channel = lightningRepo.getChannels() | ||
| ?.find { it.channelId == command.event.channelId } | ||
| ?: return@runCatching NotifyChannelReady.Result.Skip | ||
|
|
||
| val cjitEntry = blocktankRepo.getCjitEntry(channel) | ||
| ?: return@runCatching NotifyChannelReady.Result.Skip | ||
|
|
||
| val sats = channel.amountOnClose.toLong() | ||
| activityRepo.insertActivityFromCjit(cjitEntry = cjitEntry, channel = channel) | ||
|
|
||
| val details = NewTransactionSheetDetails( | ||
| type = NewTransactionSheetType.LIGHTNING, | ||
| direction = NewTransactionSheetDirection.RECEIVED, | ||
| sats = sats, | ||
| ) | ||
|
|
||
| if (command.includeNotification) { | ||
| val notification = buildNotificationContent(sats) | ||
| NotifyChannelReady.Result.ShowNotification(details, notification) | ||
| } else { | ||
| NotifyChannelReady.Result.ShowSheet(details) | ||
| } | ||
| }.onFailure { | ||
| Logger.error("Failed to process channel ready notification", it, context = TAG) | ||
| } | ||
| } | ||
|
|
||
| private suspend fun buildNotificationContent(sats: Long): NotificationDetails { | ||
| val settings = settingsStore.data.first() | ||
| val title = context.getString(R.string.notification__received__title) | ||
| val body = if (settings.showNotificationDetails) { | ||
| formatNotificationAmount(sats, settings) | ||
| } else { | ||
| context.getString(R.string.notification__received__body_hidden) | ||
| } | ||
| return NotificationDetails(title, body) | ||
| } | ||
|
|
||
| private fun formatNotificationAmount(sats: Long, settings: SettingsData): String { | ||
| val converted = currencyRepo.convertSatsToFiat(sats).getOrNull() | ||
|
|
||
| val amountText = converted?.let { | ||
| val btcDisplay = it.bitcoinDisplay(settings.displayUnit) | ||
|
ovitrif marked this conversation as resolved.
Outdated
|
||
| if (settings.primaryDisplay == PrimaryDisplay.BITCOIN) { | ||
| "${btcDisplay.symbol} ${btcDisplay.value} (${it.formattedWithSymbol()})" | ||
| } else { | ||
| "${it.formattedWithSymbol()} (${btcDisplay.symbol} ${btcDisplay.value})" | ||
| } | ||
| } ?: "$BITCOIN_SYMBOL ${sats.formatToModernDisplay()}" | ||
|
|
||
| return context.getString(R.string.notification__received__body_amount, amountText) | ||
| } | ||
| } | ||
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.