Skip to content

Commit

Permalink
Fic some issues
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillemPejo committed Jan 4, 2021
1 parent a464743 commit 2c38b14
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.animation.AnimatorInflater;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.media.Image;
import android.os.AsyncTask;
import android.os.Bundle;
Expand All @@ -21,12 +23,16 @@
import android.widget.GridLayout;
import android.widget.ImageView;

import com.google.android.material.snackbar.Snackbar;

import java.util.ArrayList;
import java.util.IllegalFormatCodePointException;
import java.util.List;

import me.guillem.cleannotes.R;
import me.guillem.cleannotes.adapters.NotesAdapter;
import me.guillem.cleannotes.callbacks.CallBackNoteTouch;
import me.guillem.cleannotes.callbacks.MyNoteTouchHelperCallback;
import me.guillem.cleannotes.database.NotesDatabase;
import me.guillem.cleannotes.entities.Note;
import me.guillem.cleannotes.listeners.NotesListener;
Expand All @@ -35,7 +41,7 @@
import static me.guillem.cleannotes.adapters.NotesAdapter.SPAN_COUNT_TWO;


public class MainActivity extends AppCompatActivity implements NotesListener {
public class MainActivity extends AppCompatActivity implements NotesListener, CallBackNoteTouch {

public static final int REQUEST_CODE_ADD_NOTE = 1;
public static final int REQUEST_CODE_UPDATE_NOTE = 2;
Expand All @@ -50,12 +56,15 @@ public class MainActivity extends AppCompatActivity implements NotesListener {
private StaggeredGridLayoutManager notesLayoutManager;

private ImageView changeLayoutList;
private View principalLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

principalLayout = findViewById(R.id.principalLayout);

ImageView imageAddNoteMain = findViewById(R.id.imageAddNoteMain);
imageAddNoteMain.setOnClickListener((v) -> {
startActivityForResult(
Expand All @@ -74,10 +83,13 @@ protected void onCreate(Bundle savedInstanceState) {
noteList = new ArrayList<>();
notesAdapter = new NotesAdapter(noteList, this);
notesRecyclerView.setAdapter(notesAdapter);
ItemTouchHelper.Callback callback = new MyNoteTouchHelperCallback(this);
ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
touchHelper.attachToRecyclerView(notesRecyclerView);

getNotes(REQUEST_CODE_SHOW_NOTE,false);

ImageView changeLayoutList = findViewById(R.id.changeLayoutList);
changeLayoutList = findViewById(R.id.changeLayoutList);

changeLayoutList.setOnClickListener(new View.OnClickListener() {
@Override
Expand Down Expand Up @@ -189,4 +201,29 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
}


@Override
public void itemTouchOnMove(int oldPositions, int newPosition) {
noteList.add(newPosition, noteList.remove(oldPositions));
notesAdapter.notifyItemMoved(oldPositions, newPosition);
}

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int position) {
String name = noteList.get(viewHolder.getAdapterPosition()).getTitle();

final Note deletedNote = noteList.get(viewHolder.getAdapterPosition());
final int deletedIndex = viewHolder.getAdapterPosition();

notesAdapter.removeNote(viewHolder.getAdapterPosition());
Snackbar snackbar = Snackbar.make(principalLayout, R.string.deleted_note, Snackbar.LENGTH_LONG);
snackbar.setAction("UNDO", new View.OnClickListener() {
@Override
public void onClick(View v) {
notesAdapter.restoreNote(deletedNote, deletedIndex);

}
});
snackbar.setActionTextColor(Color.WHITE);
snackbar.show();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
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;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -82,11 +84,13 @@ public int getItemViewType(int position) {
return position;
}

static class NoteViewHolder extends RecyclerView.ViewHolder {
public static class NoteViewHolder extends RecyclerView.ViewHolder {

TextView textTitle, textSubtitle, textDateTime;
LinearLayout layoutNote;
RoundedImageView imageNote;
public LinearLayout viewF;
public LinearLayout viewB;

public NoteViewHolder(@NonNull View itemView) {
super(itemView);
Expand All @@ -95,6 +99,8 @@ public NoteViewHolder(@NonNull View itemView) {
textDateTime = itemView.findViewById(R.id.textDateTime);
layoutNote = itemView.findViewById(R.id.layoutNote);
imageNote = itemView.findViewById(R.id.imageNote);
viewB = itemView.findViewById(R.id.layoutNote);
viewF = itemView.findViewById(R.id.layoutNote);
}

void setNote(Note note){
Expand Down Expand Up @@ -169,4 +175,15 @@ public void run() {
public void cancellTime(){
if (timer != null) {timer.cancel();}
}


public void removeNote(int position){
notes.remove(position);
notifyItemRemoved(position);
}

public void restoreNote(Note note, int position){
notes.add(position, note);
notifyItemInserted(position);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package me.guillem.cleannotes.callbacks;

import androidx.recyclerview.widget.RecyclerView;

/**
* * Created by Guillem on 30/12/20.
*/
public interface CallBackNoteTouch {

void itemTouchOnMove(int oldPositions, int newPosition);

void onSwiped(RecyclerView.ViewHolder viewHolder, int position);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package me.guillem.cleannotes.callbacks;

import android.content.ClipData;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;
import androidx.room.ColumnInfo;

import me.guillem.cleannotes.R;
import me.guillem.cleannotes.adapters.NotesAdapter;

/**
* * Created by Guillem on 30/12/20.
*/
public class MyNoteTouchHelperCallback extends ItemTouchHelper.Callback {

CallBackNoteTouch callBackNoteTouch;

public MyNoteTouchHelperCallback(CallBackNoteTouch callBackNoteTouch) {
this.callBackNoteTouch = callBackNoteTouch;
}

@Override
public boolean isLongPressDragEnabled() {
return true;
}

@Override
public boolean isItemViewSwipeEnabled() {
return true;
}

@Override
public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
final int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
return makeMovementFlags(dragFlags, swipeFlags);
}

@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
callBackNoteTouch.itemTouchOnMove(viewHolder.getAdapterPosition(),target.getAdapterPosition());
return false;
}

@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
callBackNoteTouch.onSwiped(viewHolder, viewHolder.getAdapterPosition());
}

@Override
public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
if (actionState == ItemTouchHelper.ACTION_STATE_DRAG){
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}else {
final View foregroundView = ((NotesAdapter.NoteViewHolder)viewHolder).viewB;
getDefaultUIUtil().onDrawOver(c,recyclerView,foregroundView,dX, dY, actionState,isCurrentlyActive);
}
}

@Override
public void onChildDrawOver(@NonNull Canvas c, @NonNull RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
//super.onChildDrawOver(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
if (actionState != ItemTouchHelper.ACTION_STATE_DRAG) {
final View foregroundView = ((NotesAdapter.NoteViewHolder) viewHolder).viewF;
getDefaultUIUtil().onDrawOver(c, recyclerView, foregroundView, dX, dY, actionState, isCurrentlyActive);
}
}

@Override
public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
//super.clearView(recyclerView, viewHolder);
final View foregroundView = ((NotesAdapter.NoteViewHolder) viewHolder).viewF;
//foregroundView.setBackgroundColor(ContextCompat.getColor(((NotesAdapter.NoteViewHolder)viewHolder).viewF.getContext(), R.color.colorDelete));
getDefaultUIUtil().clearView(foregroundView );
}

@Override
public void onSelectedChanged(@Nullable RecyclerView.ViewHolder viewHolder, int actionState) {
super.onSelectedChanged(viewHolder, actionState);
if (viewHolder!=null){
final View foregroundView = ((NotesAdapter.NoteViewHolder) viewHolder).viewF;
if (actionState == ItemTouchHelper.ACTION_STATE_DRAG){
}
getDefaultUIUtil().onSelected(foregroundView);
}
}

@Override
public int convertToAbsoluteDirection(int flags, int layoutDirection) {
return super.convertToAbsoluteDirection(flags, layoutDirection);
}
}
27 changes: 9 additions & 18 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey_p1"
tools:context=".activities.MainActivity">
tools:context=".activities.MainActivity"
android:id="@+id/principalLayout">

<TextView
android:id="@+id/textMyNotes"
Expand Down Expand Up @@ -66,25 +68,14 @@
android:layout_width="@dimen/_20sdp"
android:layout_height="@dimen/_20sdp"
android:contentDescription="@string/app_name"
android:src="@drawable/ic_list"
android:layout_margin="@dimen/_10sdp"
app:layout_constraintBottom_toBottomOf="@+id/layoutSearch"
app:layout_constraintStart_toEndOf="@+id/layoutSearch"
app:layout_constraintTop_toTopOf="@+id/layoutSearch"
app:tint="@color/search" />
<ImageView
android:id="@+id/changeLayoutGrid"
android:layout_width="@dimen/_20sdp"
android:layout_height="@dimen/_20sdp"
android:contentDescription="@string/app_name"
android:src="@drawable/ic_grid"
android:layout_margin="@dimen/_10sdp"
app:layout_constraintBottom_toBottomOf="@+id/layoutSearch"
app:layout_constraintStart_toEndOf="@+id/layoutSearch"
app:layout_constraintTop_toTopOf="@+id/layoutSearch"
android:visibility="gone"
app:tint="@color/search" />


<androidx.recyclerview.widget.RecyclerView
android:id="@+id/notesRecyclerView"
android:layout_width="match_parent"
Expand Down Expand Up @@ -145,7 +136,7 @@
app:layout_constraintStart_toStartOf="parent">

<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/heko"
android:id="@+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
Expand All @@ -163,10 +154,10 @@
android:layout_marginEnd="@dimen/_30sdp"
android:contentDescription="@string/app_name"
android:src="@drawable/ic_add_symb"
app:layout_anchor="@id/heko"
app:layout_constraintBottom_toTopOf="@id/heko"
app:layout_anchor="@id/bottomBar"
app:layout_constraintBottom_toTopOf="@id/bottomBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/heko"
app:layout_constraintTop_toTopOf="@id/bottomBar"
app:maxImageSize="@dimen/_35sdp"
app:tint="@color/grey_p1" />

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
<string name="delete">DELETE</string>
<string name="sure_delete">Are you sure you want to delete this note?</string>
<string name="at_date">at</string>
<string name="deleted_note">Note deleted!</string>
</resources>

0 comments on commit 2c38b14

Please sign in to comment.