feat: implemented customizable home, allowing users to toggle visibility of elements and change their order

This commit is contained in:
CappielloAntonio 2024-03-23 21:33:11 +01:00
parent 309eca0764
commit 0e97eab744
12 changed files with 507 additions and 0 deletions

View file

@ -0,0 +1,77 @@
package com.cappielloantonio.tempo.viewmodel;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import com.cappielloantonio.tempo.R;
import com.cappielloantonio.tempo.model.HomeSector;
import com.cappielloantonio.tempo.util.Constants;
import com.cappielloantonio.tempo.util.Preferences;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
public class HomeRearrangementViewModel extends AndroidViewModel {
private List<HomeSector> sectors = new ArrayList<>();
public HomeRearrangementViewModel(@NonNull Application application) {
super(application);
}
public List<HomeSector> getHomeSectorList() {
if (sectors != null && !sectors.isEmpty()) return sectors;
if (Preferences.getHomeSectorList() != null && !Preferences.getHomeSectorList().equals("null")) {
sectors = new Gson().fromJson(
Preferences.getHomeSectorList(),
new TypeToken<List<HomeSector>>() {
}.getType()
);
} else {
sectors = fillStandardHomeSectorList();
}
return sectors;
}
public void orderSectorLiveListAfterSwap(List<HomeSector> sectors) {
this.sectors = sectors;
}
public void saveHomeSectorList(List<HomeSector> sectors) {
Preferences.setHomeSectorList(sectors);
}
public void resetHomeSectorList() {
Preferences.setHomeSectorList(null);
}
public void closeDialog() {
sectors = null;
}
private List<HomeSector> fillStandardHomeSectorList() {
List<HomeSector> sectors = new ArrayList<>();
sectors.add(new HomeSector(Constants.HOME_SECTOR_DISCOVERY, getApplication().getString(R.string.home_title_discovery), true, 1));
sectors.add(new HomeSector(Constants.HOME_SECTOR_MADE_FOR_YOU, getApplication().getString(R.string.home_title_made_for_you), true, 2));
sectors.add(new HomeSector(Constants.HOME_SECTOR_BEST_OF, getApplication().getString(R.string.home_title_best_of), true, 3));
sectors.add(new HomeSector(Constants.HOME_SECTOR_RADIO_STATION, getApplication().getString(R.string.home_title_radio_station), true, 4));
sectors.add(new HomeSector(Constants.HOME_SECTOR_TOP_SONGS, getApplication().getString(R.string.home_title_top_songs), true, 5));
sectors.add(new HomeSector(Constants.HOME_SECTOR_STARRED_TRACKS, getApplication().getString(R.string.home_title_starred_tracks), true, 6));
sectors.add(new HomeSector(Constants.HOME_SECTOR_STARRED_ALBUMS, getApplication().getString(R.string.home_title_starred_albums), true, 7));
sectors.add(new HomeSector(Constants.HOME_SECTOR_STARRED_ARTISTS, getApplication().getString(R.string.home_title_starred_artists), true, 8));
sectors.add(new HomeSector(Constants.HOME_SECTOR_NEW_RELEASES, getApplication().getString(R.string.home_title_new_releases), true, 9));
sectors.add(new HomeSector(Constants.HOME_SECTOR_FLASHBACK, getApplication().getString(R.string.home_title_flashback), true, 10));
sectors.add(new HomeSector(Constants.HOME_SECTOR_MOST_PLAYED, getApplication().getString(R.string.home_title_most_played), true, 11));
sectors.add(new HomeSector(Constants.HOME_SECTOR_LAST_PLAYED, getApplication().getString(R.string.home_title_last_played), true, 12));
sectors.add(new HomeSector(Constants.HOME_SECTOR_RECENTLY_ADDED, getApplication().getString(R.string.home_title_recently_added), true, 13));
sectors.add(new HomeSector(Constants.HOME_SECTOR_SHARED, getApplication().getString(R.string.home_title_shares), true, 14));
return sectors;
}
}

View file

@ -11,6 +11,7 @@ import androidx.lifecycle.MutableLiveData;
import com.cappielloantonio.tempo.interfaces.StarCallback;
import com.cappielloantonio.tempo.model.Chronology;
import com.cappielloantonio.tempo.model.Favorite;
import com.cappielloantonio.tempo.model.HomeSector;
import com.cappielloantonio.tempo.repository.AlbumRepository;
import com.cappielloantonio.tempo.repository.ArtistRepository;
import com.cappielloantonio.tempo.repository.ChronologyRepository;
@ -22,6 +23,8 @@ import com.cappielloantonio.tempo.subsonic.models.ArtistID3;
import com.cappielloantonio.tempo.subsonic.models.Child;
import com.cappielloantonio.tempo.subsonic.models.Share;
import com.cappielloantonio.tempo.util.Preferences;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.Calendar;
@ -59,10 +62,13 @@ public class HomeViewModel extends AndroidViewModel {
private final MutableLiveData<List<Child>> artistBestOf = new MutableLiveData<>(null);
private final MutableLiveData<List<Share>> shares = new MutableLiveData<>(null);
private List<HomeSector> sectors;
public HomeViewModel(@NonNull Application application) {
super(application);
setHomeSectorList();
songRepository = new SongRepository();
albumRepository = new AlbumRepository();
artistRepository = new ArtistRepository();
@ -266,6 +272,26 @@ public class HomeViewModel extends AndroidViewModel {
sharingRepository.getShares().observe(owner, this.shares::postValue);
}
private void setHomeSectorList() {
if (Preferences.getHomeSectorList() != null && !Preferences.getHomeSectorList().equals("null")) {
sectors = new Gson().fromJson(
Preferences.getHomeSectorList(),
new TypeToken<List<HomeSector>>() {
}.getType()
);
}
}
public List<HomeSector> getHomeSectorList() {
return sectors;
}
public boolean checkHomeSectorVisibility(String sectorId) {
return sectors != null && sectors.stream().filter(sector -> sector.getId().equals(sectorId))
.findAny()
.orElse(null) != null;
}
public void setOfflineFavorite() {
ArrayList<Favorite> favorites = getFavorites();
ArrayList<Favorite> favoritesToSave = getFavoritesToSave(favorites);