Keep track of item in recylerview? - android

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.

Related

Show local notification with Stream Builder

I have made a chat app with Flutter and Cloud Firestore the app is working but I need to show local notifications when a user gets a new message how can I do that.
I have used StreamBuilder to display the messages in the chat screen.
Please help me.
Thanks in advance.
It depends on the data you are recieving. A stream builder may rebuild on multiple occasions including on network state change. so you can't add it directly to the build method. For example if its returning a list of items, then you can create a variable and update this variable with the length of the response. for example if the length is 5 in the first response. Update the variable to 5 and on next instance from stream you may get a 6 then check if the existing value is lesser than the current length. Then perform your custom action here (print or notification) and update the length.

How to count HTTP responses per web page using WebExtension

Using WebExtensions, I am trying to write a content script which would count the number of HTTP responses per web page.
I just started looking into WebExtensions, so I may be completely off the track, but so far my approach has been to modify the background script to listen to browser.webRequest.onHeadersReceived. Once fired, callback to that function would invoke browser.tabs.sendMessage to the tabId from where the request to this response originated.
In the content script I use browser.runtime.onMessage.addListener to listen to those messages from the background script.
With this approach I have two problems:
The background script is trying to send messages to the content script even before the content script has started listening, resulting in errors from the sendMessage and thus lost information.
On page reload, some of the messages from the background script are received before the content script is reloaded, some are lost (presumably during the period where one content script was unloaded and before another one started) and some are received after.
I've been looking whether browser.storage.local can help me with this situation. With this I can avoid the problem of messages being lost by simply keeping the count in that storage.
But I still have the problem in the background script that I don't know whether to increment the count for the web page that was displayed before the reload happened, or the one after (due to the problem #2 above).
I think instead of using tabId as a "pointer" to where the count is increased, having some kind of an ID for the web page to which the response belongs to would help. But I haven't been able to find anything like that in the documentation yet. But again, I'm novice to WebExtensions, so there may be a completely different approach I'm missing.

Having problems applying android code architecture/design patterns

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

Android - lazy loading of ListView with web-SQL results using JSON and http?

I'm currently working on an app which has to query a web SQL db, and show the results in a ListView, and I would really appreciate some input as to what is the best way to do that.
I would like the results to be shown as quickly as possible, so if I can somehow show the first result immediately while still loading the rest that would be great.
Reading on the subject, it seems the best way to send the data (which includes a small image) is using a JSON object (or array).
The ideas I had so far:
* Http requests with index of last result - downside is that the server will run the same query over and over again and just send me a few results at a time.
* Open a socket between device and server until user leaves the results view - downside is excessive use of network resource.
Do they sound OK?
Is there something else I'm missing?
Thanks!
The ideas I had so far: * Http requests with index of last result -
downside is that the server will run the same query over and over
again and just send me a few results at a time. *
=> This is the standard idea but I would say sending request and fetching data depends on the particular requirement, like you should use Service concept if you would want to show first set of result and at the same make a call for another sets in background.
So whenever service gets another sets of data, it sends a message to your activity with the received new data set and your activity will update the UI with the available new data.

Notification of Activity from AsyncTask

Within an AsyncTask, I am making a REST call to retrieve data. Within that AsyncTask, I may encounter an exception which I would like to package up (the HTTP code) and inform the Activity, which based on the HTTP response code (Timeout, Unauthorized, etc), would display different messages to the user.
What would be the best way to bubble that information up to the Activity for processing?
I have looked at a number of different Android mechanisms such as Notification, Handler, etc but I can't seem to determine a good architectural pattern for this situation.
If the error causes a halt in the users workflow, then you need to obtrusively interrupt using a dialog alert and then direct the user to the fix. If the error does not stop the user, then interrupt unobtrusively using a toast or notification.

Categories

Resources