static method in react native bug(callback) - react-native

I will try my best to explain the problem i have.
here is a sample code
auth.js
static authenticate(email,password,callbackIsLogged)
{
//some logic
if(passwordCorrect)
{
callbackIsLogged(true);
}
else
{
callbackIsLogged(false);
}
}
login.js
loginButtonClicked()
{
Authcontroller.authenticate(email,password,function(loginState)
{
if(loginState)
{
alert('correect');
//proceed withlogin.
}
else
{
alert('error');
}
});
}
the problem i have is weired.
lets look at a sample scenario where this bug happens.
if i login with correct username and password for the first time when the app opens everything goes according to plan.
But if i enter the incorrect password at the first time and the second time enter the correct password the callback function will get called twice with boolean value true and false.
It seems like the static function somehow hold the previous callbacks and execute them.
hope you undestood my question. how to fix this.
thanks.

Related

How to use IF / ELSE in a method of Vue

Issue: This method is supposed to accomplish the following:
IF the user is NOT in a specific room already found inside joinedRooms then join them and make the room tab above active. (this part works fine as shown below as the if statement)
ELSE (which means the user is already joined to said room), then just take them to the active tab above.
Here is where I am confused. The code below in my eyes SHOULD work. However, it isnt, it seems to always run the IF statement regardless. What is the incorrect syntax being used.
methods: {
joinRoom(room) {
if (room != this.$store.state.rooms.joinedRooms) {
this.$store.dispatch(RoomTypes.JOIN_ROOM, room);
this.$store.dispatch(RoomTypes.CURRENT_ROOM, room);
this.$emit('onClose')
}
else if (room === this.$store.state.rooms.joinedRooms ) {
this.$emit('onClose')
}
}
},
EDIT: What I am realizing thanks to a comment below is I need to check if the room is part of the array joinedRooms How would I accomplish this
Thanks to #CherryDT, I was able to realize I needed to use includes as part of the if statement. The code should look as follows
methods: {
joinRoom(room) {
if (!this.$store.state.rooms.joinedRooms.includes(room)) {
this.$store.dispatch(RoomTypes.JOIN_ROOM, room);
this.$store.dispatch(RoomTypes.CURRENT_ROOM, room);
this.$emit('onClose')
}
else if (this.$store.state.rooms.joinedRooms.includes(room)) {
this.$store.dispatch(RoomTypes.CURRENT_ROOM, room);
this.$emit('onClose')
}
}
},

Vaadin LoginOverlay

I am very new to both java and vaadin. Hope to be able to get some good tips on how I can improve my code.
I would like to rewrite the code below to use Vaadin´s -> LoginOverlay instead of the code below.
As I said, I'm trying to learn so I have used the code for this example: https://www.youtube.com/watch?v=oMKks5AjaSQ&t=1158s
However, LoginOverlay seems to be a better way to display its login part through. As in this example: https://www.youtube.com/watch?v=pteip-kZm4M
So my question is how can you write the code below as a LoginOverlay.
public class LoginView extends Div {
public LoginView(AuthService authService) {
setId("login-view");
var username = new TextField("Username");
var password = new PasswordField("Password");
add(
new H1("Welcome"),
username,
password,
new Button("Login", event -> {
try {
authService.authenticate(username.getValue(), password.getValue());
UI.getCurrent().navigate("home");
} catch (AuthService.AuthException e) {
Notification.show("Wrong credentials.");
}
}),
new RouterLink("Register", RegisterView.class)
);
}
}
I have only come this far to convert the code.
A clarification of the new part of the code. The code does not work. The part with loginOverlay.addLoginListener (event -> probably needs to be written in a different way.
I'm using IntelliJ 2020.3.4 if that is of any help.
public class LoginView2 extends Composite<LoginOverlay> {
public LoginView2(AuthService authService) {
setId("login-view");
LoginOverlay loginOverlay = getContent();
loginOverlay.setTitle("Welcom");
loginOverlay.setDescription("Manage your business tasks");
loginOverlay.setOpened(true);
loginOverlay.addLoginListener(event -> {try {
if(authService.authenticate(username.getValue(), password.getValue());
UI.getCurrent().navigate("home");
} catch (AuthService.AuthException e) {
Notification.show("Wrong credentials.");
}
}),
new RouterLink("Register", RegisterView.class);
}
}
}
}
Super grateful for all the help you can give.
Many thanks to everyone who makes stackoverflow so amazing.
Copy-pasting code snippets with little idea about what the code does is a recipe for disaster, especially when it comes to security.
You have all kinds of errors in your code: misplaced braces, using variables that do not exist, and a RouterLink far from where it belongs.
I understand you've got to start somewhere. I would recommend starting from code that actually compiles, and then gradually adding things, testing what you have so far at every step.
Here are some tips for your LoginView2:
Remove the whole loginOverlay.addLoginListener(...) code, including the authService.authenticate call and the RouterLink. Make sure that you can run the application at this point, and that you can see the login overlay.
Add back the login listener, but for now just show a notification in it. Test that you can run the application, and that if you click Login, your notification is displayed.
loginOverlay.addLoginListener(event -> {
Notification.show("This is working");
});
Implement the authentication. You have a call to authService.authenticate(...) inside an if-statement, but the method does not return anything. Instead, it throws an exception if the authentication fails, hence the try-catch. This means that inside the try { ... } block, any code you put after the authService.authenticate(...) call is only executed if it did not throw an exception, i.e. if the authentication was successful.
There are no username or password variables. The first YouTube video had defined these variables in the form of text fields. With the login overlay, it creates those fields for you, so how do you get the corresponding values from it?

