From 288e06be7c139fabfd125417264d86f9b4d3ba86 Mon Sep 17 00:00:00 2001 From: sebastian Date: Fri, 1 Nov 2024 08:48:30 +0100 Subject: [PATCH] Enable Notes with multiple Sheets --- .idea/dataSources.xml | 15 +++++ .../java/core/notevault/MainActivity.java | 17 +++--- .../core/notevault/data/MusicDatabase.java | 2 +- .../java/core/notevault/data/MusicNote.java | 24 +++----- .../core/notevault/data/MusicNoteDAO.java | 8 ++- .../java/core/notevault/data/NoteSheet.java | 46 +++++++++++++++ .../ui/home/FullScreenImageActivity.java | 18 ++++-- .../core/notevault/ui/home/HomeFragment.java | 19 ++++-- .../notevault/ui/home/NoteSongAdapter.java | 54 +++++++++++++++++- .../ui/metadatadialog/MetaDataDialog.java | 16 +++--- .../ui/noteviewer/ImagePagerAdapter.java | 50 ++++++++++++++++ .../res/layout/activity_fullscreen_image.xml | 7 +-- app/src/main/res/layout/item_image.xml | 11 ++++ music_database | Bin 0 -> 28672 bytes music_database-shm | Bin 0 -> 32768 bytes music_database-wal | 0 16 files changed, 236 insertions(+), 51 deletions(-) create mode 100644 .idea/dataSources.xml create mode 100644 app/src/main/java/core/notevault/data/NoteSheet.java create mode 100644 app/src/main/java/core/notevault/ui/noteviewer/ImagePagerAdapter.java create mode 100644 app/src/main/res/layout/item_image.xml create mode 100644 music_database create mode 100644 music_database-shm create mode 100644 music_database-wal diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..6bb5d70 --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,15 @@ + + + + + sqlite.xerial + true + org.sqlite.JDBC + jdbc:sqlite:$PROJECT_DIR$/music_database + + + + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/app/src/main/java/core/notevault/MainActivity.java b/app/src/main/java/core/notevault/MainActivity.java index 3bc9eb2..f347e7b 100644 --- a/app/src/main/java/core/notevault/MainActivity.java +++ b/app/src/main/java/core/notevault/MainActivity.java @@ -16,6 +16,7 @@ import androidx.drawerlayout.widget.DrawerLayout; import androidx.appcompat.app.AppCompatActivity; import core.notevault.data.MusicDatabase; import core.notevault.data.MusicNote; +import core.notevault.data.NoteSheet; import core.notevault.databinding.ActivityMainBinding; import core.notevault.ui.metadatadialog.MetaDataDialog; @@ -67,14 +68,16 @@ public class MainActivity extends AppCompatActivity implements MetaDataDialog.On } @Override - public void onMetadataEntered(Uri uri, String title, String composer, int year, String genre) { - String path = saveImageInternally(uri); - MusicNote musicNote = new MusicNote(title, path, composer, year, genre); - - // Anfordern der dauerhaften Lese-/Schreibberechtigungen für die URI - + public void onMetadataEntered(Uri[] uris, String title, String composer, int year, String genre) { new Thread(() -> { - musicDB.musicNoteDao().insert(musicNote); + MusicNote musicNote = new MusicNote(title, composer, year, genre); + long musicNoteID = musicDB.musicNoteDao().insert(musicNote); + + for(Uri uri: uris) { + String filePath = saveImageInternally(uri); + NoteSheet noteSheet = new NoteSheet(musicNoteID, filePath); + musicDB.musicNoteDao().insertNoteSheet(noteSheet); + } }).start(); } diff --git a/app/src/main/java/core/notevault/data/MusicDatabase.java b/app/src/main/java/core/notevault/data/MusicDatabase.java index e4f9e63..b064937 100644 --- a/app/src/main/java/core/notevault/data/MusicDatabase.java +++ b/app/src/main/java/core/notevault/data/MusicDatabase.java @@ -5,7 +5,7 @@ import androidx.room.Room; import androidx.room.RoomDatabase; import androidx.room.Database; -@Database(entities = {MusicNote.class}, version = 1, exportSchema = false) +@Database(entities = {MusicNote.class, NoteSheet.class}, version = 1, exportSchema = false) public abstract class MusicDatabase extends RoomDatabase { public abstract MusicNoteDAO musicNoteDao(); diff --git a/app/src/main/java/core/notevault/data/MusicNote.java b/app/src/main/java/core/notevault/data/MusicNote.java index d9196c9..0f8212c 100644 --- a/app/src/main/java/core/notevault/data/MusicNote.java +++ b/app/src/main/java/core/notevault/data/MusicNote.java @@ -1,22 +1,22 @@ package core.notevault.data; import androidx.room.Entity; +import androidx.room.Ignore; import androidx.room.PrimaryKey; @Entity(tableName = "music_notes") public class MusicNote { @PrimaryKey(autoGenerate = true) - private int id; + private long musicNoteId; private String title; - private String filePath; private String composer; private int year; private String genre; - public MusicNote(String title, String filePath, String composer, int year, String genre) { + @Ignore + public MusicNote(String title, String composer, int year, String genre) { this.title = title; - this.filePath = filePath; this.composer = composer; this.year = year; this.genre = genre; @@ -25,12 +25,12 @@ public class MusicNote { public MusicNote() { } - public int getId() { - return id; + public long getMusicNoteId() { + return musicNoteId; } - public void setId(int id) { - this.id = id; + public void setMusicNoteId(long musicNoteId) { + this.musicNoteId = musicNoteId; } public String getTitle() { @@ -41,14 +41,6 @@ public class MusicNote { this.title = title; } - public String getFilePath() { - return filePath; - } - - public void setFilePath(String filePath) { - this.filePath = filePath; - } - public String getComposer() { return composer; } diff --git a/app/src/main/java/core/notevault/data/MusicNoteDAO.java b/app/src/main/java/core/notevault/data/MusicNoteDAO.java index 62a9626..63c9a0a 100644 --- a/app/src/main/java/core/notevault/data/MusicNoteDAO.java +++ b/app/src/main/java/core/notevault/data/MusicNoteDAO.java @@ -10,8 +10,14 @@ import java.util.List; public interface MusicNoteDAO { @Insert - void insert(MusicNote musicNote); + long insert(MusicNote musicNote); + + @Insert + void insertNoteSheet(NoteSheet noteSheet); @Query("SELECT * FROM music_notes") List getAllNotes(); + + @Query("SELECT * FROM note_sheets") + List getNoteSheetsForMusicSong(); } diff --git a/app/src/main/java/core/notevault/data/NoteSheet.java b/app/src/main/java/core/notevault/data/NoteSheet.java new file mode 100644 index 0000000..126d63e --- /dev/null +++ b/app/src/main/java/core/notevault/data/NoteSheet.java @@ -0,0 +1,46 @@ +package core.notevault.data; + +import androidx.room.Entity; +import androidx.room.PrimaryKey; + +@Entity(tableName = "note_sheets") +public class NoteSheet { + + @PrimaryKey(autoGenerate = true) + private int id; + + private long musicNoteId; + private String filePath; + + public NoteSheet(long musicNoteID, String filePath) { + this.musicNoteId = musicNoteID; + this.filePath = filePath; + } + + public NoteSheet() { + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public long getMusicNoteId() { + return musicNoteId; + } + + public void setMusicNoteId(long musicNoteId) { + this.musicNoteId = musicNoteId; + } + + public String getFilePath() { + return filePath; + } + + public void setFilePath(String filePath) { + this.filePath = filePath; + } +} diff --git a/app/src/main/java/core/notevault/ui/home/FullScreenImageActivity.java b/app/src/main/java/core/notevault/ui/home/FullScreenImageActivity.java index 9903cca..5d00fc2 100644 --- a/app/src/main/java/core/notevault/ui/home/FullScreenImageActivity.java +++ b/app/src/main/java/core/notevault/ui/home/FullScreenImageActivity.java @@ -5,7 +5,12 @@ import android.net.Uri; import android.os.Bundle; import android.widget.ImageView; import androidx.appcompat.app.AppCompatActivity; +import androidx.viewpager2.widget.ViewPager2; import core.notevault.R; +import core.notevault.ui.noteviewer.ImagePagerAdapter; + +import java.util.Arrays; +import java.util.List; public class FullScreenImageActivity extends AppCompatActivity { @@ -14,14 +19,17 @@ public class FullScreenImageActivity extends AppCompatActivity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fullscreen_image); // Erstelle eine Layout-Datei - ImageView imageView = findViewById(R.id.fullscreen_image_view); // Angenommen, du hast ein ImageView + ViewPager2 imageView = findViewById(R.id.viewPager); // Angenommen, du hast ein ImageView // Die URI aus dem Intent erhalten Intent intent = getIntent(); - String imageUriString = intent.getStringExtra("imageUri"); - if (imageUriString != null) { - Uri imageUri = Uri.parse(imageUriString); - imageView.setImageURI(imageUri); // Setze die URI in das ImageView + String[] imageUris = intent.getStringArrayExtra("imageUris"); + if (imageUris != null) { + List uriList = Arrays.asList(imageUris); + ImagePagerAdapter adapter = new ImagePagerAdapter(this, uriList); + imageView.setAdapter(adapter); // Setze die URI in das ImageView + } else { + throw new NullPointerException(); } } } diff --git a/app/src/main/java/core/notevault/ui/home/HomeFragment.java b/app/src/main/java/core/notevault/ui/home/HomeFragment.java index 0288be2..15f7cc9 100644 --- a/app/src/main/java/core/notevault/ui/home/HomeFragment.java +++ b/app/src/main/java/core/notevault/ui/home/HomeFragment.java @@ -76,21 +76,27 @@ public class HomeFragment extends Fragment { public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == PICK_FILE_REQUEST_CODE && resultCode == getActivity().RESULT_OK && data != null) { - Uri uri = data.getData(); - if (uri != null) { + if(data.getClipData() != null) { + int count = data.getClipData().getItemCount(); + Uri[] uris = new Uri[count]; + for(int i = 0; i < count; i++) { + uris[i] = data.getClipData().getItemAt(i).getUri(); + } + handleFile(uris); + } else if(data.getData() != null) { + Uri uri = data.getData(); handleFile(uri); } } } - private void handleFile(Uri uri) { + private void handleFile(Uri... uris) { // Hier kannst du die Logik zum Speichern oder Anzeigen der Datei implementieren MetaDataDialog metaDataDialog = new MetaDataDialog(); - metaDataDialog.setFileUri(uri); + metaDataDialog.setFileUri(uris); metaDataDialog.show(getParentFragmentManager(), MetaDataDialog.TAG); - homeViewModel.addNote(uri); // Speichere die URI im ViewModel - Toast.makeText(getActivity(), "Datei ausgewählt: " + uri.getPath(), Toast.LENGTH_SHORT).show(); + Toast.makeText(getActivity(), "Datei ausgewählt: " + uris[0].getPath(), Toast.LENGTH_SHORT).show(); } private void openFileChooser() { @@ -99,6 +105,7 @@ public class HomeFragment extends Fragment { String[] mimeTypes = {"application/pdf", "image/png", "image/jpeg"}; intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes); + intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); intent.addCategory(Intent.CATEGORY_OPENABLE); startActivityForResult(intent, PICK_FILE_REQUEST_CODE); diff --git a/app/src/main/java/core/notevault/ui/home/NoteSongAdapter.java b/app/src/main/java/core/notevault/ui/home/NoteSongAdapter.java index 4314f76..51b839c 100644 --- a/app/src/main/java/core/notevault/ui/home/NoteSongAdapter.java +++ b/app/src/main/java/core/notevault/ui/home/NoteSongAdapter.java @@ -1,6 +1,8 @@ package core.notevault.ui.home; import android.content.Intent; +import android.os.AsyncTask; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -8,7 +10,10 @@ import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import core.notevault.R; +import core.notevault.data.MusicDatabase; import core.notevault.data.MusicNote; +import core.notevault.data.MusicNoteDAO; +import core.notevault.data.NoteSheet; import java.util.ArrayList; import java.util.List; @@ -49,7 +54,7 @@ public class NoteSongAdapter extends RecyclerView.Adapter noteSheets) { + String[] noteSheetFiles = new String[noteSheets.size()]; + for (int i = 0; i < noteSheets.size(); i++) { + noteSheetFiles[i] = noteSheets.get(i).getFilePath(); + } + intent.putExtra("imageUris", noteSheetFiles); + itemView.getContext().startActivity(intent); + } + }).execute(); + } }); } @@ -71,5 +87,37 @@ public class NoteSongAdapter extends RecyclerView.Adapter noteSheets); + } + + private class LoadNoteSheetsTask extends AsyncTask> { + private final long musicNoteId; + private final MusicNoteDAO musicNoteDAO; + private final OnNoteSheedsLoadedListener listener; + + + + public LoadNoteSheetsTask(long musicNoteId, MusicNoteDAO musicNoteDAO, OnNoteSheedsLoadedListener listener) { + this.musicNoteId = musicNoteId; + this.musicNoteDAO = musicNoteDAO; + this.listener = listener; + } + + @Override + protected List doInBackground(Void... voids) { + List sheets = musicNoteDAO.getNoteSheetsForMusicSong(); + Log.d("LoadNoteSheetsTask", "Loaded NoteSheets: " + sheets.get(0)); + return sheets; + } + + @Override + protected void onPostExecute(List sheets) { + listener.onNoteSheetsLoaded(sheets); + } } } diff --git a/app/src/main/java/core/notevault/ui/metadatadialog/MetaDataDialog.java b/app/src/main/java/core/notevault/ui/metadatadialog/MetaDataDialog.java index 75eef02..784309f 100644 --- a/app/src/main/java/core/notevault/ui/metadatadialog/MetaDataDialog.java +++ b/app/src/main/java/core/notevault/ui/metadatadialog/MetaDataDialog.java @@ -16,10 +16,10 @@ import androidx.fragment.app.DialogFragment; import core.notevault.R; public class MetaDataDialog extends DialogFragment { - private Uri fileUri; + private Uri[] fileUri; public interface OnMetadataListener { - void onMetadataEntered(Uri uri, String title, String composer, int year, String genre); + void onMetadataEntered(Uri[] uri, String title, String composer, int year, String genre); } private OnMetadataListener listener; @@ -67,11 +67,11 @@ public class MetaDataDialog extends DialogFragment { } } - public Uri getFileUri() { + public Uri[] getFileUri() { return fileUri; } - public void setFileUri(Uri fileUri) { + public void setFileUri(Uri[] fileUri) { this.fileUri = fileUri; } @@ -79,9 +79,9 @@ public class MetaDataDialog extends DialogFragment { String fileName = ""; // Überprüfen, ob die Uri ein Content-Uri ist - if (this.fileUri.getScheme().equals("content")) { + if (this.fileUri[0].getScheme().equals("content")) { // ContentResolver verwenden, um die Datei zu finden - Cursor cursor = requireContext().getContentResolver().query(this.fileUri, null, null, null, null); + Cursor cursor = requireContext().getContentResolver().query(this.fileUri[0], null, null, null, null); if (cursor != null) { int nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); if (cursor.moveToFirst()) { @@ -90,9 +90,9 @@ public class MetaDataDialog extends DialogFragment { } cursor.close(); } - } else if (this.fileUri.getScheme().equals("file")) { + } else if (this.fileUri[0].getScheme().equals("file")) { // Bei einer Datei-Uri einfach den letzten Pfadsegment verwenden - fileName = this.fileUri.getLastPathSegment(); + fileName = this.fileUri[0].getLastPathSegment(); } if(fileName.contains(".")) { diff --git a/app/src/main/java/core/notevault/ui/noteviewer/ImagePagerAdapter.java b/app/src/main/java/core/notevault/ui/noteviewer/ImagePagerAdapter.java new file mode 100644 index 0000000..ade40ba --- /dev/null +++ b/app/src/main/java/core/notevault/ui/noteviewer/ImagePagerAdapter.java @@ -0,0 +1,50 @@ +package core.notevault.ui.noteviewer; + +import android.content.Context; +import android.net.Uri; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; +import com.github.chrisbanes.photoview.PhotoView; +import core.notevault.R; + +import java.util.List; + +public class ImagePagerAdapter extends RecyclerView.Adapter { + private final List imageUris; + private final Context context; + + public ImagePagerAdapter(Context context, List imageUris) { + this.context = context; + this.imageUris = imageUris; + } + + @NonNull + @Override + public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View view = LayoutInflater.from(context).inflate(R.layout.item_image, parent, false); + return new ImageViewHolder(view); + } + + @Override + public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) { + Uri uri = Uri.parse(imageUris.get(position)); + holder.photoView.setImageURI(uri); + } + + @Override + public int getItemCount() { + return imageUris.size(); + } + + static class ImageViewHolder extends RecyclerView.ViewHolder { + PhotoView photoView; + + ImageViewHolder(View itemView) { + super(itemView); + photoView = itemView.findViewById(R.id.photoView); + } + } +} diff --git a/app/src/main/res/layout/activity_fullscreen_image.xml b/app/src/main/res/layout/activity_fullscreen_image.xml index 7e9e79e..d771ad9 100644 --- a/app/src/main/res/layout/activity_fullscreen_image.xml +++ b/app/src/main/res/layout/activity_fullscreen_image.xml @@ -4,9 +4,8 @@ android:layout_height="match_parent"> - + android:layout_height="match_parent" /> diff --git a/app/src/main/res/layout/item_image.xml b/app/src/main/res/layout/item_image.xml new file mode 100644 index 0000000..1ccfcc7 --- /dev/null +++ b/app/src/main/res/layout/item_image.xml @@ -0,0 +1,11 @@ + + + + + diff --git a/music_database b/music_database new file mode 100644 index 0000000000000000000000000000000000000000..ae2b226c219e90db566709451d6b12fd5435c545 GIT binary patch literal 28672 zcmeI)!Ee$~90%~%Qp$8;Z#S~rb9i8Gb3o?;7bDJQAp%3jqC1sZ9?}SOqur9-oPUEi z{{a6BO^hcGizg4x#KeQ~WHd1`@#JgSOu;ScX~unDc)#@Z^|im>hj3`1tlcWv9%n75 z-!(m^kx3%UrSM(aI){5mqZG+v^H|Fdn?|HVjYqU+b&FcD{`ph0vVu@r@dKe2X z)pI;x-Ix?B+nO#07R?h5Q zl?O#H2j>j(G5o6B(55nUu0hg+gZ!e8ts!K6a`oa3=l!oqU8n~ux-KX>eMQw)yY zy^zX1-s>N(o4GBiiTQcS3eq=w&AwwdjV|}hrsgd~hc} z8qUZk5oJOE0uX=z1Rwwb2tWV=5P$##&Vqpd-MmD<5%EHU00bZa0SG_<0uX=z1Rwwb z2teTf5{OB1qREXLx_|$#(gLAx=rj75KA>C-&>#Q-2tWV=5P$##AOHafKmY;|7#5gT zRieko;wybF9-v?2JN^?F++{V#y3fRK?^)H^oQkSM6UP+GD=eGNE@sl%9Lr|3%wl>< zIdN(_@&{*@Pgt&9xhScm_?yI7HcmeineG$1Pu~p}h7klH009U<00Izz00bZa0SG_< z0{^A}l_E(g^p^%ImBo(G-#3hlyeGrC```bM)2D=fr(fwO`jNh;Z|Q6LioT>T#BMYQ zKmY;|fB*y_009U<00Izz00d55Kp9ua7*Wm#@mvtcf*1>;8pLQ2l^{lfC