Skip to content
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

Add local notifications and listen for challenge requests #902

Merged
merged 199 commits into from
Oct 2, 2024

Conversation

incogg
Copy link
Contributor

@incogg incogg commented Aug 5, 2024

Closes #432
Closes #438
Closes #439

Adds an api for interacting with the flutter_local_notifications package
Adds persistant websocket connection to listen for challenges in the form of a notifierProvider
Adds a screen to show, accept, and cancel incoming and outgoing challenges, both realtime and correspondance

Committing the locales makes the PR huge rip

EDIT:

@incogg incogg changed the title Feat/challenges Add local notifications and listen for challenge requests Aug 5, 2024
@veloce
Copy link
Contributor

veloce commented Aug 6, 2024

Whoa! This is a lot of work, I did not expect to receive such a PR :)

I'll take the time to review it properly.

I don't think adding a new translation was necessary here. As explained in the doc, we should not translate new strings for new feature right away.

Could you also solve the conflicts please? If you remove the added translation, only the ios/Runner/AppDelegate.swift conflict should be left.

@veloce
Copy link
Contributor

veloce commented Aug 6, 2024

Also, can you post a screen recording showcasing this new feature? Thanks.

@incogg
Copy link
Contributor Author

incogg commented Aug 6, 2024

Resolving that was a pain

@incogg
Copy link
Contributor Author

incogg commented Aug 6, 2024

There is a bug that I am aware of but I don't really have a good solution for at the moment, I would appreciate some advice.

When sending a notification with flutter_local_notifications, if the notification was interacted with in a way that doesnt open the application, for example when the user presses decline on the challenge notification, the resulting callback is spawned in a background isolate. From the background isolate I can send a request to the server to decline the challenge, but I have no idea how to go about updating the state of the application for when the user goes back to it. This results in declining a challenge with the notification and then it still showing in the challenges list as the state of the challengesProvider hasnt been updated.

Do you have any idea how I could send a message from the background isolate to the main isolate, I have looked around and I cannot find any method of doing so. Perhaps I am just approaching the problem the wrong way.

Would love to hear your thoughts Veloce.

@incogg
Copy link
Contributor Author

incogg commented Aug 6, 2024

I'll record the videos and see if i can fix the checks failing tomorrow 😃.

@ijm8710
Copy link

ijm8710 commented Aug 6, 2024

Hey @incogg do you think the issue you described would be similar to the one mentioned in my first bullet here (the first reply by me in that issue ticket)?

I've outlined a few correspondence game issues for some time and I feel it matches what you outlined

@incogg
Copy link
Contributor Author

incogg commented Aug 7, 2024

There is a bug that I am aware of but I don't really have a good solution for at the moment, I would appreciate some advice.

When sending a notification with flutter_local_notifications, if the notification was interacted with in a way that doesnt open the application, for example when the user presses decline on the challenge notification, the resulting callback is spawned in a background isolate. From the background isolate I can send a request to the server to decline the challenge, but I have no idea how to go about updating the state of the application for when the user goes back to it. This results in declining a challenge with the notification and then it still showing in the challenges list as the state of the challengesProvider hasnt been updated.

Do you have any idea how I could send a message from the background isolate to the main isolate, I have looked around and I cannot find any method of doing so. Perhaps I am just approaching the problem the wrong way.

Would love to hear your thoughts Veloce.

Nevermind the IsolateNameServer exists and i am aware of it now.
Working on a fix

@incogg
Copy link
Contributor Author

incogg commented Aug 7, 2024

Hey @incogg do you think the issue you described would be similar to the one mentioned in my first bullet here (the first reply by me in that issue ticket)?

I've outlined a few correspondence game issues for some time and I feel it matches what you outlined

Doesnt seem related no

@incogg incogg marked this pull request as draft August 7, 2024 07:58
@veloce
Copy link
Contributor

veloce commented Aug 7, 2024

Do you have any idea how I could send a message from the background isolate to the main isolate, I have looked around and I cannot find any method of doing so. Perhaps I am just approaching the problem the wrong way.

I cannot answer you atm sorry, and I don't know how to send a message to the main isolate. I will need to check this more thoroughly but I think the solution to this problem is to react to the app lifecycle events. We need to update the challenge list each time the app goes back to foreground (or is started)).

Oh I'm just seeing your next answer about IsolateNameServer as I'm writing this. I still think the easiest way is app lifecycle events. Sending messages between isolates looks too complex to me.

Copy link
Contributor

@veloce veloce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a stylistic review for now. I'll review the rest asap.

lib/l10n/app_en.arb Outdated Show resolved Hide resolved
translation/source/mobile.xml Outdated Show resolved Hide resolved
@incogg
Copy link
Contributor Author

