Skip to content

Commit

Permalink
- ajout de la possibilité d'ajouter ou de retirer des topics favoris.
Browse files Browse the repository at this point in the history
  • Loading branch information
FranckRJ committed Jan 2, 2017
1 parent 015cc70 commit a416c8e
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -77,27 +91,41 @@ 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) {
savedInstanceState.putString(parentActivity.getString(R.string.saveTopicUrlToFetch), urlForTopic);
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<String, Void, TopicPageInfos> {
}

public static class TopicPageInfos {
protected static class TopicPageInfos {
ArrayList<JVCParser.MessageInfos> listOfMessages;
String lastPageLink;
String nextPageLink;
String listOfInputInAString;
JVCParser.AjaxInfos ajaxInfosOfThisPage;
JVCParser.ForumAndTopicName newNames;
Boolean newIsInFavs;
String newTopicID;
}

public interface NewForumAndTopicNameAvailable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
87 changes: 87 additions & 0 deletions java/com/franckrj/respawnirc/jvcviewers/ShowTopicActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -170,6 +171,10 @@ private void stopAllCurrentTask() {
currentTaskDeleteMessage.cancel(true);
currentTaskDeleteMessage = null;
}
if (currentTaskForTopicFavs != null) {
currentTaskForTopicFavs.cancel(true);
currentTaskForTopicFavs = null;
}
}

private void reloadSettings() {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
}
Expand Down Expand Up @@ -618,4 +646,63 @@ protected void onPostExecute(String pageContent) {
currentTaskDeleteMessage = null;
}
}

private class AddOrRemoveTopicToFavs extends AsyncTask<String, Void, String> {
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);
}
}
}
33 changes: 27 additions & 6 deletions java/com/franckrj/respawnirc/utils/JVCParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import java.util.regex.Pattern;

public final class JVCParser {
private static final Pattern ajaxTimestampPattern = Pattern.compile("<input type=\"hidden\" name=\"ajax_timestamp_liste_(messages|topics)\" id=\"ajax_timestamp_liste_(messages|topics)\" value=\"([^\"]*)\" />");
private static final Pattern ajaxHashPattern = Pattern.compile("<input type=\"hidden\" name=\"ajax_hash_liste_(messages|topics)\" id=\"ajax_hash_liste_(messages|topics)\" value=\"([^\"]*)\" />");
private static final Pattern ajaxListTimestampPattern = Pattern.compile("<input type=\"hidden\" name=\"ajax_timestamp_liste_(messages|topics)\" id=\"ajax_timestamp_liste_(messages|topics)\" value=\"([^\"]*)\" />");
private static final Pattern ajaxListHashPattern = Pattern.compile("<input type=\"hidden\" name=\"ajax_hash_liste_(messages|topics)\" id=\"ajax_hash_liste_(messages|topics)\" value=\"([^\"]*)\" />");
private static final Pattern ajaxModTimestampPattern = Pattern.compile("<input type=\"hidden\" name=\"ajax_timestamp_moderation_forum\" id=\"ajax_timestamp_moderation_forum\" value=\"([^\"]*)\" />");
private static final Pattern ajaxModHashPattern = Pattern.compile("<input type=\"hidden\" name=\"ajax_hash_moderation_forum\" id=\"ajax_hash_moderation_forum\" value=\"([^\"]*)\" />");
private static final Pattern ajaxPrefTimestampPattern = Pattern.compile("<input type=\"hidden\" name=\"ajax_timestamp_preference_user\" id=\"ajax_timestamp_preference_user\" value=\"([^\"]*)\" />");
Expand Down Expand Up @@ -54,6 +54,7 @@ public final class JVCParser {
private static final Pattern favPattern = Pattern.compile("<li><a href=\"([^\"]*)\">([^<]*)</a></li>");
private static final Pattern forumInSearchPagePattern = Pattern.compile("<a class=\"list-search-forum-name\" href=\"([^\"]*)\"[^>]*>(.*?)</a>");
private static final Pattern isInFavPattern = Pattern.compile("<span class=\"picto-favoris([^\"]*)\"");
private static final Pattern topicIDInTopicPagePattern = Pattern.compile("<div (.*?)data-topic-id=\"([^\"]*)\">");
private static final Pattern htmlTagPattern = Pattern.compile("<.+?>");

private JVCParser() {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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()) {
Expand Down
3 changes: 3 additions & 0 deletions res/menu/menu_showtopic.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_change_topic_fav_value_showtopic"
android:title="@string/addToFavs"/>
<item
android:id="@+id/action_past_last_message_sended_showtopic"
android:title="@string/pastLastMessageSended"/>
Expand Down
2 changes: 2 additions & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@
<string name="favRemoved">Favori retiré</string>
<string name="saveLatestAjaxInfoPref">save.latestAjaxInfoPref</string>
<string name="saveForumIsInFav">save.forumIsInFav</string>
<string name="saveTopicIsInFav">save.topicIsInFav</string>
<string name="saveTopicID">save.topicID</string>

<string-array name="choicesForLinkContextMenu">
<item>@string/openInBrowser</item>
Expand Down

0 comments on commit a416c8e

Please sign in to comment.