how to save the reached level in a quiz app - android

I'm new to kotlin and android app developpement in general and I want to make a Quiz app with jetpack compose not XML, and this app has multiple levels and when the user starts the quiz if he decides to stop playing in a certain level and quit the app i want to save that level so next time when he come back to play he's gonna start from the same level he stopped.
I've tried to use preference datastore but it doesn't seem to work.
this is my Datastore class:
Datastore
this is how I store the current level:
save current lvl
this is how I get the lvl I stored in the datastore:
get stored lvl
if you have any idea what I'm doing wrong please and if posibile use jetpack compose.
thank you.

You should post code, not images. Although in this case, you can see from your second image that saveScreen is a function you're never calling anywhere, because it's grey (i.e. it's never used anywhere), and there's a yellow underline giving you a warning too.
You'll have to call saveScreen() or saveScreen.invoke() somewhere. Why not just make it a normal function though?
// call this when you want to save the screen
private fun saveScreen() {
// scope.launch { datastore.Save etc }
}
Unless doing things this way is a Compose thing (I've never used it). Either way, that function you're defining isn't called anywhere, so your screen isn't being stored!

Related

Best way to check if the user enable or disable any permission?

I have 2 buttons first button has this id id="#+id/button_A" and second button has this id id="#+id/button_B"
button_A I named it text="areNotificationsEnabled ( On )"
button_B I named it text="canDrawOverlays ( Off )"
I want when the user block or unblock app notification, So update button_A to On or Off. Same thing with button_B.
I can solve the problem using onResume but there is exceptions like the user can block notification from status bar without leave the app or using split screen to turn on or off canDrawOverlays
I solved the problem using CountDownTimer and make check every 250 milliseconds but I think it's not good way to do that, What about onWindowFocusChanged or there is better way?
As far as i know this is not possible in the traditional view system.
However, it is possible with Jetpack Compose, Accompanist & some experimental api - see Accompanists Github for details and the android docs for how to implement it in traditional view systems
//inside a composable
val cameraPermissionState = rememberPermissionState(android.Manifest.permission.CAMERA)
Text("camera permission enabled = ${cameraPermissionState.hasPermission}")
While this solution will work "just fine" it might come with a pretty steep learning curve. I suggest you go this route only if you intend to start using compose anyhow.

Android Work Manager: Can I force prune the Completed Jobs?

I am uploading photos in my app and want to provide the User with the upload progress. The way I was going to do this is have a global tag, UPLOAD_MEDIA_TAG and use that whenever I am creating a OneTimeWorkRequest. That way I can just observe the WorkManager.getInstance().getStatusesByTag(UPLOAD_MEDIA_TAG) LiveData, and whenever a job completes I would show a percent completed. (I.E. Uploading 3/10 photos would show 30%). Then once all 10/10 photos have been uploaded, I would hide the progress bar. This works great as it easily supports leaving the app, coming back, and you will see the Progress Bar as soon as the app launches right where they left off.
The problem is that once all 10/10 jobs are finished and marked as completed, those jobs will stick around until the WorkManger prunes them. So if the user uploads 5 more photos, the LiveData is returning 15 statuses instead of 5, throwing off the percent calculation.
I noticed there is a method OneTimeWorkRequest.keepResultsForAtLeast(duration) but what I would want is something like, keepResultsForAtMost(duration) or a WorkManager.getInstance().forcePrune() method that would cleanup all the completed jobs from the database.
Any help would be great!
After submitting an issue with Google, they are saying there will be a function called, pruneWork in Alpha03.
As of alpha release, you don't have much control over job pruning since it is managed by WorkManager. You can however observe the lifecycle of WorkManager to get job status and take necessary action as follows:
WorkManager.getInstance().getStatusById(myWork.getId())
.observe(lifecycleOwner, workStatus -> {
// Do something with the status
if (workStatus != null && workStatus.getState().isFinished()){
// Stop observing data or do other action
}
});
Note:
Use [WorkManager 1.0.0-alpha03 which has several bugs fixed and new features introduced. One you might be interested is:
Added WorkManager.pruneWork() to remove completed jobs from the internal database

Updating an UI upon changes

I am building an app that displays a bunch of information that the user can edit, and I am having trouble keeping the UI updating the data displaid so it is consistent with the new edits done at runtime.
I have implemented updateUI methods that basically look like:
void updateUI(){
((TextView) fieldDisplay).setText(fieldCurrentValue);
...
}
I know I can run this method in things like an AsyncTask or similar stuff that makes it execute continuously. But Im concerned about efficency since its not really necesary to update the UI all the time, but only when the user inputs a value wich is not that often.
What would be the best approach to this?
EDIT:
The question is how to make sure the an UI element shows the current value of a field, regardless of how or why that field is updated.
My case specifically is the with this. Im trying to make an RPG character sheet, and I have like a bazillion Skills, wich are affected by another lot of fields, such as Characteristics, Modifiers, Categories...
The application should behave so, whenever any of the many fields affects it changes, it should display the new value.
Now, since keeping track of what field affects what is part of the problem, if could update whenever any field whatsoever changes, but I dont know how to do that.

Tutorial first time you enter into an app?

I'm programming an app using android studio. I want to know in which way I can do a tutorial that users will see only the first time that use the app. Tutorial like image or screenshoots
Can someone help me? Thanks
I encountered this thread while looking for a solution for running a tutorial only at the first time (as rbaleksandar suggested), so in case it will be helpful for someone someday, here's a template of a solution that works for me (using the SharedPreferences API):
#Override
protected void onResume() {
super.onResume();
String tutorialKey = "SOME_KEY";
Boolean firstTime = getPreferences(MODE_PRIVATE).getBoolean(tutorialKey, true);
if (firstTime) {
runTutorial(); // here you do what you want to do - an activity tutorial in my case
getPreferences(MODE_PRIVATE).edit().putBoolean(tutorialKey, false).apply();
}
}
EDIT - BONUS - If you're into app tutorial - I'm messing now with the ShowcaseView library (which is amazing - try it out). Apparently they have some shortcut for that issue using a method called singleShot(long) - its input is a key for the SharedPreferences, and it does the exact same thing - runs only in the first activation. Example of usage (taken from here):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_shot);
Target viewTarget = new ViewTarget(R.id.button, this);
new ShowcaseView.Builder(this)
.setTarget(viewTarget)
.setContentTitle(R.string.title_single_shot)
.setContentText(R.string.R_string_desc_single_shot)
.singleShot(42)
.build();
}
You could always code your own solution, but, let us not reinvent the wheel.
Check this Android Library:
Tour Guide Repository
It allows you to add pointers in your screen, so the user knows where is he supposed to touch next.
It's pretty easy to use, you only need to point to the element you want the user to touch.
From the doc:
Let's say you have a button like this where you want user to click on:
Button button = (Button)findViewById(R.id.button);
You can add the tutorial pointer on top of it by:
TourGuide mTourGuideHandler = TourGuide.init(this).with(TourGuide.Technique.Click)
.setPointer(new Pointer())
.setToolTip(new ToolTip().setTitle("Welcome!").setDescription("Click on Get Started to begin..."))
.setOverlay(new Overlay())
.playOn(button);
Hope this helps!
Some links to libraries for creating introduction and/or tutorial screens.
Horizontal cards like Google Now:
https://github.com/PaoloRotolo/AppIntro
Tutorial screen:
https://github.com/amlcurran/ShowcaseView
As far as I understand the question is not How do I create a tutorial? (as the people who have already posted an answer have concluded) but instead How to show a tutorial upon first launch only?. So here are my two cents on this topic:
I'm not familiar with how your Android app stores its configuration data but I will assume that it's either in a database (SQLite) or a text file (plaintext, YAML, XML - whatever). Add a configuration entry to wherever the app's settings are being stored - something like tutorial_on : false, tutorial_on : 1 etc. depending on the format the configuration is represented in.
The way configurations work is that whenever an app (or software in general) is launched it has to be loaded in the app itself. So add the following to your app (where and how is up to you and your app design):
Check tutorial_on entry
If tutorial_on is set to true/1 whatever
2.1 Display tutorial
2.2 Change tutorial_on to false/0 whatever
2.3 Store the result in your configuration
Continue using the app
By doing so the first time your app launches the flag responsible for displaying the tutorial will be toggled and afterwards every time you start the app the toggle flag will be read leading to omitting the tutorial.
Personally I would suggest that you an option similar to Don't show tutorial anymore along with a description how to re-enable it (by triggering some action in that app's menu etc.). This has two major benefits:
Improved user experience - users like to have control (especially over trivial matters such as showing or hiding a tutorial). Whenever you take the control away from them, they get pissed off.
Enable your user to re-learn forgotten things - a general rule of thumb is to create apps that should not burden the user with a lot of stuff to remember. That is why things should be self-explanatory. However sometimes you may want to do that nonetheless. By adding the possibility that the user re-launches (by simply resetting the tutorial_on flag and repeating the steps from above) the tutorial allows just that - refreshing a user's memory.

