Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package justin_martin.portadiil;
- import android.support.design.widget.NavigationView;
- import android.support.design.widget.Snackbar;
- import android.support.v4.content.ContextCompat;
- import android.support.v4.view.GravityCompat;
- import android.support.v4.widget.DrawerLayout;
- import android.support.v7.app.ActionBarDrawerToggle;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.Toolbar;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.animation.AccelerateDecelerateInterpolator;
- import android.widget.TextView;
- import com.eftimoff.androipathview.PathView;
- import com.google.firebase.auth.FirebaseAuth;
- import com.google.firebase.auth.FirebaseUser;
- import com.google.firebase.database.DataSnapshot;
- import com.google.firebase.database.DatabaseError;
- import com.google.firebase.database.DatabaseReference;
- import com.google.firebase.database.FirebaseDatabase;
- import com.google.firebase.database.ValueEventListener;
- import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
- import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings;
- import com.roger.catloadinglibrary.CatLoadingView;
- import net.frederico.showtipsview.ShowTipsBuilder;
- import net.frederico.showtipsview.ShowTipsView;
- import net.frederico.showtipsview.ShowTipsViewInterface;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.TimeZone;
- import justin_martin.portadiil.Activity.About.AboutActivity;
- import justin_martin.portadiil.Activity.Admin.AdminActivity;
- import justin_martin.portadiil.Activity.Agenda.AgendaActivity;
- import justin_martin.portadiil.Activity.Event.AddEventActivity;
- import justin_martin.portadiil.Activity.Event.ViewEventActivity;
- import justin_martin.portadiil.Activity.Fidelity.FidelityCardActivity;
- import justin_martin.portadiil.Activity.Fidelity.FidelityCardAddPointsActivity;
- import justin_martin.portadiil.Activity.HomePageActivity;
- import justin_martin.portadiil.Activity.Idea.ViewIdeaActivity;
- import justin_martin.portadiil.Activity.Settings.SettingsActivity;
- import justin_martin.portadiil.Model.AppTheme;
- import justin_martin.portadiil.Model.ConfigAccess;
- import justin_martin.portadiil.Model.User.User;
- /**
- * Created by justin on 18/10/2017.
- */
- public class MasterActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, Constant {
- /*== Loading Dialog ==*/
- private CatLoadingView loadingView;
- private boolean loadingDisplay = false;
- /*=== Firebase ===*/
- public final static FirebaseAuth mAuth = FirebaseAuth.getInstance(); //instance de l'application
- public FirebaseUser user;
- /*===REFERENCES===*/
- public static DatabaseReference mBase;
- public static DatabaseReference mUserReference;
- public static DatabaseReference mIdeaReference;
- public static DatabaseReference mEventReference;
- public static DatabaseReference mConfigAccessReference;
- public static DatabaseReference mLibsReference;
- public static DatabaseReference mInfosReference;
- private static DatabaseReference mAgendaReference;
- private static DatabaseReference mDevsReference;
- public static User userConnected;
- public static ConfigAccess configAccess;
- /*=== AGENDA ==*/
- public boolean noActionBar = false;
- public AppTheme appTheme;
- public FirebaseRemoteConfig mFirebaseRemoteConfig;
- /*============*/
- private static boolean isInitialized = false;
- private boolean asNav = false;
- private MasterActivity thisActivity;
- public static DateFormat dfm = new SimpleDateFormat("dd-MM-yyyy HH:mm");
- public List<User> usersList = new ArrayList<>();
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- dfm.setTimeZone(TimeZone.getTimeZone("Europe/Paris"));
- /*========== Agenda Filter =============*/
- if(this instanceof AgendaActivity || this instanceof AddEventActivity){
- appTheme = new AppTheme(getApplicationContext(), noActionBar);
- setTheme(appTheme.getStyle());
- }
- /*======================================*/
- super.onCreate(savedInstanceState);
- thisActivity = this;
- /*============ Agenda =============*/
- if(!isInitialized){
- FirebaseDatabase.getInstance().setPersistenceEnabled(true);
- isInitialized=true;
- }
- mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
- final FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
- .setDeveloperModeEnabled(false)
- .build();
- mFirebaseRemoteConfig.setConfigSettings(configSettings);
- /*===================================*/
- mBase = FirebaseDatabase.getInstance().getReference();
- mUserReference = mBase.child("users");
- mEventReference = mBase.child("events");
- mIdeaReference = mBase.child("ideas");
- mDevsReference = mBase.child("ConfigApp").child("AboutStorage").child("listDeveloppers");
- mInfosReference = mBase.child("ConfigApp").child("AboutStorage").child("listInfo");
- mLibsReference = mBase.child("ConfigApp").child("AboutStorage").child("listLibs");
- mConfigAccessReference = mBase.child("ConfigApp").child("ConfigAccess");
- mAgendaReference = mBase.child("agenda").child("departement");
- //RAJOUTER LA DATE
- if (isConnected()) {
- final ValueEventListener userConnectedListener = new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- userConnected = dataSnapshot.getValue(User.class);
- if(userConnected.isToDelete()) deleteAccount();
- if(thisActivity!=null && asNav) disalbeItemAuthLvl();
- changeNbPoint();
- changeUsersList();
- changeUsersListAddPoint();
- // ...
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- // Getting Post failed, log a message
- Log.w("loadPost:onCancelled", databaseError.toException());
- // ...
- }
- };
- mUserReference.child(user.getUid()).addValueEventListener(userConnectedListener);
- final ValueEventListener configAccessListener = new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- configAccess = dataSnapshot.getValue(ConfigAccess.class);
- dimissLoadingDialog();
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- }
- };
- mConfigAccessReference.addValueEventListener(configAccessListener);
- }
- }
- public boolean isConnected() {
- user = mAuth.getCurrentUser();
- if (user == null) return false;
- else return true;
- }
- public void logout() {
- mAuth.signOut();
- }
- public void addNav(){
- if(asNav){
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
- NavigationView navView = (NavigationView) findViewById(R.id.nav_view);
- navView.inflateMenu(R.menu.menu_admin_drawer);
- navView.setNavigationItemSelectedListener(this);
- ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
- this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
- drawer.setDrawerListener(toggle);
- toggle.syncState();
- TextView textView = navView.getHeaderView(0).getRootView().findViewById(R.id.versionAppTextView);
- textView.append(BuildConfig.VERSION_NAME);
- if (this instanceof ViewEventActivity)
- navView.getHeaderView(0).setBackground(getDrawable(R.drawable.nav_bar_header_bg_event));
- else if (this instanceof ViewIdeaActivity)
- navView.getHeaderView(0).setBackground(getDrawable(R.drawable.nav_bar_header_bg_idea));
- else if (this instanceof FidelityCardActivity)
- navView.getHeaderView(0).setBackground(getDrawable(R.drawable.nav_bar_header_bg_fidelity));
- }
- }
- public static int safeLongToInt(long l) {
- int i = (int)l;
- if ((long)i != l) {
- throw new IllegalArgumentException(l + " cannot be cast to int without changing its value.");
- }
- return i;
- }
- public void setAsNav(boolean asNav){
- this.asNav = asNav;
- }
- private void changeNbPoint() {
- if (this instanceof FidelityCardActivity) {
- ((FidelityCardActivity) this).getNbTampons();
- }
- }
- private void changeUsersList() {
- if (this instanceof FidelityCardActivity) {
- ((FidelityCardActivity) this).fillUsersList();
- }
- }
- private void changeUsersListAddPoint() {
- if (this instanceof FidelityCardAddPointsActivity) {
- ((FidelityCardAddPointsActivity) this).fillUsersList();
- }
- }
- public ShowTipsView howToUse(View target, String title, String description, final ShowTipsView nextActive, final MasterActivity displayActivity){
- int BUTTON_NEXT_BG = ContextCompat.getColor(getBaseContext(), R.color.colorAccent);
- int BUTTON_NEXT_TEXTCOLOR = ContextCompat.getColor(getBaseContext(), R.color.colorWhite);
- ShowTipsView showtips = new ShowTipsBuilder(this)
- .setTarget(target)
- .setTitle(title)
- .setDescription(description)
- .setDelay(100)
- .setButtonText(BUTTON_NEXT_TEXT)
- .setCloseButtonColor(BUTTON_NEXT_BG)
- .setCloseButtonTextColor(BUTTON_NEXT_TEXTCOLOR)
- .setBackgroundAlpha(40)
- .setCircleColor(BUTTON_NEXT_BG)
- .build();
- showtips.setCallback(new ShowTipsViewInterface(){
- @Override
- public void gotItClicked() {
- nextActive.show(displayActivity);
- }
- });
- return showtips;
- }
- public ShowTipsView howToUse(final View target, String title, String description){
- int BUTTON_NEXT_BG = ContextCompat.getColor(getBaseContext(), R.color.colorAccent);
- int BUTTON_NEXT_TEXTCOLOR = ContextCompat.getColor(getBaseContext(), R.color.colorWhite);
- ShowTipsView showtips = new ShowTipsBuilder(this)
- .setTarget(target)
- .setTitle(title)
- .setDescription(description)
- .setDelay(100)
- .setButtonText(BUTTON_NEXT_TEXT)
- .setCloseButtonColor(BUTTON_NEXT_BG)
- .setCloseButtonTextColor(BUTTON_NEXT_TEXTCOLOR)
- .setBackgroundAlpha(40)
- .setCircleColor(BUTTON_NEXT_BG)
- .build();
- showtips.setCallback(new ShowTipsViewInterface(){
- @Override
- public void gotItClicked() {
- SharedPreferences preferences = getSharedPreferences(PREFERENCES_FILE, MODE_PRIVATE);
- SharedPreferences.Editor editor = preferences.edit();
- Log.e("tag", getResources().getResourceEntryName(target.getId()));
- editor.putBoolean(getResources().getResourceEntryName(target.getId()), true);
- editor.apply();
- }
- });
- return showtips;
- }
- public void maintenanceActivity() {
- findViewById(R.id.classicView).setVisibility(View.INVISIBLE);
- findViewById(R.id.maintenanceView).setVisibility(View.VISIBLE);
- final PathView pathView = (PathView) findViewById(R.id.pathView);
- pathView.getPathAnimator()
- .delay(100)
- .duration(2000)
- .interpolator(new AccelerateDecelerateInterpolator())
- .start();
- }
- private void deleteAccount() {
- mUserReference.child(userConnected.getUid()).removeValue();
- user.delete();
- }
- public void showSnackBar(String snackbarText) {
- Snackbar mySnackbar = Snackbar.make(findViewById(android.R.id.content),
- snackbarText, Snackbar.LENGTH_SHORT);
- mySnackbar.show();
- }
- public void showLoadingDialog(){
- if(!loadingDisplay) {
- loadingView = new CatLoadingView();
- loadingView.show(getSupportFragmentManager(), "");
- loadingDisplay = true;
- }
- }
- public void dimissLoadingDialog(){
- if(loadingDisplay){
- loadingView.getDialog().cancel();
- loadingDisplay = false;
- }
- }
- public String generateToken(int length) {
- String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; // Tu supprimes les lettres dont tu ne veux pas
- String pass = "";
- for (int x = 0; x < length; x++) {
- int i = (int) Math.floor(Math.random() * 62); // Si tu supprimes des lettres tu diminues ce nb
- pass += chars.charAt(i);
- }
- return pass;
- }
- protected void disalbeItemAuthLvl() {
- final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
- Menu menuNav = navigationView.getMenu();
- final DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
- final List<Integer> ressourceListToDisable = new ArrayList<>();
- final List<MenuItem> menuItemListToDisable = new ArrayList<>();
- String userRole = userConnected.getAuthLvl();
- switch (userRole) {
- case "Non adhérent":
- menuItemListToDisable.add(menuNav.getItem(4));
- menuItemListToDisable.add(menuNav.getItem(5));
- ressourceListToDisable.add(R.id.nav_administration);
- ressourceListToDisable.add(R.id.nav_fidelity);
- break;
- case "Adhérent":
- menuItemListToDisable.add(menuNav.getItem(5));
- ressourceListToDisable.add(R.id.nav_administration);
- break;
- }
- drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
- @Override
- public void onDrawerSlide(View drawerView, float slideOffset)
- {
- }
- @RequiresApi(api = Build.VERSION_CODES.M)
- @Override
- public void onDrawerOpened(View drawerView) {
- for (MenuItem item : menuItemListToDisable) {
- item.setEnabled(false);
- }
- }
- @Override
- public void onDrawerClosed(View drawerView) {
- }
- @Override
- public void onDrawerStateChanged(int newState) {
- }
- });
- }
- @Override
- public boolean onNavigationItemSelected(MenuItem item) {
- // Handle navigation view item clicks here.
- int id = item.getItemId();
- Intent switchAcitity = new Intent(this, HomePageActivity.class);
- Log.e("item", item.getItemId()+"");
- if (id == R.id.nav_home) {
- switchAcitity = new Intent(this, HomePageActivity.class);
- } else if (id == R.id.nav_administration) {
- switchAcitity = new Intent(this, AdminActivity.class);
- } else if (id == R.id.nav_agenda) {
- switchAcitity = new Intent(this, AgendaActivity.class);
- } else if (id == R.id.nav_event) {
- switchAcitity = new Intent(this, ViewEventActivity.class);
- } else if (id == R.id.nav_fidelity) {
- switchAcitity = new Intent(this, FidelityCardActivity.class);
- } else if (id == R.id.nav_idea) {
- switchAcitity = new Intent(this, ViewIdeaActivity.class);
- } else if (id == R.id.nav_logout) {
- logout();
- }else if (id == R.id.nav_param) {
- switchAcitity = new Intent(this, SettingsActivity.class);
- } else if (id == R.id.nav_about) {
- switchAcitity = new Intent(this, AboutActivity.class);
- }
- DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
- startActivity(switchAcitity);
- return true;
- }
- @Override
- public void onBackPressed() {
- if(asNav) {
- DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
- if (drawer.isDrawerOpen(GravityCompat.START)) {
- drawer.closeDrawer(GravityCompat.START);
- } else {
- if(this instanceof HomePageActivity) return;
- super.onBackPressed();
- }
- }
- else {
- if(this instanceof HomePageActivity) return;
- super.onBackPressed();
- }
- }
- public static DatabaseReference getmBase() {
- return mBase;
- }
- public static DatabaseReference getmUserReference() {
- return mUserReference;
- }
- public static DatabaseReference getmIdeaReference() {
- return mIdeaReference;
- }
- public static DatabaseReference getmEventReference() {
- return mEventReference;
- }
- public static User getUserConnected() {
- return userConnected;
- }
- public static FirebaseAuth getmAuth() {
- return mAuth;
- }
- public static DatabaseReference getmLibsReference() {
- return mLibsReference;
- }
- public static DatabaseReference getmInfosReference() {
- return mInfosReference;
- }
- public static DatabaseReference getmAgendaReference() {return mAgendaReference;}
- public static ConfigAccess getConfigAccess() {
- return configAccess;
- }
- public static DatabaseReference getmDevsReference() {
- return mDevsReference;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement