diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 61c75bb..8707365 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -42,6 +42,7 @@ dependencies { implementation(libs.navigation.fragment) implementation(libs.navigation.ui) implementation(libs.room.common) + implementation("androidx.recyclerview:recyclerview:1.3.2") testImplementation(libs.junit) androidTestImplementation(libs.ext.junit) androidTestImplementation(libs.espresso.core) 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 b91234e..ce58354 100644 --- a/app/src/main/java/core/notevault/ui/home/HomeFragment.java +++ b/app/src/main/java/core/notevault/ui/home/HomeFragment.java @@ -2,6 +2,7 @@ package core.notevault.ui.home; import android.content.Intent; import android.net.Uri; +import android.os.AsyncTask; import android.os.Bundle; import android.provider.OpenableColumns; import android.util.Log; @@ -14,13 +15,23 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; import com.google.android.material.floatingactionbutton.FloatingActionButton; import core.notevault.R; +import core.notevault.data.MusicDatabase; +import core.notevault.data.MusicNote; +import core.notevault.data.MusicNoteDAO; import core.notevault.databinding.FragmentHomeBinding; import core.notevault.ui.metadatadialog.MetaDataDialog; +import java.util.ArrayList; +import java.util.List; + public class HomeFragment extends Fragment { + private NoteSongAdapter noteSongAdapter; + private static final int PICK_FILE_REQUEST_CODE = 1; private FragmentHomeBinding binding; private HomeViewModel homeViewModel; @@ -36,9 +47,37 @@ public class HomeFragment extends Fragment { FloatingActionButton importBtn = root.findViewById(R.id.importMusicNotesBtn); importBtn.setOnClickListener(v -> openFileChooser()); + RecyclerView recyclerView = root.findViewById(R.id.note_recycler_view); + noteSongAdapter = new NoteSongAdapter(); + recyclerView.setAdapter(noteSongAdapter); + recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); + + new LoadSongTitlesTask().execute(); + return root; } + private class LoadSongTitlesTask extends AsyncTask> { + + @Override + protected List doInBackground(Void... voids) { + MusicDatabase db = MusicDatabase.getDatabase(getContext()); + MusicNoteDAO musicNoteDAO = db.musicNoteDao(); + List songs = musicNoteDAO.getAllNotes(); + + List result = new ArrayList<>(); + for (MusicNote song : songs) { + result.add(song.getTitle()); + } + return result; + } + + @Override + protected void onPostExecute(List songTitles) { + noteSongAdapter.updateSongTitles(songTitles); // Aktualisiere den Adapter mit den Titeln + } + } + @Override public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); diff --git a/app/src/main/java/core/notevault/ui/home/HomeViewModel.java b/app/src/main/java/core/notevault/ui/home/HomeViewModel.java index 7bb2267..b87b25f 100644 --- a/app/src/main/java/core/notevault/ui/home/HomeViewModel.java +++ b/app/src/main/java/core/notevault/ui/home/HomeViewModel.java @@ -4,19 +4,27 @@ import android.net.Uri; import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.ViewModel; +import core.notevault.MainActivity; +import core.notevault.data.MusicDatabase; import core.notevault.ui.metadatadialog.MetaDataDialog; +import java.util.ArrayList; +import java.util.List; + public class HomeViewModel extends ViewModel { - private final MutableLiveData mText; + private final MutableLiveData> noteTitles; public HomeViewModel() { - mText = new MutableLiveData<>(); - mText.setValue("This is home fragment"); + noteTitles = new MutableLiveData<>(new ArrayList<>()); } - public LiveData getText() { - return mText; + public LiveData> getNoteTitles() { + return noteTitles; + } + + public void setNoteTitles(ArrayList noteTitles) { + this.noteTitles.setValue(noteTitles); } public void addNote(Uri uri) { diff --git a/app/src/main/java/core/notevault/ui/home/NoteSongAdapter.java b/app/src/main/java/core/notevault/ui/home/NoteSongAdapter.java new file mode 100644 index 0000000..5330b92 --- /dev/null +++ b/app/src/main/java/core/notevault/ui/home/NoteSongAdapter.java @@ -0,0 +1,58 @@ +package core.notevault.ui.home; + +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; +import core.notevault.R; + +import java.util.ArrayList; +import java.util.List; + +public class NoteSongAdapter extends RecyclerView.Adapter { + + private final List noteTitles; + + public NoteSongAdapter(List noteTitles) { + this.noteTitles = noteTitles; + } + + public NoteSongAdapter() { + this.noteTitles = new ArrayList<>(); + } + + @NonNull + @Override + public NoteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View view = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.item_song, parent, false); + return new NoteViewHolder(view); + } + + @Override + public void onBindViewHolder(@NonNull NoteViewHolder holder, int position) { + holder.titleTextView.setText(noteTitles.get(position)); + } + + @Override + public int getItemCount() { + return noteTitles.size(); + } + + public void updateSongTitles(List songTitles) { + this.noteTitles.clear(); + this.noteTitles.addAll(songTitles); + notifyDataSetChanged(); + } + + static class NoteViewHolder extends RecyclerView.ViewHolder { + TextView titleTextView; + + NoteViewHolder(View itemView) { + super(itemView); + titleTextView = itemView.findViewById(R.id.note_title); + } + } +} diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index fb49c97..cf4735b 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -7,19 +7,11 @@ android:layout_height="match_parent" tools:context=".ui.home.HomeFragment"> - + android:layout_height="match_parent" + android:layout_above="@+id/importMusicNotesBtn"/> + app:backgroundTint="@color/teal_200" + android:contentDescription="Add Music Notes"/> \ No newline at end of file diff --git a/app/src/main/res/layout/item_song.xml b/app/src/main/res/layout/item_song.xml new file mode 100644 index 0000000..4c373e2 --- /dev/null +++ b/app/src/main/res/layout/item_song.xml @@ -0,0 +1,13 @@ + + + + +