How to use yield call in React-Native?

I am implementing fingerprint scanning in my RN app and I found a good tutorial for that but the code there has a syntax which I have never used - yield call(), however, I googled it and couldn't find a proper explanation for it.
Here is the code:
if (isFingerPrintSupported === true) {
yield call(KeychainService.setCredentials, user_name,
JSON.stringify({ password }));
}
Is there something else I can use instead in this case? if not then how can I import this or install in order to make it work?
EDIT(example code added):
componentWillMount() {
let credentials = yield call(KeychainService.getCredentials);
if (credentials && credentials.username)) {
let isFingerPrintSupported = yield call(KeychainService.checkBiometricSupportednEnrolled);
if (isFingerPrintSupported === true) {
// show fingerprint alert on login page
// and authenticate FingerPrint when user touch the sensor
}
} else {
// else don’t show fingerprint option on login
}
}
yield can be used inside a generator function & it helps to pause and resume a function at any time asynchronously. Also Additionally it helps to return value from a generator function.
Check this document for more information.
call is redux-saga effects which help to make asynchronous calls. Check this for more information.
import { call } from 'redux-saga/effects'
function* authorize(user, password) {
try {
const response = yield call(/** Api call */, user, password)
...
} catch(error) {
...
}
}
Note
If you don't want to use yield, you can directly call you API with params using axios or fetch.
Hope this helps you. Feel free for doubts.

Kotlin: lambda run alternative scenario

I have userDto, contains programs, which contains actual field. Actual program can be only one. I need to get it. Than, I run this:
userDto.programs.sortedBy { it.created }.findLast { it.actual }?
Okay, but I want to foresee case, when findLast returns null, & throw exception. Please, advice, how to do it?
UPD:
ProgramType.valueOf(userDto.programs
.sortedBy { it.created }
.findLast { it.actual }
//check here
!!.programType!!).percentage
You are pretty close actually :)! What you could do is:
userDto.programs.sortedBy { it.created }.findLast { it.actual } ?: throw RuntimeException()
Or if you're trying to actually avoid throwing an error(couldn't really tell with the way question is asked), you could just do an error check like this:
userDto.programs.sortedBy { it.created }.findLast { it.actual }?.let{
//rest of your code goes here
}
Hope this helps, cheers!

XCode UITesting check if a text field exists

I can't find any way to check if a text field exists without trying to get it which then fails the tests and shows an error if it can't be found. No matches found for TextField
Current code
XCUIElement *usernameTextField = app.textFields[#"username"];
Reason/detail
I've got a Objective C UITest in XCode which logs into my app in setUp and logs out in tearDown however sometimes my app is already logged in when the test starts (if the simulator has been used for anything else in the meantime). I'd like to be able to check to see if the username textfield exists in my setUp and then if it doesn't I can skip the login or call my logout function and continue as normal.
Not sure about Obj-C but here's how it would work in Swift.
let usernameTextField = app.textFields["username"]
if usernameTextField.exists {
do something
} else {
do something else
}
Here is the code in Swift, which can easily be converted to Obj-C:
// given:
// usernameTextField exists
// The username that is possibly entered there is "username".
// then:
if usernameTextField.value as! String == "username" {
// logged in
} else {
// not logged in
}