From 67ffb59c67941ae19fb5c8e42d98f542cdd5f6af Mon Sep 17 00:00:00 2001 From: Apostolof Date: Sat, 7 Jul 2018 20:27:18 +0300 Subject: [PATCH] Fixes for settings, Add delete post --- app/src/main/AndroidManifest.xml | 1 - .../bookmarks/BookmarkActivity.java | 1 - .../activities/settings/SettingsActivity.java | 5 +- .../activities/settings/SettingsFragment.java | 44 +++++++++++-- .../activities/topic/TopicActivity.java | 54 +++++++++++++++ .../mthmmy/activities/topic/TopicAdapter.java | 62 +++++++++++++++--- .../mthmmy/activities/topic/TopicParser.java | 24 +++++-- .../gr/thmmy/mthmmy/base/BaseActivity.java | 8 +-- .../main/java/gr/thmmy/mthmmy/model/Post.java | 20 +++++- .../main/res/drawable-hdpi/ic_more_vert.png | Bin 0 -> 164 bytes .../main/res/drawable-mdpi/ic_more_vert.png | Bin 0 -> 127 bytes .../main/res/drawable-xhdpi/ic_more_vert.png | Bin 0 -> 203 bytes .../main/res/drawable-xxhdpi/ic_more_vert.png | Bin 0 -> 297 bytes .../res/drawable-xxxhdpi/ic_more_vert.png | Bin 0 -> 406 bytes .../layout-v21/activity_topic_post_row.xml | 15 ++++- .../res/layout/activity_topic_post_row.xml | 15 ++++- app/src/main/res/menu/post_menu.xml | 19 ++++++ app/src/main/res/values-v21/styles.xml | 1 + app/src/main/res/values/strings.xml | 7 +- app/src/main/res/values/styles.xml | 8 +++ app/src/main/res/xml/app_preferences.xml | 5 +- 21 files changed, 251 insertions(+), 38 deletions(-) create mode 100644 app/src/main/res/drawable-hdpi/ic_more_vert.png create mode 100644 app/src/main/res/drawable-mdpi/ic_more_vert.png create mode 100644 app/src/main/res/drawable-xhdpi/ic_more_vert.png create mode 100644 app/src/main/res/drawable-xxhdpi/ic_more_vert.png create mode 100644 app/src/main/res/drawable-xxxhdpi/ic_more_vert.png create mode 100644 app/src/main/res/menu/post_menu.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b9789d97..84fe83a8 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -108,7 +108,6 @@ { + + @Override + protected void onPreExecute() { + progressBar.setVisibility(ProgressBar.VISIBLE); + paginationEnabled(false); + replyFAB.setEnabled(false); + } + + @Override + protected Boolean doInBackground(String... args) { + Request delete = new Request.Builder() + .url(args[0]) + .header("User-Agent", + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36") + .build(); + + try { + client.newCall(delete).execute(); + Response response = client.newCall(delete).execute(); + //Response response = client.newCall(delete).execute(); + switch (replyStatus(response)) { + case SUCCESSFUL: + return true; + default: + Timber.e("Something went wrong. Request string: %s", delete.toString()); + return true; + } + } catch (IOException e) { + Timber.e(e, "Delete failed."); + return false; + } + } + + @Override + protected void onPostExecute(Boolean result) { + progressBar.setVisibility(ProgressBar.GONE); + replyFAB.setVisibility(View.VISIBLE); + bottomNavBar.setVisibility(View.VISIBLE); + + if (!result) + Toast.makeText(TopicActivity.this, "Post deleted!", Toast.LENGTH_SHORT).show(); + paginationEnabled(true); + replyFAB.setEnabled(true); + + if (result) { + topicTask = new TopicTask(); + reloadingPage = true; + topicTask.execute(loadedPageUrl); + } + } + } } \ No newline at end of file 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 54560b58..c1516327 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 @@ -11,12 +11,19 @@ import android.os.Build; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.content.res.ResourcesCompat; +import android.support.v7.view.menu.MenuBuilder; +import android.support.v7.view.menu.MenuPopupHelper; import android.support.v7.widget.AppCompatImageButton; +import android.support.v7.widget.PopupMenu; import android.support.v7.widget.RecyclerView; import android.text.Editable; import android.text.TextUtils; import android.text.TextWatcher; +import android.view.Gravity; import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.InputMethodManager; @@ -87,6 +94,7 @@ class TopicAdapter extends RecyclerView.Adapter { private static final int isQuoteButtonChecked = 1; private TopicActivity.TopicTask topicTask; private TopicActivity.ReplyTask replyTask; + private TopicActivity.DeleteTask deleteTask; private final int VIEW_TYPE_POST = 0; private final int VIEW_TYPE_QUICK_REPLY = 1; @@ -113,7 +121,7 @@ class TopicAdapter extends RecyclerView.Adapter { this.topicTask = topicTask; } - ArrayList getToQuoteList(){ + ArrayList getToQuoteList() { return toQuoteList; } @@ -128,6 +136,10 @@ class TopicAdapter extends RecyclerView.Adapter { this.buildedQuotes = buildedQuotes; } + void prepareForDelete(TopicActivity.DeleteTask deleteTask) { + this.deleteTask = deleteTask; + } + @Override public int getItemViewType(int position) { return postsList.get(position) == null ? VIEW_TYPE_QUICK_REPLY : VIEW_TYPE_POST; @@ -409,13 +421,44 @@ class TopicAdapter extends RecyclerView.Adapter { holder.userExtraInfo.setOnClickListener(null); } - holder.sharePostButton.setOnClickListener(new View.OnClickListener() { + holder.overflowButton.setOnClickListener(new View.OnClickListener() { + @SuppressLint("RestrictedApi") @Override public void onClick(View view) { - Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND); - sendIntent.setType("text/plain"); - sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, currentPost.getPostURL()); - context.startActivity(Intent.createChooser(sendIntent, "Share via")); + //Inflates menu + PopupMenu popup = new PopupMenu(holder.overflowButton.getContext(), holder.overflowButton, Gravity.END); + MenuInflater inflater = popup.getMenuInflater(); + inflater.inflate(R.menu.post_menu, popup.getMenu()); + + Menu popupMenu = popup.getMenu(); + if(currentPost.getPostDeleteURL() == null || currentPost.getPostDeleteURL().equals("")){ + popupMenu.findItem(R.id.delete_post).setEnabled(false); + popupMenu.findItem(R.id.delete_post).setVisible(false); + } + + MenuPopupHelper optionsMenu = new MenuPopupHelper(holder.overflowButton.getContext() + , new MenuBuilder(holder.overflowButton.getContext()), holder.overflowButton); + optionsMenu.setForceShowIcon(true); + + popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { + @Override + public boolean onMenuItemClick(MenuItem item) { + switch (item.getItemId()) { + case R.id.post_share_button: + Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND); + sendIntent.setType("text/plain"); + sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, currentPost.getPostURL()); + context.startActivity(Intent.createChooser(sendIntent, "Share via")); + return true; + case R.id.delete_post: + deleteTask.execute(currentPost.getPostDeleteURL()); + return true; + default: + } + return false; + } + }); + popup.show(); } }); @@ -482,8 +525,7 @@ class TopicAdapter extends RecyclerView.Adapter { holder.submitButton.setEnabled(true); } }); - if(backPressHidden) - { + if (backPressHidden) { holder.quickReply.requestFocus(); backPressHidden = false; } @@ -514,7 +556,7 @@ class TopicAdapter extends RecyclerView.Adapter { final TextView postDate, postNum, username, subject; final ImageView thumbnail; final public WebView post; - final ImageButton quoteToggle, sharePostButton; + final ImageButton quoteToggle, overflowButton; final RelativeLayout header; final LinearLayout userExtraInfo; final View bodyFooterDivider; @@ -535,7 +577,7 @@ class TopicAdapter extends RecyclerView.Adapter { post = view.findViewById(R.id.post); post.setBackgroundColor(Color.argb(1, 255, 255, 255)); quoteToggle = view.findViewById(R.id.toggle_quote_button); - sharePostButton = view.findViewById(R.id.post_share_button); + overflowButton = view.findViewById(R.id.post_overflow_menu); bodyFooterDivider = view.findViewById(R.id.body_footer_divider); postFooter = view.findViewById(R.id.post_footer); diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java index 97f134ae..3ead462c 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java @@ -156,7 +156,8 @@ class TopicParser { for (Element thisRow : postRows) { //Variables for Post constructor String p_userName, p_thumbnailURL, p_subject, p_post, p_postDate, p_profileURL, p_rank, - p_specialRank, p_gender, p_personalText, p_numberOfPosts, p_postLastEditDate, p_postURL; + p_specialRank, p_gender, p_personalText, p_numberOfPosts, p_postLastEditDate, + p_postURL, p_deletePostURL; int p_postNum, p_postIndex, p_numberOfStars, p_userColor; boolean p_isDeleted = false; ArrayList p_attachedFiles; @@ -172,6 +173,7 @@ class TopicParser { p_userColor = USER_COLOR_YELLOW; p_attachedFiles = new ArrayList<>(); p_postLastEditDate = null; + p_deletePostURL = null; //Language independent parsing //Finds thumbnail url @@ -232,6 +234,12 @@ class TopicParser { p_profileURL = userName.attr("href"); } + //Finds post delete url + Element postDelete = thisRow.select("a:has(img[alt='Διαγραφή'])").first(); + if (postDelete!=null){ + p_deletePostURL = postDelete.attr("href"); + } + //Finds post's submit date Element postDate = thisRow.select("div.smalltext:matches(στις:)").first(); p_postDate = postDate.text(); @@ -292,6 +300,12 @@ class TopicParser { p_profileURL = userName.attr("href"); } + //Finds post delete url + Element postDelete = thisRow.select("a:has(img[alt='Remove message'])").first(); + if (postDelete!=null){ + p_deletePostURL = postDelete.attr("href"); + } + //Finds post's submit date Element postDate = thisRow.select("div.smalltext:matches(on:)").first(); p_postDate = postDate.text(); @@ -412,16 +426,18 @@ class TopicParser { p_personalText = p_personalText.replace("\n", "").replace("\r", "").trim(); } } + //Add new post in postsList, extended information needed parsedPostsList.add(new Post(p_thumbnailURL, p_userName, p_subject, p_post, p_postIndex , p_postNum, p_postDate, p_profileURL, p_rank, p_specialRank, p_gender , p_numberOfPosts, p_personalText, p_numberOfStars, p_userColor - , p_attachedFiles, p_postLastEditDate, p_postURL)); + , p_attachedFiles, p_postLastEditDate, p_postURL, p_deletePostURL)); } else { //Deleted user //Add new post in postsList, only standard information needed - parsedPostsList.add(new Post(p_thumbnailURL, p_userName, p_subject, p_post, p_postIndex - , p_postNum, p_postDate, p_userColor, p_attachedFiles, p_postLastEditDate, p_postURL)); + parsedPostsList.add(new Post(p_thumbnailURL, p_userName, p_subject, p_post + , p_postIndex , p_postNum, p_postDate, p_userColor, p_attachedFiles + , p_postLastEditDate, p_postURL, p_deletePostURL)); } } return parsedPostsList; diff --git a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java index 64096b56..bbbc41af 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java @@ -42,8 +42,8 @@ import java.util.ArrayList; import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.activities.AboutActivity; -import gr.thmmy.mthmmy.activities.bookmarks.BookmarkActivity; import gr.thmmy.mthmmy.activities.LoginActivity; +import gr.thmmy.mthmmy.activities.bookmarks.BookmarkActivity; import gr.thmmy.mthmmy.activities.downloads.DownloadsActivity; import gr.thmmy.mthmmy.activities.main.MainActivity; import gr.thmmy.mthmmy.activities.profile.ProfileActivity; @@ -370,7 +370,7 @@ public abstract class BaseActivity extends AppCompatActivity { if (sessionManager.isLoggedIn()) drawerBuilder.addDrawerItems(homeItem, bookmarksItem, downloadsItem, settingsItem, loginLogoutItem, aboutItem); else - drawerBuilder.addDrawerItems(homeItem, bookmarksItem, loginLogoutItem, aboutItem); + drawerBuilder.addDrawerItems(homeItem, bookmarksItem, settingsItem, loginLogoutItem, aboutItem); drawer = drawerBuilder.build(); @@ -391,7 +391,6 @@ public abstract class BaseActivity extends AppCompatActivity { if (!sessionManager.isLoggedIn()) //When logged out or if user is guest { drawer.removeItem(DOWNLOADS_ID); - drawer.removeItem(SETTINGS_ID); loginLogoutItem.withName(R.string.login).withIcon(loginIcon); //Swap logout with login profileDrawerItem.withName(sessionManager.getUsername()); setDefaultAvatar(); @@ -399,9 +398,6 @@ public abstract class BaseActivity extends AppCompatActivity { if (!drawer.getDrawerItems().contains(downloadsItem)){ drawer.addItemAtPosition(settingsItem, 2); } - if (!drawer.getDrawerItems().contains(settingsItem)){ - drawer.addItemAtPosition(settingsItem, 3); - } loginLogoutItem.withName(R.string.logout).withIcon(logoutIcon); //Swap login with logout profileDrawerItem.withName(sessionManager.getUsername()); if (sessionManager.hasAvatar()) diff --git a/app/src/main/java/gr/thmmy/mthmmy/model/Post.java b/app/src/main/java/gr/thmmy/mthmmy/model/Post.java index 76a335fd..3206061d 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/model/Post.java +++ b/app/src/main/java/gr/thmmy/mthmmy/model/Post.java @@ -29,6 +29,7 @@ public class Post { private final ArrayList attachedFiles; private final String lastEdit; private final String postURL; + private final String postDeleteURL; //Extra info private final String profileURL; @@ -61,6 +62,7 @@ public class Post { attachedFiles = null; lastEdit = null; postURL = null; + postDeleteURL = null; } /** @@ -91,7 +93,8 @@ public class Post { , int postIndex, int postNumber, String postDate, String profileURl, @Nullable String rank , @Nullable String special_rank, @Nullable String gender, @Nullable String numberOfPosts , @Nullable String personalText, int numberOfStars, int userColor - , @Nullable ArrayList attachedFiles, @Nullable String lastEdit, String postURL) { + , @Nullable ArrayList attachedFiles, @Nullable String lastEdit, String postURL + , @Nullable String postDeleteURL) { if (Objects.equals(thumbnailUrl, "")) this.thumbnailUrl = null; else this.thumbnailUrl = thumbnailUrl; this.author = author; @@ -112,6 +115,7 @@ public class Post { this.personalText = personalText; this.numberOfStars = numberOfStars; this.postURL = postURL; + this.postDeleteURL = postDeleteURL; } /** @@ -133,7 +137,8 @@ public class Post { */ public Post(@Nullable String thumbnailUrl, String author, String subject, String content , int postIndex, int postNumber, String postDate, int userColor - , @Nullable ArrayList attachedFiles, @Nullable String lastEdit, String postURL) { + , @Nullable ArrayList attachedFiles, @Nullable String lastEdit, String postURL + , @Nullable String postDeleteURL) { if (Objects.equals(thumbnailUrl, "")) this.thumbnailUrl = null; else this.thumbnailUrl = thumbnailUrl; this.author = author; @@ -154,6 +159,7 @@ public class Post { personalText = ""; numberOfStars = 0; this.postURL = postURL; + this.postDeleteURL = postDeleteURL; } //Getters @@ -342,4 +348,14 @@ public class Post { public String getPostURL() { return postURL; } + + /** + * Gets this post's delete url. + * + * @return post's delete url + */ + @Nullable + public String getPostDeleteURL() { + return postDeleteURL; + } } diff --git a/app/src/main/res/drawable-hdpi/ic_more_vert.png b/app/src/main/res/drawable-hdpi/ic_more_vert.png new file mode 100644 index 0000000000000000000000000000000000000000..f3b7a4c4d2d500421702bd19055ca23957390472 GIT binary patch literal 164 zcmeAS@N?(olHy`uVBq!ia0vp^Dj>|k1|%Oc%$NbBvOQfKLn>~)z2ePvK!L~gV$Z76 z`XP6g&F5VrIA=@BJh62jp0QulUZoX3i-C#d#m=17+cKrMC3#O=&{{cPTgXH#&N Z6Qjo5`&zE=y+Kwnc)I$ztaD0e0sw2>DmMTC literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-xhdpi/ic_more_vert.png b/app/src/main/res/drawable-xhdpi/ic_more_vert.png new file mode 100644 index 0000000000000000000000000000000000000000..197203bf0dda15eed7888a38cbd3101adf83a25a GIT binary patch literal 203 zcmeAS@N?(olHy`uVBq!ia0vp^1|ZDA1|-9oezpUtNuDl_Ar-gYUUTGXP!MoS^cTML zRP|#_;ELCUOyREYTH5A)c>i1MS+UY1o%4)9^$ZRAOkuH?LSrww&fU(m!|vad=ay*- zX$+J0%s0Ba8|W3_t1;-=|;K4XazcFaE|I@w6Hnen|#~EdshO+!vjI o)DdT(GhiGS?vE2d#via}~)z2V5!ewE=^?iILXRDWfHGUeqh{;siV!p@n4u{%bfA1(U*_JWz3N}uYbudzJ1F&ME$~+Aa29aoZ=Od8e(H|9w9B@g^>yML-)h zySf%FTA`uU)iiSvheu$Dh^VV;U|>lkE65=Z4I&^OM9_eF2~ZhW^{zEcP=|sQ^=&l| YzJKFD?}EyuK)*A1y85}Sb4q9e0DV2t&2aSW-r_4bzIxq}W2Z4cjF zNnBDGwj|b~yTf^c73b`}$TLAJ{8~NddER6GGtb`i$0`A*rT3q@GB7wWFmM13U{GLS zU}0!rU=V0nzBW1bjnwYtzapfw8EV$2=E%p*SA2Md;aUAh|HMC*)0zUf4%pSyMKOG~ z{k(k6!j|tXLJYeM{=Q*&Qht_U2g8Z?^B4=585UGD*fBrgQP^+BoWQKGUoBR1d5m*7 zLtVM(-Cqi3@uk1265m)J5o37f{^FiZU)z`Y`!$}v*ZTPSg<9>Qy-Tv6>E10fQ?>d0 zSAiMs!rclCWsYTz3~!p=G%?&@y~)aG;LXTzK<|VeM?>s{SOJFJ3ScEna8D(Or(nWq pNz<4>nlPaUdRQ?)Ak@o-q literal 0 HcmV?d00001 diff --git a/app/src/main/res/layout-v21/activity_topic_post_row.xml b/app/src/main/res/layout-v21/activity_topic_post_row.xml index 6f00298e..bbce806e 100644 --- a/app/src/main/res/layout-v21/activity_topic_post_row.xml +++ b/app/src/main/res/layout-v21/activity_topic_post_row.xml @@ -95,7 +95,7 @@ android:contentDescription="@string/post_quote_button" android:focusable="true" android:src="@drawable/ic_format_quote_unchecked" /> - + android:src="@drawable/ic_share" />--> + - + android:src="@drawable/ic_share" />--> + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values-v21/styles.xml b/app/src/main/res/values-v21/styles.xml index 628b2a36..6863be1d 100644 --- a/app/src/main/res/values-v21/styles.xml +++ b/app/src/main/res/values-v21/styles.xml @@ -17,6 +17,7 @@ @style/AppCompatAlertDialogStyle true + @style/PopupMenuStyle + +