in progress!
This commit is contained in:
@@ -1,22 +1,29 @@
|
||||
package com.example.quiz;
|
||||
|
||||
import android.graphics.Color;
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
import android.os.Bundle;
|
||||
|
||||
import com.anggastudio.printama.Pref;
|
||||
import com.anggastudio.printama.Printama;
|
||||
import com.example.quiz.utils.BluetoothUtils;
|
||||
import com.example.quiz.data.model.dtos.auth.LoginPayload;
|
||||
import com.example.quiz.data.model.dtos.auth.LoginResponse;
|
||||
import com.example.quiz.utils.Result;
|
||||
import com.example.quiz.utils.SessionManager;
|
||||
import com.example.quiz.utils.SharedPrefsHelper;
|
||||
import com.example.quiz.viewModel.LoginViewModel;
|
||||
import com.google.android.material.snackbar.Snackbar;
|
||||
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import android.os.Handler;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.lifecycle.Observer;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.navigation.NavController;
|
||||
import androidx.navigation.Navigation;
|
||||
import androidx.navigation.ui.AppBarConfiguration;
|
||||
@@ -29,7 +36,10 @@ import dagger.hilt.android.AndroidEntryPoint;
|
||||
@AndroidEntryPoint
|
||||
public class PageQuiz extends AppCompatActivity {
|
||||
|
||||
LoginViewModel viewModel;
|
||||
private SessionManager sessionManager;
|
||||
|
||||
private AlertDialog dialog;
|
||||
private Handler handler = new Handler();
|
||||
private Runnable checkRunnable;
|
||||
|
||||
@@ -38,48 +48,15 @@ public class PageQuiz extends AppCompatActivity {
|
||||
|
||||
private SharedPrefsHelper prefsHelper;
|
||||
|
||||
private void requestPermission(){
|
||||
Pref.init(getApplicationContext());
|
||||
if (BluetoothUtils.needsBluetoothPermissions()) {
|
||||
if (!BluetoothUtils.hasBluetoothPermission(getApplicationContext())) {
|
||||
// Demande la permission si non accordée
|
||||
BluetoothUtils.requestBluetoothPermission(this);
|
||||
return; // arrête ici, la popup va apparaître
|
||||
}
|
||||
}
|
||||
|
||||
// 2️⃣ Permission OK, on peut afficher la liste
|
||||
try {
|
||||
Printama printama = Printama.with(getApplicationContext());
|
||||
if(!printama.isConnected()){
|
||||
BluetoothUtils.showPrinterList(getApplicationContext(), this);
|
||||
}
|
||||
} catch (SecurityException e) {
|
||||
Toast.makeText(getApplicationContext(),
|
||||
"Permission Bluetooth non accordée", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
requestPermission();
|
||||
binding = ActivityPageQuizBinding.inflate(getLayoutInflater());
|
||||
_showPinDialog();
|
||||
setContentView(binding.getRoot());
|
||||
sessionManager = new SessionManager();
|
||||
|
||||
// Vérification périodique
|
||||
checkRunnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (sessionManager.isExpired()) {
|
||||
Toast.makeText(getApplicationContext(), "I'm herer", Toast.LENGTH_LONG).show();
|
||||
} else {
|
||||
handler.postDelayed(this, 60 * 1000);
|
||||
}
|
||||
}
|
||||
};
|
||||
handler.postDelayed(checkRunnable, 60 * 1000);
|
||||
prefsHelper = SharedPrefsHelper.getInstance(getApplicationContext());
|
||||
viewModel = new ViewModelProvider(this).get(LoginViewModel.class);
|
||||
setSupportActionBar(binding.toolbar);
|
||||
binding.toolbar.setBackgroundColor(Color.parseColor("#501C5A29"));
|
||||
binding.fab.setOnClickListener(new View.OnClickListener() {
|
||||
@@ -90,22 +67,97 @@ public class PageQuiz extends AppCompatActivity {
|
||||
.setAction("Action", null).show();
|
||||
}
|
||||
});
|
||||
prefsHelper = SharedPrefsHelper.getInstance(getApplicationContext());
|
||||
FragmentManager fragmentManager = getSupportFragmentManager();
|
||||
}
|
||||
|
||||
if(prefsHelper.get("username") != null){
|
||||
ListOfReunions reunions = ListOfReunions.newInstance(prefsHelper.get("username"));
|
||||
fragmentManager
|
||||
.beginTransaction()
|
||||
.replace(R.id.nav_host_fragment_content_main, reunions)
|
||||
.commit();
|
||||
}else{
|
||||
Login login = Login.newInstance();
|
||||
fragmentManager
|
||||
.beginTransaction()
|
||||
.replace(R.id.nav_host_fragment_content_main, login)
|
||||
.commit();
|
||||
@Override
|
||||
public boolean dispatchTouchEvent(MotionEvent ev) {
|
||||
|
||||
if (dialog != null && dialog.isShowing()) {
|
||||
return super.dispatchTouchEvent(ev);
|
||||
}
|
||||
|
||||
|
||||
if(sessionManager.isExpired()){
|
||||
_showPinDialog();
|
||||
return true;
|
||||
}
|
||||
sessionManager.updateLastExpiredDate();
|
||||
return super.dispatchTouchEvent(ev);
|
||||
}
|
||||
|
||||
void _showPinDialog(){
|
||||
LayoutInflater inflater = getLayoutInflater();
|
||||
View view = inflater.inflate(R.layout.pin_view, null);
|
||||
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
||||
builder.setView(view);
|
||||
builder.setCancelable(false);
|
||||
|
||||
dialog = builder.create();
|
||||
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
|
||||
|
||||
// Bouton OK
|
||||
view.findViewById(R.id.pin_validate).setOnClickListener(v -> {
|
||||
TextView pin = view.findViewById(R.id.pin);
|
||||
if(pin.getText().toString().isEmpty()){
|
||||
pin.setError("Entrez le pin!");
|
||||
return;
|
||||
}
|
||||
if(pin.getText().toString().length() != 5){
|
||||
pin.setError("Le pin doit contenir 5 chiffres!");
|
||||
return;
|
||||
}
|
||||
LoginPayload loginPayload = new LoginPayload(
|
||||
pin.getText().toString()
|
||||
);
|
||||
viewModel.login(loginPayload).observe(this, new Observer<Result<LoginResponse>>() {
|
||||
@Override
|
||||
public void onChanged(Result<LoginResponse> loginResponseResult) {
|
||||
switch (loginResponseResult.status){
|
||||
case LOADING:
|
||||
Toast.makeText(getApplicationContext(), "Loading",Toast.LENGTH_SHORT).show();
|
||||
break;
|
||||
case ERROR:
|
||||
pin.setError("Pin incorrect");
|
||||
break;
|
||||
case SUCCESS:
|
||||
loginSuccess(loginResponseResult.data);
|
||||
sessionManager.updateLastExpiredDate();
|
||||
dialog.dismiss();
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
|
||||
private void loginSuccess(LoginResponse loginResponse){
|
||||
prefsHelper.save("firstName", loginResponse.getPrenom());
|
||||
prefsHelper.save("lastName", loginResponse.getNom());
|
||||
prefsHelper.save("profile", loginResponse.getProfile());
|
||||
prefsHelper.save("limit", String.valueOf(loginResponse.getLimiteSuperieure()));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
handler = new Handler();
|
||||
checkRunnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (sessionManager.isExpired()) {
|
||||
if(dialog != null && !dialog.isShowing()){
|
||||
_showPinDialog();
|
||||
}
|
||||
} else {
|
||||
handler.postDelayed(this, 10); // check every second
|
||||
}
|
||||
}
|
||||
};
|
||||
handler.postDelayed(checkRunnable, 10);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user