Create a reaction system for post in an android app - java

I am developing a social network in android studio with the java programming language and in turn I use Firebase Firestore database as a database. I want to implement a system of reactions to the publications that are made in the app, but I can not find a way that when the user presses the reaction button, the reaction is saved in the corresponding publication.
Then I want that at the time of pressing the reaction button the value of the likes is updated to the correct publication
I made a small code but it only updates the value of the likes to the publication that I indicate by code
Here I pass the collection, the unique identifier of the publication and update the value of its likes
So instead of putting that instruction by code, I want the post ID to be automatically detected and able to react to any post and update its values.

Related

Can I combine SharedPreferences and Firestore when saving data?

I'm a beginner developer and I'm currently developing a Quiz app.
Each Quiz contains 50 questions and I'm storing them using Room database.
I've got Coins, Stars, and the score and number of finished questions for each Quiz (And also user's information) that I want to save.
And I still don't know what is the best method to do that.
I was thinking about combining SharedPreferences and Firestore to save user's progress and information, SharedPreferences to save them locally, and Firestore so that when the user signs in on another device, all his progress and information will be retrieved (and saved in SharedPref).
(**Data will be saved in Firestore when the user only signs in with Google)
Now my Questions is :
Can I combine SharedPref and Firestore when saving data? and is it a good idea to do so?
if not, should I only use Firestore for that?
Can I combine SharedPref and Firestore when saving data?
Yes, you can but I cannot see any benefit at all. You say:
Firestore so that when the user signs in on another device.
To have the data available no matter what device the user is using means that each time the user closes the app, you should save the progress in Firestore. So I cannot see why would you use another data structure. Besides that, SharedPreferences do not persist along with app uninstalls. SharedPreferences data is always deleted.
It would have been an excellent idea only if you wanted to store the progress in SharedPreferences and commit the quiz to Firestore only when it's finished. In this way, you'll only be billed with a single write operation, which sounds perfect. However, this solution doesn't provide the feature to have the progress available, no matter what the device the user uses, as it's stored only on a single device.
Firestore has a feature where it will automatically store data in the device's cache, so when your user go offline, they can still view what they last had access to. Since firebase already takes care of when your users go offline, I suggest going with firestore only route.
You can read more about firestore's offline cache here(Watch the video, it's great):
https://firebase.google.com/docs/firestore/manage-data/enable-offline

Inserting The UID into Firebase Database Only once

In the main activity of Android Studio, immediately after anonymously signing in into firebase, I have this line of code:
FirebaseDatabase.getInstance().getReference().child("Online Users").push().setValue(currentUser.getUid().toString());
So, the UID of current user will be saved into firebase under the child "Online Users". Good. Problem is, when I open the app for the second or third time, or if I navigate from another activity to the main activity, this line of code gets executed again, and now I have several identical entries in the firebase. I mean, one UID has been pushed into firebase several times. But I don't want that. I want only one copy of each UID there. How can I achieve that?
Whenever you want a specific value to be unique in the Firebase Database, model it as the key of a list.
In your case that means that instead of calling push() you actually use the UID as the key:
FirebaseDatabase.getInstance().getReference().child("Online Users").child(currentUser.getUid().toString()).setValue(true);
Now whenever the user comes online again, they're just writing true to the same location again.
Note that the same user can come online from multiple devices. If you are trying to build a presence system, tracking what users are online, I recommend studying the sample presence system in the Firebase documentation which handles the edge cases correctly.

Vaadin dynamically reflect changed data to all users' screens

I am trying to dynamically reflect the changes of the data to all logged in users' screens.
There are items being added to the database or one of their attributes is being changed by users or by scheduled tasks(cron-jobs).
What I want to do is reflecting those changes to all logged-in users' screens without a need of page refresh. This includes certain parts of all screens. One part is left menu side of all screens, the other one is table contents.
I am able to do that for one logged-in user via re assigning the changed datasource to the table and re building the left-side menu after re-assignment of the datasource. But this is only for the current user. I need to change all user screens which are currently in use at that moment.
I have checked Vaadin PUSH but could not really relate to it.
How can I do that with Vaadin?
Thank you in advance.
Check this out:
https://vaadin.com/docs/v8/framework/articles/BroadcastingMessagesToOtherUsers.html
In short, you can use the Observer Pattern to notify all the UIs about changes in the database. You also need to enable push and modify the UI using the UI.access(Runnable) method.

Optimistic locking & concurrency

My application working in a multi-users environment for medical records.
One of the important parts of the application is writing the medical records, doctors should always review the current written data then adding or correcting any information's as needed.
I were aware about the situation when 2 doctors (A,B) viewing the data at same time, one of them will make changes and hit save (A).
the other doctor (B) still editing the old data & he don't know about the changes applied by doctor (A).
When doctor (B) hit save, the application will compare the Version column in the database, application then return an error message ("Data has been changed by another user !!!").
My Question in this case: What choices should be available within my solution.
I'm seeking an professional solution or idea from your experiences.
My application using: Java Swing for end user client & MySQL database.
Doctor B should absolutely be notified in some way that the record has been modified since he opened it. The rest is up to you/the requirements of the program. Does it matter if Doctor A's changes are overwritten? Would it be better to have Doctor B view the changes made by Doctor A, and then decide if the changes (made by Doctor B) should still be saved? Should the changes be merged? It all depends on what you decide to design, as well as any requirements that may be imposed on the system you are creating. Without more detail on the system you are designing or its requirements, it would be impossible to provide a specific answer.
suggestion: send a real time notification
if A makes changes and hits save you could send a message "Data has been changed by another user !!!" immediately to B before B saved his changes.
This approach is similar to stackoverflow. As I write my answer, you can change your question and I will get a message
an edit has been made to this post; click to load
So if A,B,C... open data set D, they should register themselves as observers.
You can implement this approach by using websockets or polling.

Know when app data from Application Manager is deleted

I need to have an event that is called at the moment when the user of the phone has clicked 'Clear Data' in the 'Application Manager'. Is there a way to do that?
I need to know that because I got a database that holds very important information and I need to know when that information is deleted. I tried to look for database events for that and even events for data deletion with no luck.
There is no such event.
The first opportunity to detect this is when your app is started next.
(If you're using SQLiteOpenHelper, your onCreate method will get called.)
However, there is no guarantee that the user will actually use your app again.

Categories