mirror of https://github.com/ThmmyNoLife/mTHMMY
Apostolos Fanakis
7 years ago
10 changed files with 481 additions and 216 deletions
@ -1,198 +0,0 @@ |
|||
package gr.thmmy.mthmmy.activities; |
|||
|
|||
import android.content.Intent; |
|||
import android.graphics.Typeface; |
|||
import android.graphics.drawable.Drawable; |
|||
import android.os.Build; |
|||
import android.os.Bundle; |
|||
import android.support.graphics.drawable.VectorDrawableCompat; |
|||
import android.view.LayoutInflater; |
|||
import android.view.View; |
|||
import android.widget.ImageButton; |
|||
import android.widget.LinearLayout; |
|||
import android.widget.TextView; |
|||
import android.widget.Toast; |
|||
|
|||
import gr.thmmy.mthmmy.R; |
|||
import gr.thmmy.mthmmy.activities.board.BoardActivity; |
|||
import gr.thmmy.mthmmy.activities.topic.TopicActivity; |
|||
import gr.thmmy.mthmmy.base.BaseActivity; |
|||
import gr.thmmy.mthmmy.model.Bookmark; |
|||
|
|||
import static gr.thmmy.mthmmy.activities.board.BoardActivity.BUNDLE_BOARD_TITLE; |
|||
import static gr.thmmy.mthmmy.activities.board.BoardActivity.BUNDLE_BOARD_URL; |
|||
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_TITLE; |
|||
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_URL; |
|||
|
|||
//TODO proper handling with adapter etc.
|
|||
//TODO better UI
|
|||
//TODO after clicking bookmark and then back button should return to this activity
|
|||
public class BookmarkActivity extends BaseActivity { |
|||
private TextView boardsTitle; |
|||
private TextView topicsTitle; |
|||
|
|||
private static Drawable notificationsEnabledButtonImage; |
|||
private static Drawable notificationsDisabledButtonImage; |
|||
|
|||
@Override |
|||
protected void onCreate(Bundle savedInstanceState) { |
|||
super.onCreate(savedInstanceState); |
|||
setContentView(R.layout.activity_bookmark); |
|||
|
|||
//Initialize toolbar
|
|||
toolbar = findViewById(R.id.toolbar); |
|||
toolbar.setTitle("Bookmarks"); |
|||
setSupportActionBar(toolbar); |
|||
if (getSupportActionBar() != null) { |
|||
getSupportActionBar().setDisplayHomeAsUpEnabled(true); |
|||
getSupportActionBar().setDisplayShowHomeEnabled(true); |
|||
} |
|||
|
|||
createDrawer(); |
|||
drawer.setSelection(BOOKMARKS_ID); |
|||
|
|||
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); |
|||
} |
|||
|
|||
LinearLayout bookmarksLinearView = findViewById(R.id.bookmarks_container); |
|||
LayoutInflater layoutInflater = getLayoutInflater(); |
|||
|
|||
if(!getBoardsBookmarked().isEmpty()) { |
|||
boardsTitle = new TextView(this); |
|||
boardsTitle.setLayoutParams(new LinearLayout.LayoutParams( |
|||
LinearLayout.LayoutParams.MATCH_PARENT |
|||
, LinearLayout.LayoutParams.WRAP_CONTENT)); |
|||
boardsTitle.setText(getString(R.string.board_bookmarks_title)); |
|||
boardsTitle.setTypeface(boardsTitle.getTypeface(), Typeface.BOLD); |
|||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
|||
boardsTitle.setTextColor(getColor(R.color.primary_text)); |
|||
} else { |
|||
//noinspection deprecation
|
|||
boardsTitle.setTextColor(getResources().getColor(R.color.primary_text)); |
|||
} |
|||
boardsTitle.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); |
|||
boardsTitle.setTextSize(20f); |
|||
bookmarksLinearView.addView(boardsTitle); |
|||
|
|||
for (final Bookmark bookmarkedBoard : getBoardsBookmarked()) { |
|||
if (bookmarkedBoard != null && bookmarkedBoard.getTitle() != null) { |
|||
final LinearLayout row = (LinearLayout) layoutInflater.inflate( |
|||
R.layout.activity_bookmark_board_row, bookmarksLinearView, false); |
|||
row.setOnClickListener(new View.OnClickListener() { |
|||
@Override |
|||
public void onClick(View view) { |
|||
Intent intent = new Intent(BookmarkActivity.this, BoardActivity.class); |
|||
Bundle extras = new Bundle(); |
|||
extras.putString(BUNDLE_BOARD_URL, "https://www.thmmy.gr/smf/index.php?board=" |
|||
+ bookmarkedBoard.getId() + ".0"); |
|||
extras.putString(BUNDLE_BOARD_TITLE, bookmarkedBoard.getTitle()); |
|||
intent.putExtras(extras); |
|||
startActivity(intent); |
|||
finish(); |
|||
} |
|||
}); |
|||
((TextView) row.findViewById(R.id.bookmark_title)).setText(bookmarkedBoard.getTitle()); |
|||
(row.findViewById(R.id.remove_bookmark)).setOnClickListener(new View.OnClickListener() { |
|||
@Override |
|||
public void onClick(View view) { |
|||
removeBookmark(bookmarkedBoard); |
|||
row.setVisibility(View.GONE); |
|||
updateTitles(); |
|||
} |
|||
}); |
|||
bookmarksLinearView.addView(row); |
|||
} |
|||
} |
|||
} |
|||
|
|||
|
|||
if(!getTopicsBookmarked().isEmpty()) { |
|||
topicsTitle = new TextView(this); |
|||
topicsTitle.setLayoutParams(new LinearLayout.LayoutParams( |
|||
LinearLayout.LayoutParams.MATCH_PARENT |
|||
, LinearLayout.LayoutParams.WRAP_CONTENT)); |
|||
topicsTitle.setText(getString(R.string.topic_bookmarks_title)); |
|||
topicsTitle.setTypeface(topicsTitle.getTypeface(), Typeface.BOLD); |
|||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
|||
topicsTitle.setTextColor(getColor(R.color.primary_text)); |
|||
} else { |
|||
//noinspection deprecation
|
|||
topicsTitle.setTextColor(getResources().getColor(R.color.primary_text)); |
|||
} |
|||
topicsTitle.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); |
|||
topicsTitle.setTextSize(20f); |
|||
bookmarksLinearView.addView(topicsTitle); |
|||
|
|||
for (final Bookmark bookmarkedTopic : getTopicsBookmarked()) { |
|||
if (bookmarkedTopic != null && bookmarkedTopic.getTitle() != null) { |
|||
final LinearLayout row = (LinearLayout) layoutInflater.inflate( |
|||
R.layout.activity_bookmark_topic_row, bookmarksLinearView, false); |
|||
row.setOnClickListener(new View.OnClickListener() { |
|||
@Override |
|||
public void onClick(View view) { |
|||
Intent intent = new Intent(BookmarkActivity.this, TopicActivity.class); |
|||
Bundle extras = new Bundle(); |
|||
extras.putString(BUNDLE_TOPIC_URL, "https://www.thmmy.gr/smf/index.php?topic=" |
|||
+ bookmarkedTopic.getId() + "." + 2147483647); |
|||
extras.putString(BUNDLE_TOPIC_TITLE, bookmarkedTopic.getTitle()); |
|||
intent.putExtras(extras); |
|||
startActivity(intent); |
|||
finish(); |
|||
} |
|||
}); |
|||
((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(new View.OnClickListener() { |
|||
|
|||
@Override |
|||
public void onClick(View view) { |
|||
if(toggleNotification(bookmarkedTopic)){ |
|||
notificationsEnabledButton.setImageDrawable(notificationsEnabledButtonImage); |
|||
} else { |
|||
notificationsEnabledButton.setImageDrawable(notificationsDisabledButtonImage); |
|||
} |
|||
} |
|||
}); |
|||
(row.findViewById(R.id.remove_bookmark)).setOnClickListener(new View.OnClickListener() { |
|||
@Override |
|||
public void onClick(View view) { |
|||
removeBookmark(bookmarkedTopic); |
|||
row.setVisibility(View.GONE); |
|||
Toast.makeText(BookmarkActivity.this, "Bookmark removed", Toast.LENGTH_SHORT).show(); |
|||
updateTitles(); |
|||
} |
|||
}); |
|||
bookmarksLinearView.addView(row); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
protected void onResume() { |
|||
drawer.setSelection(BOOKMARKS_ID); |
|||
super.onResume(); |
|||
} |
|||
|
|||
private void updateTitles() |
|||
{ |
|||
if(getBoardsBookmarked().isEmpty()&&boardsTitle!=null) |
|||
boardsTitle.setVisibility(View.GONE); |
|||
if(getTopicsBookmarked().isEmpty()&&topicsTitle!=null) |
|||
topicsTitle.setVisibility(View.GONE); |
|||
} |
|||
} |
@ -0,0 +1,130 @@ |
|||
package gr.thmmy.mthmmy.activities.bookmarks; |
|||
|
|||
import android.app.Activity; |
|||
import android.graphics.Typeface; |
|||
import android.os.Build; |
|||
import android.os.Bundle; |
|||
import android.support.annotation.NonNull; |
|||
import android.support.v4.app.Fragment; |
|||
import android.view.LayoutInflater; |
|||
import android.view.View; |
|||
import android.view.ViewGroup; |
|||
import android.widget.LinearLayout; |
|||
import android.widget.TextView; |
|||
|
|||
import java.util.ArrayList; |
|||
|
|||
import gr.thmmy.mthmmy.R; |
|||
import gr.thmmy.mthmmy.model.Bookmark; |
|||
|
|||
/** |
|||
* A {@link Fragment} subclass. |
|||
* Use the {@link BoardBookmarksFragment#newInstance} factory method to |
|||
* create an instance of this fragment. |
|||
*/ |
|||
public class BoardBookmarksFragment extends Fragment { |
|||
protected static final String ARG_SECTION_NUMBER = "SECTION_NUMBER"; |
|||
protected static final String ARG_BOARD_BOOKMARKS = "BOARD_BOOKMARKS"; |
|||
|
|||
public static final String INTERACTION_CLICK_BOARD_BOOKMARK = "CLICK_BOARD_BOOKMARK"; |
|||
public static final String INTERACTION_REMOVE_BOARD_BOOKMARK= "REMOVE_BOARD_BOOKMARK"; |
|||
|
|||
ArrayList<Bookmark> boardBookmarks = null; |
|||
|
|||
// Required empty public constructor
|
|||
public BoardBookmarksFragment() { |
|||
} |
|||
|
|||
/** |
|||
* 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 BoardBookmarksFragment newInstance(int sectionNumber, String boardBookmarks) { |
|||
BoardBookmarksFragment fragment = new BoardBookmarksFragment(); |
|||
Bundle args = new Bundle(); |
|||
args.putInt(ARG_SECTION_NUMBER, sectionNumber); |
|||
args.putString(ARG_BOARD_BOOKMARKS, boardBookmarks); |
|||
fragment.setArguments(args); |
|||
return fragment; |
|||
} |
|||
|
|||
@Override |
|||
public void onCreate(Bundle savedInstanceState) { |
|||
super.onCreate(savedInstanceState); |
|||
if (getArguments() != null) { |
|||
String bundledBoardBookmarks = getArguments().getString(ARG_BOARD_BOOKMARKS); |
|||
if (bundledBoardBookmarks != null) { |
|||
boardBookmarks = Bookmark.arrayFromString(bundledBoardBookmarks); |
|||
} |
|||
} |
|||
} |
|||
|
|||
@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_board_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) { |
|||
final LinearLayout row = (LinearLayout) layoutInflater.inflate( |
|||
R.layout.activity_bookmark_board_row, bookmarksLinearView, false); |
|||
row.setOnClickListener(new View.OnClickListener() { |
|||
@Override |
|||
public void onClick(View view) { |
|||
Activity activity = getActivity(); |
|||
if (activity instanceof BookmarkActivity){ |
|||
((BookmarkActivity) activity).onBoardInteractionListener(INTERACTION_CLICK_BOARD_BOOKMARK, bookmarkedBoard); |
|||
} |
|||
} |
|||
}); |
|||
((TextView) row.findViewById(R.id.bookmark_title)).setText(bookmarkedBoard.getTitle()); |
|||
(row.findViewById(R.id.remove_bookmark)).setOnClickListener(new View.OnClickListener() { |
|||
@Override |
|||
public void onClick(View view) { |
|||
Activity activity = getActivity(); |
|||
if (activity instanceof BookmarkActivity){ |
|||
((BookmarkActivity) activity).onBoardInteractionListener(INTERACTION_REMOVE_BOARD_BOOKMARK, bookmarkedBoard); |
|||
boardBookmarks.remove(bookmarkedBoard); |
|||
} |
|||
row.setVisibility(View.GONE); |
|||
|
|||
if (boardBookmarks.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_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)); |
|||
} else { |
|||
//noinspection deprecation
|
|||
emptyBookmarksCategory.setTextColor(this.getContext().getResources().getColor(R.color.primary_text)); |
|||
} |
|||
emptyBookmarksCategory.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); |
|||
return emptyBookmarksCategory; |
|||
} |
|||
} |
@ -0,0 +1,143 @@ |
|||
package gr.thmmy.mthmmy.activities.bookmarks; |
|||
|
|||
import android.content.Intent; |
|||
import android.graphics.drawable.Drawable; |
|||
import android.os.Bundle; |
|||
import android.support.design.widget.TabLayout; |
|||
import android.support.v4.app.Fragment; |
|||
import android.support.v4.app.FragmentManager; |
|||
import android.support.v4.app.FragmentPagerAdapter; |
|||
import android.support.v4.view.ViewPager; |
|||
import android.widget.Toast; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
import gr.thmmy.mthmmy.R; |
|||
import gr.thmmy.mthmmy.activities.board.BoardActivity; |
|||
import gr.thmmy.mthmmy.activities.topic.TopicActivity; |
|||
import gr.thmmy.mthmmy.base.BaseActivity; |
|||
import gr.thmmy.mthmmy.model.Bookmark; |
|||
|
|||
import static gr.thmmy.mthmmy.activities.board.BoardActivity.BUNDLE_BOARD_TITLE; |
|||
import static gr.thmmy.mthmmy.activities.board.BoardActivity.BUNDLE_BOARD_URL; |
|||
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_TITLE; |
|||
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 BookmarkActivity extends BaseActivity { |
|||
@Override |
|||
protected void onCreate(Bundle savedInstanceState) { |
|||
super.onCreate(savedInstanceState); |
|||
setContentView(R.layout.activity_bookmark); |
|||
|
|||
//Initialize toolbar
|
|||
toolbar = findViewById(R.id.toolbar); |
|||
toolbar.setTitle("Bookmarks"); |
|||
setSupportActionBar(toolbar); |
|||
if (getSupportActionBar() != null) { |
|||
getSupportActionBar().setDisplayHomeAsUpEnabled(true); |
|||
getSupportActionBar().setDisplayShowHomeEnabled(true); |
|||
} |
|||
|
|||
createDrawer(); |
|||
drawer.setSelection(BOOKMARKS_ID); |
|||
|
|||
//Creates the adapter that will return a fragment for each section of the activity
|
|||
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); |
|||
sectionsPagerAdapter.addFragment(TopicBookmarksFragment.newInstance(1, Bookmark.arrayToString(getTopicsBookmarked())), "Topics"); |
|||
sectionsPagerAdapter.addFragment(BoardBookmarksFragment.newInstance(2, Bookmark.arrayToString(getBoardsBookmarked())), "Boards"); |
|||
|
|||
//Sets up the ViewPager with the sections adapter.
|
|||
ViewPager viewPager = findViewById(R.id.bookmarks_container); |
|||
viewPager.setAdapter(sectionsPagerAdapter); |
|||
|
|||
TabLayout tabLayout = findViewById(R.id.bookmark_tabs); |
|||
tabLayout.setupWithViewPager(viewPager); |
|||
} |
|||
|
|||
@Override |
|||
protected void onResume() { |
|||
drawer.setSelection(BOOKMARKS_ID); |
|||
super.onResume(); |
|||
} |
|||
|
|||
public boolean onTopicInteractionListener(String interactionType, Bookmark bookmarkedTopic){ |
|||
if (interactionType.equals(TopicBookmarksFragment.INTERACTION_CLICK_TOPIC_BOOKMARK)){ |
|||
Intent intent = new Intent(BookmarkActivity.this, TopicActivity.class); |
|||
Bundle extras = new Bundle(); |
|||
extras.putString(BUNDLE_TOPIC_URL, "https://www.thmmy.gr/smf/index.php?topic=" |
|||
+ bookmarkedTopic.getId() + "." + 2147483647); |
|||
extras.putString(BUNDLE_TOPIC_TITLE, bookmarkedTopic.getTitle()); |
|||
intent.putExtras(extras); |
|||
startActivity(intent); |
|||
finish(); |
|||
} else if (interactionType.equals(TopicBookmarksFragment.INTERACTION_TOGGLE_TOPIC_NOTIFICATION)) { |
|||
return toggleNotification(bookmarkedTopic); |
|||
} else if (interactionType.equals(TopicBookmarksFragment.INTERACTION_REMOVE_TOPIC_BOOKMARK)){ |
|||
removeBookmark(bookmarkedTopic); |
|||
Toast.makeText(BookmarkActivity.this, "Bookmark removed", Toast.LENGTH_SHORT).show(); |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
public void onBoardInteractionListener(String interactionType, Bookmark bookmarkedBoard){ |
|||
if (interactionType.equals(BoardBookmarksFragment.INTERACTION_CLICK_BOARD_BOOKMARK)){ |
|||
Intent intent = new Intent(BookmarkActivity.this, BoardActivity.class); |
|||
Bundle extras = new Bundle(); |
|||
extras.putString(BUNDLE_BOARD_URL, "https://www.thmmy.gr/smf/index.php?board=" |
|||
+ bookmarkedBoard.getId() + ".0"); |
|||
extras.putString(BUNDLE_BOARD_TITLE, bookmarkedBoard.getTitle()); |
|||
intent.putExtras(extras); |
|||
startActivity(intent); |
|||
finish(); |
|||
} else if (interactionType.equals(BoardBookmarksFragment.INTERACTION_REMOVE_BOARD_BOOKMARK)){ |
|||
removeBookmark(bookmarkedBoard); |
|||
Toast.makeText(BookmarkActivity.this, "Bookmark removed", Toast.LENGTH_SHORT).show(); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to |
|||
* one of the sections/tabs/pages. If it becomes too memory intensive, |
|||
* it may be best to switch to a |
|||
* {@link android.support.v4.app.FragmentStatePagerAdapter}. |
|||
*/ |
|||
private class SectionsPagerAdapter extends FragmentPagerAdapter { |
|||
private final List<Fragment> fragmentList = new ArrayList<>(); |
|||
private final List<String> fragmentTitleList = new ArrayList<>(); |
|||
|
|||
SectionsPagerAdapter(FragmentManager fm) { |
|||
super(fm); |
|||
} |
|||
|
|||
void addFragment(Fragment fragment, String title) { |
|||
fragmentList.add(fragment); |
|||
fragmentTitleList.add(title); |
|||
notifyDataSetChanged(); |
|||
} |
|||
|
|||
@Override |
|||
public Fragment getItem(int position) { |
|||
return fragmentList.get(position); |
|||
} |
|||
|
|||
@Override |
|||
public int getCount() { |
|||
return fragmentList.size(); |
|||
} |
|||
|
|||
@Override |
|||
public CharSequence getPageTitle(int position) { |
|||
return fragmentTitleList.get(position); |
|||
} |
|||
|
|||
@Override |
|||
public int getItemPosition(Object object) { |
|||
@SuppressWarnings("RedundantCast") |
|||
int position = fragmentList.indexOf((Fragment) object); |
|||
return position == -1 ? POSITION_NONE : position; |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,163 @@ |
|||
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.support.annotation.NonNull; |
|||
import android.support.graphics.drawable.VectorDrawableCompat; |
|||
import android.support.v4.app.Fragment; |
|||
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 java.util.ArrayList; |
|||
|
|||
import gr.thmmy.mthmmy.R; |
|||
import gr.thmmy.mthmmy.model.Bookmark; |
|||
|
|||
public class TopicBookmarksFragment extends Fragment { |
|||
protected static final String ARG_SECTION_NUMBER = "SECTION_NUMBER"; |
|||
protected static final String ARG_TOPIC_BOOKMARKS = "BOARD_BOOKMARKS"; |
|||
|
|||
public static final String INTERACTION_CLICK_TOPIC_BOOKMARK = "CLICK_BOARD_BOOKMARK"; |
|||
public static final String INTERACTION_TOGGLE_TOPIC_NOTIFICATION = "TOGGLE_TOPIC_NOTIFICATION"; |
|||
public static final String INTERACTION_REMOVE_TOPIC_BOOKMARK = "REMOVE_BOARD_BOOKMARK"; |
|||
|
|||
ArrayList<Bookmark> topicBookmarks = null; |
|||
|
|||
private static Drawable notificationsEnabledButtonImage; |
|||
private static Drawable notificationsDisabledButtonImage; |
|||
|
|||
// Required empty public constructor
|
|||
public TopicBookmarksFragment() { |
|||
} |
|||
|
|||
/** |
|||
* 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 TopicBookmarksFragment newInstance(int sectionNumber, String boardBookmarks) { |
|||
TopicBookmarksFragment fragment = new TopicBookmarksFragment(); |
|||
Bundle args = new Bundle(); |
|||
args.putInt(ARG_SECTION_NUMBER, sectionNumber); |
|||
args.putString(ARG_TOPIC_BOOKMARKS, boardBookmarks); |
|||
fragment.setArguments(args); |
|||
return fragment; |
|||
} |
|||
|
|||
@Override |
|||
public void onCreate(Bundle savedInstanceState) { |
|||
super.onCreate(savedInstanceState); |
|||
if (getArguments() != null) { |
|||
String bundledBoardBookmarks = getArguments().getString(ARG_TOPIC_BOOKMARKS); |
|||
if (bundledBoardBookmarks != null) { |
|||
topicBookmarks = Bookmark.arrayFromString(bundledBoardBookmarks); |
|||
} |
|||
} |
|||
|
|||
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_board_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.activity_bookmark_topic_row, bookmarksLinearView, false); |
|||
row.setOnClickListener(new View.OnClickListener() { |
|||
@Override |
|||
public void onClick(View view) { |
|||
Activity activity = getActivity(); |
|||
if (activity instanceof BookmarkActivity) { |
|||
((BookmarkActivity) 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(new View.OnClickListener() { |
|||
@Override |
|||
public void onClick(View view) { |
|||
Activity activity = getActivity(); |
|||
if (activity instanceof BookmarkActivity) { |
|||
if (((BookmarkActivity) activity).onTopicInteractionListener(INTERACTION_TOGGLE_TOPIC_NOTIFICATION, bookmarkedTopic)) { |
|||
notificationsEnabledButton.setImageDrawable(notificationsEnabledButtonImage); |
|||
} else { |
|||
notificationsEnabledButton.setImageDrawable(notificationsDisabledButtonImage); |
|||
} |
|||
} |
|||
} |
|||
}); |
|||
(row.findViewById(R.id.remove_bookmark)).setOnClickListener(new View.OnClickListener() { |
|||
@Override |
|||
public void onClick(View view) { |
|||
Activity activity = getActivity(); |
|||
if (activity instanceof BookmarkActivity) { |
|||
((BookmarkActivity) 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; |
|||
} |
|||
} |
@ -0,0 +1,22 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<RelativeLayout |
|||
xmlns:android="http://schemas.android.com/apk/res/android" |
|||
xmlns:app="http://schemas.android.com/apk/res-auto" |
|||
android:layout_width="match_parent" android:layout_height="match_parent"> |
|||
|
|||
<android.support.v4.widget.NestedScrollView |
|||
android:id="@+id/bookmarks_nested_scroll" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="match_parent" |
|||
android:layout_gravity="top|start" |
|||
android:background="@color/background" |
|||
android:scrollbars="none" |
|||
app:layout_behavior="@string/appbar_scrolling_view_behavior"> |
|||
|
|||
<LinearLayout |
|||
android:id="@+id/bookmarks_container" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="match_parent" |
|||
android:orientation="vertical"/> |
|||
</android.support.v4.widget.NestedScrollView> |
|||
</RelativeLayout> |
Loading…
Reference in new issue