Some base classes and preferences in Kotlin
This commit is contained in:
@@ -1,76 +0,0 @@
|
||||
package eu.kanade.tachiyomi.ui.base.activity;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.view.MenuItem;
|
||||
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
|
||||
import eu.kanade.tachiyomi.App;
|
||||
import eu.kanade.tachiyomi.injection.component.AppComponent;
|
||||
import icepick.Icepick;
|
||||
|
||||
public class BaseActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedState) {
|
||||
super.onCreate(savedState);
|
||||
Icepick.restoreInstanceState(this, savedState);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
Icepick.saveInstanceState(this, outState);
|
||||
}
|
||||
|
||||
protected void setupToolbar(Toolbar toolbar) {
|
||||
setSupportActionBar(toolbar);
|
||||
if (getSupportActionBar() != null)
|
||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
}
|
||||
|
||||
public void setToolbarTitle(String title) {
|
||||
if (getSupportActionBar() != null)
|
||||
getSupportActionBar().setTitle(title);
|
||||
}
|
||||
|
||||
public void setToolbarTitle(int titleResource) {
|
||||
if (getSupportActionBar() != null)
|
||||
getSupportActionBar().setTitle(getString(titleResource));
|
||||
}
|
||||
|
||||
public void setToolbarSubtitle(String title) {
|
||||
if (getSupportActionBar() != null)
|
||||
getSupportActionBar().setSubtitle(title);
|
||||
}
|
||||
|
||||
public void setToolbarSubtitle(int titleResource) {
|
||||
if (getSupportActionBar() != null)
|
||||
getSupportActionBar().setSubtitle(getString(titleResource));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case android.R.id.home:
|
||||
onBackPressed();
|
||||
return true;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
public void registerForEvents() {
|
||||
EventBus.getDefault().register(this);
|
||||
}
|
||||
|
||||
public void unregisterForEvents() {
|
||||
EventBus.getDefault().unregister(this);
|
||||
}
|
||||
|
||||
protected AppComponent getApplicationComponent() {
|
||||
return App.get(this).getComponent();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package eu.kanade.tachiyomi.ui.base.activity
|
||||
|
||||
import android.os.Bundle
|
||||
import android.support.v7.app.AppCompatActivity
|
||||
import android.support.v7.widget.Toolbar
|
||||
import android.view.MenuItem
|
||||
import eu.kanade.tachiyomi.App
|
||||
import eu.kanade.tachiyomi.injection.component.AppComponent
|
||||
import icepick.Icepick
|
||||
import org.greenrobot.eventbus.EventBus
|
||||
|
||||
open class BaseActivity : AppCompatActivity() {
|
||||
|
||||
override fun onCreate(savedState: Bundle?) {
|
||||
super.onCreate(savedState)
|
||||
Icepick.restoreInstanceState(this, savedState)
|
||||
}
|
||||
|
||||
override fun onSaveInstanceState(outState: Bundle) {
|
||||
super.onSaveInstanceState(outState)
|
||||
Icepick.saveInstanceState(this, outState)
|
||||
}
|
||||
|
||||
protected fun setupToolbar(toolbar: Toolbar) {
|
||||
setSupportActionBar(toolbar)
|
||||
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
||||
}
|
||||
|
||||
fun setToolbarTitle(title: String) {
|
||||
supportActionBar?.title = title
|
||||
}
|
||||
|
||||
fun setToolbarTitle(titleResource: Int) {
|
||||
supportActionBar?.title = getString(titleResource)
|
||||
}
|
||||
|
||||
fun setToolbarSubtitle(title: String) {
|
||||
supportActionBar?.subtitle = title
|
||||
}
|
||||
|
||||
fun setToolbarSubtitle(titleResource: Int) {
|
||||
supportActionBar?.subtitle = getString(titleResource)
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||
when (item.itemId) {
|
||||
android.R.id.home -> {
|
||||
onBackPressed()
|
||||
return true
|
||||
}
|
||||
}
|
||||
return super.onOptionsItemSelected(item)
|
||||
}
|
||||
|
||||
fun registerForEvents() {
|
||||
EventBus.getDefault().register(this)
|
||||
}
|
||||
|
||||
fun unregisterForEvents() {
|
||||
EventBus.getDefault().unregister(this)
|
||||
}
|
||||
|
||||
protected val applicationComponent: AppComponent
|
||||
get() = App.get(this).component
|
||||
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package eu.kanade.tachiyomi.ui.base.decoration;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
|
||||
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
|
||||
|
||||
private Drawable mDivider;
|
||||
|
||||
public DividerItemDecoration(Context context, AttributeSet attrs) {
|
||||
final TypedArray a = context.obtainStyledAttributes(attrs, new int [] { android.R.attr.listDivider });
|
||||
mDivider = a.getDrawable(0);
|
||||
a.recycle();
|
||||
}
|
||||
|
||||
public DividerItemDecoration(Drawable divider) { mDivider = divider; }
|
||||
|
||||
@Override
|
||||
public void getItemOffsets (Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
|
||||
super.getItemOffsets(outRect, view, parent, state);
|
||||
if (mDivider == null) return;
|
||||
if (parent.getChildPosition(view) < 1) return;
|
||||
|
||||
if (getOrientation(parent) == LinearLayoutManager.VERTICAL) outRect.top = mDivider.getIntrinsicHeight();
|
||||
else outRect.left = mDivider.getIntrinsicWidth();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
|
||||
if (mDivider == null) { super.onDrawOver(c, parent, state); return; }
|
||||
|
||||
if (getOrientation(parent) == LinearLayoutManager.VERTICAL) {
|
||||
final int left = parent.getPaddingLeft();
|
||||
final int right = parent.getWidth() - parent.getPaddingRight();
|
||||
final int childCount = parent.getChildCount();
|
||||
final int dividerHeight = mDivider.getIntrinsicHeight();
|
||||
|
||||
for (int i=1; i < childCount; i++) {
|
||||
final View child = parent.getChildAt(i);
|
||||
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
|
||||
final int ty = (int)(child.getTranslationY() + 0.5f);
|
||||
final int top = child.getTop() - params.topMargin + ty;
|
||||
final int bottom = top + dividerHeight;
|
||||
mDivider.setBounds(left, top, right, bottom);
|
||||
mDivider.draw(c);
|
||||
}
|
||||
} else { //horizontal
|
||||
final int top = parent.getPaddingTop();
|
||||
final int bottom = parent.getHeight() - parent.getPaddingBottom();
|
||||
final int childCount = parent.getChildCount();
|
||||
|
||||
for (int i=1; i < childCount; i++) {
|
||||
final View child = parent.getChildAt(i);
|
||||
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
|
||||
final int size = mDivider.getIntrinsicWidth();
|
||||
final int left = child.getLeft() - params.leftMargin;
|
||||
final int right = left + size;
|
||||
mDivider.setBounds(left, top, right, bottom);
|
||||
mDivider.draw(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private int getOrientation(RecyclerView parent) {
|
||||
if (parent.getLayoutManager() instanceof LinearLayoutManager) {
|
||||
LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
|
||||
return layoutManager.getOrientation();
|
||||
} else throw new IllegalStateException("DividerItemDecoration can only be used with a LinearLayoutManager.");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
package eu.kanade.tachiyomi.ui.base.fragment;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
|
||||
import eu.kanade.tachiyomi.ui.base.activity.BaseActivity;
|
||||
import icepick.Icepick;
|
||||
|
||||
public class BaseFragment extends Fragment {
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedState) {
|
||||
super.onCreate(savedState);
|
||||
Icepick.restoreInstanceState(this, savedState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
Icepick.saveInstanceState(this, outState);
|
||||
}
|
||||
|
||||
public void setToolbarTitle(String title) {
|
||||
getBaseActivity().setToolbarTitle(title);
|
||||
}
|
||||
|
||||
public void setToolbarTitle(int resourceId) {
|
||||
getBaseActivity().setToolbarTitle(getString(resourceId));
|
||||
}
|
||||
|
||||
public BaseActivity getBaseActivity() {
|
||||
return (BaseActivity) getActivity();
|
||||
}
|
||||
|
||||
public void registerForEvents() {
|
||||
EventBus.getDefault().register(this);
|
||||
}
|
||||
|
||||
public void unregisterForEvents() {
|
||||
EventBus.getDefault().unregister(this);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package eu.kanade.tachiyomi.ui.base.fragment
|
||||
|
||||
import android.os.Bundle
|
||||
import android.support.v4.app.Fragment
|
||||
import eu.kanade.tachiyomi.ui.base.activity.BaseActivity
|
||||
import icepick.Icepick
|
||||
import org.greenrobot.eventbus.EventBus
|
||||
|
||||
open class BaseFragment : Fragment() {
|
||||
|
||||
override fun onCreate(savedState: Bundle?) {
|
||||
super.onCreate(savedState)
|
||||
Icepick.restoreInstanceState(this, savedState)
|
||||
}
|
||||
|
||||
override fun onSaveInstanceState(outState: Bundle) {
|
||||
super.onSaveInstanceState(outState)
|
||||
Icepick.saveInstanceState(this, outState)
|
||||
}
|
||||
|
||||
fun setToolbarTitle(title: String) {
|
||||
baseActivity.setToolbarTitle(title)
|
||||
}
|
||||
|
||||
fun setToolbarTitle(resourceId: Int) {
|
||||
baseActivity.setToolbarTitle(getString(resourceId))
|
||||
}
|
||||
|
||||
val baseActivity: BaseActivity
|
||||
get() = activity as BaseActivity
|
||||
|
||||
fun registerForEvents() {
|
||||
EventBus.getDefault().register(this)
|
||||
}
|
||||
|
||||
fun unregisterForEvents() {
|
||||
EventBus.getDefault().unregister(this)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package eu.kanade.tachiyomi.ui.base.presenter;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
|
||||
import icepick.Icepick;
|
||||
import nucleus.view.ViewWithPresenter;
|
||||
|
||||
public class BasePresenter<V extends ViewWithPresenter> extends RxPresenter<V> {
|
||||
|
||||
private Context context;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedState) {
|
||||
super.onCreate(savedState);
|
||||
Icepick.restoreInstanceState(this, savedState);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSave(@NonNull Bundle state) {
|
||||
super.onSave(state);
|
||||
Icepick.saveInstanceState(this, state);
|
||||
}
|
||||
|
||||
public void registerForEvents() {
|
||||
EventBus.getDefault().register(this);
|
||||
}
|
||||
|
||||
public void unregisterForEvents() {
|
||||
EventBus.getDefault().unregister(this);
|
||||
}
|
||||
|
||||
public void setContext(Context applicationContext) {
|
||||
context = applicationContext;
|
||||
}
|
||||
|
||||
public Context getContext() {
|
||||
return context;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package eu.kanade.tachiyomi.ui.base.presenter
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Bundle
|
||||
import icepick.Icepick
|
||||
import nucleus.view.ViewWithPresenter
|
||||
import org.greenrobot.eventbus.EventBus
|
||||
|
||||
open class BasePresenter<V : ViewWithPresenter<*>> : RxPresenter<V>() {
|
||||
|
||||
lateinit var context: Context
|
||||
|
||||
override fun onCreate(savedState: Bundle?) {
|
||||
super.onCreate(savedState)
|
||||
Icepick.restoreInstanceState(this, savedState)
|
||||
}
|
||||
|
||||
override fun onSave(state: Bundle) {
|
||||
super.onSave(state)
|
||||
Icepick.saveInstanceState(this, state)
|
||||
}
|
||||
|
||||
fun registerForEvents() {
|
||||
EventBus.getDefault().register(this)
|
||||
}
|
||||
|
||||
fun unregisterForEvents() {
|
||||
EventBus.getDefault().unregister(this)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user