Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.22 KB | None | 0 0
  1. package com.melnikov.universus;
  2.  
  3.  
  4. import android.animation.Animator;
  5. import android.animation.AnimatorListenerAdapter;
  6. import android.animation.ObjectAnimator;
  7. import android.content.Intent;
  8. import android.graphics.Bitmap;
  9. import android.graphics.BitmapFactory;
  10. import android.graphics.drawable.BitmapDrawable;
  11. import android.os.AsyncTask;
  12. import android.os.Bundle;
  13. import android.support.annotation.Nullable;
  14. import android.support.v4.app.Fragment;
  15. import android.util.Log;
  16. import android.util.Pair;
  17. import android.view.LayoutInflater;
  18. import android.view.View;
  19. import android.view.ViewGroup;
  20. import android.view.animation.Animation;
  21. import android.view.animation.AnimationUtils;
  22. import android.widget.ImageView;
  23. import android.widget.LinearLayout;
  24. import android.widget.RelativeLayout;
  25. import android.widget.TextView;
  26. import android.widget.Toast;
  27.  
  28. import com.squareup.picasso.Picasso;
  29.  
  30. import org.json.JSONArray;
  31. import org.json.JSONException;
  32. import org.json.JSONObject;
  33. import org.w3c.dom.Text;
  34.  
  35. import java.io.IOException;
  36. import java.util.ArrayList;
  37. import java.util.List;
  38.  
  39. import static android.support.constraint.Constraints.TAG;
  40. import static com.melnikov.universus.LoginActivity.TOKEN;
  41. import static com.melnikov.universus.LoginActivity.mToken;
  42.  
  43. public class SwiperFragment extends Fragment {
  44.  
  45.     static int index = 0;
  46.     private static List<Girl> mItems = new ArrayList<>();
  47.     private ImageView mLikeButton;
  48.     private ImageView mDislikeButton;
  49.     private static ImageView mGirlPic;
  50.     private TextView mGirlName;
  51.     private TextView mGirlUrl;
  52.     private TextView mGirlUniversity;
  53.     private static ImageView mGirlPicNext;
  54.     private TextView mGirlNameNext;
  55.     private TextView mGirlUrlNext;
  56.     private TextView mGirlUniversityNext;
  57.     private RelativeLayout mContainer;
  58.     private LinearLayout mUniversityContainer;
  59.     private LinearLayout mPickContainer;
  60.     Animation animation;
  61.     Animation animationBack;
  62.     static boolean like;
  63.     public static Bitmap bitmap;
  64.     public static ImageView photo;
  65.     public static Bitmap bitmapNext;
  66.     public static ImageView photoNext;
  67.     View view;
  68.  
  69.     public SwiperFragment() {
  70.         // Required empty public constructor
  71.     }
  72.  
  73.     @Override
  74.     public void onCreate(@Nullable Bundle savedInstanceState) {
  75.         super.onCreate(savedInstanceState);
  76.     }
  77.  
  78.  
  79.     @Override
  80.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  81.                              Bundle savedInstanceState) {
  82.         view = inflater.inflate(R.layout.fragment_swiper, container, false);
  83.         mLikeButton = view.findViewById(R.id.swiper_like_button);
  84.         mDislikeButton = view.findViewById(R.id.swiper_dislike_button);
  85.         mContainer = view.findViewById(R.id.swiper_container);
  86.         mPickContainer = view.findViewById(R.id.swiper_pick_container);
  87.         animation = AnimationUtils.loadAnimation(getContext(), R.anim.scale);
  88.         animationBack = AnimationUtils.loadAnimation(getContext(), R.anim.scale_back);
  89.  
  90.         mGirlPic = view.findViewById(R.id.swiper_item_girl_pic);
  91.         mGirlName = view.findViewById(R.id.swiper_item_girl_name);
  92.         mGirlUrl = view.findViewById(R.id.swiper_item_girl_url);
  93.         mGirlUniversity = view.findViewById(R.id.swiper_item_girl_university);
  94.  
  95.         mGirlPicNext = view.findViewById(R.id.swiper_item_girl_pic_next);
  96.         mGirlNameNext = view.findViewById(R.id.swiper_item_girl_name_next);
  97.         mGirlUrlNext = view.findViewById(R.id.swiper_item_girl_url_next);
  98.         mGirlUniversityNext = view.findViewById(R.id.swiper_item_girl_university_next);
  99.  
  100.         SetGirls();
  101.         mLikeButton.setOnClickListener(new View.OnClickListener() {
  102.             @Override
  103.             public void onClick(View view) {
  104.                 //для отправки пачками(пока нет)
  105.                 //Data.getInstance().girlLikes.add(new Pair<Girl, Boolean>(mItems.get(index), true));
  106.                 like = true;
  107.                 new GetNextGirlTask().execute();
  108.             }
  109.         });
  110.         mDislikeButton.setOnClickListener(new View.OnClickListener() {
  111.             @Override
  112.             public void onClick(View view) {
  113.                 //для отправки пачками(пока нет)
  114.                 //Data.getInstance().girlLikes.add(new Pair<Girl, Boolean>(mItems.get(index), false));
  115.                 like = false;
  116.                 new GetNextGirlTask().execute();
  117.             }
  118.         });
  119.         return view;
  120.     }
  121.  
  122.     //kek
  123.     class GetNextGirlTask extends AsyncTask<Void, Void, Boolean> {
  124.  
  125.         @Override
  126.         protected Boolean doInBackground(Void... params) {
  127.             Boolean q = sendResult(like, mItems.get(index).getID());
  128.             index++;
  129.             return q;
  130.         }
  131.  
  132.         @Override
  133.         protected void onPostExecute(Boolean q) {
  134.             if (q & index < mItems.size()) {
  135.                 SetGirls();
  136.             } else {
  137.                 new TimeTask().execute();
  138.             }
  139.         }
  140.     }
  141.  
  142.     private static boolean sendResult(boolean like, int ID) {
  143.         try {
  144.             String s;
  145.             if (like)
  146.                 s = "1";
  147.             else
  148.                 s = "0";
  149.             String t = mToken.getString(TOKEN, "");
  150.             String url = LoginActivity.server + "swiper_result/" + t + "/" + ID + "/" + s;
  151.             String jsonString = ListGetter.getUrlString(url);
  152.             Log.i(TAG, "Received JSON: " + jsonString);
  153.             JSONObject jsonBody = new JSONObject(jsonString);
  154.             String status = (String) jsonBody.get("status");
  155.             switch (status) {
  156.                 case "Confirm the registration at first": {
  157.                     //пользователь должен сначала зарегестрироваться
  158.                     return false;
  159.                 }
  160.                 case "You have used all the votes": {
  161.                     return false;
  162.                 }
  163.                 case "ok": {
  164.                     return true;
  165.                 }
  166.                 case "Token is invalid": {
  167.                     Log.i(TAG, "Послал неверный токен!!!" + "LOL");
  168.                     // какой-то бред, пусть прога упадет
  169.                 }
  170.             }
  171.         } catch (java.io.IOException e) {
  172.             System.out.println(e);
  173.             Log.i(TAG, "Failed to send the request");
  174.         } catch (JSONException jo) {
  175.             Log.i(TAG, "Failed to receive JSONbody");
  176.         }
  177.         return false;
  178.     }
  179.  
  180.     //kek
  181.     public void SetGirls() {
  182.         mGirlName.setText(mItems.get(index).getName());
  183.         mGirlUrl.setText(mItems.get(index).getUrl());
  184.         mGirlUniversity.setText(mItems.get(index).getUniversity().getName());
  185.         //photo
  186.         if (index > 0) {
  187.             bitmap = bitmapNext;
  188.         } else {
  189.             //photo
  190.             /*Picasso.get().load(mItems.get(index).getProfile_pic_url_instagram()).into(mGirlPic);
  191.             bitmap = ((BitmapDrawable) mGirlPic.getDrawable()).getBitmap();*/
  192.             try {
  193.                 byte[] urlbytes = ListGetter.getUrlBytes(mItems.get(index).getProfile_pic_url_instagram());
  194.                 bitmap = BitmapFactory.decodeByteArray(urlbytes, 0, urlbytes.length);
  195.             } catch (IOException e) {
  196.                 System.out.println(e);
  197.             }
  198.         }
  199.         mGirlPic.setImageBitmap(ImageHelper.getRoundedCornerBitmap(bitmap, 100));
  200.  
  201.         if (index < mItems.size() - 1) {
  202.             mGirlNameNext.setText(mItems.get(index + 1).getName());
  203.             mGirlUrlNext.setText(mItems.get(index + 1).getUrl());
  204.             mGirlUniversityNext.setText(mItems.get(index + 1).getUniversity().getName());
  205.  
  206.             //photo of next
  207.             /*Picasso.get().load(mItems.get(index + 1).getProfile_pic_url_instagram()).into(mGirlPicNext);
  208.             bitmapNext = ((BitmapDrawable) mGirlPicNext.getDrawable()).getBitmap();*/
  209.             try {
  210.                 byte[] urlbytes = ListGetter.getUrlBytes(mItems.get(index + 1).getProfile_pic_url_instagram());
  211.                 bitmapNext = BitmapFactory.decodeByteArray(urlbytes, 0, urlbytes.length);
  212.             } catch (IOException e) {
  213.                 System.out.println(e);
  214.             }
  215.             mGirlPicNext.setImageBitmap(ImageHelper.getRoundedCornerBitmap(bitmapNext, 100));
  216.         }
  217.  
  218.         crossfadePic();
  219.         appear(mGirlPicNext, mGirlPic, mContainer);
  220.         crossfadeName();
  221.         appear(mGirlNameNext, mGirlName, mContainer);
  222.         crossfadeUrl();
  223.         appear(mGirlUrlNext, mGirlUrl, mContainer);
  224.         crossfadeUniversity();
  225.         appear(mGirlUniversityNext, mGirlUniversity, mContainer);
  226.     }
  227.  
  228.     private void crossfadePic() {
  229.  
  230.         // Set the content view to 0% opacity but visible, so that it is visible
  231.         // (but fully transparent) during the animation.
  232.         /*contentView.setAlpha(0f);
  233.         contentView.setVisibility(View.VISIBLE);*/
  234.  
  235.         // Animate the content view to 100% opacity, and clear any animation
  236.         // listener set on the view.
  237.         /*contentView.animate()
  238.                 .alpha(1f)
  239.                 .setDuration(shortAnimationDuration)
  240.                 .setListener(null);*/
  241.  
  242.         // Animate the loading view to 0% opacity. After the animation ends,
  243.         // set its visibility to GONE as an optimization step (it won't
  244.         // participate in layout passes, etc.)
  245.         mGirlPic.startAnimation(animation);
  246.         mGirlPic.animate()
  247.                 .alpha(0f)
  248.                 .setDuration(700)
  249.                 .setListener(new AnimatorListenerAdapter() {
  250.                     @Override
  251.                     public void onAnimationEnd(Animator animation) {
  252.                         mGirlPic.setVisibility(View.INVISIBLE);
  253.                         ImageView template = mGirlPic;
  254.                         mGirlPic = mGirlPicNext;
  255.                         mGirlPicNext = template;
  256.                         mGirlNameNext.setX(mGirlPic.getX());
  257.                     }
  258.                 });
  259.         mGirlPicNext.animate()
  260.                 .alpha(1f)
  261.                 .setDuration(1)
  262.                 .setListener(new AnimatorListenerAdapter() {
  263.                     @Override
  264.                     public void onAnimationEnd(Animator animation) {
  265.  
  266.                     }
  267.                 });
  268.         mGirlPicNext.startAnimation(animationBack);
  269.  
  270.     }
  271.  
  272.     private void crossfadeName() {
  273.         mGirlName.startAnimation(animation);
  274.         mGirlName.animate()
  275.                 .alpha(0f)
  276.                 .setDuration(700)
  277.                 .setListener(new AnimatorListenerAdapter() {
  278.                     @Override
  279.                     public void onAnimationEnd(Animator animation) {
  280.                         mGirlName.setVisibility(View.INVISIBLE);
  281.                         TextView template = mGirlName;
  282.                         mGirlName = mGirlNameNext;
  283.                         mGirlNameNext = template;
  284.                         mGirlNameNext.setX(mGirlName.getX());
  285.                     }
  286.                 });
  287.         mGirlNameNext.animate()
  288.                 .alpha(1f)
  289.                 .setDuration(1)
  290.                 .setListener(new AnimatorListenerAdapter() {
  291.                     @Override
  292.                     public void onAnimationEnd(Animator animation) {
  293.  
  294.                     }
  295.                 });
  296.         mGirlNameNext.startAnimation(animationBack);
  297.  
  298.     }
  299.  
  300.     private void crossfadeUrl() {
  301.         mGirlUrl.startAnimation(animation);
  302.         mGirlUrl.animate()
  303.                 .alpha(0f)
  304.                 .setDuration(700)
  305.                 .setListener(new AnimatorListenerAdapter() {
  306.                     @Override
  307.                     public void onAnimationEnd(Animator animation) {
  308.                         mGirlUrl.setVisibility(View.INVISIBLE);
  309.                         TextView template = mGirlUrl;
  310.                         mGirlUrl = mGirlUrlNext;
  311.                         mGirlUrlNext = template;
  312.                         mGirlUrlNext.setX(mGirlUrl.getX());
  313.                     }
  314.                 });
  315.         mGirlUrlNext.animate()
  316.                 .alpha(1f)
  317.                 .setDuration(1)
  318.                 .setListener(new AnimatorListenerAdapter() {
  319.                     @Override
  320.                     public void onAnimationEnd(Animator animation) {
  321.  
  322.                     }
  323.                 });
  324.         mGirlUrlNext.startAnimation(animationBack);
  325.  
  326.     }
  327.  
  328.     private void crossfadeUniversity() {
  329.         mGirlUniversity.setElevation(1);
  330.         mGirlUniversity.startAnimation(animation);
  331.         mGirlUniversity.animate()
  332.                 .alpha(0f)
  333.                 .setDuration(700)
  334.                 .setListener(new AnimatorListenerAdapter() {
  335.                     @Override
  336.                     public void onAnimationEnd(Animator animation) {
  337.                         mGirlUniversity.setVisibility(View.INVISIBLE);
  338.                         TextView template = mGirlUniversity;
  339.                         mGirlUniversity = mGirlUniversityNext;
  340.                         mGirlUniversityNext = template;
  341.                         mGirlUniversityNext.setX(mGirlUniversity.getX());
  342.                     }
  343.                 });
  344.         mGirlUniversityNext.animate()
  345.                 .alpha(1f)
  346.                 .setDuration(1)
  347.                 .setListener(new AnimatorListenerAdapter() {
  348.                     @Override
  349.                     public void onAnimationEnd(Animator animation) {
  350.  
  351.                     }
  352.                 });
  353.         mGirlUniversityNext.startAnimation(animationBack);
  354.  
  355.     }
  356.  
  357.     private void appear(View view, View parent, View container) {
  358.         ObjectAnimator widthAnimator = ObjectAnimator
  359.                 .ofFloat(view, "x", container.getRight(), parent.getLeft())
  360.                 .setDuration(700);
  361.         view.setElevation(10);
  362.         parent.setElevation(1);
  363.         //Toast.makeText(getActivity(), parent.getRight() + " right", Toast.LENGTH_SHORT).show();
  364.         view.setVisibility(View.VISIBLE);
  365.         widthAnimator.start();
  366.     }
  367.  
  368.     public void setmItems(ArrayList<Girl> mItems) {
  369.         this.mItems = mItems;
  370.     }
  371.  
  372.     //kek
  373.     private String GetTime() {
  374.         try {
  375.             String t = mToken.getString(TOKEN, "");
  376.             String url = LoginActivity.server + "get_current_time/" + t;
  377.             String jsonString = ListGetter.getUrlString(url);
  378.             Log.i(TAG, "Received JSON: " + jsonString);
  379.             JSONObject jsonBody = new JSONObject(jsonString);
  380.             String status = (String) jsonBody.get("status");
  381.             switch (status) {
  382.                 case "Confirm the registration at first": {
  383.                 }
  384.                 case "ok": {
  385.                     //kek
  386.                     String s = jsonBody.getString("time");
  387.                     int x = (int) s.charAt(3);
  388.                     int y = (int) s.charAt(4);
  389.                     int z = (int) s.charAt(6);
  390.                     int k = (int) s.charAt(7);
  391.                     int time = 60 * (10 * x + y) + 10 * z + k;
  392.                     int OSTtime = 3600 - time;
  393.                     int min_left = OSTtime / 60;
  394.                     int sec_left = OSTtime % 60;
  395.                     return min_left + ":" + sec_left;
  396.                 }
  397.                 case "Token is invalid": {
  398.                     Log.i(TAG, "Послал неверный токен!!!" + "LOL");
  399.                     System.exit(120);
  400.                 }
  401.             }
  402.         } catch (java.io.IOException e) {
  403.             System.out.println(e);
  404.             Log.i(TAG, "Failed to send the request");
  405.         } catch (JSONException jo) {
  406.             Log.i(TAG, "Failed to receive JSONbody");
  407.         }
  408.         return "";
  409.     }
  410.  
  411.     //kek
  412.     public class TimeTask extends AsyncTask<Void, Void, Boolean> {
  413.         @Override
  414.         protected Boolean doInBackground(Void... params) {
  415.             MainActivity.mainActivity.NoVoicesFragment.setTime(GetTime());
  416.             return true;
  417.         }
  418.  
  419.         @Override
  420.         protected void onPostExecute(Boolean q) {
  421.             MainActivity.mainActivity.setFragment(MainActivity.mainActivity.NoVoicesFragment);
  422.         }
  423.     }
  424.  
  425.     public int getIndex() {
  426.         return index;
  427.     }
  428.  
  429.     public void setIndex(int index) {
  430.         this.index = index;
  431.     }
  432.  
  433. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement