C#, Android: Build own event listener - android

here is the problem. I am using a custom component from someone else called a "Fancy showcase view". It focuses on buttons in my activity on highlights them with a text as a tutorial through the app. I am starting the first message, and when the user dissmises this by clicking anywhere in the activity, the next button is supposed to be highlighted. Unfortunately, the component, which otherwise is perfect, doesnt have a listener implemented like "OnDismis" of the first tutorial view so the next could start. Just putting both into code one after the other skips the second one. It also tried working with lifecycle methods, such as OnFocuseChanged() but even after the tutorial gets dismissed, this method isnt called a second time. What would you guys say is the best way to handle this? Here is what is NOT working:
try
{
new FancyShowCaseView.Builder(this) // if this crashes, we need clean rebuild
.Title(title1)
.TitleStyle(0, (int)GravityFlags.Center | (int)GravityFlags.Center)
.Build()
.Show();
}
catch (Exception e)
{
Toast.MakeText(this, "There was an error ... " + e, ToastLength.Short).Show();
}
try
{
new FancyShowCaseView.Builder(this) // if this crashes, we need clean rebuild
.Title("TEST")
//.TitleStyle(0, (int)GravityFlags.Center | (int)GravityFlags.Center)
.FocusOn(txtL)
.Build()
.Show();
}
catch (Exception e)
{
Toast.MakeText(this, "There was an error ... " + e, ToastLength.Short).Show();
}
The second one doesnt show up. There are no event handlers and I cannot make use of lifecycle methods. Click counting wouldnt work either, since the user might click on the activity while it is loading so hard coded values arent a good option either. Any ideas?
Thanks:)

Use the FancyShowCaseQueue to control the sequence.
You add individual FancyShowCaseViews to it and when you "Show()" the queue, each FancyShowCaseView happens in the order in-which you added them to the queue.
Example:
var fancyView1 = new FancyShowCaseView.Builder(this)
.Title("StackOverflow 1")
.FocusOn(button1)
.Build();
var fancyView2 = new FancyShowCaseView.Builder(this)
.Title("StackOverflow 2")
.FocusOn(button1)
.Build();
var fancyQueue = new FancyShowCaseQueue()
.Add(fancyView1)
.Add(fancyView2);
fancyQueue.Show();
I am using a Xamarin.Android binding library of FancyShowCaseView, but you can review the Java-based examples are in the sample app in the repo, ie:AnimatedActivity.java
Also you can implement the ME.Toptas.Fancyshowcase.IDismissListener interface:
public void OnDismiss(string p0)
{
//
}
public void OnSkipped(string p0)
{
//
}
And use that implementation on each of your FancyShowCaseViews:
var fancyView2 = new FancyShowCaseView.Builder(this)
.Title("StackOverflow 2")
.FocusOn(button2)
.DismissListener(this)
.Build();

Related

Code after `pagingAdapter.submitData()` is not executed

I am writing a toy Android app using Kotlin flow and Android Paging 3 library. The app calls some remote API to get a list of photos, and display them using a RecyclerView with a PagingDataAdapter.
I find that the code after pagingAdapter.submitData() is not executed.
Here is the code snippet (this function is in a Fragment):
fun refreshList() {
lifecycleScope.launch {
photosViewModel.listPhotos().collect {
// `it` is PagingData<Photo>
pagingAdapter.submitData(it)
Log.e(TAG, "After submitData")
}
}
}
The log After submitData is not printed.
However, if I put the logging in front of the pagingAdapter.submitData() line, it is printed, like this:
fun refreshList() {
lifecycleScope.launch {
photosViewModel.listPhotos().collect {
// `it` is PagingData<Photo>
Log.e(TAG, "Before submitData")
pagingAdapter.submitData(it)
}
}
}
The log Before submitData is printed with no problem.
Why does this happen, please?
.submitData is a suspending function which does not return until invalidation or refresh. As long as Paging is actively loading (collecting) from the PagingData you provided, it will not finish. This is why it must be done in a launched job.
For the same reason, make sure to use collectLatest instead of collect to make sure you cancel and start displaying new generations as soon as possible.

Show progress of multiple website downloads during button press

