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 1b848a0..f5887d1 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt @@ -1,16 +1,27 @@ package apps.amine.bou.readerforselfoss +import android.content.Context import android.os.Bundle +import android.preference.PreferenceManager import android.support.v4.app.FragmentManager import android.support.v4.app.FragmentStatePagerAdapter +import android.support.v4.view.ViewPager import android.support.v7.app.AppCompatActivity import android.view.MenuItem import apps.amine.bou.readerforselfoss.api.selfoss.Item +import apps.amine.bou.readerforselfoss.api.selfoss.SelfossApi +import apps.amine.bou.readerforselfoss.api.selfoss.SuccessResponse import apps.amine.bou.readerforselfoss.fragments.ArticleFragment import apps.amine.bou.readerforselfoss.transformers.DepthPageTransformer +import apps.amine.bou.readerforselfoss.utils.Config +import apps.amine.bou.readerforselfoss.utils.succeeded +import com.crashlytics.android.Crashlytics import com.ftinc.scoop.Scoop import kotlinx.android.synthetic.main.activity_reader.* import me.relex.circleindicator.CircleIndicator +import retrofit2.Call +import retrofit2.Callback +import retrofit2.Response class ReaderActivity : AppCompatActivity() { @@ -25,6 +36,19 @@ class ReaderActivity : AppCompatActivity() { supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayShowHomeEnabled(true) + val settings = getSharedPreferences(Config.settingsName, Context.MODE_PRIVATE) + val sharedPref = PreferenceManager.getDefaultSharedPreferences(this) + + val debugReadingItems = sharedPref.getBoolean("read_debug", false) + val userIdentifier = sharedPref.getString("unique_id", "") + + val api = SelfossApi( + this, + this@ReaderActivity, + settings.getBoolean("isSelfSignedCert", false), + sharedPref.getBoolean("should_log_everything", false) + ) + allItems = intent.getParcelableArrayListExtra("allItems") val currentItem = intent.getIntExtra("currentItem", 0) @@ -34,6 +58,56 @@ class ReaderActivity : AppCompatActivity() { pager.setPageTransformer(true, DepthPageTransformer()) (indicator as CircleIndicator).setViewPager(pager) + + pager.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() { + var isLastItem = false + + override fun onPageSelected(position: Int) { + isLastItem = (position === (allItems.size - 1)) + } + + override fun onPageScrollStateChanged(state: Int) { + if (state === ViewPager.SCROLL_STATE_DRAGGING || (state === ViewPager.SCROLL_STATE_IDLE && isLastItem)) { + api.markItem(allItems[pager.currentItem].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}" + Crashlytics.setUserIdentifier(userIdentifier) + Crashlytics.log(100, "READ_DEBUG_SUCCESS", message) + Crashlytics.logException(Exception("Was success, but did it work ?")) + } + } + + override fun onFailure(call: Call, t: Throwable) { + if (debugReadingItems) { + Crashlytics.setUserIdentifier(userIdentifier) + Crashlytics.log(100, "READ_DEBUG_ERROR", t.message) + Crashlytics.logException(t) + } + } + } + ) + } + } + }) + + + } + + override fun onPause() { + super.onPause() + pager.clearOnPageChangeListeners() } private inner class ScreenSlidePagerAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm) {