55import android .view .LayoutInflater ;
66import android .view .View ;
77import android .view .ViewGroup ;
8+ import android .widget .ImageView ;
89import android .widget .TextView ;
910
1011import androidx .annotation .NonNull ;
1516import androidx .recyclerview .widget .RecyclerView ;
1617
1718import com .certified .notes .R ;
18- import com .certified .notes .model .BookMark ;
1919import com .certified .notes .model .Note ;
2020import com .certified .notes .room .NotesViewModel ;
2121import com .certified .notes .util .PreferenceKeys ;
2222import com .like .LikeButton ;
23- import com .like .OnLikeListener ;
2423
2524import java .util .HashSet ;
2625import java .util .Set ;
@@ -41,23 +40,20 @@ public boolean areContentsTheSame(@NonNull Note oldItem, @NonNull Note newItem)
4140 }
4241 };
4342
44- private final Context mContext ;
4543 private final LifecycleOwner mOwner ;
4644 private final NotesViewModel mViewModel ;
4745 private final SharedPreferences mPreferences ;
4846 private final Set <String > mNoteIds ;
49- private final Set <String > mDefValues ;
5047 private OnNoteClickedListener listener ;
5148
5249 public NoteRecyclerAdapter (Context context , LifecycleOwner owner , NotesViewModel viewModel ) {
5350 super (DIFF_CALLBACK );
54- this .mContext = context ;
5551 this .mOwner = owner ;
5652 this .mViewModel = viewModel ;
5753
58- mPreferences = PreferenceManager .getDefaultSharedPreferences (mContext .getApplicationContext ());
54+ mPreferences = PreferenceManager .getDefaultSharedPreferences (context .getApplicationContext ());
5955
60- mDefValues = new HashSet <>();
56+ Set < String > mDefValues = new HashSet <>();
6157 mDefValues .add ("-1" );
6258
6359 mNoteIds = new HashSet <>(mPreferences .getStringSet (PreferenceKeys .NOTE_IDS , mDefValues ));
@@ -72,42 +68,42 @@ public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
7268
7369 @ Override
7470 public void onBindViewHolder (ViewHolder holder , int position ) {
75- SharedPreferences .Editor editor = mPreferences .edit ();
71+ // SharedPreferences.Editor editor = mPreferences.edit();
7672
7773 Note currentNote = getItem (position );
7874 holder .mNoteContent .setText (currentNote .getContent ());
7975 holder .mNoteTitle .setText (currentNote .getTitle ());
80- holder .mLikeButton .setOnLikeListener (new OnLikeListener () {
81- @ Override
82- public void liked (LikeButton likeButton ) {
83- int noteId = currentNote .getId ();
84- String courseCode = currentNote .getCourseCode ();
85- String noteTitle = currentNote .getTitle ();
86- String noteContent = currentNote .getContent ();
87-
88- BookMark bookMark = new BookMark (noteId , courseCode , noteTitle , noteContent );
89- mViewModel .insertBookMark (bookMark );
90-
91- mNoteIds .add (String .valueOf (noteId ));
92- editor .putStringSet (PreferenceKeys .NOTE_IDS , mNoteIds );
93- editor .apply ();
94- }
95-
96- @ Override
97- public void unLiked (LikeButton likeButton ) {
98- mViewModel .getBookMarkAt (currentNote .getId ()).observe (mOwner , bookMarks -> {
99- for (BookMark bookMark : bookMarks ) {
100- mViewModel .deleteBookMark (bookMark );
101- }
102-
103- mNoteIds .remove (String .valueOf (currentNote .getId ()));
104- editor .putStringSet (PreferenceKeys .NOTE_IDS , mNoteIds );
105- editor .apply ();
106- });
107- }
108- });
109-
110- holder .checkIfBookMarked (currentNote .getId (), holder .mLikeButton );
76+ // holder.mLikeButton.setOnLikeListener(new OnLikeListener() {
77+ // @Override
78+ // public void liked(LikeButton likeButton) {
79+ // int noteId = currentNote.getId();
80+ // String courseCode = currentNote.getCourseCode();
81+ // String noteTitle = currentNote.getTitle();
82+ // String noteContent = currentNote.getContent();
83+ //
84+ // BookMark bookMark = new BookMark(noteId, courseCode, noteTitle, noteContent);
85+ // mViewModel.insertBookMark(bookMark);
86+ //
87+ // mNoteIds.add(String.valueOf(noteId));
88+ // editor.putStringSet(PreferenceKeys.NOTE_IDS, mNoteIds);
89+ // editor.apply();
90+ // }
91+ //
92+ // @Override
93+ // public void unLiked(LikeButton likeButton) {
94+ // mViewModel.getBookMarkAt(currentNote.getId()).observe(mOwner, bookMarks -> {
95+ // for (BookMark bookMark : bookMarks) {
96+ // mViewModel.deleteBookMark(bookMark);
97+ // }
98+ //
99+ // mNoteIds.remove(String.valueOf(currentNote.getId()));
100+ // editor.putStringSet(PreferenceKeys.NOTE_IDS, mNoteIds);
101+ // editor.apply();
102+ // });
103+ // }
104+ // });
105+
106+ holder .checkIfBookMarked (currentNote .getId (), holder .ivBookmark );
111107 }
112108
113109 public Note getNoteAt (int position ) {
@@ -127,12 +123,14 @@ public class ViewHolder extends RecyclerView.ViewHolder {
127123 public final TextView mNoteContent ;
128124 public final TextView mNoteTitle ;
129125 public final LikeButton mLikeButton ;
126+ public final ImageView ivBookmark ;
130127
131128 public ViewHolder (View itemView ) {
132129 super (itemView );
133130 mNoteContent = itemView .findViewById (R .id .tv_note_content );
134131 mNoteTitle = itemView .findViewById (R .id .tv_note_title );
135132 mLikeButton = itemView .findViewById (R .id .likeButton );
133+ ivBookmark = itemView .findViewById (R .id .iv_bookmark );
136134
137135 itemView .setOnClickListener (v -> {
138136 int position = getAdapterPosition ();
@@ -142,13 +140,14 @@ public ViewHolder(View itemView) {
142140 });
143141 }
144142
145- private void checkIfBookMarked (int noteId , LikeButton likeButton ) {
143+ private void checkIfBookMarked (int noteId , ImageView imageView ) {
146144 Set <String > defValues = new HashSet <>();
147145 defValues .add ("-1" );
148146 Set <String > noteIds = new HashSet <>(mPreferences .getStringSet (PreferenceKeys .NOTE_IDS , defValues ));
149- if (noteIds .contains (String .valueOf (noteId ))) {
150- likeButton .setLiked (true );
151- }
147+ if (noteIds .contains (String .valueOf (noteId )))
148+ imageView .setVisibility (View .VISIBLE );
149+ else
150+ imageView .setVisibility (View .INVISIBLE );
152151 }
153152 }
154153}
0 commit comments