#1909 Convert AnimationUtil to Kotlin - convert AnimatUtil - modify codestyle so R. does not get omitted when converting to kotlin - force import optimisation

This commit is contained in:
Sean Mac Gillicuddy 2020-04-02 13:59:52 +01:00
parent 740c460406
commit 2b8e321791
5 changed files with 79 additions and 113 deletions

3
.gitignore vendored
View File

@ -59,11 +59,10 @@ captures/
# IDEA/Android Studio Ignore exceptions # IDEA/Android Studio Ignore exceptions
!/.idea/vcs.xml !/.idea/vcs.xml
!/.idea/fileTemplates/
!/.idea/inspectionProfiles/ !/.idea/inspectionProfiles/
!/.idea/scopes/ !/.idea/scopes/
!/.idea/codeStyles/ !/.idea/codeStyles/
!/.idea/encodings.xml !/.idea/encodings.xml
!/.idea/copyright/ !/.idea/copyright/
!/.idea/compiler.xml !/.idea/kotlinCodeInsightSettings.xml
app/jacoco.exec app/jacoco.exec

View File

@ -39,7 +39,6 @@
</option> </option>
<option name="NAME_COUNT_TO_USE_STAR_IMPORT" value="2147483647" /> <option name="NAME_COUNT_TO_USE_STAR_IMPORT" value="2147483647" />
<option name="NAME_COUNT_TO_USE_STAR_IMPORT_FOR_MEMBERS" value="2147483647" /> <option name="NAME_COUNT_TO_USE_STAR_IMPORT_FOR_MEMBERS" value="2147483647" />
<option name="IMPORT_NESTED_CLASSES" value="true" />
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" /> <option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
</JetCodeStyleSettings> </JetCodeStyleSettings>
<XML> <XML>

7
.idea/kotlinCodeInsightSettings.xml generated Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="KotlinCodeInsightWorkspaceSettings">
<option name="addUnambiguousImportsOnTheFly" value="true" />
<option name="optimizeImportsOnTheFly" value="true" />
</component>
</project>

View File

@ -1,110 +0,0 @@
/*
* Kiwix Android
* Copyright (c) 2019 Kiwix <android.kiwix.org>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.kiwix.kiwixmobile.core.utils;
import android.animation.Animator;
import android.animation.ValueAnimator;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import org.kiwix.kiwixmobile.core.R;
public class AnimationUtils {
public static void expand(final View v) {
v.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final int targetHeight = v.getMeasuredHeight();
// Older versions of android (pre API 21) cancel animations for views with a height of 0.
v.getLayoutParams().height = 1;
v.setVisibility(View.VISIBLE);
ValueAnimator va = ValueAnimator.ofInt(1, targetHeight);
va.addUpdateListener(animation -> {
v.getLayoutParams().height = (Integer) animation.getAnimatedValue();
v.requestLayout();
});
va.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationEnd(Animator animation) {
v.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
}
@Override public void onAnimationStart(Animator animation) {}
@Override public void onAnimationCancel(Animator animation) {}
@Override public void onAnimationRepeat(Animator animation) {}
});
va.setDuration(100);
va.setInterpolator(new LinearInterpolator());
va.start();
}
public static void collapse(final @NonNull View v) {
final int initialHeight = v.getMeasuredHeight();
ValueAnimator va = ValueAnimator.ofInt(initialHeight, 0);
va.addUpdateListener(animation -> {
v.getLayoutParams().height = (Integer) animation.getAnimatedValue();
v.requestLayout();
});
va.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationEnd(Animator animation) {
v.setVisibility(View.GONE);
}
@Override public void onAnimationStart(Animator animation) {}
@Override public void onAnimationCancel(Animator animation) {}
@Override public void onAnimationRepeat(Animator animation) {}
});
va.setDuration(100);
va.setInterpolator(new LinearInterpolator());
va.start();
}
//rotate animation for closeAllTabs FAB
public static void rotate(@NonNull FloatingActionButton v) {
RotateAnimation wheelRotation =
new RotateAnimation(0.0f, 360f, v.getWidth() / 2.0f, v.getHeight() / 2.0f);
wheelRotation.setDuration(200);
wheelRotation.setRepeatCount(0);
wheelRotation.setInterpolator(v.getContext(), android.R.interpolator.cycle);
v.startAnimation(wheelRotation);
wheelRotation.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationEnd(Animation animation) {
v.setImageDrawable(
ContextCompat.getDrawable(v.getContext(), R.drawable.ic_done_white_24dp));
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation) {
v.setImageDrawable(
ContextCompat.getDrawable(v.getContext(), R.drawable.ic_close_black_24dp));
}
});
}
}

View File

@ -0,0 +1,71 @@
/*
* Kiwix Android
* Copyright (c) 2019 Kiwix <android.kiwix.org>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.kiwix.kiwixmobile.core.utils
import android.animation.ValueAnimator
import android.view.View
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import android.view.animation.LinearInterpolator
import androidx.core.animation.addListener
import com.google.android.material.floatingactionbutton.FloatingActionButton
import org.kiwix.kiwixmobile.core.R
import org.kiwix.kiwixmobile.core.extensions.setImageDrawableCompat
private const val HEIGHT_ANIM_DURATION = 100L
private const val ROTATE_ANIM_DURATION = 200L
private const val FULL_ROTATION = 360.0f
object AnimationUtils {
@JvmStatic fun View.expand() {
measure(MATCH_PARENT, WRAP_CONTENT)
// Older versions of android (pre API 21) cancel animations for views with a height of 0.
layoutParams.height = 1
visibility = View.VISIBLE
animateHeight(1, measuredHeight) {
layoutParams.height = WRAP_CONTENT
}
}
@JvmStatic fun View.collapse() {
animateHeight(measuredHeight, 0) { visibility = View.GONE }
}
private fun View.animateHeight(start: Int, end: Int, onEndAction: () -> Unit) {
ValueAnimator.ofInt(start, end).apply {
addUpdateListener {
layoutParams.height = it.animatedValue as Int
requestLayout()
}
addListener(onEnd = { onEndAction.invoke() })
duration = HEIGHT_ANIM_DURATION
interpolator = LinearInterpolator()
}.start()
}
@JvmStatic fun FloatingActionButton.rotate() {
animate().apply {
withStartAction { setImageDrawableCompat(R.drawable.ic_close_black_24dp) }
withEndAction { setImageDrawableCompat(R.drawable.ic_done_white_24dp) }
rotationBy(FULL_ROTATION)
duration = ROTATE_ANIM_DURATION
}.start()
}
}