Browse Source

Refresh sesc/markAsReadUrl if needed

pull/70/head
Ezerous 5 years ago
parent
commit
104b568467
No known key found for this signature in database GPG Key ID: 262B2954BBA319E3
  1. 15
      app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java
  2. 4
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java
  3. 25
      app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java
  4. 2
      app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java

15
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 topBar = document.select("table:not(.bordercolor):not(#bodyarea):has(td.middletext)").first();
Element pagesElement = null; Element pagesElement = null, markRead = null;
if (topBar != null) if (topBar != null) {
pagesElement = topBar.select("td.middletext").first(); pagesElement = topBar.select("td.middletext").first();
markRead = document.select("table:not(.bordercolor):not([width])").select("a")
.first();
}
if (numberOfPages == 0 && pagesElement != null) { if (numberOfPages == 0 && pagesElement != null) {
Elements pages = pagesElement.select("a"); Elements pages = pagesElement.select("a");
@ -319,6 +322,14 @@ public class UnreadFragment extends BaseFragment {
numberOfPages = 1; 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 fetchedTopicSummaries;
} }
return new ArrayList<>(); return new ArrayList<>();

4
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java

@ -967,7 +967,7 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
if (viewModel.getCurrentPageIndex() == viewModel.getPageCount()) { if (viewModel.getCurrentPageIndex() == viewModel.getPageCount()) {
//same page //same page
postFocusListener.onPostFocusChange(getItemCount() - 1); postFocusListener.onPostFocusChange(getItemCount() - 1);
Timber.e("new"); Timber.d("new");
return true; return true;
} }
} }
@ -979,7 +979,7 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
for (int i = 0; i < topicItems.size(); i++) { for (int i = 0; i < topicItems.size(); i++) {
if (topicItems.get(i) instanceof Post && ((Post) topicItems.get(i)).getPostIndex() == testAgainst) { if (topicItems.get(i) instanceof Post && ((Post) topicItems.get(i)).getPostIndex() == testAgainst) {
//same page //same page
Timber.e(Integer.toString(i)); Timber.d(Integer.toString(i));
postFocusListener.onPostFocusChange(i); postFocusListener.onPostFocusChange(i);
return true; return true;
} }

25
app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java

@ -244,6 +244,15 @@ public class SessionManager {
guestLogin(); guestLogin();
} }
} }
public void refreshSescFromUrl(String url){
String sesc = extractSescFromLink(url);
if(sesc!=null){
setSesc(sesc);
setLogoutLink(generateLogoutLink(sesc));
setMarkAsReadLink(sesc);
}
}
//--------------------------------------AUTH ENDS----------------------------------------------- //--------------------------------------AUTH ENDS-----------------------------------------------
//---------------------------------------GETTERS------------------------------------------------ //---------------------------------------GETTERS------------------------------------------------
@ -271,7 +280,7 @@ public class SessionManager {
public String getMarkAllAsReadLink() { public String getMarkAllAsReadLink() {
String markAsReadLink = sharedPrefs.getString(MARK_ALL_AS_READ_LINK, null); 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) 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) { if(sesc!=null) {
setSesc(sesc); setSesc(sesc);
markAsReadLink = generateMarkAllAsReadLink(sesc); markAsReadLink = generateMarkAllAsReadLink(sesc);
@ -313,6 +322,12 @@ public class SessionManager {
editor.apply(); editor.apply();
} }
private void setLogoutLink(String logoutLink){
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putString(LOGOUT_LINK, logoutLink);
editor.apply();
}
//--------------------------------------SETTERS END--------------------------------------------- //--------------------------------------SETTERS END---------------------------------------------
//------------------------------------OTHER FUNCTIONS------------------------------------------- //------------------------------------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=]"); Elements logoutLink = doc.select("a[href^=https://www.thmmy.gr/smf/index.php?action=logout;sesc=]");
if (!logoutLink.isEmpty()) { if (!logoutLink.isEmpty()) {
String link = logoutLink.first().attr("href"); String link = logoutLink.first().attr("href");
return extractSescFromLogoutLink(link); return extractSescFromLink(link);
} }
Timber.e(new ParseException("Parsing failed(extractSesc)"),"ParseException"); Timber.e(new ParseException("Parsing failed(extractSesc)"),"ParseException");
return null; return null;
} }
private String extractSescFromLogoutLink(String logoutLink){ private String extractSescFromLink(String link){
if (logoutLink != null){ if (link != null){
Pattern pattern = Pattern.compile(".+;sesc=(\\w+)"); Pattern pattern = Pattern.compile(".+;sesc=(\\w+)");
Matcher matcher = pattern.matcher(logoutLink); Matcher matcher = pattern.matcher(link);
if (matcher.find()) if (matcher.find())
return matcher.group(1); return matcher.group(1);
} }

2
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; int pageRequested = pageIndicatorIndex.getValue() - 1;
if (pageRequested != currentPageIndex - 1) { if (pageRequested != currentPageIndex - 1) {
Timber.i("Changing to page " + pageRequested + 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); pageIndicatorIndex.setValue(pageRequested + 1);
} else { } else {
stopLoading(); stopLoading();

Loading…
Cancel
Save