package nl.ictrek.ictrix;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * Created by jelle on 15/04/2017.
 */

public class LoginActivity extends AppCompatActivity {
    private boolean dropoutRotatedUp;
    private View dropoutButton;
    private ImageView dropoutIcon;
    private View dropoutMenu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        dropoutButton = findViewById(R.id.advanced_options);
        dropoutIcon = (ImageView) findViewById(R.id.dropout_icon);
        dropoutMenu = findViewById(R.id.server_options);

        final AnimatorSet rotateUp = new AnimatorSet();
        rotateUp.play(ObjectAnimator.ofFloat(dropoutIcon, "rotation", 0, 180));

        final AnimatorSet rotateDown = new AnimatorSet();
        rotateDown.play(ObjectAnimator.ofFloat(dropoutIcon, "rotation", 180, 0));

        dropoutRotatedUp = false;

        dropoutButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dropoutRotatedUp) {
                    rotateDown.start();
                    dropoutRotatedUp = false;
                    collapseView(dropoutMenu);
                } else {
                    rotateUp.start();
                    dropoutRotatedUp = true;
                    expandView(dropoutMenu);
                }
            }
        });
    }

    public static void expandView(final View view) {
        view.measure(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        final int targetHeight = view.getMeasuredHeight();

        // Older versions of android (pre API 21) cancel animations for views with a height of 0.
        view.getLayoutParams().height = 1;
        view.setVisibility(View.VISIBLE);
        Animation animation = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                view.getLayoutParams().height = interpolatedTime == 1
                        ? LinearLayout.LayoutParams.WRAP_CONTENT
                        : (int) (targetHeight * interpolatedTime);
                view.requestLayout();
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };

        // 1dp/ms
        animation.setDuration((int) (targetHeight / view.getContext().getResources().getDisplayMetrics().density));
        view.startAnimation(animation);
    }

    public static void collapseView(final View view) {
        final int initialHeight = view.getMeasuredHeight();

        Animation animation = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                if (interpolatedTime == 1) {
                    view.setVisibility(View.GONE);
                } else {
                    view.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime);
                    view.requestLayout();
                }
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };

        // 1dp/ms
        animation.setDuration((int) (initialHeight / view.getContext().getResources().getDisplayMetrics().density));
        view.startAnimation(animation);
    }
}

@@ -456,16 +456,72 @@
 ivity %7B%0A
+    public static final int INTERPOLATED_TIME_END = 1;%0A%0A
     priv
@@ -532,17 +532,18 @@
 boolean 
-d
+mD
 ropoutRo
@@ -564,25 +564,26 @@
 rivate View 
-d
+mD
 ropoutButton
@@ -602,25 +602,26 @@
 e ImageView 
-d
+mD
 ropoutIcon;%0A
@@ -637,17 +637,18 @@
 te View 
-d
+mD
 ropoutMe
@@ -817,33 +817,34 @@
 ogin);%0A%0A        
-d
+mD
 ropoutButton = f
@@ -883,25 +883,26 @@
 s);%0A        
-d
+mD
 ropoutIcon =
@@ -951,25 +951,26 @@
 n);%0A        
-d
+mD
 ropoutMenu =
@@ -1099,33 +1099,34 @@
 nimator.ofFloat(
-d
+mD
 ropoutIcon, %22rot
@@ -1249,17 +1249,18 @@
 ofFloat(
-d
+mD
 ropoutIc
@@ -1286,33 +1286,34 @@
 , 0));%0A%0A        
-d
+mD
 ropoutRotatedUp 
@@ -1326,25 +1326,26 @@
 e;%0A%0A        
-d
+mD
 ropoutButton
@@ -1477,13 +1477,70 @@
     
-if (d
+mDropoutRotatedUp = !mDropoutRotatedUp;%0A                if (mD
 ropo
@@ -1598,54 +1598,8 @@
 ();%0A
-                    dropoutRotatedUp = false;%0A
     
@@ -1623,25 +1623,26 @@
 ollapseView(
-d
+mD
 ropoutMenu);
@@ -1709,53 +1709,8 @@
 ();%0A
-                    dropoutRotatedUp = true;%0A
     
@@ -1736,17 +1736,18 @@
 andView(
-d
+mD
 ropoutMe
@@ -3191,32 +3191,193 @@
 sformation t) %7B%0A
+                // interpolatedTime - The value of the normalized time (0.0 to 1.0) after it has%0A                // been run through the interpolation function.%0A
                 
@@ -3400,17 +3400,37 @@
 Time == 
-1
+INTERPOLATED_TIME_END
 ) %7B%0A    

Fix problems from PR review