Refactor : IntroActivity to Kotlin

This commit is contained in:
Rutvik Rajendra Panchal 2020-05-15 15:30:12 +05:30
parent c40d0586d4
commit 09f5ebab74
2 changed files with 138 additions and 143 deletions

View File

@ -1,143 +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.intro;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.OnClick;
import com.pixelcan.inkpageindicator.InkPageIndicator;
import java.util.Timer;
import java.util.TimerTask;
import javax.inject.Inject;
import org.jetbrains.annotations.NotNull;
import org.kiwix.kiwixmobile.ActivityExtensionsKt;
import org.kiwix.kiwixmobile.R;
import org.kiwix.kiwixmobile.core.Intents;
import org.kiwix.kiwixmobile.core.base.BaseActivity;
import org.kiwix.kiwixmobile.core.di.components.CoreComponent;
import org.kiwix.kiwixmobile.core.main.CoreMainActivity;
public class IntroActivity extends BaseActivity implements IntroContract.View {
private final Handler handler = new Handler();
private final Timer timer = new Timer();
@BindView(R.id.view_pager)
ViewPager viewPager;
@BindView(R.id.tab_indicator)
InkPageIndicator tabIndicator;
@Inject
IntroContract.Presenter presenter;
private ImageView airPlane;
private int currentPage = 0;
private final ViewPager.OnPageChangeListener pageChangeListener =
new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position == 1) {
airPlane.setVisibility(View.VISIBLE);
airPlane.animate()
.translationX(airPlane.getWidth())
.setDuration(800);
} else {
airPlane.setVisibility(View.INVISIBLE);
airPlane.animate()
.translationX(-airPlane.getWidth());
}
currentPage = position;
}
@Override
public void onPageScrollStateChanged(int state) {
if (state == ViewPager.SCROLL_STATE_DRAGGING) {
dismissAutoRotate();
}
}
};
private View[] views;
@Override protected void injection(@NotNull CoreComponent coreComponent) {
ActivityExtensionsKt.getKiwixActivityComponent(this).inject(this);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intro);
LayoutInflater layoutInflater = getLayoutInflater();
views = new View[] {
layoutInflater.inflate(R.layout.item_intro_1, viewPager, false),
layoutInflater.inflate(R.layout.item_intro_2, viewPager, false)
};
IntroPagerAdapter introPagerAdapter = new IntroPagerAdapter(views);
viewPager.setAdapter(introPagerAdapter);
tabIndicator.setViewPager(viewPager);
airPlane = views[1].findViewById(R.id.airplane);
viewPager.addOnPageChangeListener(pageChangeListener);
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(() -> {
if (currentPage == views.length) {
currentPage = 0;
}
viewPager.setCurrentItem(currentPage++, true);
});
}
}, 0, 2000);
for (View view : views) {
view.findViewById(R.id.root).setOnClickListener(v -> dismissAutoRotate());
}
}
@Override
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacksAndMessages(null);
timer.cancel();
for (View view : views) {
view.findViewById(R.id.root).setOnClickListener(null);
}
}
@OnClick(R.id.get_started)
void startMainActivity() {
dismissAutoRotate();
startActivity(Intents.internal(CoreMainActivity.class));
presenter.setIntroShown();
finish();
}
private void dismissAutoRotate() {
handler.removeCallbacksAndMessages(null);
timer.cancel();
}
}

View File

@ -0,0 +1,138 @@
/*
* 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.intro
import android.os.Bundle
import android.os.Handler
import android.view.View
import android.widget.ImageView
import androidx.viewpager.widget.ViewPager
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import butterknife.BindView
import butterknife.OnClick
import com.pixelcan.inkpageindicator.InkPageIndicator
import org.kiwix.kiwixmobile.R
import org.kiwix.kiwixmobile.core.Intents.internal
import org.kiwix.kiwixmobile.core.base.BaseActivity
import org.kiwix.kiwixmobile.core.di.components.CoreComponent
import org.kiwix.kiwixmobile.core.main.CoreMainActivity
import org.kiwix.kiwixmobile.kiwixActivityComponent
import java.util.Timer
import java.util.TimerTask
import javax.inject.Inject
class IntroActivity : BaseActivity(), IntroContract.View {
companion object {
private const val timerDelay: Long = 0
private const val timerPeriod: Long = 2000
private const val animationDuration: Long = 800
}
private val handler = Handler()
private val timer = Timer()
@BindView(R.id.view_pager)
lateinit var viewPager: ViewPager
@BindView(R.id.tab_indicator)
lateinit var tabIndicator: InkPageIndicator
@Inject
internal lateinit var presenter: IntroContract.Presenter
private var airPlane: ImageView? = null
private var currentPage = 0
private lateinit var views: Array<View>
@Suppress("EmptyFunctionBlock")
private val pageChangeListener: OnPageChangeListener = object : OnPageChangeListener {
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
}
override fun onPageSelected(position: Int) {
if (position == 1) {
airPlane!!.visibility = View.VISIBLE
airPlane!!.animate().translationX(airPlane!!.width.toFloat()).duration = animationDuration
} else {
airPlane!!.visibility = View.INVISIBLE
airPlane!!.animate().translationX(-airPlane!!.width.toFloat())
}
currentPage = position
}
override fun onPageScrollStateChanged(state: Int) {
if (state == ViewPager.SCROLL_STATE_DRAGGING) {
dismissAutoRotate()
}
}
}
override fun injection(coreComponent: CoreComponent) {
this.kiwixActivityComponent.inject(this)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_intro)
val layoutInflater = layoutInflater
views = arrayOf(
layoutInflater.inflate(R.layout.item_intro_1, viewPager, false),
layoutInflater.inflate(R.layout.item_intro_2, viewPager, false)
)
val introPagerAdapter = IntroPagerAdapter(views)
viewPager.adapter = introPagerAdapter
tabIndicator.setViewPager(viewPager)
airPlane = views[1].findViewById(R.id.airplane)
viewPager.addOnPageChangeListener(pageChangeListener)
timer.schedule(object : TimerTask() {
override fun run() {
handler.post {
if (currentPage == views.size) {
currentPage = 0
}
viewPager.setCurrentItem(currentPage++, true)
}
}
}, timerDelay, timerPeriod)
for (view in views) {
view.findViewById<View>(R.id.root).setOnClickListener { dismissAutoRotate() }
}
}
override fun onDestroy() {
super.onDestroy()
handler.removeCallbacksAndMessages(null)
timer.cancel()
for (view in views) {
view.findViewById<View>(R.id.root).setOnClickListener(null)
}
}
@OnClick(R.id.get_started)
fun startMainActivity() {
dismissAutoRotate()
startActivity(internal(CoreMainActivity::class.java))
presenter.setIntroShown()
finish()
}
private fun dismissAutoRotate() {
handler.removeCallbacksAndMessages(null)
timer.cancel()
}
}