I need to retrieve some data from roughly 50 different URLS with the press of a button.
The code goes through them one at a time, and although it doesn't take that long, it will take around 20 seconds, and I have all this code running inside of a button.
I was hoping I could update a TextView or something to say "Loading page 1 of 50" then "Loading page 2 of 50" etc, in between accessing the different websites.
The code below works, just the button gets stuck down for an unknown amount of time, and I want the user to have some indication of how far along the loading is doing.
btnGetData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//regionRetrieve 1 page of auction data, so we know how many future pages to retrieve. -P
String auctionURL = "https://api.hypixel.net/skyblock/auctions?page=";
String firstPage = null;
try {
firstPage = new RetrieveData().execute(auctionURL + "0").get();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
auctionInfo = new JSONObject(firstPage);
} catch (JSONException e) {
e.printStackTrace();
}
//endregion
//regionRetrieve the remaining pages
int totalPages = 0;
try {
totalPages = auctionInfo.getInt("totalPages");
} catch (JSONException e) {
e.printStackTrace();
}
//Place to put the rest of the pages
ArrayList<String> remainingPages = new ArrayList<>();
//Starts at 1, because we already retrieved the 0 page as the first page.
//Also, I checked, and you do need to retrieve the 52nd page if there are say, 52 pages.
for (int i = 1; i <= totalPages; ++i) {
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//ADD SOME KIND OF NOTIFICATION HERE
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
String newPage = null;
try {
newPage = new RetrieveData().execute(auctionURL + i).get();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
remainingPages.add(newPage);
}
Toast.makeText(getApplicationContext(),"All data received.",Toast.LENGTH_SHORT).show();
tvLoading.setVisibility(View.GONE);
//endregion
//stuff below this point is irrelevant to the question
}
});
I tried wrapping all of the code above inside an AsyncTask, and utilizing the "onProgressUpdate", but that did not work. Furthermore, I heard that by now, AsyncTask has been deprecated, and that there are better ways to do it.
I also tried using Toast messages, but they all show up at the end, which kind of defeats the purpose.
I even put the Toast messages in the AsyncTasks that I call in order to get the data, but that didn't work either. (The RetrieveData() is an AsyncTask that reads all the information from the URLS, and returns it as a String. I know you aren't supposed to use get, but in this case it is important the data arrives in the correct order. Unless, after retrieving the first one, and knowing how many pages there are, I could launch 50 threads at the same time to retrieve the data? But still, you are limited by your internet connection, and the user is still sitting there confused.)
Is there a proper way to do this?
Any help would be appreciated!
Instead of putting all the code in the button, make the button launch another activity, and put the data retrieval code in that activity.
Launch the code inside of a thread, and from within the thread, update UI elements to inform the user of the progress.
After the data is retrieved, store it, then start an Intent to go back to the previous activity, access the data where you stored it, and use it for whatever you needed to do.
(I know I answered my own question, but I figured it out a few hours later and nobody had responded up until now, hopefully this helps someone in the future.)

How to show AlertDialog within Override function?

Last week I started learning Android as I needed to create an application for one of the projects at Uni.
The application is a simple barcode/QRcode scanner and it should scan the code, compare its result with the database (I'm using Firebase) and either return other data from database if the barcode is found or ask the user if he wants to add the barcode to the database if it's not found.
I thought the easiest way to do it would be to use AlertDialog, but the app crashes every single time I scan the code.
I debugged the app and checked the Logcat, what I get is:
You need to use a Theme.AppCompat theme (or descendant) with this activity.
This is exactly where I get the error and where I wanted to use AlertDialog - based on the value in the variable details.
private BarcodeCallback callback = new BarcodeCallback() {
#Override
public void barcodeResult(final BarcodeResult result) {
barcodeView.decodeSingle(callback);
dbRef.child("Items").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Iterator<DataSnapshot> item = dataSnapshot.getChildren().iterator();
Boolean isFound = false;
while (!isFound || item == null) {
DataSnapshot i = item.next();
String check = i.child("ID").getValue().toString();
if (result.getText().equals(check)) {
isFound = true;
details = "Consumption: " + i.child("Consumption").getValue().toString()
+ "\nCost: " + i.child("Cost").getValue().toString()
+ "\nName: " + i.child("Name").getValue().toString();
} else {
details = "Not found";
}
}
new AlertDialog.Builder(getApplicationContext())
.setMessage("This is just an example for the purpose of the question.")
.create()
.show();
}
I get the error exactly on the line with .show();.
In the previous posts I found that you can't display AlertDialog in this place, and you need to use runOnUiThread function or Handler, none of those options worked for me, and I was getting the error in the same place.
Do you guys have any advice or suggestions?
Also, I'm sorry for the way this post looks like or for any missing but required information. I know it's not an excuse, but this is my first post here.
Thanks in advance for any replies.
The problem is here:
new AlertDialog.Builder(getApplicationContext())
You can't build a Dialog using the application context. To reach this you need an Activity Context.
Read this question or this article for further understanding

Subscribe to observable after dispose

i am building my app on android repository by Fernando Cejas and i have a problem with subscribing to observable after calling dispose.
When i come to dashboard, i call method subscribeOnUserMessages.execute(new Subscriber(), new Params(token)), which is method in UseCase class
public void execute(DisposableObserver<T> observer, Params params) {
Preconditions.checkNotNull(observer);
final Observable<T> observable = this.buildUseCaseObservable(params)
.subscribeOn(Schedulers.from(threadExecutor))
.observeOn(postExecutionThread.getScheduler());
addDisposable(observable.subscribeWith(observer));
}
In child class SubscribeOnUserMessages i simply call repository like this
return messageRepository.subscribeOnUserMessages(params);
In my socket implementation i create like this
return Observable.create(emitter -> {
if (!isThereInternetConnection()) {
Timber.w("Network connection exception");
emitter.onError(new NetworkConnectionException());
return;
}
/*
* Open socket if not opened
*/
openSocket(params.getToken());
String channelName = CHANNEL_PRIVATE_USER + params.getAuthenticated().getUuid();
if (subscribedChannels.contains(channelName)) {
Timber.d("Channel %s is already subscribed", channelName);
return;
}
JSONObject auth;
try {
auth = createAuthJson(CHANNEL, channelName, params.getToken());
} catch (JSONException e) {
Timber.e("Couldn't create auth json");
emitter.onError(e);
return;
}
mSocket.emit(SUBSCRIBE, auth);
Timber.d("Emitted subscribe with channel: %s ", CHANNEL_PRIVATE_USER + params.getAuthenticated().getUuid());
subscribedChannels.add(CHANNEL_PRIVATE_USER + params.getAuthenticated().getUuid());
Timber.d("Subscribing on event: %s\n with user: %s", EVENT_USER_NEW_MESSAGE, params.getAuthenticated().getUuid());
if (mSocket.hasListeners(EVENT_USER_NEW_MESSAGE)) {
Timber.v("Socket already has listener on event: %s", EVENT_USER_NEW_MESSAGE);
return;
}
mSocket.on(EVENT_USER_NEW_MESSAGE, args -> {
if (args[1] == null) {
emitter.onError(new EmptyResponseException());
}
Timber.d("Event - %s %s", EVENT_USER_NEW_MESSAGE, args[1].toString());
try {
MessageEntity messageEntity = messageEntityJsonMapper.transform(args[1]);
emitter.onNext(messageEntity);
} catch (JSONException e) {
Timber.e(e, "Could not parse message json");
emitter.onError(e);
}
});
});
Symptoms are that first time i subscribe everything is going through to presentation layer. When i dispose after going to second screen and come back i only see logs coming to socket implementation, but not going through.
My question is: Is there a method for subscribing to same observable again? I've already tried to save that observable in my use case in singleton and subscribe to that observable, didn't help.
Without additional info and details regrading socket implementation it is hard to spot the problem exactly, but, from the code you've posted, you don't have dispose logic, so while you might properly call dispose() to the Observable at the correct lifecycle event, your socket will actually stay open, and it might not got disconnected/closed properly ever.
That might lead to a problems opening and connecting to the socket at the 2nd time, as you might try to reopen already open socket and depends on your internal socket impl that might be a problem.
(I can see in the comment that openSocket if not already opened, but still there might be problem elsewhere calling some method on the socket multiple times or setting listeners, again depends on the socket impl)
As a general guidelines, you should add dispose logic using emitter.setCancellable()/emitter.setDisposable() in order to dispose properly the socket resources when you no longer need them, thus - when applying subscribe again (whether the same object or not) will invoke your subscription logic again that will reopen the socket and listen to it.
It is not clear to me if you like to keep the socket open when you moving to a different screen (I don't think it is a good practice, as you will keep this resource open and might never get back to the screen again to use it), but if that's the case as #Phoenix Wang mentioned, you can use publish kind operators to multicast the Observable, so every new Subscriber will not try to reopen the socket (i.e. invoking the subscription logic) but will just get notify about messages running in the already opened socket.

Using MrEngineer13's SnackBar library implementation

I am using #MrEngineer13's SnackBar implementation and was wondering how to capture 2 separate "ActionClick" events - depending on when the actionclick event occurs, I need to do different things.
The builder looks like this -
new SnackBar.Builder(this)
.withOnClickListener(this)
.withMessage("This library is awesome!") // OR
.withMessageId(messageId)
.withTypeFace(myAwesomeTypeFace)
.withActionMessage("Action") // OR
.withActionMessageId(actionMsgId)
.withTextColorId(textColorId)
.withBackGroundColorId(bgColorId)
.withVisibilityChangeListener(this)
.withStyle(style)
.withDuration(duration)
.show();`
and the onMessageClick takes a "token" parameter -
#Override
public void onMessageClick(Parcelable token) {
}
What I am not able to figure out is, how to pass this "token" when the click happens.
depending on when the actionclick event occurs, I need to do different things
Handle that in the body of onMessageClick():
#Override
public void onMessageClick(Parcelable token) {
if (shouldIDoX()) {
doX();
}
else {
doY();
}
}
(where you supply relevant implementations of shouldIDoX(), doX(), and doY().
What I am not able to figure out is, how to pass this "token" when the click happens
There is a withToken() method on the Builder that you can use to supply the Parcelable to be passed into onMessageClick(). That being said, the JavaDocs describe it as "The token used to restore the SnackBar state", which would make me a bit nervous about messing with it.

Categories

Resources