From 6a88192e77882089193b896f35de40fe7e5eb14b Mon Sep 17 00:00:00 2001 From: Amine Date: Fri, 21 Sep 2018 21:39:37 +0200 Subject: [PATCH] Fixes #221 --- .../bou/readerforselfoss/ReaderActivity.kt | 63 ++++++++++++++++++- .../fragments/ArticleFragment.kt | 33 ---------- 2 files changed, 62 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt index 54aaf3e..3efa2a0 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt @@ -7,6 +7,7 @@ import android.preference.PreferenceManager import android.support.v4.app.FragmentManager import android.support.v4.app.FragmentStatePagerAdapter import android.support.v4.content.ContextCompat +import android.support.v4.view.ViewPager import android.support.v7.app.AppCompatActivity import android.view.Menu import android.view.MenuItem @@ -19,10 +20,13 @@ import apps.amine.bou.readerforselfoss.fragments.ArticleFragment import apps.amine.bou.readerforselfoss.themes.AppColors import apps.amine.bou.readerforselfoss.themes.Toppings import apps.amine.bou.readerforselfoss.transformers.DepthPageTransformer +import apps.amine.bou.readerforselfoss.utils.maybeHandleSilentException +import apps.amine.bou.readerforselfoss.utils.succeeded import apps.amine.bou.readerforselfoss.utils.toggleStar import com.ftinc.scoop.Scoop import kotlinx.android.synthetic.main.activity_reader.* import me.relex.circleindicator.CircleIndicator +import org.acra.ACRA import retrofit2.Call import retrofit2.Callback import retrofit2.Response @@ -30,7 +34,9 @@ import retrofit2.Response class ReaderActivity : AppCompatActivity() { private var markOnScroll: Boolean = false + private var debugReadingItems: Boolean = false private var currentItem: Int = 0 + private lateinit var userIdentifier: String private lateinit var api: SelfossApi @@ -66,6 +72,10 @@ class ReaderActivity : AppCompatActivity() { val prefs = PreferenceManager.getDefaultSharedPreferences(this) + debugReadingItems = prefs.getBoolean("read_debug", false) + userIdentifier = prefs.getString("unique_id", "") + markOnScroll = prefs.getBoolean("mark_on_scroll", false) + api = SelfossApi( this, this@ReaderActivity, @@ -73,13 +83,14 @@ class ReaderActivity : AppCompatActivity() { prefs.getBoolean("should_log_everything", false) ) - if (allItems.isEmpty()) { finish() } currentItem = intent.getIntExtra("currentItem", 0) + readItem(allItems[currentItem].id) + pager.adapter = ScreenSlidePagerAdapter(supportFragmentManager, AppColors(this@ReaderActivity)) pager.currentItem = currentItem } @@ -91,6 +102,56 @@ class ReaderActivity : AppCompatActivity() { pager.setPageTransformer(true, DepthPageTransformer()) (indicator as CircleIndicator).setViewPager(pager) + + pager.addOnPageChangeListener( + object : ViewPager.SimpleOnPageChangeListener() { + + override fun onPageSelected(position: Int) { + + if (allItems[position].starred) { + canRemoveFromFavorite() + } else { + canFavorite() + } + readItem(allItems[pager.currentItem].id) + } + } + ) + } + + fun readItem(id: String) { + if (markOnScroll) { + api.markItem(id).enqueue( + object : Callback { + override fun onResponse( + call: Call, + response: Response + ) { + if (!response.succeeded() && debugReadingItems) { + val message = + "message: ${response.message()} " + + "response isSuccess: ${response.isSuccessful} " + + "response code: ${response.code()} " + + "response message: ${response.message()} " + + "response errorBody: ${response.errorBody()?.string()} " + + "body success: ${response.body()?.success} " + + "body isSuccess: ${response.body()?.isSuccess}" + ACRA.getErrorReporter() + .maybeHandleSilentException(Exception(message), this@ReaderActivity) + } + } + + override fun onFailure( + call: Call, + t: Throwable + ) { + if (debugReadingItems) { + ACRA.getErrorReporter().maybeHandleSilentException(t, this@ReaderActivity) + } + } + } + ) + } } private fun notifyAdapter() { diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/fragments/ArticleFragment.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/fragments/ArticleFragment.kt index bb72e91..cf45abe 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/fragments/ArticleFragment.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/fragments/ArticleFragment.kt @@ -101,7 +101,6 @@ class ArticleFragment : Fragment() { val settings = activity!!.getSharedPreferences(Config.settingsName, Context.MODE_PRIVATE) val debugReadingItems = prefs.getBoolean("read_debug", false) - val markOnScroll = prefs.getBoolean("mark_on_scroll", false) val api = SelfossApi( context!!, @@ -211,38 +210,6 @@ class ArticleFragment : Fragment() { } ) - if (markOnScroll) { - api.markItem(allItems[pageNumber.toInt()].id).enqueue( - object : Callback { - override fun onResponse( - call: Call, - response: Response - ) { - if (!response.succeeded() && debugReadingItems) { - val message = - "message: ${response.message()} " + - "response isSuccess: ${response.isSuccessful} " + - "response code: ${response.code()} " + - "response message: ${response.message()} " + - "response errorBody: ${response.errorBody()?.string()} " + - "body success: ${response.body()?.success} " + - "body isSuccess: ${response.body()?.isSuccess}" - ACRA.getErrorReporter().maybeHandleSilentException(Exception(message), activity!!) - } - } - - override fun onFailure( - call: Call, - t: Throwable - ) { - if (debugReadingItems) { - ACRA.getErrorReporter().maybeHandleSilentException(t, activity!!) - } - } - } - ) - } - return rootView }