Having problems applying android code architecture/design patterns - android

I want to achieve the following:
I receive a notification via FCM with a specific id in the data properties. I get the id both when my app is in foreground and in the background/closed...so far so good.
Now I want to do the following:
Make a volley request to e.g. /books/id to get the corresponding book informations.
Save those informations persistently in a Room database
Display them in a RecyclerView
Scenario 1: App with RecyclerView is in foreground:
As soon as I receive a new id via the FCM notification data, I want my RecyclerView to append another list item with the newly fetched book informations.
Scenario 2: App is in background - notification gets clicked:
When the notification gets clicked, the MainActivity with the RecyclerView gets loaded, showing the newly fetched book informations.
Scenario 3: App is in background - notification gets swiped away:
After some time the notification was swiped away the user re-opens or restarts the app. The MainActivity with the RecyclerView and the newly fetched book informations is shown.
My problem:
I don't quite know how to structure my code to accomplish this.
I tried making the volley request inside of FCM's onMessageReceived() but that resulted in an error and I don't know where and how I could do that otherwise. I tested the volley request in another project and it worked there.
Currently I try to learn how to work with the Room database.
I would really appreciate if someone can give me an insight of how to accomplish something like that. I really need to get this to work soon.
Thanks in advance! :)
EDIT 1:
I dont't get the error inside of the onMessageReceived anymore. I receive the correct response with the given book id.
Now I want to save these informations into the Room database and reflect the change in my RecyclerView. I think I'm going to use androids architecture components like here:
android architecture components example
I think that's exactly what I need. However, I still don't quite understand how to deal with it and structure my code accordingly.

Scenario 1: You may save it in DB, or just do broadcast and save it for instance in Activity then display a list.
Scenario 2: You should keep all display data in DB. After click on notification when RecyclerView is in background, you should handle the intent that pass through android.intent.category.LAUNCHER of intent-filter and put some info there to make a request and update the list.
Scenario 3: Finally, just do a request, save in DB, then display all in the list. This is a good approche, it takes extra time, but you always keep it updated.
Also read about efficient hot swap data with DiffUtils

Related

Should I store my contacts list in DB or Sharedpreference to avoid running the same task

