Skip to content

Commit

Permalink
Android Development | Notes App | Tutorial #11 | Search Notes | Andro…
Browse files Browse the repository at this point in the history
  • Loading branch information
Rick Ren authored and rickbsr committed Jan 15, 2021
1 parent cc7c43c commit 75a4741
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 4 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,7 @@ dependencies {

// https://github.com/vinc3m1/RoundedImageView
implementation 'com.makeramen:roundedimageview:2.3.0'

// https://github.com/square/picasso
implementation 'com.squareup.picasso:picasso:2.71828'
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.rick.notes.R;
import com.rick.notes.database.NotesDatabase;
import com.rick.notes.entities.Note;
import com.squareup.picasso.Picasso;

import java.io.InputStream;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -371,9 +372,14 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
Uri selectedImageUri = data.getData();
if (selectedImageUri != null) {
try {
InputStream inputStream = getContentResolver().openInputStream(selectedImageUri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
imageNote.setImageBitmap(bitmap);
// InputStream inputStream = getContentResolver().openInputStream(selectedImageUri);
// Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
// imageNote.setImageBitmap(bitmap);

Picasso.get()
.load(selectedImageUri)
.into(imageNote);

imageNote.setVisibility(View.VISIBLE);
findViewById(R.id.imageRemoveImage).setVisibility(View.VISIBLE);

Expand Down
23 changes: 23 additions & 0 deletions app/src/main/java/com/rick/notes/activites/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.Nullable;
Expand Down Expand Up @@ -53,6 +56,26 @@ protected void onCreate(Bundle savedInstanceState) {
notesRecyclerView.setAdapter(notesAdapter);

getNotes(REQUEST_CODE_SHOW_NOTES, false);

EditText inputSearch = findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
notesAdapter.cancelTimer();
}

@Override
public void afterTextChanged(Editable s) {
if (noteList.size() != 0) {
notesAdapter.searchNotes(s.toString());
}
}
});
}

@Override
Expand Down
41 changes: 40 additions & 1 deletion app/src/main/java/com/rick/notes/adapters/NotesAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -17,16 +19,23 @@
import com.rick.notes.entities.Note;
import com.rick.notes.listeners.NotesListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.NoteViewHolder> {

private final List<Note> notes;
private List<Note> notes;
private final List<Note> notesSource;
private final NotesListener notesListener;

private Timer timer;

public NotesAdapter(List<Note> notes, NotesListener notesListener) {
this.notes = notes;
this.notesListener = notesListener;
notesSource = notes;
}

@NonNull
Expand Down Expand Up @@ -94,4 +103,34 @@ void setNote(Note note) {
}
}
}

public void searchNotes(final String searchKeyword) {
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if (searchKeyword.trim().isEmpty()) {
notes = notesSource;
} else {
ArrayList<Note> temp = new ArrayList<>();
for (Note note : notesSource) {
if (note.getTitle().toLowerCase().contains(searchKeyword.toLowerCase()) ||
note.getSubtitle().toLowerCase().contains(searchKeyword.toLowerCase()) ||
note.getNoteText().toLowerCase().contains(searchKeyword.toLowerCase())) {
temp.add(note);
}
}
notes = temp;
}

new Handler(Looper.getMainLooper()).post(() -> notifyDataSetChanged());
}
}, 500);
}

public void cancelTimer() {
if (timer != null) {
timer.cancel();
}
}
}

0 comments on commit 75a4741

Please sign in to comment.