List Inserted Notes

This commit is contained in:
sebastian 2024-10-31 19:25:10 +01:00
parent f90919fb77
commit d3b94fdcb3
6 changed files with 130 additions and 18 deletions

View File

@ -42,6 +42,7 @@ dependencies {
implementation(libs.navigation.fragment) implementation(libs.navigation.fragment)
implementation(libs.navigation.ui) implementation(libs.navigation.ui)
implementation(libs.room.common) implementation(libs.room.common)
implementation("androidx.recyclerview:recyclerview:1.3.2")
testImplementation(libs.junit) testImplementation(libs.junit)
androidTestImplementation(libs.ext.junit) androidTestImplementation(libs.ext.junit)
androidTestImplementation(libs.espresso.core) androidTestImplementation(libs.espresso.core)

View File

@ -2,6 +2,7 @@ package core.notevault.ui.home;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.provider.OpenableColumns; import android.provider.OpenableColumns;
import android.util.Log; import android.util.Log;
@ -14,13 +15,23 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.floatingactionbutton.FloatingActionButton;
import core.notevault.R; 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.databinding.FragmentHomeBinding;
import core.notevault.ui.metadatadialog.MetaDataDialog; import core.notevault.ui.metadatadialog.MetaDataDialog;
import java.util.ArrayList;
import java.util.List;
public class HomeFragment extends Fragment { public class HomeFragment extends Fragment {
private NoteSongAdapter noteSongAdapter;
private static final int PICK_FILE_REQUEST_CODE = 1; private static final int PICK_FILE_REQUEST_CODE = 1;
private FragmentHomeBinding binding; private FragmentHomeBinding binding;
private HomeViewModel homeViewModel; private HomeViewModel homeViewModel;
@ -36,9 +47,37 @@ public class HomeFragment extends Fragment {
FloatingActionButton importBtn = root.findViewById(R.id.importMusicNotesBtn); FloatingActionButton importBtn = root.findViewById(R.id.importMusicNotesBtn);
importBtn.setOnClickListener(v -> openFileChooser()); 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; return root;
} }
private class LoadSongTitlesTask extends AsyncTask<Void, Void, List<String>> {
@Override
protected List<String> doInBackground(Void... voids) {
MusicDatabase db = MusicDatabase.getDatabase(getContext());
MusicNoteDAO musicNoteDAO = db.musicNoteDao();
List<MusicNote> songs = musicNoteDAO.getAllNotes();
List<String> result = new ArrayList<>();
for (MusicNote song : songs) {
result.add(song.getTitle());
}
return result;
}
@Override
protected void onPostExecute(List<String> songTitles) {
noteSongAdapter.updateSongTitles(songTitles); // Aktualisiere den Adapter mit den Titeln
}
}
@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);

View File

@ -4,19 +4,27 @@ import android.net.Uri;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel; import androidx.lifecycle.ViewModel;
import core.notevault.MainActivity;
import core.notevault.data.MusicDatabase;
import core.notevault.ui.metadatadialog.MetaDataDialog; import core.notevault.ui.metadatadialog.MetaDataDialog;
import java.util.ArrayList;
import java.util.List;
public class HomeViewModel extends ViewModel { public class HomeViewModel extends ViewModel {
private final MutableLiveData<String> mText; private final MutableLiveData<List<String>> noteTitles;
public HomeViewModel() { public HomeViewModel() {
mText = new MutableLiveData<>(); noteTitles = new MutableLiveData<>(new ArrayList<>());
mText.setValue("This is home fragment");
} }
public LiveData<String> getText() { public LiveData<List<String>> getNoteTitles() {
return mText; return noteTitles;
}
public void setNoteTitles(ArrayList<String> noteTitles) {
this.noteTitles.setValue(noteTitles);
} }
public void addNote(Uri uri) { public void addNote(Uri uri) {

View File

@ -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<NoteSongAdapter.NoteViewHolder> {
private final List<String> noteTitles;
public NoteSongAdapter(List<String> 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<String> 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);
}
}
}

View File

@ -7,19 +7,11 @@
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".ui.home.HomeFragment"> tools:context=".ui.home.HomeFragment">
<TextView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/text_home" android:id="@+id/note_recycler_view"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_marginStart="8dp" android:layout_above="@+id/importMusicNotesBtn"/>
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton <com.google.android.material.floatingactionbutton.FloatingActionButton
android:src="@drawable/add_24dp_e8eaed_fill0_wght400_grad0_opsz24" android:src="@drawable/add_24dp_e8eaed_fill0_wght400_grad0_opsz24"
@ -31,5 +23,6 @@
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp" android:layout_marginBottom="16dp"
app:backgroundTint="@color/teal_200"/> app:backgroundTint="@color/teal_200"
android:contentDescription="Add Music Notes"/>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/note_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp" />
</LinearLayout>