UI improvements

This commit is contained in:
ari melody 2024-09-12 20:10:31 +01:00
parent 163b285ce5
commit f5e50cf481
Signed by: ari
GPG key ID: CF99829C92678188
4 changed files with 107 additions and 50 deletions

View file

@ -65,7 +65,7 @@ public class MainActivity extends AppCompatActivity {
Thread thread = new Thread(() -> { Thread thread = new Thread(() -> {
try { try {
URL url = URI.create(BASE_URL + "/api/v1/music").toURL(); URL url = URI.create(BASE_URL + "/api/v1/music").toURL();
System.out.printf("Attempting to fetch %s...\n", url); System.out.printf("Fetching %s...\n", url);
HttpURLConnection http = (HttpURLConnection) url.openConnection(); HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("GET"); http.setRequestMethod("GET");
http.setRequestProperty("User-Agent", "aridroid"); http.setRequestProperty("User-Agent", "aridroid");
@ -79,14 +79,13 @@ public class MainActivity extends AppCompatActivity {
} }
updateReleaseList(stream.toString(), items); updateReleaseList(stream.toString(), items);
} catch (Exception e) { } catch (Exception e) {
System.err.printf("FATAL: Failed to fetch music data: %s\n", e.toString()); System.err.printf("Failed to fetch music data: %s\n", e.toString());
} }
}); });
thread.start(); thread.start();
} }
void updateReleaseList(String res, ArrayList<MusicItemModel> items) { void updateReleaseList(String res, ArrayList<MusicItemModel> items) {
System.out.println(res);
try { try {
JSONArray json = new JSONArray(res); JSONArray json = new JSONArray(res);
items.clear(); items.clear();
@ -110,7 +109,7 @@ public class MainActivity extends AppCompatActivity {
try { try {
date = df.parse(obj.getString("releaseDate")); date = df.parse(obj.getString("releaseDate"));
} catch (ParseException e) { } catch (ParseException e) {
System.err.printf("FATAL: Failed to parse date for release %s: %s\n", id, e.toString()); System.err.printf("Failed to parse date for release %s: %s\n", id, e.toString());
continue; continue;
} }
@ -133,7 +132,7 @@ public class MainActivity extends AppCompatActivity {
}); });
} }
} catch (JSONException e) { } catch (JSONException e) {
System.err.printf("FATAL: Failed to parse JSON response: %s\n", e.toString()); System.err.printf("Failed to parse JSON response: %s\n", e.toString());
} }
} }
} }

View file

