Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class HomePage extends AppCompatActivity {
- private Drawer result = null;
- private Boolean isCoverEdit = false, isProfileEdit = false;
- String username;
- private RelativeLayout splash;
- private Toolbar toolbar;
- private StorageReference mStorage;
- private ProgressDialog progressDialog;
- private ImageView searchBtn;
- private AHBottomNavigationViewPager fragContainer;
- private AHBottomNavigation bottomNavigation;
- private ImageView postBtn;
- private View child;
- private ImageView cover;
- private CircleImageView profilePic;
- private TextView star;
- private TextView id;
- private PopupMenu p;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_home_page);
- if (FirebaseAuth.getInstance().getCurrentUser() == null) {
- Intent i = new Intent(HomePage.this, Login.class);
- startActivity(i);
- finish();
- } else {
- getUsername();
- initiateViews();
- setupOnClickListeners();
- setupBottomNavigation();
- mStorage = FirebaseStorage.getInstance().getReference();
- progressDialog = new ProgressDialog(this);
- }
- }
- private void getUsername() {
- username = new UserData(this).getUsername();
- if (username==null){
- FirebaseDatabase.getInstance().getReference().child("users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("username").addListenerForSingleValueEvent(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- username = dataSnapshot.getValue(String.class);
- SharedPreferences settings = getSharedPreferences("AyePref", MODE_PRIVATE);
- SharedPreferences.Editor editor = settings.edit();
- editor.putString("username", username);
- editor.apply();
- setupNavigationDrawer();
- FirebaseDatabase.getInstance().getReference().child("userdata").child(username).child("token").setValue(FirebaseInstanceId.getInstance().getToken());
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- }
- });
- }else {
- setupNavigationDrawer();
- FirebaseDatabase.getInstance().getReference().child("userdata").child(username).child("token").setValue(FirebaseInstanceId.getInstance().getToken());
- }
- }
- private void setupNavigationDrawer() {
- result = new DrawerBuilder()
- .withActivity(HomePage.this)
- .withHeader(child)
- .withDisplayBelowStatusBar(false)
- .withTranslucentStatusBar(false)
- .withDrawerLayout(R.layout.material_drawer_fits_not)
- .addDrawerItems(
- new PrimaryDrawerItem().withName("Favourites").withIcon(GoogleMaterial.Icon.gmd_forum),
- new PrimaryDrawerItem().withName("Settings").withIcon(GoogleMaterial.Icon.gmd_settings),
- new PrimaryDrawerItem().withName("Contact").withIcon(GoogleMaterial.Icon.gmd_contact_mail),
- new PrimaryDrawerItem().withName("Commands").withIcon(GoogleMaterial.Icon.gmd_help),
- new PrimaryDrawerItem().withName("Log Out").withIcon(GoogleMaterial.Icon.gmd_security)
- )
- .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
- @Override
- public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
- if (drawerItem instanceof Nameable) {
- FragmentTransaction t = getSupportFragmentManager().beginTransaction();
- switch (position) {
- case 3:
- final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
- emailIntent.setType("plain/text");
- emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"ayesupport@yandex.com"});
- startActivity(Intent.createChooser(emailIntent, "Send mail..."));
- break;
- case 5:
- break;
- case 2:
- Intent i= new Intent(HomePage.this, SettingsActivity.class);
- startActivity(i);
- break;
- case 4:
- Intent i2 = new Intent(HomePage.this,CommandsPage.class);
- startActivity(i2);
- break;
- }
- }
- return false;
- }
- }).build();
- FirebaseDatabase.getInstance().getReference().child("userdata").child(username).addListenerForSingleValueEvent(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- try {
- Glide.with(HomePage.this).load(dataSnapshot.child("pic").getValue(String.class)).override(100,100).thumbnail(0.5f).into(profilePic);
- //Glide.with(HomePage.this).load(dataSnapshot.child("cover").getValue(String.class)).into(cover);
- } catch (Exception e) {
- e.printStackTrace();
- }
- id.setText(username);
- star.setText("4.7");
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- }
- });
- }
- private void setupBottomNavigation() {
- AHBottomNavigationItem item1 = new AHBottomNavigationItem("Topics", new IconicsDrawable(this, GoogleMaterial.Icon.gmd_whatshot));
- AHBottomNavigationItem item2 = new AHBottomNavigationItem("Rooms", new IconicsDrawable(this, GoogleMaterial.Icon.gmd_group_work));
- AHBottomNavigationItem item3 = new AHBottomNavigationItem("Contacts", new IconicsDrawable(this, GoogleMaterial.Icon.gmd_people));
- AHBottomNavigationItem item4 = new AHBottomNavigationItem("Leaderboard", new IconicsDrawable(this, GoogleMaterial.Icon.gmd_star));
- AHBottomNavigationItem item5 = new AHBottomNavigationItem("Stats", new IconicsDrawable(this, GoogleMaterial.Icon.gmd_insert_chart));
- bottomNavigation.addItem(item1);
- bottomNavigation.addItem(item2);
- bottomNavigation.addItem(item3);
- bottomNavigation.addItem(item4);
- bottomNavigation.addItem(item5);
- bottomNavigation.setInactiveColor(R.color.md_grey_800);
- bottomNavigation.setAccentColor(R.color.md_grey_600);
- bottomNavigation.setTitleState(AHBottomNavigation.TitleState.ALWAYS_HIDE);
- fragContainer.setOffscreenPageLimit(4);
- HomePageAdapter adapter = new HomePageAdapter(getSupportFragmentManager());
- setSupportActionBar(toolbar);
- toolbar.setNavigationIcon(R.drawable.ic_menu_white_24dp);
- //fragContainer.setAdapter(adapter);
- bottomNavigation.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() {
- @Override
- public boolean onTabSelected(int position, boolean wasSelected) {
- //fragContainer.setCurrentItem(position);
- return wasSelected;
- }
- });
- }
- private void setupOnClickListeners() {
- searchBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent i = new Intent(HomePage.this, SearchPage.class);
- startActivity(i);
- }
- });
- postBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- p = new PopupMenu(HomePage.this, postBtn);
- p.inflate(R.menu.add);
- p.show();
- p.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
- @Override
- public boolean onMenuItemClick(MenuItem menuItem) {
- if (menuItem.getItemId() == R.id.action_topic) {
- Intent i = new Intent(HomePage.this, WritePost.class);
- startActivity(i);
- } else {
- Intent i = new Intent(HomePage.this, CreateGroupPage.class);
- startActivity(i);
- }
- return false;
- }
- });
- }
- });
- child.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- p = new PopupMenu(HomePage.this, profilePic);
- p.inflate(R.menu.profile_header);
- p.show();
- p.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
- @Override
- public boolean onMenuItemClick(MenuItem menuItem) {
- switch (menuItem.getItemId()) {
- case R.id.action_profile:
- Intent i2 = new Intent(HomePage.this, ProfileView.class);
- i2.putExtra("username", username);
- i2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- startActivity(i2);
- break;
- case R.id.action_pic:
- isCoverEdit = false;
- isProfileEdit = true;
- EasyImage.openChooserWithGallery(HomePage.this, "Select Image", 10);
- break;
- case R.id.action_cover:
- isCoverEdit = true;
- isProfileEdit = false;
- EasyImage.openChooserWithGallery(HomePage.this, "Select Image", 10);
- break;
- }
- return false;
- }
- });
- }
- });
- }
- private void initiateViews() {
- splash = findViewById(R.id.splash);
- searchBtn = findViewById(R.id.search);
- fragContainer = findViewById(R.id.frame);
- toolbar = findViewById(R.id.toolbar);
- bottomNavigation = findViewById(R.id.bottom_navigation);
- postBtn = findViewById(R.id.post);
- child = getLayoutInflater().inflate(R.layout.header, null);
- profilePic = child.findViewById(R.id.pic);
- id = child.findViewById(R.id.id);
- cover = child.findViewById(R.id.cover);
- star = child.findViewById(R.id.karma);
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case android.R.id.home:
- result.openDrawer();
- return true;
- default:
- return super.onOptionsItemSelected(item);
- }
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if (resultCode == RESULT_OK && requestCode == UCrop.REQUEST_CROP) {
- final Uri resultUri = UCrop.getOutput(data);
- uploadImage(resultUri);
- }
- EasyImage.handleActivityResult(requestCode, resultCode, data, this, new DefaultCallback() {
- @Override
- public void onImagePickerError(Exception e, EasyImage.ImageSource source, int type) {
- }
- @Override
- public void onImagePicked(File imageFile, EasyImage.ImageSource source, int type) {
- Log.i("Image URI ", imageFile.getAbsolutePath());
- openEditor(Uri.fromFile(imageFile));
- }
- });
- }
- private void openEditor(Uri absolutePath) {
- File folder = new File(Environment.getExternalStorageDirectory() +
- File.separator + "Aye/Image/");
- boolean success = true;
- if (!folder.exists()) {
- success = folder.mkdirs();
- }
- if (success) {
- String imageURI = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Aye/Image/" + UUID.randomUUID().toString() + ".jpg";
- if (isCoverEdit){
- UCrop.of(absolutePath, Uri.parse(imageURI))
- .withAspectRatio(16, 9)
- .withMaxResultSize(1280, 720)
- .start(HomePage.this);
- }else {
- UCrop.of(absolutePath, Uri.parse(imageURI))
- .withAspectRatio(5, 5)
- .withMaxResultSize(1280, 720)
- .start(HomePage.this);
- }
- } else {
- Toast.makeText(this, R.string.accept_premission, Toast.LENGTH_SHORT).show();
- }
- }
- private void uploadImage(Uri resultUri) {
- StorageReference filepath = mStorage.child("bu").child(username).child("image").child(UUID.randomUUID().toString());
- filepath.putFile(resultUri).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
- @Override
- public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
- progressDialog.setMessage(getString(R.string.uploading));
- progressDialog.show();
- }
- }).addOnFailureListener(new OnFailureListener() {
- @Override
- public void onFailure(@NonNull Exception e) {
- progressDialog.dismiss();
- e.printStackTrace();
- }
- }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
- @Override
- public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
- progressDialog.dismiss();
- if (isCoverEdit){
- FirebaseDatabase.getInstance().getReference().child("userdata").child(username).child("cover").setValue(taskSnapshot.getDownloadUrl());
- }else {
- FirebaseDatabase.getInstance().getReference().child("userdata").child(username).child("pic").setValue(taskSnapshot.getDownloadUrl().toString());
- }
- }
- });
- }
- @Override
- public void onBackPressed() {
- if (result != null && result.isDrawerOpen()) {
- result.closeDrawer();
- } else {
- AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
- this);
- alertDialogBuilder
- .setMessage("Are you sure you want to exit?")
- .setCancelable(true)
- .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- finish();
- }
- }).setNegativeButton("No", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.cancel();
- }
- });
- AlertDialog alertDialog = alertDialogBuilder.create();
- alertDialog.show();
- }
- }
- public void dismissSplashScreen(){
- toolbar.setVisibility(View.VISIBLE);
- splash.setVisibility(View.GONE);
- }
- }
Add Comment
Please, Sign In to add comment