mirror of
https://github.com/mihonapp/mihon.git
synced 2025-11-12 20:19:05 +01:00
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
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package eu.kanade.tachiyomi.ui.decoration;
|
||||
package eu.kanade.tachiyomi.ui.base.decoration;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,8 +16,8 @@ import com.afollestad.materialdialogs.MaterialDialog
|
||||
import eu.kanade.tachiyomi.R
|
||||
import eu.kanade.tachiyomi.data.database.models.Manga
|
||||
import eu.kanade.tachiyomi.ui.base.adapter.FlexibleViewHolder
|
||||
import eu.kanade.tachiyomi.ui.base.decoration.DividerItemDecoration
|
||||
import eu.kanade.tachiyomi.ui.base.fragment.BaseRxFragment
|
||||
import eu.kanade.tachiyomi.ui.decoration.DividerItemDecoration
|
||||
import eu.kanade.tachiyomi.ui.main.MainActivity
|
||||
import eu.kanade.tachiyomi.ui.manga.MangaActivity
|
||||
import eu.kanade.tachiyomi.util.ToastUtil
|
||||
@@ -204,10 +204,10 @@ class CatalogueFragment : BaseRxFragment<CataloguePresenter>(), FlexibleViewHold
|
||||
toolbar.addView(spinner)
|
||||
}
|
||||
|
||||
override fun onSaveInstanceState(bundle: Bundle) {
|
||||
bundle.putInt(SELECTED_INDEX_KEY, selectedIndex)
|
||||
bundle.putString(QUERY_KEY, query)
|
||||
super.onSaveInstanceState(bundle)
|
||||
override fun onSaveInstanceState(outState: Bundle) {
|
||||
outState.putInt(SELECTED_INDEX_KEY, selectedIndex)
|
||||
outState.putString(QUERY_KEY, query)
|
||||
super.onSaveInstanceState(outState)
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
|
||||
@@ -309,7 +309,7 @@ class CatalogueFragment : BaseRxFragment<CataloguePresenter>(), FlexibleViewHold
|
||||
*/
|
||||
private fun restartRequest(newQuery: String) {
|
||||
// If text didn't change, do nothing
|
||||
if (query == newQuery || presenter.source == null)
|
||||
if (query == newQuery)
|
||||
return
|
||||
|
||||
query = newQuery
|
||||
|
||||
@@ -30,8 +30,8 @@ import eu.kanade.tachiyomi.data.database.models.Manga;
|
||||
import eu.kanade.tachiyomi.data.download.DownloadService;
|
||||
import eu.kanade.tachiyomi.data.download.model.Download;
|
||||
import eu.kanade.tachiyomi.ui.base.adapter.FlexibleViewHolder;
|
||||
import eu.kanade.tachiyomi.ui.base.decoration.DividerItemDecoration;
|
||||
import eu.kanade.tachiyomi.ui.base.fragment.BaseRxFragment;
|
||||
import eu.kanade.tachiyomi.ui.decoration.DividerItemDecoration;
|
||||
import eu.kanade.tachiyomi.ui.manga.MangaActivity;
|
||||
import eu.kanade.tachiyomi.ui.reader.ReaderActivity;
|
||||
import eu.kanade.tachiyomi.util.ToastUtil;
|
||||
|
||||
@@ -13,8 +13,8 @@ import eu.kanade.tachiyomi.data.database.models.MangaChapter
|
||||
import eu.kanade.tachiyomi.data.download.DownloadService
|
||||
import eu.kanade.tachiyomi.data.download.model.Download
|
||||
import eu.kanade.tachiyomi.ui.base.adapter.FlexibleViewHolder
|
||||
import eu.kanade.tachiyomi.ui.base.decoration.DividerItemDecoration
|
||||
import eu.kanade.tachiyomi.ui.base.fragment.BaseRxFragment
|
||||
import eu.kanade.tachiyomi.ui.decoration.DividerItemDecoration
|
||||
import eu.kanade.tachiyomi.ui.reader.ReaderActivity
|
||||
import kotlinx.android.synthetic.main.fragment_recent_chapters.*
|
||||
import nucleus.factory.RequiresPresenter
|
||||
|
||||
@@ -19,18 +19,19 @@ import rx.Observable
|
||||
* @param view the inflated view for this holder.
|
||||
* @param adapter the adapter handling this holder.
|
||||
* @param listener a listener to react to single tap and long tap events.
|
||||
* @constructor creates a new library holder.
|
||||
* @constructor creates a new recent chapter holder.
|
||||
*/
|
||||
class RecentChaptersHolder(view: View, private val adapter: RecentChaptersAdapter, listener: FlexibleViewHolder.OnListItemClickListener) : FlexibleViewHolder(view, adapter, listener) {
|
||||
class RecentChaptersHolder(view: View, private val adapter: RecentChaptersAdapter, listener: FlexibleViewHolder.OnListItemClickListener) :
|
||||
FlexibleViewHolder(view, adapter, listener) {
|
||||
/**
|
||||
* Color of read chapter
|
||||
*/
|
||||
private val readColor: Int
|
||||
private val readColor = ContextCompat.getColor(view.context, R.color.hint_text)
|
||||
|
||||
/**
|
||||
* Color of unread chapter
|
||||
*/
|
||||
private val unreadColor: Int
|
||||
private val unreadColor = ContextCompat.getColor(view.context, R.color.primary_text)
|
||||
|
||||
/**
|
||||
* Object containing chapter information
|
||||
@@ -38,10 +39,6 @@ class RecentChaptersHolder(view: View, private val adapter: RecentChaptersAdapte
|
||||
private var mangaChapter: MangaChapter? = null
|
||||
|
||||
init {
|
||||
// Set colors.
|
||||
readColor = ContextCompat.getColor(view.context, R.color.hint_text)
|
||||
unreadColor = ContextCompat.getColor(view.context, R.color.primary_text)
|
||||
|
||||
//Set OnClickListener for download menu
|
||||
itemView.chapterMenu.setOnClickListener { v -> v.post({ showPopupMenu(v) }) }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user