Skip to content

Instantly share code, notes, and snippets.

@kasali kasali/UserRepo.java Secret
Created Apr 1, 2019

Embed
What would you like to do?
public LiveData<User> getUser( final String email,final String mdp) {
service=Webprovide.getClient().create(UserService.class);
Call<User> callUser=service.getUserRemote(email,mdp);
callUser.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
Log.d(TAG, "onResponse: "+response.code());
if(response.isSuccessful())
{
data.setValue(response.body());
}
// else
// {
// user=userDao.getUser(email,mdp);
// }
}
@Override
public void onFailure(Call<User> call, Throwable t) {
Log.d(TAG, "onFailure: "+t.getMessage());
data.setValue(null);
}
});
return user;
}
the code where i get the error is in the MainActivity class (line 30):
public void isvalid(final String login, final String mdp) {
userViewModel.getUser(login,mdp).observe(this, new Observer<User>() {
@Override
public void onChanged(@Nullable User user) {
if (user!=null)
{
item=user.getRole();
if(item.equals("Chef des services"))
{
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
finish();
}
if(item.equals("Agent logistique"))
{
Intent intent=new Intent(MainActivity.this,Main2Activityag.class);
startActivity(intent);
finish();
}
if(item.equals("Responsable de classe")||item.equals("Professeur principal")||item.equals("Surveillant général")||item.equals("Chef des vigiles"))
{
Intent intent=new Intent(MainActivity.this,ClientActivity.class);
id=user.getId();
Log.d(TAG, "onChanged: "+id);
intent.putExtra(ClientActivity.idu,id);
startActivity(intent);
finish();
}
}
else {
loader.setVisibility(View.INVISIBLE);
Toast.makeText(MainActivity.this, "login and password incorrect!", Toast.LENGTH_SHORT).show();
}
}
});
}
@kasali

This comment has been minimized.

Copy link
Owner Author

commented Apr 3, 2019

this is the class where i get error:

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.wang.avi.AVLoadingIndicatorView;

import sn.ba.mba.app_report.R;
import sn.ba.mba.app_report.presentator.ReportViewModel.ReportviewModel_User;
import sn.ba.mba.app_report.presentator.Repository.UserRepository;
import sn.ba.mba.app_report.presentator.model.Materiel;
import sn.ba.mba.app_report.presentator.model.User;

public class MainActivity extends AppCompatActivity {
EditText edit_email, edit_pwd;
Button connect;
TextView reset;
// Spinner role;
int id;
private Toolbar toolbar1;
private String mdpe="";
private String log="";
private String item;
ReportviewModel_User userViewModel;
private User mUser;
private static final String TAG = "MainActivity";
private AVLoadingIndicatorView loader;

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	if (userViewModel==null)
	{
		userViewModel = ViewModelProviders.of(this).get(ReportviewModel_User.class);
	}

	edit_email = findViewById(R.id.edit_email);
	edit_pwd = findViewById(R.id.password);
	connect = findViewById(R.id.btn_c);
	loader=findViewById(R.id.loader);
	reset = findViewById(R.id.reset);
	connect.setOnClickListener(new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			log = edit_email.getText().toString().trim();
			mdpe = edit_pwd.getText().toString().trim();
			notMatch();
			loader.setVisibility(View.VISIBLE);
			isvalid(log, mdpe);

// loader.setVisibility(View.INVISIBLE);
vide();
}
});
}

public void isvalid(String login,String mdp) {
	userViewModel.getUser(login,mdp).observe(this, new Observer<User>() {
		@Override
		public void onChanged(@Nullable User user) {
			if (user!=null)
			{
				item=user.getRole();
				if(item.equals("Chef des services"))
				{
					Intent intent=new Intent(MainActivity.this,Main2Activity.class);
					startActivity(intent);
					finish();
				}
				if(item.equals("Agent logistique"))
				{
					Intent intent=new Intent(MainActivity.this,Main2Activityag.class);
					startActivity(intent);
					finish();
				}
				if(item.equals("Responsable de classe")||item.equals("Professeur principal")||item.equals("Surveillant général")||item.equals("Chef des vigiles"))
				{
					Intent intent=new Intent(MainActivity.this,ClientActivity.class);
					id=user.getId();
					Log.d(TAG, "onChanged: "+id);
					intent.putExtra(ClientActivity.idu,id);
					startActivity(intent);
					finish();
				}

			}
			else {
				loader.setVisibility(View.INVISIBLE);
				Toast.makeText(MainActivity.this, "Veuillez vérifier vos identifiants!", Toast.LENGTH_SHORT).show();
			}
		}

	});
}
public void vide()
{
	edit_email.setText("");
	edit_pwd.setText("");
}
public void notMatch()
{
	if (TextUtils.isEmpty(log) || TextUtils.isEmpty(mdpe)) {
		loader.setVisibility(View.INVISIBLE);

		Toast.makeText(MainActivity.this, "Veuillez remplir tous les champs!", Toast.LENGTH_SHORT).show();
		vide();
	}

}

}

