From a416c8eef6b7b3644a1d41afa46a649e928bdfb1 Mon Sep 17 00:00:00 2001 From: LEpigeon888 Date: Mon, 2 Jan 2017 15:02:32 +0100 Subject: [PATCH] =?UTF-8?q?-=20ajout=20de=20la=20possibilit=C3=A9=20d'ajou?= =?UTF-8?q?ter=20ou=20de=20retirer=20des=20topics=20favoris.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jvcmsggetters/AbsJVCMessageGetter.java | 30 ++++++- .../jvcmsggetters/JVCForumMessageGetter.java | 4 + .../jvcmsggetters/JVCIRCMessageGetter.java | 4 + .../jvcmsgviewers/AbsShowTopicFragment.java | 12 +++ .../jvcviewers/ShowTopicActivity.java | 87 +++++++++++++++++++ .../franckrj/respawnirc/utils/JVCParser.java | 33 +++++-- res/menu/menu_showtopic.xml | 3 + res/values/strings.xml | 2 + 8 files changed, 168 insertions(+), 7 deletions(-) diff --git a/java/com/franckrj/respawnirc/jvcmsggetters/AbsJVCMessageGetter.java b/java/com/franckrj/respawnirc/jvcmsggetters/AbsJVCMessageGetter.java index 2b262302..e022c998 100644 --- a/java/com/franckrj/respawnirc/jvcmsggetters/AbsJVCMessageGetter.java +++ b/java/com/franckrj/respawnirc/jvcmsggetters/AbsJVCMessageGetter.java @@ -24,6 +24,8 @@ public abstract class AbsJVCMessageGetter { protected NewGetterStateListener listenerForNewGetterState = null; protected NewForumAndTopicNameAvailable listenerForNewForumAndTopicName = null; protected JVCParser.ForumAndTopicName currentNames = new JVCParser.ForumAndTopicName(); + protected Boolean isInFavs = null; + protected String topicID = ""; public AbsJVCMessageGetter(Activity newParentActivity) { parentActivity = newParentActivity; @@ -45,6 +47,18 @@ public long getLastIdOfMessage() { return lastIdOfMessage; } + public Boolean getIsInFavs() { + return isInFavs; + } + + public String getTopicID() { + return topicID; + } + + public void setIsInFavs(Boolean newVal) { + isInFavs = newVal; + } + public void setCookieListInAString(String newCookieListInAString) { cookieListInAString = newCookieListInAString; } @@ -77,7 +91,14 @@ public void loadFromBundle(Bundle savedInstanceState) { latestListOfInputInAString = savedInstanceState.getString(parentActivity.getString(R.string.saveLatestListOfInputInAString), null); latestAjaxInfos.list = savedInstanceState.getString(parentActivity.getString(R.string.saveLatestAjaxInfoList), null); latestAjaxInfos.mod = savedInstanceState.getString(parentActivity.getString(R.string.saveLatestAjaxInfoMod), null); + latestAjaxInfos.pref = savedInstanceState.getString(parentActivity.getString(R.string.saveLatestAjaxInfoPref), null); lastIdOfMessage = savedInstanceState.getLong(parentActivity.getString(R.string.saveLastIdOfMessage), 0); + topicID = savedInstanceState.getString(parentActivity.getString(R.string.saveTopicID), ""); + if (savedInstanceState.containsKey(parentActivity.getString(R.string.saveTopicIsInFav))) { + isInFavs = savedInstanceState.getBoolean(parentActivity.getString(R.string.saveTopicIsInFav), false); + } else { + isInFavs = null; + } } public void saveToBundle(Bundle savedInstanceState) { @@ -85,19 +106,26 @@ public void saveToBundle(Bundle savedInstanceState) { savedInstanceState.putString(parentActivity.getString(R.string.saveLatestListOfInputInAString), latestListOfInputInAString); savedInstanceState.putString(parentActivity.getString(R.string.saveLatestAjaxInfoList), latestAjaxInfos.list); savedInstanceState.putString(parentActivity.getString(R.string.saveLatestAjaxInfoMod), latestAjaxInfos.mod); + savedInstanceState.putString(parentActivity.getString(R.string.saveLatestAjaxInfoPref), latestAjaxInfos.pref); savedInstanceState.putLong(parentActivity.getString(R.string.saveLastIdOfMessage), lastIdOfMessage); + savedInstanceState.putString(parentActivity.getString(R.string.saveTopicID), topicID); + if (isInFavs != null) { + savedInstanceState.putBoolean(parentActivity.getString(R.string.saveTopicIsInFav), isInFavs); + } } protected abstract class AbsGetJVCLastMessages extends AsyncTask { } - public static class TopicPageInfos { + protected static class TopicPageInfos { ArrayList listOfMessages; String lastPageLink; String nextPageLink; String listOfInputInAString; JVCParser.AjaxInfos ajaxInfosOfThisPage; JVCParser.ForumAndTopicName newNames; + Boolean newIsInFavs; + String newTopicID; } public interface NewForumAndTopicNameAvailable { diff --git a/java/com/franckrj/respawnirc/jvcmsggetters/JVCForumMessageGetter.java b/java/com/franckrj/respawnirc/jvcmsggetters/JVCForumMessageGetter.java index ea8af555..e4475349 100644 --- a/java/com/franckrj/respawnirc/jvcmsggetters/JVCForumMessageGetter.java +++ b/java/com/franckrj/respawnirc/jvcmsggetters/JVCForumMessageGetter.java @@ -60,6 +60,8 @@ protected TopicPageInfos doInBackground(String... params) { newPageInfos.listOfInputInAString = JVCParser.getListOfInputInAString(pageContent); newPageInfos.ajaxInfosOfThisPage = JVCParser.getAllAjaxInfos(pageContent); newPageInfos.newNames = JVCParser.getForumAndTopicNameInTopicPage(pageContent); + newPageInfos.newIsInFavs = JVCParser.getIsInFavsFromPage(pageContent); + newPageInfos.newTopicID = JVCParser.getTopicIDInThisTopicPage(pageContent); } return newPageInfos; @@ -80,6 +82,8 @@ protected void onPostExecute(TopicPageInfos infoOfCurrentPage) { if (infoOfCurrentPage != null) { latestListOfInputInAString = infoOfCurrentPage.listOfInputInAString; latestAjaxInfos = infoOfCurrentPage.ajaxInfosOfThisPage; + isInFavs = infoOfCurrentPage.newIsInFavs; + topicID = infoOfCurrentPage.newTopicID; if (!infoOfCurrentPage.listOfMessages.isEmpty()) { lastIdOfMessage = infoOfCurrentPage.listOfMessages.get(infoOfCurrentPage.listOfMessages.size() - 1).id; diff --git a/java/com/franckrj/respawnirc/jvcmsggetters/JVCIRCMessageGetter.java b/java/com/franckrj/respawnirc/jvcmsggetters/JVCIRCMessageGetter.java index cf5eeb0a..a885b00d 100644 --- a/java/com/franckrj/respawnirc/jvcmsggetters/JVCIRCMessageGetter.java +++ b/java/com/franckrj/respawnirc/jvcmsggetters/JVCIRCMessageGetter.java @@ -132,6 +132,8 @@ protected TopicPageInfos doInBackground(String... params) { newPageInfos.listOfInputInAString = JVCParser.getListOfInputInAString(pageContent); newPageInfos.ajaxInfosOfThisPage = JVCParser.getAllAjaxInfos(pageContent); newPageInfos.newNames = JVCParser.getForumAndTopicNameInTopicPage(pageContent); + newPageInfos.newIsInFavs = JVCParser.getIsInFavsFromPage(pageContent); + newPageInfos.newTopicID = JVCParser.getTopicIDInThisTopicPage(pageContent); } return newPageInfos; @@ -155,6 +157,8 @@ protected void onPostExecute(TopicPageInfos infoOfCurrentPage) { if (infoOfCurrentPage != null) { latestListOfInputInAString = infoOfCurrentPage.listOfInputInAString; latestAjaxInfos = infoOfCurrentPage.ajaxInfosOfThisPage; + isInFavs = infoOfCurrentPage.newIsInFavs; + topicID = infoOfCurrentPage.newTopicID; if (infoOfCurrentPage.lastPageLink.isEmpty() || !firstTimeGetMessages) { if (!infoOfCurrentPage.listOfMessages.isEmpty()) { diff --git a/java/com/franckrj/respawnirc/jvcmsgviewers/AbsShowTopicFragment.java b/java/com/franckrj/respawnirc/jvcmsgviewers/AbsShowTopicFragment.java index ecc50109..77a6dc7c 100644 --- a/java/com/franckrj/respawnirc/jvcmsgviewers/AbsShowTopicFragment.java +++ b/java/com/franckrj/respawnirc/jvcmsgviewers/AbsShowTopicFragment.java @@ -118,6 +118,18 @@ public String getCurrentUrlOfTopic() { return absGetterForMessages.getUrlForTopic(); } + public Boolean getIsInFavs() { + return absGetterForMessages.getIsInFavs(); + } + + public String getTopicID() { + return absGetterForMessages.getTopicID(); + } + + public void setIsInFavs(Boolean newVal) { + absGetterForMessages.setIsInFavs(newVal); + } + public void setPseudoAndCookies(String newPseudo, String newCookieList) { pseudoOfUser = newPseudo; cookieListInAString = newCookieList; diff --git a/java/com/franckrj/respawnirc/jvcviewers/ShowTopicActivity.java b/java/com/franckrj/respawnirc/jvcviewers/ShowTopicActivity.java index aea4c310..ca7fad73 100644 --- a/java/com/franckrj/respawnirc/jvcviewers/ShowTopicActivity.java +++ b/java/com/franckrj/respawnirc/jvcviewers/ShowTopicActivity.java @@ -55,6 +55,7 @@ public class ShowTopicActivity extends AbsShowSomethingActivity implements AbsSh private View layoutForAllNavigationButtons = null; private View shadowForAllNavigationButtons = null; private String lastMessageSended = ""; + private AddOrRemoveTopicToFavs currentTaskForTopicFavs = null; private final JVCMessageSender.NewMessageWantEditListener listenerForNewMessageWantEdit = new JVCMessageSender.NewMessageWantEditListener() { @Override @@ -170,6 +171,10 @@ private void stopAllCurrentTask() { currentTaskDeleteMessage.cancel(true); currentTaskDeleteMessage = null; } + if (currentTaskForTopicFavs != null) { + currentTaskForTopicFavs.cancel(true); + currentTaskForTopicFavs = null; + } } private void reloadSettings() { @@ -351,6 +356,19 @@ public boolean onCreateOptionsMenu(Menu menu) { public boolean onPrepareOptionsMenu(Menu menu) { super.onPrepareOptionsMenu(menu); menu.findItem(R.id.action_past_last_message_sended_showtopic).setEnabled(!lastMessageSended.isEmpty()); + + if (!pseudoOfUser.isEmpty()) { + if (getCurrentFragment().getIsInFavs() != null) { + menu.findItem(R.id.action_change_topic_fav_value_showtopic).setEnabled(true); + if (getCurrentFragment().getIsInFavs()) { + menu.findItem(R.id.action_change_topic_fav_value_showtopic).setTitle(R.string.removeOfFavs); + } else { + menu.findItem(R.id.action_change_topic_fav_value_showtopic).setTitle(R.string.addToFavs); + } + return true; + } + } + menu.findItem(R.id.action_change_topic_fav_value_showtopic).setEnabled(false); return true; } @@ -359,9 +377,19 @@ public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: onBackPressed(); + return true; + case R.id.action_change_topic_fav_value_showtopic: + if (currentTaskForTopicFavs == null) { + currentTaskForTopicFavs = new AddOrRemoveTopicToFavs(!getCurrentFragment().getIsInFavs()); + currentTaskForTopicFavs.execute(JVCParser.getForumIDOfThisTopic(currentLink), getCurrentFragment().getTopicID(), getCurrentFragment().getLatestAjaxInfos().pref, cookieListInAString); + } else { + Toast.makeText(ShowTopicActivity.this, R.string.errorActionAlreadyRunning, Toast.LENGTH_SHORT).show(); + } + return true; case R.id.action_past_last_message_sended_showtopic: messageSendEdit.setText(lastMessageSended); + return true; default: return super.onOptionsItemSelected(item); } @@ -618,4 +646,63 @@ protected void onPostExecute(String pageContent) { currentTaskDeleteMessage = null; } } + + private class AddOrRemoveTopicToFavs extends AsyncTask { + final boolean addToFavs; + + AddOrRemoveTopicToFavs(boolean itsAnAdd) { + addToFavs = itsAnAdd; + } + + @Override + protected String doInBackground(String... params) { + if (params.length > 3) { + String actionToDo; + String pageContent; + WebManager.WebInfos currentWebInfos = new WebManager.WebInfos(); + currentWebInfos.followRedirects = true; + + if (addToFavs) { + actionToDo = "add"; + } else { + actionToDo = "delete"; + } + + pageContent = WebManager.sendRequest("http://www.jeuxvideo.com/forums/ajax_forum_prefere.php", "GET", "id_forum=" + params[0] + "&id_topic=" + params[1] + "&action=" + actionToDo + "&type=topic&" + params[2], params[3], currentWebInfos); + + if (pageContent != null) { + if (!pageContent.isEmpty()) { + return JVCParser.getErrorMessageInJSONMode(pageContent); + } + } + return null; + } else { + return ""; + } + } + + @Override + protected void onPostExecute(String errorResult) { + super.onPostExecute(errorResult); + + currentTaskForTopicFavs = null; + + if (errorResult != null) { + if (!errorResult.isEmpty()) { + Toast.makeText(ShowTopicActivity.this, errorResult, Toast.LENGTH_SHORT).show(); + } else { + Toast.makeText(ShowTopicActivity.this, R.string.errorInfosMissings, Toast.LENGTH_SHORT).show(); + } + return; + } + + if (addToFavs) { + Toast.makeText(ShowTopicActivity.this, R.string.favAdded, Toast.LENGTH_SHORT).show(); + } else { + Toast.makeText(ShowTopicActivity.this, R.string.favRemoved, Toast.LENGTH_SHORT).show(); + } + + getCurrentFragment().setIsInFavs(addToFavs); + } + } } diff --git a/java/com/franckrj/respawnirc/utils/JVCParser.java b/java/com/franckrj/respawnirc/utils/JVCParser.java index 96e743ff..cc35da3c 100644 --- a/java/com/franckrj/respawnirc/utils/JVCParser.java +++ b/java/com/franckrj/respawnirc/utils/JVCParser.java @@ -10,8 +10,8 @@ import java.util.regex.Pattern; public final class JVCParser { - private static final Pattern ajaxTimestampPattern = Pattern.compile(""); - private static final Pattern ajaxHashPattern = Pattern.compile(""); + private static final Pattern ajaxListTimestampPattern = Pattern.compile(""); + private static final Pattern ajaxListHashPattern = Pattern.compile(""); private static final Pattern ajaxModTimestampPattern = Pattern.compile(""); private static final Pattern ajaxModHashPattern = Pattern.compile(""); private static final Pattern ajaxPrefTimestampPattern = Pattern.compile(""); @@ -54,6 +54,7 @@ public final class JVCParser { private static final Pattern favPattern = Pattern.compile("
  • ([^<]*)
  • "); private static final Pattern forumInSearchPagePattern = Pattern.compile("]*>(.*?)"); private static final Pattern isInFavPattern = Pattern.compile(""); private static final Pattern htmlTagPattern = Pattern.compile("<.+?>"); private JVCParser() { @@ -132,6 +133,26 @@ public static String getForumIDOfThisForum(String forumLink) { } } + public static String getForumIDOfThisTopic(String topicLink) { + Matcher topicLinkNumberMatcher = pageTopicLinkNumberPattern.matcher(topicLink); + + if (topicLinkNumberMatcher.find()) { + return topicLinkNumberMatcher.group(2); + } else { + return ""; + } + } + + public static String getTopicIDInThisTopicPage(String topicContent) { + Matcher topicIDInTopicPageMatcher = topicIDInTopicPagePattern.matcher(topicContent); + + if (topicIDInTopicPageMatcher.find()) { + return topicIDInTopicPageMatcher.group(2); + } else { + return ""; + } + } + public static String getPageNumberForThisTopicLink(String topicLink) { Matcher pageTopicLinkNumberMatcher = pageTopicLinkNumberPattern.matcher(topicLink); @@ -380,15 +401,15 @@ public static String getMessageQuoted(String pageSource) { public static AjaxInfos getAllAjaxInfos(String pageSource) { AjaxInfos newAjaxInfos = new AjaxInfos(); - Matcher ajaxTimestampMatcher = ajaxTimestampPattern.matcher(pageSource); - Matcher ajaxHashMatcher = ajaxHashPattern.matcher(pageSource); + Matcher ajaxListTimestampMatcher = ajaxListTimestampPattern.matcher(pageSource); + Matcher ajaxListHashMatcher = ajaxListHashPattern.matcher(pageSource); Matcher ajaxModTimestampMatcher = ajaxModTimestampPattern.matcher(pageSource); Matcher ajaxModHashMatcher = ajaxModHashPattern.matcher(pageSource); Matcher ajaxPrefTimestampMatcher = ajaxPrefTimestampPattern.matcher(pageSource); Matcher ajaxPrefHashMatcher = ajaxPrefHashPattern.matcher(pageSource); - if (ajaxTimestampMatcher.find() && ajaxHashMatcher.find()) { - newAjaxInfos.list = "ajax_timestamp=" + ajaxTimestampMatcher.group(3) + "&ajax_hash=" + ajaxHashMatcher.group(3); + if (ajaxListTimestampMatcher.find() && ajaxListHashMatcher.find()) { + newAjaxInfos.list = "ajax_timestamp=" + ajaxListTimestampMatcher.group(3) + "&ajax_hash=" + ajaxListHashMatcher.group(3); } if (ajaxModTimestampMatcher.find() && ajaxModHashMatcher.find()) { diff --git a/res/menu/menu_showtopic.xml b/res/menu/menu_showtopic.xml index df7476a3..b6010ca5 100644 --- a/res/menu/menu_showtopic.xml +++ b/res/menu/menu_showtopic.xml @@ -1,6 +1,9 @@ + diff --git a/res/values/strings.xml b/res/values/strings.xml index e4da2bb7..0ad6c395 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -134,6 +134,8 @@ Favori retirĂ© save.latestAjaxInfoPref save.forumIsInFav + save.topicIsInFav + save.topicID @string/openInBrowser