Guest User

Homepage

a guest
Jan 20th, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.07 KB | None | 0 0
  1.  
  2. public class HomePage extends AppCompatActivity {
  3. private Drawer result = null;
  4. private Boolean isCoverEdit = false, isProfileEdit = false;
  5. String username;
  6. private RelativeLayout splash;
  7. private Toolbar toolbar;
  8. private StorageReference mStorage;
  9. private ProgressDialog progressDialog;
  10. private ImageView searchBtn;
  11. private AHBottomNavigationViewPager fragContainer;
  12. private AHBottomNavigation bottomNavigation;
  13. private ImageView postBtn;
  14. private View child;
  15. private ImageView cover;
  16. private CircleImageView profilePic;
  17. private TextView star;
  18. private TextView id;
  19. private PopupMenu p;
  20.  
  21.  
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_home_page);
  26. if (FirebaseAuth.getInstance().getCurrentUser() == null) {
  27. Intent i = new Intent(HomePage.this, Login.class);
  28. startActivity(i);
  29. finish();
  30. } else {
  31. getUsername();
  32. initiateViews();
  33. setupOnClickListeners();
  34. setupBottomNavigation();
  35.  
  36. mStorage = FirebaseStorage.getInstance().getReference();
  37. progressDialog = new ProgressDialog(this);
  38.  
  39. }
  40.  
  41.  
  42. }
  43.  
  44.  
  45. private void getUsername() {
  46. username = new UserData(this).getUsername();
  47.  
  48. if (username==null){
  49. FirebaseDatabase.getInstance().getReference().child("users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("username").addListenerForSingleValueEvent(new ValueEventListener() {
  50. @Override
  51. public void onDataChange(DataSnapshot dataSnapshot) {
  52. username = dataSnapshot.getValue(String.class);
  53. SharedPreferences settings = getSharedPreferences("AyePref", MODE_PRIVATE);
  54. SharedPreferences.Editor editor = settings.edit();
  55. editor.putString("username", username);
  56. editor.apply();
  57. setupNavigationDrawer();
  58. FirebaseDatabase.getInstance().getReference().child("userdata").child(username).child("token").setValue(FirebaseInstanceId.getInstance().getToken());
  59.  
  60. }
  61.  
  62. @Override
  63. public void onCancelled(DatabaseError databaseError) {
  64.  
  65. }
  66. });
  67. }else {
  68. setupNavigationDrawer();
  69. FirebaseDatabase.getInstance().getReference().child("userdata").child(username).child("token").setValue(FirebaseInstanceId.getInstance().getToken());
  70. }
  71. }
  72.  
  73. private void setupNavigationDrawer() {
  74. result = new DrawerBuilder()
  75. .withActivity(HomePage.this)
  76. .withHeader(child)
  77. .withDisplayBelowStatusBar(false)
  78. .withTranslucentStatusBar(false)
  79. .withDrawerLayout(R.layout.material_drawer_fits_not)
  80. .addDrawerItems(
  81. new PrimaryDrawerItem().withName("Favourites").withIcon(GoogleMaterial.Icon.gmd_forum),
  82. new PrimaryDrawerItem().withName("Settings").withIcon(GoogleMaterial.Icon.gmd_settings),
  83. new PrimaryDrawerItem().withName("Contact").withIcon(GoogleMaterial.Icon.gmd_contact_mail),
  84. new PrimaryDrawerItem().withName("Commands").withIcon(GoogleMaterial.Icon.gmd_help),
  85. new PrimaryDrawerItem().withName("Log Out").withIcon(GoogleMaterial.Icon.gmd_security)
  86. )
  87.  
  88. .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
  89. @Override
  90. public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
  91. if (drawerItem instanceof Nameable) {
  92. FragmentTransaction t = getSupportFragmentManager().beginTransaction();
  93. switch (position) {
  94. case 3:
  95. final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
  96. emailIntent.setType("plain/text");
  97. emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"ayesupport@yandex.com"});
  98. startActivity(Intent.createChooser(emailIntent, "Send mail..."));
  99. break;
  100. case 5:
  101.  
  102. break;
  103. case 2:
  104. Intent i= new Intent(HomePage.this, SettingsActivity.class);
  105. startActivity(i);
  106. break;
  107. case 4:
  108. Intent i2 = new Intent(HomePage.this,CommandsPage.class);
  109. startActivity(i2);
  110. break;
  111.  
  112. }
  113.  
  114.  
  115. }
  116.  
  117. return false;
  118. }
  119. }).build();
  120.  
  121. FirebaseDatabase.getInstance().getReference().child("userdata").child(username).addListenerForSingleValueEvent(new ValueEventListener() {
  122. @Override
  123. public void onDataChange(DataSnapshot dataSnapshot) {
  124. try {
  125. Glide.with(HomePage.this).load(dataSnapshot.child("pic").getValue(String.class)).override(100,100).thumbnail(0.5f).into(profilePic);
  126. //Glide.with(HomePage.this).load(dataSnapshot.child("cover").getValue(String.class)).into(cover);
  127.  
  128. } catch (Exception e) {
  129. e.printStackTrace();
  130. }
  131. id.setText(username);
  132. star.setText("4.7");
  133.  
  134. }
  135.  
  136. @Override
  137. public void onCancelled(DatabaseError databaseError) {
  138.  
  139. }
  140. });
  141. }
  142.  
  143. private void setupBottomNavigation() {
  144. AHBottomNavigationItem item1 = new AHBottomNavigationItem("Topics", new IconicsDrawable(this, GoogleMaterial.Icon.gmd_whatshot));
  145. AHBottomNavigationItem item2 = new AHBottomNavigationItem("Rooms", new IconicsDrawable(this, GoogleMaterial.Icon.gmd_group_work));
  146. AHBottomNavigationItem item3 = new AHBottomNavigationItem("Contacts", new IconicsDrawable(this, GoogleMaterial.Icon.gmd_people));
  147. AHBottomNavigationItem item4 = new AHBottomNavigationItem("Leaderboard", new IconicsDrawable(this, GoogleMaterial.Icon.gmd_star));
  148. AHBottomNavigationItem item5 = new AHBottomNavigationItem("Stats", new IconicsDrawable(this, GoogleMaterial.Icon.gmd_insert_chart));
  149. bottomNavigation.addItem(item1);
  150. bottomNavigation.addItem(item2);
  151. bottomNavigation.addItem(item3);
  152. bottomNavigation.addItem(item4);
  153. bottomNavigation.addItem(item5);
  154. bottomNavigation.setInactiveColor(R.color.md_grey_800);
  155. bottomNavigation.setAccentColor(R.color.md_grey_600);
  156. bottomNavigation.setTitleState(AHBottomNavigation.TitleState.ALWAYS_HIDE);
  157. fragContainer.setOffscreenPageLimit(4);
  158. HomePageAdapter adapter = new HomePageAdapter(getSupportFragmentManager());
  159. setSupportActionBar(toolbar);
  160. toolbar.setNavigationIcon(R.drawable.ic_menu_white_24dp);
  161. //fragContainer.setAdapter(adapter);
  162. bottomNavigation.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() {
  163. @Override
  164. public boolean onTabSelected(int position, boolean wasSelected) {
  165. //fragContainer.setCurrentItem(position);
  166. return wasSelected;
  167.  
  168. }
  169. });
  170. }
  171.  
  172. private void setupOnClickListeners() {
  173. searchBtn.setOnClickListener(new View.OnClickListener() {
  174. @Override
  175. public void onClick(View view) {
  176. Intent i = new Intent(HomePage.this, SearchPage.class);
  177. startActivity(i);
  178. }
  179. });
  180.  
  181. postBtn.setOnClickListener(new View.OnClickListener() {
  182. @Override
  183. public void onClick(View view) {
  184. p = new PopupMenu(HomePage.this, postBtn);
  185. p.inflate(R.menu.add);
  186. p.show();
  187. p.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
  188. @Override
  189. public boolean onMenuItemClick(MenuItem menuItem) {
  190. if (menuItem.getItemId() == R.id.action_topic) {
  191. Intent i = new Intent(HomePage.this, WritePost.class);
  192. startActivity(i);
  193. } else {
  194. Intent i = new Intent(HomePage.this, CreateGroupPage.class);
  195. startActivity(i);
  196. }
  197. return false;
  198. }
  199. });
  200.  
  201. }
  202. });
  203.  
  204. child.setOnClickListener(new View.OnClickListener() {
  205. @Override
  206. public void onClick(View view) {
  207. p = new PopupMenu(HomePage.this, profilePic);
  208. p.inflate(R.menu.profile_header);
  209. p.show();
  210. p.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
  211. @Override
  212. public boolean onMenuItemClick(MenuItem menuItem) {
  213. switch (menuItem.getItemId()) {
  214. case R.id.action_profile:
  215. Intent i2 = new Intent(HomePage.this, ProfileView.class);
  216. i2.putExtra("username", username);
  217. i2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  218. startActivity(i2);
  219. break;
  220. case R.id.action_pic:
  221. isCoverEdit = false;
  222. isProfileEdit = true;
  223. EasyImage.openChooserWithGallery(HomePage.this, "Select Image", 10);
  224. break;
  225. case R.id.action_cover:
  226. isCoverEdit = true;
  227. isProfileEdit = false;
  228. EasyImage.openChooserWithGallery(HomePage.this, "Select Image", 10);
  229. break;
  230.  
  231. }
  232. return false;
  233. }
  234. });
  235. }
  236. });
  237. }
  238.  
  239. private void initiateViews() {
  240. splash = findViewById(R.id.splash);
  241. searchBtn = findViewById(R.id.search);
  242. fragContainer = findViewById(R.id.frame);
  243. toolbar = findViewById(R.id.toolbar);
  244. bottomNavigation = findViewById(R.id.bottom_navigation);
  245. postBtn = findViewById(R.id.post);
  246. child = getLayoutInflater().inflate(R.layout.header, null);
  247. profilePic = child.findViewById(R.id.pic);
  248. id = child.findViewById(R.id.id);
  249. cover = child.findViewById(R.id.cover);
  250. star = child.findViewById(R.id.karma);
  251. }
  252.  
  253. @Override
  254. public boolean onOptionsItemSelected(MenuItem item) {
  255. switch (item.getItemId()) {
  256. case android.R.id.home:
  257. result.openDrawer();
  258. return true;
  259.  
  260. default:
  261. return super.onOptionsItemSelected(item);
  262. }
  263. }
  264.  
  265. @Override
  266. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  267. super.onActivityResult(requestCode, resultCode, data);
  268. if (resultCode == RESULT_OK && requestCode == UCrop.REQUEST_CROP) {
  269. final Uri resultUri = UCrop.getOutput(data);
  270. uploadImage(resultUri);
  271.  
  272. }
  273. EasyImage.handleActivityResult(requestCode, resultCode, data, this, new DefaultCallback() {
  274. @Override
  275. public void onImagePickerError(Exception e, EasyImage.ImageSource source, int type) {
  276.  
  277. }
  278.  
  279. @Override
  280. public void onImagePicked(File imageFile, EasyImage.ImageSource source, int type) {
  281. Log.i("Image URI ", imageFile.getAbsolutePath());
  282. openEditor(Uri.fromFile(imageFile));
  283.  
  284. }
  285.  
  286.  
  287. });
  288. }
  289.  
  290. private void openEditor(Uri absolutePath) {
  291. File folder = new File(Environment.getExternalStorageDirectory() +
  292. File.separator + "Aye/Image/");
  293. boolean success = true;
  294. if (!folder.exists()) {
  295. success = folder.mkdirs();
  296. }
  297. if (success) {
  298. String imageURI = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Aye/Image/" + UUID.randomUUID().toString() + ".jpg";
  299. if (isCoverEdit){
  300. UCrop.of(absolutePath, Uri.parse(imageURI))
  301. .withAspectRatio(16, 9)
  302. .withMaxResultSize(1280, 720)
  303. .start(HomePage.this);
  304. }else {
  305. UCrop.of(absolutePath, Uri.parse(imageURI))
  306. .withAspectRatio(5, 5)
  307. .withMaxResultSize(1280, 720)
  308. .start(HomePage.this);
  309. }
  310.  
  311.  
  312.  
  313. } else {
  314. Toast.makeText(this, R.string.accept_premission, Toast.LENGTH_SHORT).show();
  315. }
  316. }
  317.  
  318. private void uploadImage(Uri resultUri) {
  319.  
  320.  
  321. StorageReference filepath = mStorage.child("bu").child(username).child("image").child(UUID.randomUUID().toString());
  322. filepath.putFile(resultUri).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
  323. @Override
  324. public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
  325. progressDialog.setMessage(getString(R.string.uploading));
  326. progressDialog.show();
  327. }
  328. }).addOnFailureListener(new OnFailureListener() {
  329. @Override
  330. public void onFailure(@NonNull Exception e) {
  331. progressDialog.dismiss();
  332. e.printStackTrace();
  333. }
  334. }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
  335. @Override
  336. public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
  337. progressDialog.dismiss();
  338. if (isCoverEdit){
  339. FirebaseDatabase.getInstance().getReference().child("userdata").child(username).child("cover").setValue(taskSnapshot.getDownloadUrl());
  340.  
  341. }else {
  342. FirebaseDatabase.getInstance().getReference().child("userdata").child(username).child("pic").setValue(taskSnapshot.getDownloadUrl().toString());
  343.  
  344. }
  345.  
  346. }
  347. });
  348. }
  349.  
  350. @Override
  351. public void onBackPressed() {
  352. if (result != null && result.isDrawerOpen()) {
  353. result.closeDrawer();
  354. } else {
  355. AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
  356. this);
  357. alertDialogBuilder
  358. .setMessage("Are you sure you want to exit?")
  359. .setCancelable(true)
  360. .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
  361. @Override
  362. public void onClick(DialogInterface dialog, int which) {
  363. finish();
  364. }
  365. }).setNegativeButton("No", new DialogInterface.OnClickListener() {
  366. @Override
  367. public void onClick(DialogInterface dialog, int which) {
  368. dialog.cancel();
  369. }
  370. });
  371.  
  372. AlertDialog alertDialog = alertDialogBuilder.create();
  373.  
  374. alertDialog.show();
  375. }
  376. }
  377.  
  378. public void dismissSplashScreen(){
  379. toolbar.setVisibility(View.VISIBLE);
  380. splash.setVisibility(View.GONE);
  381. }
  382. }
Add Comment
Please, Sign In to add comment