Commit 4738181f authored by 9731301's avatar 9731301

add gallery to show notedPics

parent fe5d7417
package com.example.mynotepad.MenuFeatures.AllNotes;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import androidx.annotation.NonNull;
......@@ -11,15 +9,12 @@ import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.example.mynotepad.Dialogs.AddTxtToPicsDialog.AddTextToPicDialog;
import com.example.mynotepad.Dialogs.CustomDialog;
import com.example.mynotepad.Dialogs.onCustomDialogClickListener;
import com.example.mynotepad.MenuFeatures.AllNotes.DataBase.Note;
......
......@@ -3,37 +3,38 @@ package com.example.mynotepad.MenuFeatures.AllNotes.DataBase;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
@Entity
public class Note {
@PrimaryKey(autoGenerate = true)
int ID;
@ColumnInfo(name = "title")
String title ;
@ColumnInfo(name = "description")
String txtBody;
@ColumnInfo(name = "archived")
boolean isarchived;
public Note(String title , String txtBody , boolean isarchived){
this.title = title;
this.txtBody = txtBody;
this.isarchived = isarchived;
}
public int getID() {
return ID;
}
public String getTitle() {
return title;
}
public String getDescription() {
return txtBody;
}
public boolean isarchived(){
return isarchived;
}
@Entity
public class Note {
@PrimaryKey(autoGenerate = true)
int ID;
@ColumnInfo(name = "title")
String title;
@ColumnInfo(name = "description")
String txtBody;
@ColumnInfo(name = "archived")
boolean isarchived;
public Note(String title, String txtBody, boolean isarchived) {
this.title = title;
this.txtBody = txtBody;
this.isarchived = isarchived;
}
public int getID() {
return ID;
}
public String getTitle() {
return title;
}
public String getDescription() {
return txtBody;
}
public boolean isarchived() {
return isarchived;
}
}
......@@ -22,10 +22,6 @@ public interface NoteDAO {
@Delete
void deleteNote(Note myNote);
@Query("UPDATE Note SET title = :title , description = :description ,archived = :isarchived Where ID = :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;
import androidx.room.Database;
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 NoteDAO noteDao();
......
package com.example.mynotepad.MenuFeatures.AllNotes.DataBase;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import java.util.List;
@Dao
public interface PicsDao {
// a method list type
@Query("SELECT * FROM PicsEntity")
List<DateEntity> getAllPicsNotes();
List<PicsEntity> getAllPicsNotes();
//insert data in database
@Insert
......@@ -17,7 +19,7 @@ public interface PicsDao {
//delete data in database
@Delete
void deletePicsNote(DateEntity dateEntity);
void deletePicsNote(PicsEntity picsEntity);
//update data
@Query("UPDATE PicsEntity SET txt = :txt , url = :url ")
......
......@@ -2,12 +2,14 @@ package com.example.mynotepad.MenuFeatures.AllNotes.DataBase;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
@Entity
public class PicsEntity {
@PrimaryKey(autoGenerate = true)
int ID;
@ColumnInfo(name = "url")
String url ;
String url;
@ColumnInfo(name = "txt")
String txt;
......
......@@ -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
File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "MyNotePad");
if (!mediaStorageDir.exists()) {
......
......@@ -3,10 +3,13 @@ package com.example.mynotepad.MenuFeatures.Pics;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
......@@ -15,16 +18,25 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.example.mynotepad.Dialogs.AddTxtToPicsDialog.AddTextToPicDialog;
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.Utils;
import com.example.mynotepad.R;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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 {
......@@ -82,27 +94,42 @@ public class ChoosingPicsFragment extends Fragment {
});
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
newFilePath = "";
System.out.println("================================================================================================");
if (data != null) {
if (requestCode == 1) {// get image from gallery
newFilePath = data.getData().getPath();
File pictureFile = getOutputMediaFile();
if (pictureFile == null) { return; }
InputStream stream = null;
try {
newFilePath = pictureFile.getPath();
BitmapFactory.decodeStream(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) {
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
newFilePath = data.getStringExtra("filePath");
File imgFile = new File(newFilePath);
if (imgFile.exists()) {
System.out.println("((((((((((((((((((((((((((((((((((((((((((((" + newFilePath);
showDialogOnResult(BitmapFactory.decodeFile(imgFile.getAbsolutePath()));
}
}
......@@ -116,8 +143,8 @@ public class ChoosingPicsFragment extends Fragment {
addTextToPicDialog.setOnSavePicListenerClick(new OnSavePicListenerClick() {
@Override
public void saveClicked(String editTextTxt) {
//todo save image path and txt in data base
//todo txt should be save in database with pic path if file pah is not null and add it to my list
PicsEntity picsEntity = new PicsEntity(newFilePath, editTextTxt);
noteDataBase.picsDao().insertPicsNote(picsEntity);
}
});
}
......
......@@ -10,18 +10,15 @@ import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.mynotepad.R;
import java.util.ArrayList;
import java.util.List;
public class GalleryAdaptor extends RecyclerView.Adapter<GalleryAdaptor.GalleryViewHolder> {
private ArrayList<MyGalleryPic> picsUrlAndTxt;
private Context context;
private List<MyGalleryPic> picsUrlAndTxt;
private OnGalleryItemClickListener onItemClickListener;
public GalleryAdaptor(ArrayList<MyGalleryPic> picsUrlAndTxt, Context context) {
public GalleryAdaptor(List<MyGalleryPic> picsUrlAndTxt) {
this.picsUrlAndTxt = picsUrlAndTxt;
this.context = context;
}
@NonNull
......@@ -43,7 +40,15 @@ public class GalleryAdaptor extends RecyclerView.Adapter<GalleryAdaptor.GalleryV
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;
ImageView imageView;
......@@ -52,5 +57,19 @@ public class GalleryAdaptor extends RecyclerView.Adapter<GalleryAdaptor.GalleryV
textView = itemView.findViewById(R.id.tv_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;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
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.R;
import java.util.ArrayList;
import java.util.List;
import static com.example.mynotepad.MainActivity.noteDataBase;
public class NotedPicsListFragment extends Fragment {
private List<MyGalleryPic> myGalleryPics;
private List<PicsEntity> picsEntityList;
private GalleryAdaptor adapter;
private CustomToolbarOption customToolbarOption;
private MyGalleryPic myChosenPic;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
......@@ -27,6 +49,71 @@ public class NotedPicsListFragment extends Fragment {
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle 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 @@
android:id="@+id/nav_pic_fragment_place"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintBottom_toTopOf="@+id/nav_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:navGraph="@navigation/bottom_nav_pic" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@color/colorPrimaryDark"
android:background="?colorButtonNormal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
......@@ -12,7 +12,7 @@
<TextView
android:id="@+id/tv_gallery"
android:layout_gravity="center"
android:textColor="#FFFFFF"
android:textColor="?android:textColorPrimary"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
......
......@@ -6,6 +6,7 @@
tools:context=".MenuFeatures.Pics.Gallery.NotedPicsListFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/picsRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
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