Add user defined place in the input field by clicking a button to stop the geocomplete

My requirement is to put in a place name in a text field and show that in the map, so i used geocomplete js, which works well.
Now my user should be able to put in user defined places like 'my house', for that I need to remove the geocomplete on clicking a 'x' button on top of the map.
How can I implement this?
Thanks in advance
I wouldn't customize the package! When a new version comes out, you'll have to make the same changes you before.
Since you haven't provided any code, I can give you an idea of what I've done with JQuery validation method overrides.
You'll simply have to find the listener (something like $('#listenToThis').on('click', function(){ doThings(); }) in the geocomplete.js file, then override it in a file that is included after geocomplete.
If you're using bundles, just include your file after the geocomplete listener response is defined.
So, after you find those, you can do something similar to the following:
$.validator.methods.number = function (value, element) {
value = floatValue(value);
return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:[\s\.,]\d{3})+)(?:[\.,]\d+)?$/.test(value);
}
The function above allowed me to validate client-side numbers that were formatted as currency in JQuery ($).
This overrides the JQuery.validator.methods.number function (a cheating way to override the function without changing the package code), so if you can pinpoint the geocomplete.addSomethingToMap or geocomplete.reactToClick function, you should override it and it will essentially work that way.
Warning, though: you will need to reinclude the changes when you want to reenable the feature. You'll have to override, override, override again. This may not be the best way if they're going to be adding hundreds of different new locations on one screen, but for up to a small unit, such as a dozen, it should be a good solution.

Categories

Resources