@ -10,6 +10,7 @@ import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import java.io.InputStream; import java.io.InputStream;
@ -17,15 +18,26 @@ import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
public class MusicListAdapter extends RecyclerView.Adapter<MusicListAdapter.MyViewHolder> { public class MusicListAdapter extends RecyclerView.Adapter<MusicListAdapter.MyViewHolder> {
Context context; Context context;
ArrayList<MusicItemModel> data; ArrayList<MusicItemModel> data;
public HashMap<String, Integer> ReleaseTypes = new HashMap();
public MusicListAdapter(Context context, ArrayList<MusicItemModel> data) { public MusicListAdapter(Context context, ArrayList<MusicItemModel> data) {
this.context = context; this.context = context;
this.data = data; this.data = data;
ReleaseTypes.put("single", R.string.release_type_single);
ReleaseTypes.put("album", R.string.release_type_album);
ReleaseTypes.put("ep", R.string.release_type_ep);
ReleaseTypes.put("lp", R.string.release_type_lp);
ReleaseTypes.put("compilation", R.string.release_type_compilation);
ReleaseTypes.put("upcoming", R.string.release_type_upcoming);
} }
@NonNull @NonNull
@ -45,9 +57,9 @@ public class MusicListAdapter extends RecyclerView.Adapter<MusicListAdapter.MyVi
holder.title.setText(release.getTitle()); holder.title.setText(release.getTitle());
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance(Locale.GERMANY);
calendar.setTime(release.getReleaseDate()); calendar.setTime(release.getReleaseDate());
holder.year.setText(calendar.getDisplayName(Calendar.YEAR, Calendar.LONG_FORMAT, Locale.ENGLISH)); holder.year.setText(String.valueOf(calendar.get(Calendar.YEAR)));
StringBuilder artist = new StringBuilder(); StringBuilder artist = new StringBuilder();
ArrayList<MusicCreditModel> primaryCredits = new ArrayList<>(); ArrayList<MusicCreditModel> primaryCredits = new ArrayList<>();
@ -67,6 +79,9 @@ public class MusicListAdapter extends RecyclerView.Adapter<MusicListAdapter.MyVi
artist.append(primaryCredits.get(i).getName()).append(", "); artist.append(primaryCredits.get(i).getName()).append(", ");
} }
holder.artist.setText(artist.toString()); holder.artist.setText(artist.toString());
// i can't make this linting error go away, please ignore it
holder.type.setText(ReleaseTypes.getOrDefault(release.getType(), R.string.release_type_unknown));
} }
@Override @Override
@ -79,6 +94,7 @@ public class MusicListAdapter extends RecyclerView.Adapter<MusicListAdapter.MyVi
TextView title; TextView title;
TextView year; TextView year;
TextView artist; TextView artist;
TextView type;
public MyViewHolder(@NonNull View itemView) { public MyViewHolder(@NonNull View itemView) {
super(itemView); super(itemView);
@ -87,13 +103,14 @@ public class MusicListAdapter extends RecyclerView.Adapter<MusicListAdapter.MyVi
title = itemView.findViewById(R.id.musicTitle); title = itemView.findViewById(R.id.musicTitle);
year = itemView.findViewById(R.id.musicYear); year = itemView.findViewById(R.id.musicYear);
artist = itemView.findViewById(R.id.musicArtist); artist = itemView.findViewById(R.id.musicArtist);
type = itemView.findViewById(R.id.musicType);
} }
void fetchReleaseArtwork(String artworkURL) { void fetchReleaseArtwork(String artworkURL) {
Thread thread = new Thread(() -> { Thread thread = new Thread(() -> {
try { try {
URL url = new URL(MainActivity.BASE_URL + artworkURL); URL url = new URL(MainActivity.BASE_URL + artworkURL);
System.out.printf("Attempting to fetch %s...\n", url); System.out.printf("Fetching %s...\n", url);
HttpURLConnection http = (HttpURLConnection) url.openConnection(); HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("GET"); http.setRequestMethod("GET");
http.setRequestProperty("User-Agent", "aridroid"); http.setRequestProperty("User-Agent", "aridroid");
@ -103,9 +120,9 @@ public class MusicListAdapter extends RecyclerView.Adapter<MusicListAdapter.MyVi
Bitmap img = BitmapFactory.decodeStream(stream); Bitmap img = BitmapFactory.decodeStream(stream);
// TODO: image caching // TODO: image caching
// https://developer.android.com/topic/performance/graphics/cache-bitmap#java // https://developer.android.com/topic/performance/graphics/cache-bitmap#java
artwork.setImageBitmap(img); artwork.post(() -> artwork.setImageBitmap(img));
} catch (Exception e) { } catch (Exception e) {
System.err.printf("FATAL: Failed to fetch music data: %s\n", e.toString()); System.err.printf("FATAL: Failed to fetch release artwork: %s\n", e.toString());
} }
}); });
thread.start(); thread.start();

View file

@ -1,48 +1,72 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/musicItem" android:id="@+id/musicItem"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@color/material_dynamic_neutral100" android:layout_margin="10dp"
app:cardBackgroundColor="@color/material_dynamic_neutral100"
android:padding="10dp"> android:padding="10dp">
<ImageView <androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/musicArtwork" android:layout_width="match_parent"
android:layout_width="64dp" android:layout_height="match_parent"
android:layout_height="64dp" android:layout_margin="10dp">
android:contentDescription="@string/music_artwork"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/avatars" />
<TextView <ImageView
android:id="@+id/musicTitle" android:id="@+id/musicArtwork"
android:layout_width="wrap_content" android:layout_width="64dp"
android:layout_height="wrap_content" android:layout_height="64dp"
android:layout_marginStart="10dp" android:contentDescription="@string/music_artwork_description"
app:layout_constraintTop_toTopOf="@id/musicArtwork" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintLeft_toRightOf="@id/musicArtwork" app:layout_constraintTop_toTopOf="parent"
android:text="@string/untitled_track" tools:srcCompat="@tools:sample/avatars" />
android:textStyle="bold" />
<TextView <androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/musicYear" android:layout_width="0dp"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:layout_marginStart="10dp"
android:text="@string/default_year" app:layout_constraintBottom_toBottomOf="@id/musicArtwork"
android:textStyle="bold|italic" app:layout_constraintLeft_toRightOf="@id/musicArtwork"
android:alpha=".5" app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toRightOf="@id/musicTitle" app:layout_constraintTop_toTopOf="@id/musicArtwork">
app:layout_constraintTop_toTopOf="@id/musicTitle"
android:layout_marginStart="5dp" />
<TextView <TextView
android:id="@+id/musicArtist" android:id="@+id/musicTitle"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="@id/musicTitle" android:text="@string/default_release_title"
app:layout_constraintTop_toBottomOf="@id/musicTitle" android:textStyle="bold"
android:text="@string/unknown_artist" /> app:layout_constraintLeft_toLeftOf="parent"
</androidx.constraintlayout.widget.ConstraintLayout> app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/musicYear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:alpha=".6"
android:text="@string/default_year"
android:textStyle="bold|italic"
app:layout_constraintLeft_toRightOf="@id/musicTitle"
app:layout_constraintTop_toTopOf="@id/musicTitle" />
<TextView
android:id="@+id/musicArtist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/default_artist"
app:layout_constraintLeft_toLeftOf="@id/musicTitle"
app:layout_constraintTop_toBottomOf="@id/musicTitle" />
<TextView
android:id="@+id/musicType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/default_release_type"
app:layout_constraintLeft_toLeftOf="@id/musicArtist"
app:layout_constraintTop_toBottomOf="@id/musicArtist" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

View file

@ -1,8 +1,25 @@
<resources> <resources>
<string name="base_url">https://arimelody.me</string> <string name="base_url">https://arimelody.me</string>
<string name="app_name">AriDroid</string> <string name="app_name">AriDroid</string>
<string name="untitled_track">Untitled Track</string>
<string name="unknown_artist">Unknown Artist</string> <string name="music_artwork_description">%s artwork</string>
<string name="default_release_title">Untitled Release</string>
<string name="default_track_title">Untitled Track</string>
<string name="default_artist">Unknown Artist</string>
<string name="default_year">2002</string> <string name="default_year">2002</string>
<string name="music_artwork">Music artwork</string> <string name="default_release_type">Release</string>
<string name="default_release_track_count">0 tracks</string>
<string name="release_type_unknown">Unknown</string>
<string name="release_type_single">Single</string>
<string name="release_type_album">Album</string>
<string name="release_type_ep">EP</string>
<string name="release_type_lp">LP</string>
<string name="release_type_compilation">Compilation</string>
<string name="release_type_upcoming">Upcoming</string>
<plurals name="release_track_count">
<item quantity="one">(%d track)</item>
<item quantity="other">(%d tracks)</item>
</plurals>
</resources> </resources>