Sort NoteSheets by Date

This commit is contained in:
sebastian 2024-11-01 09:19:57 +01:00
parent 288e06be7c
commit 8cb290ab19
4 changed files with 79 additions and 0 deletions

View File

@ -19,6 +19,7 @@ import core.notevault.data.MusicNote;
import core.notevault.data.NoteSheet; import core.notevault.data.NoteSheet;
import core.notevault.databinding.ActivityMainBinding; import core.notevault.databinding.ActivityMainBinding;
import core.notevault.ui.metadatadialog.MetaDataDialog; import core.notevault.ui.metadatadialog.MetaDataDialog;
import core.notevault.util.NoteSheetsUtil;
import java.io.*; import java.io.*;
import java.nio.file.Files; import java.nio.file.Files;

View File

@ -24,6 +24,7 @@ import core.notevault.data.MusicNote;
import core.notevault.data.MusicNoteDAO; 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 core.notevault.util.NoteSheetsUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -92,6 +93,8 @@ public class HomeFragment extends Fragment {
private void handleFile(Uri... uris) { private void handleFile(Uri... uris) {
// Hier kannst du die Logik zum Speichern oder Anzeigen der Datei implementieren // Hier kannst du die Logik zum Speichern oder Anzeigen der Datei implementieren
NoteSheetsUtil.sortNoteSheetsByTimestamp(getContext(), uris);
MetaDataDialog metaDataDialog = new MetaDataDialog(); MetaDataDialog metaDataDialog = new MetaDataDialog();
metaDataDialog.setFileUri(uris); metaDataDialog.setFileUri(uris);
metaDataDialog.show(getParentFragmentManager(), MetaDataDialog.TAG); metaDataDialog.show(getParentFragmentManager(), MetaDataDialog.TAG);

View File

@ -0,0 +1,54 @@
package core.notevault.util;
import android.content.Context;
import android.database.Cursor;
import android.media.ExifInterface;
import android.net.Uri;
import android.provider.MediaStore;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class NoteSheetsUtil {
private static long getImageTimestamp(Context context, Uri imageUri) {
try {
InputStream inputStream = context.getContentResolver().openInputStream(imageUri);
ExifInterface exif = new ExifInterface(inputStream);
String dateTime = exif.getAttribute(ExifInterface.TAG_DATETIME);
if(dateTime != null) {
SimpleDateFormat format = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss", Locale.getDefault());
Date date = format.parse(dateTime);
return date != null ? date.getTime() : 0;
} else {
File file = new File(imageUri.getPath());
return file.lastModified();
}
} catch (IOException | ParseException e) {
throw new RuntimeException(e);
}
}
public static void sortNoteSheetsByTimestamp(Context context, Uri[] uris) {
ArrayList<UriTimestamp> uriTimestamps = new ArrayList<>();
for(Uri uri : uris) {
long timestamp = getImageTimestamp(context, uri);
uriTimestamps.add(new UriTimestamp(uri, timestamp));
}
Collections.sort(uriTimestamps, Comparator.comparingLong(UriTimestamp::getTimestamp));
for(int i=0; i<uriTimestamps.size(); i++) {
uris[i] = uriTimestamps.get(i).getUri();
}
}
}

View File

@ -0,0 +1,21 @@
package core.notevault.util;
import android.net.Uri;
public class UriTimestamp {
private final Uri uri;
private final long timestamp;
public UriTimestamp(Uri uri, long timestamp) {
this.uri = uri;
this.timestamp = timestamp;
}
public Uri getUri() {
return uri;
}
public long getTimestamp() {
return timestamp;
}
}