Skip to content

Commit d9c447b

Browse files
committed
fixed some minor bugs
changed the like button to an imageview added a decoration to the swipe to delete function in the BookMarksFragment there's a bug with bookmarking a note
1 parent cdaa084 commit d9c447b

16 files changed

Lines changed: 260 additions & 181 deletions

app/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ dependencies {
8686
implementation 'com.github.bumptech.glide:glide:4.11.0'
8787
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
8888

89+
// recyclerViewSwipeDecorator
90+
implementation 'it.xabaras.android:recyclerview-swipedecorator:1.2.3'
91+
8992
implementation "androidx.core:core-ktx:1.3.2"
9093
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0"
9194
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.4.21"

app/src/main/java/com/certified/notes/adapters/BookMarkRecyclerAdapter.java

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
11
package com.certified.notes.adapters;
22

33
import android.content.Context;
4-
import android.content.SharedPreferences;
54
import android.view.LayoutInflater;
65
import android.view.View;
76
import android.view.ViewGroup;
7+
import android.widget.ImageView;
88
import android.widget.TextView;
99

1010
import androidx.annotation.NonNull;
11-
import androidx.preference.PreferenceManager;
1211
import androidx.recyclerview.widget.DiffUtil;
1312
import androidx.recyclerview.widget.ListAdapter;
1413
import androidx.recyclerview.widget.RecyclerView;
1514

1615
import com.certified.notes.R;
1716
import com.certified.notes.model.BookMark;
1817
import com.certified.notes.room.NotesViewModel;
19-
import com.certified.notes.util.PreferenceKeys;
2018
import com.like.LikeButton;
21-
import com.like.OnLikeListener;
22-
23-
import java.util.HashSet;
24-
import java.util.Set;
2519

2620
public class BookMarkRecyclerAdapter extends ListAdapter<BookMark, BookMarkRecyclerAdapter.ViewHolder> {
2721

@@ -62,35 +56,36 @@ public void onBindViewHolder(ViewHolder holder, int position) {
6256
BookMark currentBookMark = getItem(position);
6357
holder.mNoteContent.setText(currentBookMark.getNoteContent());
6458
holder.mNoteTitle.setText(currentBookMark.getNoteTitle());
65-
holder.mLikeButton.setLiked(true);
66-
holder.mLikeButton.setOnLikeListener(new OnLikeListener() {
67-
@Override
68-
public void liked(LikeButton likeButton) {
69-
70-
}
71-
72-
@Override
73-
public void unLiked(LikeButton likeButton) {
74-
int noteId = currentBookMark.getNoteId();
75-
String courseCode = currentBookMark.getCourseCode();
76-
String noteTitle = currentBookMark.getNoteTitle();
77-
String noteContent = currentBookMark.getNoteContent();
78-
BookMark bookMark = new BookMark(noteId, courseCode, noteTitle, noteContent);
79-
bookMark.setId(currentBookMark.getId());
80-
mViewModel.deleteBookMark(bookMark);
81-
82-
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext.getApplicationContext());
83-
SharedPreferences.Editor editor = preferences.edit();
84-
85-
Set<String> defValues = new HashSet<>();
86-
defValues.add("-1");
87-
Set<String> noteIds = new HashSet<>(preferences.getStringSet(PreferenceKeys.NOTE_IDS, defValues));
88-
noteIds.remove(String.valueOf(noteId));
89-
90-
editor.putStringSet(PreferenceKeys.NOTE_IDS, noteIds);
91-
editor.apply();
92-
}
93-
});
59+
holder.ivBookmark.setVisibility(View.VISIBLE);
60+
// holder.mLikeButton.setLiked(true);
61+
// holder.mLikeButton.setOnLikeListener(new OnLikeListener() {
62+
// @Override
63+
// public void liked(LikeButton likeButton) {
64+
//
65+
// }
66+
//
67+
// @Override
68+
// public void unLiked(LikeButton likeButton) {
69+
// int noteId = currentBookMark.getNoteId();
70+
// String courseCode = currentBookMark.getCourseCode();
71+
// String noteTitle = currentBookMark.getNoteTitle();
72+
// String noteContent = currentBookMark.getNoteContent();
73+
// BookMark bookMark = new BookMark(noteId, courseCode, noteTitle, noteContent);
74+
// bookMark.setId(currentBookMark.getId());
75+
// mViewModel.deleteBookMark(bookMark);
76+
//
77+
// SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext.getApplicationContext());
78+
// SharedPreferences.Editor editor = preferences.edit();
79+
//
80+
// Set<String> defValues = new HashSet<>();
81+
// defValues.add("-1");
82+
// Set<String> noteIds = new HashSet<>(preferences.getStringSet(PreferenceKeys.NOTE_IDS, defValues));
83+
// noteIds.remove(String.valueOf(noteId));
84+
//
85+
// editor.putStringSet(PreferenceKeys.NOTE_IDS, noteIds);
86+
// editor.apply();
87+
// }
88+
// });
9489
}
9590

9691
public BookMark getBookMarkAt(int position) {
@@ -110,12 +105,14 @@ public class ViewHolder extends RecyclerView.ViewHolder {
110105
public final TextView mNoteContent;
111106
public final TextView mNoteTitle;
112107
public final LikeButton mLikeButton;
108+
public final ImageView ivBookmark;
113109

114110
public ViewHolder(View itemView) {
115111
super(itemView);
116112
mNoteContent = itemView.findViewById(R.id.tv_note_content);
117113
mNoteTitle = itemView.findViewById(R.id.tv_note_title);
118114
mLikeButton = itemView.findViewById(R.id.likeButton);
115+
ivBookmark = itemView.findViewById(R.id.iv_bookmark);
119116

120117
itemView.setOnClickListener(v -> {
121118
int position = getAdapterPosition();

app/src/main/java/com/certified/notes/adapters/NoteRecyclerAdapter.java

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.view.LayoutInflater;
66
import android.view.View;
77
import android.view.ViewGroup;
8+
import android.widget.ImageView;
89
import android.widget.TextView;
910

1011
import androidx.annotation.NonNull;
@@ -15,12 +16,10 @@
1516
import androidx.recyclerview.widget.RecyclerView;
1617

1718
import com.certified.notes.R;
18-
import com.certified.notes.model.BookMark;
1919
import com.certified.notes.model.Note;
2020
import com.certified.notes.room.NotesViewModel;
2121
import com.certified.notes.util.PreferenceKeys;
2222
import com.like.LikeButton;
23-
import com.like.OnLikeListener;
2423

2524
import java.util.HashSet;
2625
import 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
}

app/src/main/java/com/certified/notes/adapters/NoteRecyclerAdapterKt.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ class NoteRecyclerAdapterKt(private val mContext: Context, private val mOwner: L
5858
}
5959

6060
override fun unLiked(likeButton: LikeButton) {
61-
mViewModel.getBookMarkAt(currentNote.id).observe(mOwner, Observer { bookMarks: List<BookMark?> ->
62-
for (bookMark in bookMarks) {
61+
mViewModel.getBookMarkAt(currentNote.id).observe(mOwner, Observer { bookMark :BookMark? ->
62+
if (bookMark != null) {
6363
mViewModel.deleteBookMark(bookMark)
6464
}
6565
mNoteIds.remove((currentNote.id).toString())

app/src/main/java/com/certified/notes/room/NotesDao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public interface NotesDao {
9595
void deleteCompletedTodos();
9696

9797
@Query("SELECT * FROM bookmark_table WHERE note_id = :noteId")
98-
LiveData<List<BookMark>> getBookMarkAt(int noteId);
98+
LiveData<BookMark> getBookMarkAt(int noteId);
9999

100100
@Query("SELECT * FROM note_table WHERE course_code = :course_code")
101101
LiveData<List<Note>> getNotesAt(String course_code);

app/src/main/java/com/certified/notes/room/NotesViewModel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public void deleteBookMarkedNote(int noteId) {
169169
mRepository.deleteBookMarkedNote(noteId);
170170
}
171171

172-
public LiveData<List<BookMark>> getBookMarkAt(int noteId) {
172+
public LiveData<BookMark> getBookMarkAt(int noteId) {
173173
return mRepository.getBookMarkAt(noteId);
174174
}
175175

0 commit comments

Comments
 (0)