diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksActivity.java index 3a00881c..ef2b79b6 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksActivity.java @@ -29,6 +29,9 @@ import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_URL; //TODO proper handling with adapter etc. //TODO after clicking bookmark and then back button should return to this activity public class BookmarksActivity extends BaseActivity { + private static final String TOPIC_URL = "https://www.thmmy.gr/smf/index.php?topic="; + private static final String BOARD_URL = "https://www.thmmy.gr/smf/index.php?board="; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -48,8 +51,8 @@ public class BookmarksActivity extends BaseActivity { //Creates the adapter that will return a fragment for each section of the activity SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); - sectionsPagerAdapter.addFragment(BookmarksTopicFragment.newInstance(1, Bookmark.arrayListToString(getTopicsBookmarked())), "Topics"); - sectionsPagerAdapter.addFragment(BookmarksBoardFragment.newInstance(2, Bookmark.arrayListToString(getBoardsBookmarked())), "Boards"); + sectionsPagerAdapter.addFragment(BookmarksFragment.newInstance(1, Bookmark.arrayListToString(getTopicsBookmarked()), BookmarksFragment.Type.TOPIC), "Topics"); + sectionsPagerAdapter.addFragment(BookmarksFragment.newInstance(2, Bookmark.arrayListToString(getBoardsBookmarked()), BookmarksFragment.Type.BOARD), "Boards"); //Sets up the ViewPager with the sections adapter. ViewPager viewPager = findViewById(R.id.bookmarks_container); @@ -65,44 +68,57 @@ public class BookmarksActivity extends BaseActivity { super.onResume(); } - public boolean onTopicInteractionListener(String interactionType, Bookmark bookmarkedTopic) { + public boolean onFragmentRowInteractionListener(BookmarksFragment.Type type, String interactionType, Bookmark bookmark) { + if(type== BookmarksFragment.Type.TOPIC) + return onTopicInteractionListener(interactionType, bookmark); + else if (type==BookmarksFragment.Type.BOARD) + return onBoardInteractionListener(interactionType, bookmark); + + return false; + } + + private boolean onTopicInteractionListener(String interactionType, Bookmark bookmarkedTopic) { switch (interactionType) { - case BookmarksTopicFragment.INTERACTION_CLICK_TOPIC_BOOKMARK: + case BookmarksFragment.INTERACTION_CLICK_TOPIC_BOOKMARK: Intent intent = new Intent(BookmarksActivity.this, TopicActivity.class); Bundle extras = new Bundle(); - extras.putString(BUNDLE_TOPIC_URL, "https://www.thmmy.gr/smf/index.php?topic=" + extras.putString(BUNDLE_TOPIC_URL, TOPIC_URL + bookmarkedTopic.getId() + "." + 2147483647); extras.putString(BUNDLE_TOPIC_TITLE, bookmarkedTopic.getTitle()); intent.putExtras(extras); startActivity(intent); break; - case BookmarksTopicFragment.INTERACTION_TOGGLE_TOPIC_NOTIFICATION: + case BookmarksFragment.INTERACTION_TOGGLE_TOPIC_NOTIFICATION: return toggleNotification(bookmarkedTopic); - case BookmarksTopicFragment.INTERACTION_REMOVE_TOPIC_BOOKMARK: + case BookmarksFragment.INTERACTION_REMOVE_TOPIC_BOOKMARK: removeBookmark(bookmarkedTopic); Toast.makeText(BookmarksActivity.this, "Bookmark removed", Toast.LENGTH_SHORT).show(); break; + default: + break; } return true; } - public boolean onBoardInteractionListener(String interactionType, Bookmark bookmarkedBoard) { + private boolean onBoardInteractionListener(String interactionType, Bookmark bookmarkedBoard) { switch (interactionType) { - case BookmarksBoardFragment.INTERACTION_CLICK_BOARD_BOOKMARK: + case BookmarksFragment.INTERACTION_CLICK_BOARD_BOOKMARK: Intent intent = new Intent(BookmarksActivity.this, BoardActivity.class); Bundle extras = new Bundle(); - extras.putString(BUNDLE_BOARD_URL, "https://www.thmmy.gr/smf/index.php?board=" + extras.putString(BUNDLE_BOARD_URL, BOARD_URL + bookmarkedBoard.getId() + ".0"); extras.putString(BUNDLE_BOARD_TITLE, bookmarkedBoard.getTitle()); intent.putExtras(extras); startActivity(intent); break; - case BookmarksBoardFragment.INTERACTION_TOGGLE_BOARD_NOTIFICATION: + case BookmarksFragment.INTERACTION_TOGGLE_BOARD_NOTIFICATION: return toggleNotification(bookmarkedBoard); - case BookmarksBoardFragment.INTERACTION_REMOVE_BOARD_BOOKMARK: + case BookmarksFragment.INTERACTION_REMOVE_BOARD_BOOKMARK: removeBookmark(bookmarkedBoard); Toast.makeText(BookmarksActivity.this, "Bookmark removed", Toast.LENGTH_SHORT).show(); break; + default: + break; } return true; } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksBoardFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksFragment.java similarity index 65% rename from app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksBoardFragment.java rename to app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksFragment.java index 814586ea..67de4c2e 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksBoardFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksFragment.java @@ -21,38 +21,53 @@ import java.util.ArrayList; import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.model.Bookmark; -/** - * A {@link Fragment} subclass. - * Use the {@link BookmarksBoardFragment#newInstance} factory method to - * create an instance of this fragment. - */ -public class BookmarksBoardFragment extends Fragment { +public class BookmarksFragment extends Fragment { + enum Type {TOPIC, BOARD} private static final String ARG_SECTION_NUMBER = "SECTION_NUMBER"; - private static final String ARG_BOARD_BOOKMARKS = "BOARD_BOOKMARKS"; + private static final String ARG_BOOKMARKS = "BOOKMARKS"; + + static final String INTERACTION_CLICK_TOPIC_BOOKMARK = "CLICK_TOPIC_BOOKMARK"; + static final String INTERACTION_TOGGLE_TOPIC_NOTIFICATION = "TOGGLE_TOPIC_NOTIFICATION"; + static final String INTERACTION_REMOVE_TOPIC_BOOKMARK = "REMOVE_TOPIC_BOOKMARK"; static final String INTERACTION_CLICK_BOARD_BOOKMARK = "CLICK_BOARD_BOOKMARK"; static final String INTERACTION_TOGGLE_BOARD_NOTIFICATION = "TOGGLE_BOARD_NOTIFICATION"; static final String INTERACTION_REMOVE_BOARD_BOOKMARK= "REMOVE_BOARD_BOOKMARK"; - private ArrayList boardBookmarks = null; + private ArrayList bookmarks = null; + private Type type; + private String interactionClick, interactionToggle, interactionRemove; + + private Drawable notificationsEnabledButtonImage; + private Drawable notificationsDisabledButtonImage; - private static Drawable notificationsEnabledButtonImage; - private static Drawable notificationsDisabledButtonImage; + public BookmarksFragment() {/* Required empty public constructor */} - // Required empty public constructor - public BookmarksBoardFragment() { } + private BookmarksFragment(Type type) { + this.type=type; + if(type==Type.TOPIC){ + this.interactionClick=INTERACTION_CLICK_TOPIC_BOOKMARK; + this.interactionToggle=INTERACTION_TOGGLE_TOPIC_NOTIFICATION; + this.interactionRemove=INTERACTION_REMOVE_TOPIC_BOOKMARK; + } + else if (type==Type.BOARD){ + this.interactionClick=INTERACTION_CLICK_BOARD_BOOKMARK; + this.interactionToggle=INTERACTION_TOGGLE_BOARD_NOTIFICATION; + this.interactionRemove=INTERACTION_REMOVE_BOARD_BOOKMARK; + } + } /** * Use ONLY this factory method to create a new instance of - * this fragment using the provided parameters. + * the desired fragment using the provided parameters. * * @return A new instance of fragment Forum. */ - public static BookmarksBoardFragment newInstance(int sectionNumber, String boardBookmarks) { - BookmarksBoardFragment fragment = new BookmarksBoardFragment(); + protected static BookmarksFragment newInstance(int sectionNumber, String bookmarks, Type type) { + BookmarksFragment fragment = new BookmarksFragment(type); Bundle args = new Bundle(); args.putInt(ARG_SECTION_NUMBER, sectionNumber); - args.putString(ARG_BOARD_BOOKMARKS, boardBookmarks); + args.putString(ARG_BOOKMARKS, bookmarks); fragment.setArguments(args); return fragment; } @@ -61,9 +76,9 @@ public class BookmarksBoardFragment extends Fragment { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { - String bundledBoardBookmarks = getArguments().getString(ARG_BOARD_BOOKMARKS); - if (bundledBoardBookmarks != null) { - boardBookmarks = Bookmark.stringToArrayList(bundledBoardBookmarks); + String bundledBookmarks = getArguments().getString(ARG_BOOKMARKS); + if (bundledBookmarks != null) { + bookmarks = Bookmark.stringToArrayList(bundledBookmarks); } } @@ -83,47 +98,45 @@ public class BookmarksBoardFragment extends Fragment { Bundle savedInstanceState) { // Inflates the layout for this fragment final View rootView = layoutInflater.inflate(R.layout.fragment_bookmarks, container, false); - //bookmarks_board_container + //bookmarks container final LinearLayout bookmarksLinearView = rootView.findViewById(R.id.bookmarks_container); - if(this.boardBookmarks != null && !this.boardBookmarks.isEmpty()) { - for (final Bookmark bookmarkedBoard : boardBookmarks) { - if (bookmarkedBoard != null && bookmarkedBoard.getTitle() != null) { + if(this.bookmarks != null && !this.bookmarks.isEmpty()) { + for (final Bookmark bookmark : bookmarks) { + if (bookmark != null && bookmark.getTitle() != null) { final LinearLayout row = (LinearLayout) layoutInflater.inflate( R.layout.fragment_bookmarks_row, bookmarksLinearView, false); row.setOnClickListener(view -> { Activity activity = getActivity(); - if (activity instanceof BookmarksActivity){ - ((BookmarksActivity) activity).onBoardInteractionListener(INTERACTION_CLICK_BOARD_BOOKMARK, bookmarkedBoard); - } + if (activity instanceof BookmarksActivity) + ((BookmarksActivity) activity).onFragmentRowInteractionListener(type, interactionClick, bookmark); }); - ((TextView) row.findViewById(R.id.bookmark_title)).setText(bookmarkedBoard.getTitle()); + ((TextView) row.findViewById(R.id.bookmark_title)).setText(bookmark.getTitle()); final ImageButton notificationsEnabledButton = row.findViewById(R.id.toggle_notification); - if (!bookmarkedBoard.isNotificationsEnabled()) { + if (!bookmark.isNotificationsEnabled()) { notificationsEnabledButton.setImageDrawable(notificationsDisabledButtonImage); } notificationsEnabledButton.setOnClickListener(view -> { Activity activity = getActivity(); if (activity instanceof BookmarksActivity) { - if (((BookmarksActivity) activity).onBoardInteractionListener(INTERACTION_TOGGLE_BOARD_NOTIFICATION, bookmarkedBoard)) { + if (((BookmarksActivity) activity).onFragmentRowInteractionListener(type, interactionToggle, bookmark)) notificationsEnabledButton.setImageDrawable(notificationsEnabledButtonImage); - } else { + else notificationsEnabledButton.setImageDrawable(notificationsDisabledButtonImage); - } } }); (row.findViewById(R.id.remove_bookmark)).setOnClickListener(view -> { Activity activity = getActivity(); if (activity instanceof BookmarksActivity){ - ((BookmarksActivity) activity).onBoardInteractionListener(INTERACTION_REMOVE_BOARD_BOOKMARK, bookmarkedBoard); - boardBookmarks.remove(bookmarkedBoard); + ((BookmarksActivity) activity).onFragmentRowInteractionListener(type, interactionRemove, bookmark); + bookmarks.remove(bookmark); } row.setVisibility(View.GONE); - if (boardBookmarks.isEmpty()){ + if (bookmarks.isEmpty()){ bookmarksLinearView.addView(bookmarksListEmptyMessage()); } }); @@ -142,7 +155,11 @@ public class BookmarksBoardFragment extends Fragment { LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); params.setMargins(0, 12, 0, 0); emptyBookmarksCategory.setLayoutParams(params); - emptyBookmarksCategory.setText(getString(R.string.empty_board_bookmarks)); + if(type==Type.TOPIC) + emptyBookmarksCategory.setText(getString(R.string.empty_topic_bookmarks)); + else if(type==Type.BOARD) + emptyBookmarksCategory.setText(getString(R.string.empty_board_bookmarks)); + emptyBookmarksCategory.setTypeface(emptyBookmarksCategory.getTypeface(), Typeface.BOLD); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) emptyBookmarksCategory.setTextColor(this.getContext().getColor(R.color.primary_text)); @@ -153,4 +170,5 @@ public class BookmarksBoardFragment extends Fragment { emptyBookmarksCategory.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); return emptyBookmarksCategory; } + } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksTopicFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksTopicFragment.java deleted file mode 100644 index e9081fbd..00000000 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksTopicFragment.java +++ /dev/null @@ -1,158 +0,0 @@ -package gr.thmmy.mthmmy.activities.bookmarks; - -import android.app.Activity; -import android.graphics.Typeface; -import android.graphics.drawable.Drawable; -import android.os.Build; -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ImageButton; -import android.widget.LinearLayout; -import android.widget.TextView; - -import androidx.annotation.NonNull; -import androidx.fragment.app.Fragment; -import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat; - -import java.util.ArrayList; - -import gr.thmmy.mthmmy.R; -import gr.thmmy.mthmmy.model.Bookmark; - -/** - * A {@link Fragment} subclass. - * Use the {@link BookmarksTopicFragment#newInstance} factory method to - * create an instance of this fragment. - */ -public class BookmarksTopicFragment extends Fragment { - private static final String ARG_SECTION_NUMBER = "SECTION_NUMBER"; - private static final String ARG_TOPIC_BOOKMARKS = "TOPIC_BOOKMARKS"; - - static final String INTERACTION_CLICK_TOPIC_BOOKMARK = "CLICK_TOPIC_BOOKMARK"; - static final String INTERACTION_TOGGLE_TOPIC_NOTIFICATION = "TOGGLE_TOPIC_NOTIFICATION"; - static final String INTERACTION_REMOVE_TOPIC_BOOKMARK = "REMOVE_TOPIC_BOOKMARK"; - - ArrayList topicBookmarks = null; - - private static Drawable notificationsEnabledButtonImage; - private static Drawable notificationsDisabledButtonImage; - - // Required empty public constructor - public BookmarksTopicFragment() { - } - - /** - * Use ONLY this factory method to create a new instance of - * this fragment using the provided parameters. - * - * @return A new instance of fragment Forum. - */ - public static BookmarksTopicFragment newInstance(int sectionNumber, String topicBookmarks) { - BookmarksTopicFragment fragment = new BookmarksTopicFragment(); - Bundle args = new Bundle(); - args.putInt(ARG_SECTION_NUMBER, sectionNumber); - args.putString(ARG_TOPIC_BOOKMARKS, topicBookmarks); - fragment.setArguments(args); - return fragment; - } - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - if (getArguments() != null) { - String bundledTopicBookmarks = getArguments().getString(ARG_TOPIC_BOOKMARKS); - if (bundledTopicBookmarks != null) { - topicBookmarks = Bookmark.stringToArrayList(bundledTopicBookmarks); - } - } - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) - notificationsEnabledButtonImage = getResources().getDrawable(R.drawable.ic_notification_on, null); - else - notificationsEnabledButtonImage = VectorDrawableCompat.create(getResources(), R.drawable.ic_notification_on, null); - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) - notificationsDisabledButtonImage = getResources().getDrawable(R.drawable.ic_notification_off, null); - else - notificationsDisabledButtonImage = VectorDrawableCompat.create(getResources(), R.drawable.ic_notification_off, null); - } - - @Override - public View onCreateView(@NonNull LayoutInflater layoutInflater, ViewGroup container, - Bundle savedInstanceState) { - // Inflates the layout for this fragment - final View rootView = layoutInflater.inflate(R.layout.fragment_bookmarks, container, false); - //bookmarks_topic_container - final LinearLayout bookmarksLinearView = rootView.findViewById(R.id.bookmarks_container); - - if(this.topicBookmarks != null && !this.topicBookmarks.isEmpty()) { - for (final Bookmark bookmarkedTopic : topicBookmarks) { - if (bookmarkedTopic != null && bookmarkedTopic.getTitle() != null) { - final LinearLayout row = (LinearLayout) layoutInflater.inflate( - R.layout.fragment_bookmarks_row, bookmarksLinearView, false); - row.setOnClickListener(view -> { - Activity activity = getActivity(); - if (activity instanceof BookmarksActivity) { - ((BookmarksActivity) activity).onTopicInteractionListener(INTERACTION_CLICK_TOPIC_BOOKMARK, bookmarkedTopic); - } - }); - ((TextView) row.findViewById(R.id.bookmark_title)).setText(bookmarkedTopic.getTitle()); - - final ImageButton notificationsEnabledButton = row.findViewById(R.id.toggle_notification); - if (!bookmarkedTopic.isNotificationsEnabled()) { - notificationsEnabledButton.setImageDrawable(notificationsDisabledButtonImage); - } - - notificationsEnabledButton.setOnClickListener(view -> { - Activity activity = getActivity(); - if (activity instanceof BookmarksActivity) { - if (((BookmarksActivity) activity).onTopicInteractionListener(INTERACTION_TOGGLE_TOPIC_NOTIFICATION, bookmarkedTopic)) { - notificationsEnabledButton.setImageDrawable(notificationsEnabledButtonImage); - } else { - notificationsEnabledButton.setImageDrawable(notificationsDisabledButtonImage); - } - } - }); - (row.findViewById(R.id.remove_bookmark)).setOnClickListener(view -> { - Activity activity = getActivity(); - if (activity instanceof BookmarksActivity) { - ((BookmarksActivity) activity).onTopicInteractionListener(INTERACTION_REMOVE_TOPIC_BOOKMARK, bookmarkedTopic); - topicBookmarks.remove(bookmarkedTopic); - } - row.setVisibility(View.GONE); - - if (topicBookmarks.isEmpty()){ - bookmarksLinearView.addView(bookmarksListEmptyMessage()); - } - }); - bookmarksLinearView.addView(row); - } - } - } else { - bookmarksLinearView.addView(bookmarksListEmptyMessage()); - } - - - return rootView; - } - - private TextView bookmarksListEmptyMessage() { - TextView emptyBookmarksCategory = new TextView(this.getContext()); - LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( - LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); - params.setMargins(0, 12, 0, 0); - emptyBookmarksCategory.setLayoutParams(params); - emptyBookmarksCategory.setText(getString(R.string.empty_topic_bookmarks)); - emptyBookmarksCategory.setTypeface(emptyBookmarksCategory.getTypeface(), Typeface.BOLD); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - emptyBookmarksCategory.setTextColor(this.getContext().getColor(R.color.primary_text)); - } else { - //noinspection deprecation - emptyBookmarksCategory.setTextColor(this.getContext().getResources().getColor(R.color.primary_text)); - } - emptyBookmarksCategory.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); - return emptyBookmarksCategory; - } -}