I have my app such that when entered in a certain activity, I show a list of contacts in my phone( similar to Chat app where you see a list of contacts when trying to start a new chat).
So I just wanted to know about the implementation details of these task for best/ efficient work.
Is the contact list is fetched everytime from my device whenever I enter the activity(which is actually redundant, because we maybe doing same work over and over again,
But again we cannot rely on saved data in DB/SharedPref as contacts data is dynamic and bound to change,
so need your expert suggestion how to actually make this work in best possible way(I have already written method to fetch contacts- so whould I fetch the contacts afresh everytime or any other way around is there, what the popular chat apps implemet to show the contacts everytime?
Personally, I would implement the fetching of the contacts on a background thread (using coroutines) that is called periodically (perhaps each time they the activity is created, as you suggested, or maybe once when they open the app).
Because it's on a background thread, it would prevent them using the app (or any dropped frames while the activity is created). They might have thousands of contacts or be using a really slow device.
Presumably, you would want to store some other data associated with each contact: for example, if you opened up a chat with the user, you would want to associate all the messages with the user, so I would use a database such a realm rather than shared preferences.
So the entire process would probably look something like this:
Opening the app triggers a service that fetches all of the user's contacts on a background thread.
Service updates the database, perhaps removing contacts which don't exist and adding the new ones.
If you were handling this in a view model, your fragment or activity could observe the list of contacts and update the UI once it had changed.

Keep track of item in recylerview?

I am making a chat application in which when the user hits send button , the message is added to the recycler view and at the same time sent to the server.How can i keep track of the message item in recycler view so that if the message is not successfully sent to the server (due to network problem ),i can show a resend option next to that message.
following is the flow
sendMessageToServer(message);
messageItems.add(message);
notifyItemRangeInserted
The easiest solution -but not the most correct one- would be as the following:
add a boolean attribute isSuccessful to the message class.
when you send the message and the get the response back from the server change the corresponding message. and call notifyDataSetChanged on the Adapter.
and I recommend this take where they build this open source messing app to get a better understanding how to design you Application Architecture, as my solution is not the best but I guess is the easiest.

How can I update certain values in recyclerview whenever onMessage function called?

I am making an android app and facing this similar issue I am sending data my SQL server using FCM which received in onMessage function in my app, but at the time of receiving the data I am not sure weather that specific screen exist or not? maybe user was on another screen at that time.
And along with this there will be number of different data objects for different screens? How can I make sure that data will properly update the existing data I know how to update data so please don't think about that, problem is how to call update functions of different screen which may or may not exist at that time.
I have spent more than 3 days on this solution but can't figure out. Please any good suggestion could prove a lot of help.

Android Design- Content Provider plus real time information- How to display?

Here's something interesting- How do I display information both from my content provider, and some real-time data from the web (which I don't want to save to my content provider?).
1.CursorLoader and CursorAdapter won't do IMO since I don't want to save the information to my content provider.
2.AsyncTask and updating the view in onPostExecute won't work, since right now I am displaying information from my content provider through cursorAdapter etc. and since the screen itself is an AdapterView subclass, when the loading is finished, the view might belong to some other element (recycled)
3.Service won't do for the same reason as #2 (and besides that, in this case, the background thread is coupled with the UI, so that doesn't seem like a natural solution).
**********Optional specific details starting from here if the picture isn't clear******
Say that I have some app which allows users to follow stocks.
I have a content provider, that at the path content://whatever.my.package.name/follows
has some information about which stock the user is following, whether or not it was sent to my server already (so it does have already some 'real time' data displaying through it), the parameters the user is interested in following, etc.
When displaying this information, I want to include some real time information from the web. I already have the necessary method implemented, but I can't think of a natural solution (see above). In particular, the real time data certainly cannot be saved on the same path (/follows) since this isn't a natural part of what I have in mind when I am thinking about the object "follow",but I do want to present the real time information about the stock, and it does relate to the follow presented on the screen (for example, a follow includes a start price, so we want to present the change from that start price to the real time price of the same stock etc).
I'm can't think of a good design I could use, so help will be appreciated :)
If the only thing that stops you from using the content provider is that you don't want to store the informations in it, then don't store it. Remember that a provider is just some abstraction above some data source. Nobody is going to stop you from using a in memory sqlite database for storing the live data.
Then you have two data sources and can build relations on them for displaying purposes like with sqlites attach_database or in code. Of course the live data is gone as soon as the provider is shut down so you must be able to handle that case.
EDIT
Hmm, ok. So touching the provider is a no go. You said the views are adapter views. How about using Volley or something similar to fetch the data in the adapter itself and cache it there. Whenever a view is requested (i.e in 'onBindView' when using RecyclerView) check the adapter cache for the data. If it does not exist or is outdated start fetching the data. When the request returns notify the adapter that the dataset changed. It then would start requesting views again making the next cache probe a hit. If you are fetching the data for each item in the cursor try to pass the index/position of the item to the request so that you can notify the adapter that a specific item has changed.

Update Fragment when new element is added to database

I'm writing an android app. The app is able to get Push notifications from Parse, and I have been able to take the notification from Parse and put it into an SQLiteDatabase. I have a fragment which, when selected from Navigation Drawer, reads the values from the database and displays them with a Custom Adapter. This is where I need help.
If the user has the Notifications tab currently selected and a new Push notification is received, the notification is put in the database but not displayed until the user navigates away from the Notifications tab and back into the Notifications tab. I'm ok with this, but I'd like the Notifications fragment to update immediately upon Push reception. Is there any way to set a listener on the database to implement this behavior?
Thanks for any replies - I'm able to paste code if it is needed.
I believe a Loader may be what you're looking for. This Udacity course helped me understand how to use Loaders.
Through a Loader/LoaderManager, the onLoadFinished method is called in your fragment/activity when the database is updated. Seeing you are using a SQLite DB, you could use a CursorLoader, which would pass a Cursor in to onLoadFinished and then you can do whatever you want with it, such as send it to an Adapter to display the new data.

Categories

Resources