daily pastebin goal
21%
SHARE
TWEET

Untitled

a guest Apr 16th, 2018 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.platfomni.rulybka.ui.menu;
  2.  
  3. import android.Manifest;
  4. import android.arch.lifecycle.ViewModelProvider;
  5. import android.arch.lifecycle.ViewModelProviders;
  6. import android.content.ActivityNotFoundException;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.content.pm.PackageManager;
  10. import android.net.Uri;
  11. import android.os.Build;
  12. import android.os.Bundle;
  13. import android.support.annotation.NonNull;
  14. import android.support.annotation.Nullable;
  15. import android.view.View;
  16. import android.widget.LinearLayout;
  17.  
  18. import com.google.gson.internal.LinkedTreeMap;
  19. import com.mbiamont.ga_annotations.annotation.TrackEvent;
  20. import com.mbiamont.ga_annotations.annotation.TrackScreen;
  21. import com.platfomni.rulybka.R;
  22. import com.platfomni.rulybka.analytics.AnEvents;
  23. import com.platfomni.rulybka.ui.auth.AuthActivity;
  24. import com.platfomni.rulybka.ui.city_choose.CityChooseActivity;
  25. import com.platfomni.rulybka.ui.common.BasePageInjectableFragment;
  26. import com.platfomni.rulybka.ui.dashboard.DashboardFragment;
  27. import com.platfomni.rulybka.ui.feedback.FeedbackFragment;
  28. import com.platfomni.rulybka.ui.web.WebFragment;
  29. import com.platfomni.rulybka.ui.widget.MenuItemView;
  30. import com.platfomni.rulybka.util.GAUtils;
  31. import com.platfomni.rulybka.util.MailUtils;
  32. import com.platfomni.rulybka.valueobject.Client;
  33. import com.platfomni.rulybka.valueobject.Parameter;
  34. import com.platfomni.rulybka.valueobject.Resource;
  35.  
  36. import java.util.Map;
  37.  
  38. import javax.inject.Inject;
  39.  
  40. import butterknife.BindView;
  41. import butterknife.OnClick;
  42.  
  43. public class MenuFragment extends BasePageInjectableFragment {
  44.  
  45.     @BindView(R.id.my_city)
  46.     protected MenuItemView myCity;
  47.  
  48.     @BindView(R.id.report)
  49.     protected LinearLayout report;
  50.  
  51.     Client client;
  52.     String messageTheme;
  53.  
  54.  
  55.     @Inject
  56.     ViewModelProvider.Factory viewModelFactory;
  57.  
  58.     private MenuViewModel viewModel;
  59.  
  60.     public MenuFragment() {
  61.         // Required empty public constructor
  62.     }
  63.  
  64.     public static MenuFragment newInstance() {
  65.         return new MenuFragment();
  66.     }
  67.  
  68.     @TrackScreen(name = AnEvents.SCREEN_MENU)
  69.     @Override
  70.     public void onCreate(@Nullable Bundle savedInstanceState) {
  71.         super.onCreate(savedInstanceState);
  72.     }
  73.  
  74.     @Override
  75.     public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
  76.         super.onViewCreated(view, savedInstanceState);
  77.     }
  78.  
  79.     @Override
  80.     public void onResume() {
  81.         super.onResume();
  82.         viewModel.getClient().observe(this, this::showClient);
  83.     }
  84.  
  85.     @Override
  86.     public void onActivityCreated(@Nullable final Bundle savedInstanceState) {
  87.         super.onActivityCreated(savedInstanceState);
  88.  
  89.         viewModel = ViewModelProviders.of(this, viewModelFactory).get(MenuViewModel.class);
  90.         viewModel.getCurrentCity().observe(this, city -> {
  91.             myCity.setClarification(city.getName());
  92.         });
  93.         viewModel.getClient().observe(this, this::showClient);
  94.  
  95.     }
  96.  
  97.     private void showClient(Resource<Client> resource) {
  98.         if (resource != null) {
  99.             client = resource.data;
  100.         }
  101.     }
  102.  
  103.     @Override
  104.     public CharSequence getTitle(final Context context, final int toolbarFreeWidth) {
  105.         return context.getString(R.string.label_menu);
  106.     }
  107.  
  108.     @Override
  109.     protected int getLayoutResId() {
  110.         return R.layout.menu_fragment;
  111.     }
  112.  
  113.     @TrackEvent(action = AnEvents.TRANSITION_SPECIALS_TO_REPORT, category = AnEvents.CATEGORY_INFO, label = AnEvents.LABEL_VIEW)
  114.     @OnClick(R.id.report)
  115.     protected void onReportClick() {
  116.  
  117.         viewModel.getErrorNumber().observe(this, this::sendMail);
  118.     }
  119.  
  120.     private void openUrl(Resource<Parameter> resource) {
  121.  
  122.         if (resource.isSucceed() && resource.data != null) {
  123.             switch (resource.data.getKey()) {
  124.                 case Parameter.SOCIAL_URL_TM:
  125.                     openTelegram(resource.data.getValue());
  126.                     break;
  127.                 case Parameter.SOCIAL_URL_IG:
  128.                     openInstagram(resource.data.getValue());
  129.                     break;
  130.                 case Parameter.SOCIAL_URL_VK:
  131.                     openVK(resource.data.getValue());
  132.                     break;
  133.                 case Parameter.SOCIAL_URL_OK:
  134.                     openOK(resource.data.getValue());
  135.                     break;
  136.             }
  137.         }
  138.     }
  139.  
  140.  
  141.     private void sendMail(Resource<Object> objectResource) {
  142.         if (objectResource.isLoading()) return;
  143.         if (objectResource.isError()) {
  144.             messageTheme = "Сообщение о проблеме";
  145.         } else if (objectResource.data != null) {
  146.             Map<String, String> mapError = ((LinkedTreeMap) objectResource.data);
  147.             String errorNumber = mapError.get("error_number");
  148.             messageTheme = "Сообщение о проблеме № " + errorNumber;
  149.         }
  150.  
  151.         if (Build.VERSION.SDK_INT >= 23) {
  152.             if (getActivity().checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
  153.                 MailUtils.sendUserMail(getActivity(), getString(R.string.email_support), true, messageTheme);
  154.             } else {
  155.                 requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
  156.             }
  157.         } else {
  158.             MailUtils.sendUserMail(getActivity(), getString(R.string.email_support), true, messageTheme);
  159.         }
  160.     }
  161.  
  162.     @Override
  163.     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  164.         super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  165.         if (requestCode == 1) {
  166.             boolean havePermissions = grantResults[0] == PackageManager.PERMISSION_GRANTED;
  167.             MailUtils.sendUserMail(getActivity(), getString(R.string.email_support), havePermissions, messageTheme);
  168.         }
  169.     }
  170.  
  171.     @TrackEvent(action = AnEvents.TRANSITION_SPECIALS_TO_LK, category = AnEvents.CATEGORY_INFO, label = AnEvents.LABEL_VIEW)
  172.     @OnClick(R.id.dashboard)
  173.     protected void onDashBoardClick() {
  174.         GAUtils.sendEvent(GAUtils.TRANSITION_SPECIALS_TO_LK);
  175.         if (viewModel.hasAccount()) {
  176.             nextFragment(DashboardFragment.newInstance());
  177.         } else {
  178.             AuthActivity.start(getActivity());
  179.         }
  180.     }
  181.  
  182.     @TrackEvent(action = AnEvents.TRANSITION_SPECIALS_TO_SPECIALS_FEEDBACK, category = AnEvents.CATEGORY_INFO, label = AnEvents.LABEL_VIEW)
  183.     @OnClick(R.id.feedback)
  184.     protected void onFeedbackClick() {
  185.         GAUtils.sendEvent(GAUtils.TRANSITION_SPECIALS_TO_SPECIALS_FEEDBACK);
  186.         nextFragment(FeedbackFragment.newInstance());
  187.     }
  188.  
  189.     @TrackEvent(action = AnEvents.TRANSITION_SPECIALS_TO_CITY_CHOOSE, category = AnEvents.CATEGORY_INFO, label = AnEvents.LABEL_VIEW)
  190.     @OnClick(R.id.my_city)
  191.     protected void onMyCityClick() {
  192.         GAUtils.sendEvent(GAUtils.TRANSITION_SPECIALS_TO_CITY_CHOOSE);
  193.         CityChooseActivity.start(getActivity());
  194.     }
  195.  
  196.     @TrackEvent(action = AnEvents.TRANSITION_SPECIALS_TO_AGREEMENTS, category = AnEvents.CATEGORY_INFO, label = AnEvents.LABEL_VIEW)
  197.     @OnClick(R.id.agreement)
  198.     protected void onAgreementClick() {
  199.         GAUtils.sendEvent(GAUtils.TRANSITION_SPECIALS_TO_AGREEMENTS);
  200.         nextFragment(WebFragment.newInstance(WebFragment.TYPE_AGREEMENT));
  201.     }
  202.  
  203.     @TrackEvent(action = AnEvents.TRANSITION_SPECIALS_TO_AGREEMENTS, category = AnEvents.CATEGORY_INFO, label = AnEvents.LABEL_VIEW)
  204.     @OnClick(R.id.talkAboutProblem)
  205.     protected void onProblemClick() {
  206.         GAUtils.sendEvent(GAUtils.TRANSITION_SPECIALS_TO_AGREEMENTS);
  207.         viewModel.getErrorNumber().observe(this, this::sendMail);
  208.     }
  209.  
  210.     @TrackEvent(action = AnEvents.TRANSITION_SPECIALS_TO_AGREEMENTS, category = AnEvents.CATEGORY_INFO, label = AnEvents.LABEL_VIEW)
  211.     @OnClick({R.id.telegram, R.id.oklassniki, R.id.instagram, R.id.vk})
  212.     protected void onMessangerClick(View v) {
  213.         String key = "";
  214.         switch (v.getId()) {
  215.             case R.id.telegram:
  216.                 viewModel.getTelegramUrl().observe(this, this::openUrl);
  217.                 break;
  218.             case R.id.instagram:
  219.                 viewModel.getInstaUrl().observe(this, this::openUrl);
  220.                 break;
  221.             case R.id.vk:
  222.                 viewModel.getVkUrl().observe(this, this::openUrl);
  223.                 break;
  224.             case R.id.oklassniki:
  225.                 viewModel.getOkUrl().observe(this, this::openUrl);
  226.                 break;
  227.         }
  228.     }
  229.  
  230.     private void openTelegram(String value) {
  231.         try {
  232.             Uri telegramUri = Uri.parse("tg://resolve?domain=" + value.split("me/")[1]);
  233.             Intent telegramIntent = new Intent(Intent.ACTION_VIEW, telegramUri);
  234.             startActivity(telegramIntent);
  235.         } catch (ActivityNotFoundException e) {
  236.             startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(value)));
  237.         } catch (Exception e) {
  238.             e.getLocalizedMessage();
  239.         }
  240.     }
  241.  
  242.     private void openInstagram(String value) {
  243.         try {
  244.             Uri instaUri = Uri.parse("http://instagram.com/_u/" + value.split("com/")[1]);
  245.             Intent telegramIntent = new Intent(Intent.ACTION_VIEW, instaUri);
  246.             startActivity(telegramIntent);
  247.         } catch (ActivityNotFoundException e) {
  248.             startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(value)));
  249.         } catch (Exception e) {
  250.             e.getLocalizedMessage();
  251.         }
  252.     }
  253.  
  254.     private void openVK(String value) {
  255.         try {
  256.             Uri vkUri = Uri.parse(value);
  257.             Intent vkIntent = new Intent(Intent.ACTION_VIEW, vkUri);
  258.             vkIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  259.             startActivity(vkIntent);
  260.         } catch (ActivityNotFoundException e) {
  261.             startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(value)));
  262.         } catch (Exception e) {
  263.             e.getLocalizedMessage();
  264.         }
  265.     }
  266.  
  267.     private void openOK(String value) {
  268.  
  269.         try {
  270.             Uri okUri = Uri.parse(value);
  271.             Intent okIntent = new Intent(Intent.ACTION_VIEW, okUri);
  272.             startActivity(okIntent);
  273.         } catch (ActivityNotFoundException e) {
  274.             startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(value)));
  275.         } catch (Exception e) {
  276.             e.getLocalizedMessage();
  277.         }
  278.     }
  279.  
  280. }
RAW Paste Data
Top