From eab9df8ed9b403c8290ab3c0eda36d3143eb78ae Mon Sep 17 00:00:00 2001 From: Amine Bou Date: Wed, 6 Dec 2017 20:24:21 +0100 Subject: [PATCH] Fixes #144. Added some logs as I'm not sure if it's a good solution. --- .../bou/readerforselfoss/ReaderActivity.kt | 113 +++++++++++------- .../bou/readerforselfoss/utils/LinksUtils.kt | 2 +- 2 files changed, 73 insertions(+), 42 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 27131bf..ca4897e 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt @@ -25,8 +25,6 @@ import retrofit2.Response class ReaderActivity : AppCompatActivity() { - private lateinit var allItems: ArrayList - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) Scoop.getInstance().apply(this) @@ -43,6 +41,18 @@ class ReaderActivity : AppCompatActivity() { val userIdentifier = sharedPref.getString("unique_id", "") val markOnScroll = sharedPref.getBoolean("mark_on_scroll", false) + if (allItems.isEmpty()) { + Crashlytics.setUserIdentifier(userIdentifier) + Crashlytics.log( + 100, + "READER_ITEMS_EMPTY", + "Items empty when trying to open the Article Reader. Was (static) companion object field set ?" + ) + Crashlytics.logException(Exception("Empty items on Reader Activity.")) + + finish() + } + val api = SelfossApi( this, this@ReaderActivity, @@ -50,7 +60,6 @@ class ReaderActivity : AppCompatActivity() { sharedPref.getBoolean("should_log_everything", false) ) - allItems = intent.getParcelableArrayListExtra("allItems") val currentItem = intent.getIntExtra("currentItem", 0) var adapter = ScreenSlidePagerAdapter(supportFragmentManager) @@ -61,48 +70,61 @@ class ReaderActivity : AppCompatActivity() { (indicator as CircleIndicator).setViewPager(pager) if (markOnScroll) { - pager.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() { - var isLastItem = false + pager.addOnPageChangeListener( + object : ViewPager.SimpleOnPageChangeListener() { + var isLastItem = false - override fun onPageSelected(position: Int) { - isLastItem = (position === (allItems.size - 1)) - } + 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 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 onFailure(call: Call, t: Throwable) { - if (debugReadingItems) { - Crashlytics.setUserIdentifier(userIdentifier) - Crashlytics.log(100, "READ_DEBUG_ERROR", t.message) - Crashlytics.logException(t) - } - } - } - ) + ) + } + } } - } - }) + ) } } @@ -111,6 +133,11 @@ class ReaderActivity : AppCompatActivity() { pager.clearOnPageChangeListeners() } + override fun onStop() { + super.onStop() + allItems = ArrayList() + } + private inner class ScreenSlidePagerAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm) { override fun getCount(): Int { return allItems.size @@ -130,4 +157,8 @@ class ReaderActivity : AppCompatActivity() { } return super.onOptionsItemSelected(item) } + + companion object { + var allItems: ArrayList = ArrayList() + } } diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/LinksUtils.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/LinksUtils.kt index ae990a9..e95179a 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/LinksUtils.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/LinksUtils.kt @@ -67,8 +67,8 @@ fun Context.openItemUrlInternally( app: Activity ) { if (articleViewer) { + ReaderActivity.allItems = allItems val intent = Intent(this, ReaderActivity::class.java) - intent.putParcelableArrayListExtra("allItems", allItems) intent.putExtra("currentItem", currentItem) app.startActivity(intent) } else {