and here we have the repository where i call the api:

import android.app.Application;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.os.AsyncTask;
import android.util.Log;

import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import sn.ba.mba.app_report.presentator.Database.Database_Project;
import sn.ba.mba.app_report.presentator.Network.UserService;
import sn.ba.mba.app_report.presentator.Network.Webprovide;
import sn.ba.mba.app_report.presentator.model.Dao.UserDao;
import sn.ba.mba.app_report.presentator.model.User;

import static android.support.constraint.Constraints.TAG;

public class UserRepository {
private UserDao userDao;
private LiveData<List> allusers;
LiveData user;
final MutableLiveData data = new MutableLiveData<>();
private UserService service;

public UserRepository(Application app) {
	Database_Project database= Database_Project.getInstance(app);
	userDao=database.userDao();
	allusers=userDao.getUsers();

}
public void insert(User user)
{
	new Insert_Asyntask(userDao).execute(user);
}
public void insertS(String nom, String username,String email, String mdp, String role)
{
	service = Webprovide.getClient().create(UserService.class);
	User user=new User(nom,username,email,mdp,role);
	user.setNom(nom);
	user.setUsername(username);
	user.setRole(role);
	user.setEmail(email);
	user.setMdp(mdp);
	Call<User> call=service.createUser(user.getNom(),user.getUsername(),user.getEmail(),user.getMdp(),user.getRole());
	Log.d("user", "insertS: "+call.request());
	call.enqueue(new Callback<User>() {
		@Override
		public void onResponse(Call<User> call, Response<User> response) {
			Log.d("user", "insertS: "+call);
			Log.d("Response", "onResponse: "+response.body());
			data.setValue(response.body());
			Log.d("role", "insertS: "+response.body());

		}

		@Override
		public void onFailure(Call<User> call, Throwable t) {
			Log.d("Onfaillue", "onFailure: "+t.getLocalizedMessage() );
			Log.d("getmessage", "onFailure: "+t.getMessage());
			Log.d("Userservieces", "onFailure: "+service.toString());
			Log.d("on", "onFailure: ");
		}
	});
}
public void update(User user)
{
	new Update_Asyntask(userDao).execute(user);
}
public void delete(User user)
{
	new Delete_Asyntask(userDao).execute(user);
}
public LiveData<User> findMaterialsBorrowedByUser(int fk)
{
	return userDao.findMaterialsBorrowedByUser(fk);
}
public LiveData<List<User>> getAllusers(){

	return allusers;
}
public LiveData<User> getUser( final String email,final String mdp)  {
	service=Webprovide.getClient().create(UserService.class);
	Call<User> callUser=service.getUserRemote(email,mdp);
	callUser.enqueue(new Callback<User>() {
		@Override
		public void onResponse(Call<User> call, Response<User> response) {
			Log.d(TAG, "onResponse: "+response.code());
			if(response.isSuccessful())
			{
                data.setValue(response.body());
                user=data;
			}

// else
// {
// user=userDao.getUser(email,mdp);
// }
}

		@Override
		public void onFailure(Call<User> call, Throwable t) {
			Log.d(TAG, "onFailure: "+t.getMessage());
			data.setValue(null);

		}
	});
	return user;
}

private static class Insert_Asyntask extends AsyncTask<User, Void, Void>
{
	public Insert_Asyntask(UserDao userdao) {
		this.userdao = userdao;
	}

	private UserDao userdao;

	@Override
	protected Void doInBackground(User... users) {
		userdao.insert(users[0]);
		return null;
	}
}
private static class Update_Asyntask extends AsyncTask<User, Void, Void>
{
	public Update_Asyntask(UserDao userdao) {
		this.userdao = userdao;
	}

	private UserDao userdao;

	@Override
	protected Void doInBackground(User... users) {
		userdao.update(users[0]);
		return null;
	}
}
private static class Delete_Asyntask extends AsyncTask<User, Void, Void>
{
	public Delete_Asyntask(UserDao userdao) {
		this.userdao = userdao;
	}

	private UserDao userdao;

	@Override
	protected Void doInBackground(User... users) {
		userdao.delete(users[0]);
		return null;
	}


}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.