Commit 4738181f authored by 9731301's avatar 9731301

add gallery to show notedPics

parent fe5d7417
package com.example.mynotepad.MenuFeatures.AllNotes; package com.example.mynotepad.MenuFeatures.AllNotes;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
...@@ -11,15 +9,12 @@ import androidx.navigation.NavController; ...@@ -11,15 +9,12 @@ import androidx.navigation.NavController;
import androidx.navigation.Navigation; import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.Toast; import android.widget.Toast;
import com.example.mynotepad.Dialogs.AddTxtToPicsDialog.AddTextToPicDialog;
import com.example.mynotepad.Dialogs.CustomDialog; import com.example.mynotepad.Dialogs.CustomDialog;
import com.example.mynotepad.Dialogs.onCustomDialogClickListener; import com.example.mynotepad.Dialogs.onCustomDialogClickListener;
import com.example.mynotepad.MenuFeatures.AllNotes.DataBase.Note; import com.example.mynotepad.MenuFeatures.AllNotes.DataBase.Note;
......
...@@ -3,37 +3,38 @@ package com.example.mynotepad.MenuFeatures.AllNotes.DataBase; ...@@ -3,37 +3,38 @@ package com.example.mynotepad.MenuFeatures.AllNotes.DataBase;
import androidx.room.ColumnInfo; import androidx.room.ColumnInfo;
import androidx.room.Entity; import androidx.room.Entity;
import androidx.room.PrimaryKey; import androidx.room.PrimaryKey;
@Entity
public class Note { @Entity
@PrimaryKey(autoGenerate = true) public class Note {
int ID; @PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "title") int ID;
String title ; @ColumnInfo(name = "title")
@ColumnInfo(name = "description") String title;
String txtBody; @ColumnInfo(name = "description")
@ColumnInfo(name = "archived") String txtBody;
boolean isarchived; @ColumnInfo(name = "archived")
boolean isarchived;
public Note(String title , String txtBody , boolean isarchived){
this.title = title; public Note(String title, String txtBody, boolean isarchived) {
this.txtBody = txtBody; this.title = title;
this.isarchived = isarchived; this.txtBody = txtBody;
} this.isarchived = isarchived;
}
public int getID() {
return ID; public int getID() {
} return ID;
}
public String getTitle() {
return title; public String getTitle() {
} return title;
}
public String getDescription() {
return txtBody; public String getDescription() {
} return txtBody;
}
public boolean isarchived(){
return isarchived; public boolean isarchived() {
} return isarchived;
}
} }
...@@ -22,10 +22,6 @@ public interface NoteDAO { ...@@ -22,10 +22,6 @@ public interface NoteDAO {
@Delete @Delete
void deleteNote(Note myNote); void deleteNote(Note myNote);
@Query("UPDATE Note SET title = :title , description = :description ,archived = :isarchived Where ID = :id") @Query("UPDATE Note SET title = :title , description = :description ,archived = :isarchived Where ID = :id")
void updateNote(String title , String description , boolean isarchived , int id); void updateNote(String title , String description , boolean isarchived , int id);
} }
\ No newline at end of file
...@@ -3,7 +3,7 @@ package com.example.mynotepad.MenuFeatures.AllNotes.DataBase; ...@@ -3,7 +3,7 @@ package com.example.mynotepad.MenuFeatures.AllNotes.DataBase;
import androidx.room.Database; import androidx.room.Database;
import androidx.room.RoomDatabase; import androidx.room.RoomDatabase;
@Database(entities = {Note.class, DateEntity.class},version = 1) @Database(entities = {Note.class, DateEntity.class ,PicsEntity.class},version = 1)
public abstract class NoteDataBase extends RoomDatabase { public abstract class NoteDataBase extends RoomDatabase {
public abstract NoteDAO noteDao(); public abstract NoteDAO noteDao();
......
package com.example.mynotepad.MenuFeatures.AllNotes.DataBase; package com.example.mynotepad.MenuFeatures.AllNotes.DataBase;
import androidx.room.Dao;
import androidx.room.Delete; import androidx.room.Delete;
import androidx.room.Insert; import androidx.room.Insert;
import androidx.room.Query; import androidx.room.Query;
import java.util.List; import java.util.List;
@Dao
public interface PicsDao { public interface PicsDao {
// a method list type // a method list type
@Query("SELECT * FROM PicsEntity") @Query("SELECT * FROM PicsEntity")
List<DateEntity> getAllPicsNotes(); List<PicsEntity> getAllPicsNotes();
//insert data in database //insert data in database
@Insert @Insert
...@@ -17,7 +19,7 @@ public interface PicsDao { ...@@ -17,7 +19,7 @@ public interface PicsDao {
//delete data in database //delete data in database
@Delete @Delete
void deletePicsNote(DateEntity dateEntity); void deletePicsNote(PicsEntity picsEntity);
//update data //update data
@Query("UPDATE PicsEntity SET txt = :txt , url = :url ") @Query("UPDATE PicsEntity SET txt = :txt , url = :url ")
......
...@@ -2,12 +2,14 @@ package com.example.mynotepad.MenuFeatures.AllNotes.DataBase; ...@@ -2,12 +2,14 @@ package com.example.mynotepad.MenuFeatures.AllNotes.DataBase;
import androidx.room.ColumnInfo; import androidx.room.ColumnInfo;
import androidx.room.Entity; import androidx.room.Entity;
import androidx.room.PrimaryKey;
@Entity @Entity
public class PicsEntity { public class PicsEntity {
@PrimaryKey(autoGenerate = true)
int ID;
@ColumnInfo(name = "url") @ColumnInfo(name = "url")
String url ; String url;
@ColumnInfo(name = "txt") @ColumnInfo(name = "txt")
String txt; String txt;
......
...@@ -146,7 +146,7 @@ public class CameraActivity extends AppCompatActivity { ...@@ -146,7 +146,7 @@ public class CameraActivity extends AppCompatActivity {
} }
}; };
private static File getOutputMediaFile() { public static File getOutputMediaFile() {
//create a file to put pics in it //create a file to put pics in it
File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "MyNotePad"); File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "MyNotePad");
if (!mediaStorageDir.exists()) { if (!mediaStorageDir.exists()) {
......
...@@ -3,10 +3,13 @@ package com.example.mynotepad.MenuFeatures.Pics; ...@@ -3,10 +3,13 @@ package com.example.mynotepad.MenuFeatures.Pics;
import android.content.Intent; import android.content.Intent;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.constraintlayout.widget.ConstraintLayout; import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
...@@ -15,16 +18,25 @@ import android.view.LayoutInflater; ...@@ -15,16 +18,25 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.Toast;
import com.example.mynotepad.Dialogs.AddTxtToPicsDialog.AddTextToPicDialog; import com.example.mynotepad.Dialogs.AddTxtToPicsDialog.AddTextToPicDialog;
import com.example.mynotepad.Dialogs.AddTxtToPicsDialog.OnSavePicListenerClick; import com.example.mynotepad.Dialogs.AddTxtToPicsDialog.OnSavePicListenerClick;
import com.example.mynotepad.MainActivity;
import com.example.mynotepad.MenuFeatures.AllNotes.DataBase.PicsEntity;
import com.example.mynotepad.MenuFeatures.Pics.Camera.CameraActivity; import com.example.mynotepad.MenuFeatures.Pics.Camera.CameraActivity;
import com.example.mynotepad.MenuFeatures.Utils; import com.example.mynotepad.MenuFeatures.Utils;
import com.example.mynotepad.R; import com.example.mynotepad.R;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import static com.example.mynotepad.MainActivity.noteDataBase;
import static com.example.mynotepad.MenuFeatures.Pics.Camera.CameraActivity.getOutputMediaFile;
public class ChoosingPicsFragment extends Fragment { public class ChoosingPicsFragment extends Fragment {
...@@ -82,27 +94,42 @@ public class ChoosingPicsFragment extends Fragment { ...@@ -82,27 +94,42 @@ public class ChoosingPicsFragment extends Fragment {
}); });
} }
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override @Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
newFilePath = ""; newFilePath = "";
System.out.println("================================================================================================");
if (data != null) { if (data != null) {
if (requestCode == 1) {// get image from gallery if (requestCode == 1) {// get image from gallery
newFilePath = data.getData().getPath();
File pictureFile = getOutputMediaFile();
if (pictureFile == null) { return; }
InputStream stream = null; InputStream stream = null;
try { try {
newFilePath = pictureFile.getPath();
BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(data.getData()));
stream = getActivity().getContentResolver().openInputStream(data.getData()); stream = getActivity().getContentResolver().openInputStream(data.getData());
try (OutputStream output = new FileOutputStream(pictureFile)) {
byte[] buffer = new byte[4 * 1024]; // or other buffer size
int read;
while ((read = stream.read(buffer)) != -1) {
output.write(buffer, 0, read);
}
output.flush();
}
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} }
System.out.println(")))))))))))))))))))))))))))))))))))))))))))))))" + newFilePath);
showDialogOnResult(BitmapFactory.decodeStream(stream)); showDialogOnResult(BitmapFactory.decodeFile(pictureFile.getAbsolutePath()));
} else if (requestCode == 2) {// get image from camera } else if (requestCode == 2) {// get image from camera
newFilePath = data.getStringExtra("filePath"); newFilePath = data.getStringExtra("filePath");
File imgFile = new File(newFilePath); File imgFile = new File(newFilePath);
if (imgFile.exists()) { if (imgFile.exists()) {
System.out.println("((((((((((((((((((((((((((((((((((((((((((((" + newFilePath);
showDialogOnResult(BitmapFactory.decodeFile(imgFile.getAbsolutePath())); showDialogOnResult(BitmapFactory.decodeFile(imgFile.getAbsolutePath()));
} }
} }
...@@ -116,8 +143,8 @@ public class ChoosingPicsFragment extends Fragment { ...@@ -116,8 +143,8 @@ public class ChoosingPicsFragment extends Fragment {
addTextToPicDialog.setOnSavePicListenerClick(new OnSavePicListenerClick() { addTextToPicDialog.setOnSavePicListenerClick(new OnSavePicListenerClick() {
@Override @Override
public void saveClicked(String editTextTxt) { public void saveClicked(String editTextTxt) {
//todo save image path and txt in data base PicsEntity picsEntity = new PicsEntity(newFilePath, editTextTxt);
//todo txt should be save in database with pic path if file pah is not null and add it to my list noteDataBase.picsDao().insertPicsNote(picsEntity);
} }
}); });
} }
......
...@@ -10,18 +10,15 @@ import android.widget.TextView; ...@@ -10,18 +10,15 @@ import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import com.example.mynotepad.R; import com.example.mynotepad.R;
import java.util.List;
import java.util.ArrayList;
public class GalleryAdaptor extends RecyclerView.Adapter<GalleryAdaptor.GalleryViewHolder> { public class GalleryAdaptor extends RecyclerView.Adapter<GalleryAdaptor.GalleryViewHolder> {
private ArrayList<MyGalleryPic> picsUrlAndTxt; private List<MyGalleryPic> picsUrlAndTxt;
private Context context; private OnGalleryItemClickListener onItemClickListener;
public GalleryAdaptor(ArrayList<MyGalleryPic> picsUrlAndTxt, Context context) { public GalleryAdaptor(List<MyGalleryPic> picsUrlAndTxt) {
this.picsUrlAndTxt = picsUrlAndTxt; this.picsUrlAndTxt = picsUrlAndTxt;
this.context = context;
} }
@NonNull @NonNull
...@@ -43,7 +40,15 @@ public class GalleryAdaptor extends RecyclerView.Adapter<GalleryAdaptor.GalleryV ...@@ -43,7 +40,15 @@ public class GalleryAdaptor extends RecyclerView.Adapter<GalleryAdaptor.GalleryV
return picsUrlAndTxt.size(); return picsUrlAndTxt.size();
} }
public class GalleryViewHolder extends RecyclerView.ViewHolder { public OnGalleryItemClickListener getOnItemClickListener() {
return onItemClickListener;
}
public void setOnItemClickListener(OnGalleryItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
public class GalleryViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener , View.OnLongClickListener {
TextView textView; TextView textView;
ImageView imageView; ImageView imageView;
...@@ -52,5 +57,19 @@ public class GalleryAdaptor extends RecyclerView.Adapter<GalleryAdaptor.GalleryV ...@@ -52,5 +57,19 @@ public class GalleryAdaptor extends RecyclerView.Adapter<GalleryAdaptor.GalleryV
textView = itemView.findViewById(R.id.tv_gallery); textView = itemView.findViewById(R.id.tv_gallery);
imageView = itemView.findViewById(R.id.img_gallery); imageView = itemView.findViewById(R.id.img_gallery);
} }
@Override
public void onClick(View view) {
if (onItemClickListener != null) {
onItemClickListener.onItemClicked(picsUrlAndTxt.get(getAdapterPosition()));
}
}
@Override
public boolean onLongClick(View view) {
if (onItemClickListener != null)
onItemClickListener.onItemLongClicked(picsUrlAndTxt.get(getAdapterPosition()));
return true;
}
} }
} }
...@@ -4,17 +4,39 @@ import android.os.Bundle; ...@@ -4,17 +4,39 @@ import android.os.Bundle;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Toast;
import com.example.mynotepad.Dialogs.CustomDialog;
import com.example.mynotepad.Dialogs.onCustomDialogClickListener;
import com.example.mynotepad.MenuFeatures.AllNotes.DataBase.DateEntity;
import com.example.mynotepad.MenuFeatures.AllNotes.DataBase.Note;
import com.example.mynotepad.MenuFeatures.AllNotes.DataBase.PicsEntity;
import com.example.mynotepad.MenuFeatures.AllNotes.MyNoteRecyclerView.MyNote;
import com.example.mynotepad.MenuFeatures.AllNotes.MyNoteRecyclerView.OnAllNotesItemClickListener;
import com.example.mynotepad.MenuFeatures.CustomToolbarOption;
import com.example.mynotepad.MenuFeatures.Pics.PicsActivity; import com.example.mynotepad.MenuFeatures.Pics.PicsActivity;
import com.example.mynotepad.R; import com.example.mynotepad.R;
import java.util.ArrayList;
import java.util.List;
import static com.example.mynotepad.MainActivity.noteDataBase;
public class NotedPicsListFragment extends Fragment { public class NotedPicsListFragment extends Fragment {
private List<MyGalleryPic> myGalleryPics;
private List<PicsEntity> picsEntityList;
private GalleryAdaptor adapter;
private CustomToolbarOption customToolbarOption;
private MyGalleryPic myChosenPic;
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
...@@ -27,6 +49,71 @@ public class NotedPicsListFragment extends Fragment { ...@@ -27,6 +49,71 @@ public class NotedPicsListFragment extends Fragment {
@Override @Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
RecyclerView recyclerView = view.findViewById(R.id.picsRecyclerView);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getContext(), 2);
recyclerView.setLayoutManager(layoutManager);
//todo add customtoolbar
setData();
adapter = new GalleryAdaptor(myGalleryPics);
recyclerView.setAdapter(adapter);
addListeners();
}
private void setData() {
picsEntityList = noteDataBase.picsDao().getAllPicsNotes();
myGalleryPics = new ArrayList<>();
for (PicsEntity picsEntity : picsEntityList) {
MyGalleryPic myGalleryPic = new MyGalleryPic(picsEntity.getUrl(), picsEntity.getTxt());
myGalleryPics.add(myGalleryPic);
}
}
private void addListeners() {
// add listener to recyclerView
adapter.setOnItemClickListener(new OnGalleryItemClickListener() {
@Override
public void onItemClicked(MyGalleryPic myGalleryPic) {
myChosenPic = myGalleryPic;
//todo show big img or go to a new fragment
//set noteFragment data
final Bundle bundle = new Bundle();
bundle.putString("url", myGalleryPic.getUrl());
bundle.putString("txt", myGalleryPic.getText());
}
@Override
public void onItemLongClicked(MyGalleryPic myGalleryPic) {
myChosenPic = myGalleryPic;
}
});
}
protected void closeClicked(CustomToolbarOption customToolbarOption) {
customToolbarOption.setVisibility(View.GONE);
}
protected void deleteClicked(final PicsEntity pic) {
CustomDialog customDialog = new CustomDialog();
customDialog.showAddTxtDialog(getActivity(), "are you sure you wanna delete it ??? ");
customDialog.setOnCustomDialogClickListener(new onCustomDialogClickListener() {
@Override
public void yesClicked() {
noteDataBase.picsDao().deletePicsNote(pic);
myGalleryPics.remove(myChosenPic);
adapter.notifyDataSetChanged();
}
@Override
public void noClicked() {
}
});
// allNoteToolBar.setVisibility(View.VISIBLE);
customToolbarOption.setVisibility(View.GONE);
// allNoteToolBar.setVisibility(View.VISIBLE);
} }
} }
\ No newline at end of file
package com.example.mynotepad.MenuFeatures.Pics.Gallery;
public interface OnGalleryItemClickListener {
void onItemClicked(MyGalleryPic myGalleryPic);
void onItemLongClicked(MyGalleryPic myGalleryPic);
}
...@@ -23,16 +23,15 @@ ...@@ -23,16 +23,15 @@
android:id="@+id/nav_pic_fragment_place" android:id="@+id/nav_pic_fragment_place"
android:name="androidx.navigation.fragment.NavHostFragment" android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="0dp"
app:defaultNavHost="true" app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view" app:layout_constraintBottom_toTopOf="@+id/nav_view"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0" app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:navGraph="@navigation/bottom_nav_pic" /> app:navGraph="@navigation/bottom_nav_pic" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:orientation="vertical"
android:background="@color/colorPrimaryDark" android:background="?colorButtonNormal"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content">
<ImageView <ImageView
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
<TextView <TextView
android:id="@+id/tv_gallery" android:id="@+id/tv_gallery"
android:layout_gravity="center" android:layout_gravity="center"
android:textColor="#FFFFFF" android:textColor="?android:textColorPrimary"
android:textStyle="bold" android:textStyle="bold"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" /> android:layout_height="wrap_content" />
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
tools:context=".MenuFeatures.Pics.Gallery.NotedPicsListFragment"> tools:context=".MenuFeatures.Pics.Gallery.NotedPicsListFragment">
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/picsRecyclerView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:scrollbars="vertical"/> android:scrollbars="vertical"/>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment