From 104b5684671424b77287e55814784a76f8c9c570 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Fri, 29 May 2020 19:17:47 +0300 Subject: [PATCH] Refresh sesc/markAsReadUrl if needed --- .../main/unread/UnreadFragment.java | 15 +++++++++-- .../mthmmy/activities/topic/TopicAdapter.java | 4 +-- .../thmmy/mthmmy/session/SessionManager.java | 25 +++++++++++++++---- .../mthmmy/viewmodel/TopicViewModel.java | 2 +- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java index 7a4dd005..c3088094 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java @@ -307,9 +307,12 @@ public class UnreadFragment extends BaseFragment { } Element topBar = document.select("table:not(.bordercolor):not(#bodyarea):has(td.middletext)").first(); - Element pagesElement = null; - if (topBar != null) + Element pagesElement = null, markRead = null; + if (topBar != null) { pagesElement = topBar.select("td.middletext").first(); + markRead = document.select("table:not(.bordercolor):not([width])").select("a") + .first(); + } if (numberOfPages == 0 && pagesElement != null) { Elements pages = pagesElement.select("a"); @@ -319,6 +322,14 @@ public class UnreadFragment extends BaseFragment { numberOfPages = 1; } + if (markRead != null && loadedPages == numberOfPages - 1){ + String retrievedMarkAsReadUrl = markRead.attr("href"); + if(!retrievedMarkAsReadUrl.equals(markAsReadUrl)) { + markAsReadUrl = retrievedMarkAsReadUrl; + BaseApplication.getInstance().getSessionManager().refreshSescFromUrl(retrievedMarkAsReadUrl); + } + } + return fetchedTopicSummaries; } return new ArrayList<>(); diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java index 50b1f157..213de341 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java @@ -967,7 +967,7 @@ class TopicAdapter extends RecyclerView.Adapter { if (viewModel.getCurrentPageIndex() == viewModel.getPageCount()) { //same page postFocusListener.onPostFocusChange(getItemCount() - 1); - Timber.e("new"); + Timber.d("new"); return true; } } @@ -979,7 +979,7 @@ class TopicAdapter extends RecyclerView.Adapter { for (int i = 0; i < topicItems.size(); i++) { if (topicItems.get(i) instanceof Post && ((Post) topicItems.get(i)).getPostIndex() == testAgainst) { //same page - Timber.e(Integer.toString(i)); + Timber.d(Integer.toString(i)); postFocusListener.onPostFocusChange(i); return true; } diff --git a/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java b/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java index 81dd2f31..2dab5489 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java +++ b/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java @@ -244,6 +244,15 @@ public class SessionManager { guestLogin(); } } + + public void refreshSescFromUrl(String url){ + String sesc = extractSescFromLink(url); + if(sesc!=null){ + setSesc(sesc); + setLogoutLink(generateLogoutLink(sesc)); + setMarkAsReadLink(sesc); + } + } //--------------------------------------AUTH ENDS----------------------------------------------- //---------------------------------------GETTERS------------------------------------------------ @@ -271,7 +280,7 @@ public class SessionManager { public String getMarkAllAsReadLink() { String markAsReadLink = sharedPrefs.getString(MARK_ALL_AS_READ_LINK, null); if(markAsReadLink == null){ //For older versions, extract it from logout link (otherwise user would have to login again) - String sesc = extractSescFromLogoutLink(getLogoutLink()); + String sesc = extractSescFromLink(getLogoutLink()); if(sesc!=null) { setSesc(sesc); markAsReadLink = generateMarkAllAsReadLink(sesc); @@ -313,6 +322,12 @@ public class SessionManager { editor.apply(); } + private void setLogoutLink(String logoutLink){ + SharedPreferences.Editor editor = sharedPrefs.edit(); + editor.putString(LOGOUT_LINK, logoutLink); + editor.apply(); + } + //--------------------------------------SETTERS END--------------------------------------------- //------------------------------------OTHER FUNCTIONS------------------------------------------- @@ -421,16 +436,16 @@ public class SessionManager { Elements logoutLink = doc.select("a[href^=https://www.thmmy.gr/smf/index.php?action=logout;sesc=]"); if (!logoutLink.isEmpty()) { String link = logoutLink.first().attr("href"); - return extractSescFromLogoutLink(link); + return extractSescFromLink(link); } Timber.e(new ParseException("Parsing failed(extractSesc)"),"ParseException"); return null; } - private String extractSescFromLogoutLink(String logoutLink){ - if (logoutLink != null){ + private String extractSescFromLink(String link){ + if (link != null){ Pattern pattern = Pattern.compile(".+;sesc=(\\w+)"); - Matcher matcher = pattern.matcher(logoutLink); + Matcher matcher = pattern.matcher(link); if (matcher.find()) return matcher.group(1); } diff --git a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java index bac3952a..8f8f4c3a 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java +++ b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java @@ -132,7 +132,7 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa int pageRequested = pageIndicatorIndex.getValue() - 1; if (pageRequested != currentPageIndex - 1) { Timber.i("Changing to page " + pageRequested + 1); - loadUrl(ParseHelpers.getBaseURL(topicUrl) + "." + String.valueOf(pageRequested * 15)); + loadUrl(ParseHelpers.getBaseURL(topicUrl) + "." + pageRequested * 15); pageIndicatorIndex.setValue(pageRequested + 1); } else { stopLoading();