incogg commented Aug 7, 2024

Do you have any idea how I could send a message from the background isolate to the main isolate, I have looked around and I cannot find any method of doing so. Perhaps I am just approaching the problem the wrong way.

I cannot answer you atm sorry, and I don't know how to send a message to the main isolate. I will need to check this more thoroughly but I think the solution to this problem is to react to the app lifecycle events. We need to update the challenge list each time the app goes back to foreground (or is started)).

Oh I'm just seeing your next answer about IsolateNameServer as I'm writing this. I still think the easiest way is app lifecycle events. Sending messages between isolates looks too complex to me.

Sounds like that would work in this case.
I feel like messaging the main isolate is a little more flexible as you can just register any old callback to modify the applications state.
Seems like this way writing different types of notifications in future will be simpler.

@incogg
Copy link
Contributor Author

incogg commented Aug 7, 2024

2024-08-08.04-40-06.mp4

Here is a video of the feature

Copy link
Contributor

@veloce veloce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again for your efforts!

Most the review is about renaming but the most important aspect is about riverpod usage which should be refactored (cf review comments).

android/app/src/main/AndroidManifest.xml Outdated Show resolved Hide resolved
lib/src/model/challenge/challenge.dart Show resolved Hide resolved
lib/src/model/challenge/challenge_repository.dart Outdated Show resolved Hide resolved
lib/src/model/challenge/challenge_repository.dart Outdated Show resolved Hide resolved
lib/src/model/challenge/challenge_repository.dart Outdated Show resolved Hide resolved
lib/src/view/play/challenge_screen.dart Show resolved Hide resolved
lib/src/widgets/challenge_display.dart Outdated Show resolved Hide resolved
lib/src/model/challenge/challenge_repository.dart Outdated Show resolved Hide resolved
@incogg incogg marked this pull request as ready for review August 9, 2024 06:48
@incogg
Copy link
Contributor Author

incogg commented Aug 9, 2024

Thanks for all the feedback Veloce, this is the first time I have ever used flutter or riverpod, it seems like I didn't fully comprehend how to use riverpod correctly. I feel like I have a much better grasp now.

@incogg
Copy link
Contributor Author

incogg commented Aug 9, 2024

2024-08-09.16-53-47.mp4

And here is a video showing the now enabled user challenges

Copy link
Contributor

@veloce veloce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is getting in the right direction, thanks again for your work!

lib/src/app.dart Outdated Show resolved Hide resolved
lib/src/app.dart Outdated Show resolved Hide resolved
lib/src/app.dart Outdated Show resolved Hide resolved
lib/src/app.dart Outdated Show resolved Hide resolved
lib/src/app.dart Outdated Show resolved Hide resolved
lib/src/view/play/challenge_screen.dart Outdated Show resolved Hide resolved
lib/src/widgets/challenge_display.dart Outdated Show resolved Hide resolved
@incogg
Copy link
Contributor Author

incogg commented Aug 18, 2024

okay i think i have solved all the problems.

i got rid of the messaging between isolates and i fixed the problems with not having access to refs by creating a separate riverpod provider scope that exists on the background isolate

no more dealing with threads and the notification declarations are all in one file

@incogg incogg requested a review from veloce August 19, 2024 14:23
Copy link
Contributor

@veloce veloce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicely done with the ProviderScope. Thanks! Now there are a couple of things to fix related to the socket listening.

lib/src/app.dart Outdated Show resolved Hide resolved
lib/src/model/challenge/challenge_repository.dart Outdated Show resolved Hide resolved
lib/src/model/challenge/challenges.dart Outdated Show resolved Hide resolved
lib/src/model/challenge/challenges.dart Outdated Show resolved Hide resolved
lib/src/model/lobby/create_game_service.dart Outdated Show resolved Hide resolved
lib/src/model/notifications/challenge_notification.dart Outdated Show resolved Hide resolved
lib/src/utils/system.dart Show resolved Hide resolved
lib/src/widgets/challenge_list_item.dart Outdated Show resolved Hide resolved
lib/src/widgets/challenge_list_item.dart Outdated Show resolved Hide resolved
@veloce
Copy link
Contributor

veloce commented Aug 28, 2024

I must thank you again for your work and your patience @incogg , this has probably taken longer than expected on your side. This feature is essential to the app and is quite tricky, that's why the review process takes more time here. But I think we're heading in the right direction now.

@veloce
Copy link
Contributor

veloce commented Sep 26, 2024

To see the correct diff without extra main commits: main...incogg:mobile:feat/challenges

@veloce veloce merged commit af1328c into lichess-org:main Oct 2, 2024
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
7 participants