Commit 0e4fcb70 authored by Geoffrey Métais's avatar Geoffrey Métais

Pass app context to viewmodels

parent 3afe8ffa
......@@ -24,10 +24,8 @@ import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.ProcessLifecycleOwner;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import android.support.v4.app.DialogFragment;
import android.support.v4.util.SimpleArrayMap;
import android.util.Log;
......@@ -41,6 +39,7 @@ import org.videolan.vlc.gui.helpers.AudioUtil;
import org.videolan.vlc.gui.helpers.BitmapCache;
import org.videolan.vlc.gui.helpers.NotificationHelper;
import org.videolan.vlc.util.AndroidDevices;
import org.videolan.vlc.util.Settings;
import org.videolan.vlc.util.Strings;
import org.videolan.vlc.util.Util;
import org.videolan.vlc.util.VLCInstance;
......@@ -48,7 +47,6 @@ import org.videolan.vlc.util.WorkersKt;
import java.lang.ref.WeakReference;
import java.util.Calendar;
import java.util.Locale;
import static org.videolan.vlc.gui.helpers.UiTools.setLocale;
......@@ -62,7 +60,6 @@ public class VLCApplication extends Application {
public static Calendar sPlayerSleepTime = null;
private static boolean sTV;
private static SharedPreferences sSettings;
private static SimpleArrayMap<String, WeakReference<Object>> sDataMap = new SimpleArrayMap<>();
......@@ -79,9 +76,8 @@ public class VLCApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
sSettings = PreferenceManager.getDefaultSharedPreferences(this);
sTV = AndroidDevices.isAndroidTv || (!AndroidDevices.isChromeBook && !AndroidDevices.hasTsp);
locale = sSettings.getString("set_locale", "");
locale = Settings.INSTANCE.getInstance(this).getString("set_locale", "");
// Set the locale for API < 24 and set application resources and direction for API >=24
setLocale(instance);
......@@ -140,16 +136,12 @@ public class VLCApplication extends Application {
return instance.getResources();
}
public static SharedPreferences getSettings() {
return sSettings;
}
public static String getLocale(){
return locale;
}
public static boolean showTvUi() {
return sTV || (sSettings != null && sSettings.getBoolean("tv_ui", false));
return sTV || (Settings.INSTANCE.getInstance(instance).getBoolean("tv_ui", false));
}
public static void storeData(String key, Object data) {
......
......@@ -74,7 +74,7 @@ public class HistoryFragment extends MediaBrowserFragment<HistoryModel> implemen
super.onViewCreated(view, savedInstanceState);
mEmptyView = view.findViewById(android.R.id.empty);
mRecyclerView = view.findViewById(android.R.id.list);
viewModel = ViewModelProviders.of(requireActivity()).get(HistoryModel.class);
viewModel = ViewModelProviders.of(requireActivity(), new HistoryModel.Factory(requireContext())).get(HistoryModel.class);
viewModel.getDataset().observe(this, new Observer<List<MediaWrapper>>() {
@Override
public void onChanged(@Nullable List<MediaWrapper> mediaWrappers) {
......
......@@ -109,7 +109,7 @@ public class PlaylistActivity extends AudioPlayerContainerActivity implements IE
mBinding.songs.setLayoutManager(new LinearLayoutManager(this));
mBinding.songs.setAdapter(mAdapter);
tracksModel = ViewModelProviders.of(this, new TracksModel.Factory(mPlaylist)).get(TracksModel.class);
tracksModel = ViewModelProviders.of(this, new TracksModel.Factory(this, mPlaylist)).get(TracksModel.class);
tracksModel.getDataset().observe(this, new Observer<List<MediaLibraryItem>>() {
@Override
public void onChanged(@Nullable List<MediaLibraryItem> tracks) {
......
......@@ -90,8 +90,8 @@ public class AudioAlbumsSongsFragment extends BaseAudioBrowser implements SwipeR
mItem = (MediaLibraryItem) (savedInstanceState != null ?
savedInstanceState.getParcelable(AudioBrowserFragment.TAG_ITEM) :
getArguments().getParcelable(AudioBrowserFragment.TAG_ITEM));
albumModel = ViewModelProviders.of(this, new AlbumModel.Factory(mItem)).get(AlbumModel.class);
tracksModel = ViewModelProviders.of(this, new TracksModel.Factory(mItem)).get(TracksModel.class);
albumModel = ViewModelProviders.of(this, new AlbumModel.Factory(requireContext(), mItem)).get(AlbumModel.class);
tracksModel = ViewModelProviders.of(this, new TracksModel.Factory(requireContext(), mItem)).get(TracksModel.class);
audioModels = new AudioModel[] {albumModel, tracksModel};
}
......
......@@ -150,23 +150,23 @@ public class AudioBrowserFragment extends BaseAudioBrowser implements SwipeRefre
private void setupAdapters(final int currentTab) {
if (mArtistsAdapter == null) {
mArtistsAdapter = new AudioBrowserAdapter(MediaLibraryItem.TYPE_ARTIST, this);
artistModel = ViewModelProviders.of(requireActivity(), new ArtistModel.Factory(mSettings.getBoolean(Constants.KEY_ARTISTS_SHOW_ALL, false))).get(ArtistModel.class);
artistModel = ViewModelProviders.of(requireActivity(), new ArtistModel.Factory(requireContext(), mSettings.getBoolean(Constants.KEY_ARTISTS_SHOW_ALL, false))).get(ArtistModel.class);
}
if (mAlbumsAdapter == null) {
mAlbumsAdapter = new AudioBrowserAdapter(MediaLibraryItem.TYPE_ALBUM, this);
albumModel = ViewModelProviders.of(requireActivity()).get(AlbumModel.class);
albumModel = ViewModelProviders.of(requireActivity(), new AlbumModel.Factory(requireContext(), null)).get(AlbumModel.class);
}
if (mSongsAdapter == null) {
mSongsAdapter = new AudioBrowserAdapter(MediaLibraryItem.TYPE_MEDIA, this);
tracksModel = ViewModelProviders.of(requireActivity()).get(TracksModel.class);
tracksModel = ViewModelProviders.of(requireActivity(), new TracksModel.Factory(requireContext(), null)).get(TracksModel.class);
}
if (mGenresAdapter == null) {
mGenresAdapter = new AudioBrowserAdapter(MediaLibraryItem.TYPE_GENRE, this);
genresModel = ViewModelProviders.of(requireActivity()).get(GenresModel.class);
genresModel = ViewModelProviders.of(requireActivity(), new GenresModel.Factory(requireContext())).get(GenresModel.class);
}
if (mPlaylistAdapter == null) {
mPlaylistAdapter = new AudioBrowserAdapter(MediaLibraryItem.TYPE_PLAYLIST, this);
playlistsModel = ViewModelProviders.of(requireActivity()).get(PlaylistsModel.class);
playlistsModel = ViewModelProviders.of(requireActivity(), new PlaylistsModel.Factory(requireContext())).get(PlaylistsModel.class);
}
mAdapters = new AudioBrowserAdapter[] {mArtistsAdapter, mAlbumsAdapter, mSongsAdapter, mGenresAdapter, mPlaylistAdapter};
mProvidersList = new AudioModel[] {artistModel, albumModel, tracksModel, genresModel, playlistsModel};
......
......@@ -44,7 +44,6 @@ import org.videolan.vlc.BuildConfig
import org.videolan.vlc.ExternalMonitor
import org.videolan.vlc.R
import org.videolan.vlc.RecommendationsService
import org.videolan.vlc.database.MediaDatabase
import org.videolan.vlc.gui.preferences.PreferencesFragment
import org.videolan.vlc.gui.tv.MainTvActivity.ACTIVITY_RESULT_PREFERENCES
import org.videolan.vlc.gui.tv.MainTvActivity.BROWSER_TYPE
......@@ -152,7 +151,7 @@ class MainTvFragment : BrowseSupportFragment(), OnItemViewSelectedListener, OnIt
rowsAdapter.add(miscRow)
adapter = rowsAdapter
videoModel = VideosModel.get(this, null, 0, Medialibrary.SORT_INSERTIONDATE)
videoModel = VideosModel.get(requireContext(), this, null, 0, Medialibrary.SORT_INSERTIONDATE)
videoModel.dataset.observe(this, Observer {
updateVideos(it)
(requireActivity() as MainTvActivity).hideLoading()
......@@ -258,7 +257,7 @@ class MainTvFragment : BrowseSupportFragment(), OnItemViewSelectedListener, OnIt
val historyEnabled = settings.getBoolean(PreferencesFragment.PLAYBACK_HISTORY, true)
if (historyEnabled == displayHistory) return
if (historyEnabled) {
historyModel = ViewModelProviders.of(this).get(HistoryModel::class.java)
historyModel = ViewModelProviders.of(this, HistoryModel.Factory(requireContext())).get(HistoryModel::class.java)
historyModel.dataset.observe(this, this)
} else {
displayHistory = false
......
......@@ -11,7 +11,7 @@ class AlbumsFragment : MediaLibBrowserFragment<AlbumModel>() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
title = currentItem?.title ?: getString(R.string.albums)
model = ViewModelProviders.of(this, AlbumModel.Factory(currentItem)).get(AlbumModel::class.java)
model = ViewModelProviders.of(this, AlbumModel.Factory(requireContext(), currentItem)).get(AlbumModel::class.java)
model.dataset.observe(this, Observer { update(it!!) })
}
}
\ No newline at end of file
......@@ -11,7 +11,7 @@ class ArtistsFragment : MediaLibBrowserFragment<ArtistModel>() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
title = currentItem?.title ?: getString(R.string.artists)
model = ViewModelProviders.of(this, ArtistModel.Factory(false)).get(ArtistModel::class.java)
model = ViewModelProviders.of(this, ArtistModel.Factory(requireContext(), false)).get(ArtistModel::class.java)
model.dataset.observe(this, Observer { update(it!!) })
}
}
\ No newline at end of file
package org.videolan.vlc.gui.tv.browser
import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.os.Bundle
import org.videolan.vlc.R
import org.videolan.vlc.util.getModel
import org.videolan.vlc.viewmodels.audio.GenresModel
......@@ -11,7 +11,7 @@ class GenresFragment : MediaLibBrowserFragment<GenresModel>() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
title = currentItem?.title ?: getString(R.string.genres)
model = getModel()
model = ViewModelProviders.of(this, GenresModel.Factory(requireContext())).get(GenresModel::class.java)
model.dataset.observe(this, Observer { update(it!!) })
}
}
\ No newline at end of file
......@@ -34,7 +34,7 @@ import org.videolan.vlc.viewmodels.audio.TracksModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
title = currentItem?.title ?: getString(R.string.tracks)
model = ViewModelProviders.of(this, TracksModel.Factory(currentItem)).get(TracksModel::class.java)
model = ViewModelProviders.of(this, TracksModel.Factory(requireContext(), currentItem)).get(TracksModel::class.java)
model.dataset.observe(this, Observer { update(it!!) })
}
}
\ No newline at end of file
......@@ -52,7 +52,7 @@ public class SongsBrowserFragment extends CategoriesFragment<TracksModel> {
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewModel = ViewModelProviders.of(this).get(TracksModel.class);
viewModel = ViewModelProviders.of(this, new TracksModel.Factory(requireContext(), null)).get(TracksModel.class);
viewModel.getCategories().observe(this, new Observer<Map<String, List<MediaLibraryItem>>>() {
@Override
public void onChanged(@Nullable Map<String, List<MediaLibraryItem>> stringListMap) {
......
......@@ -36,7 +36,7 @@ class VideoBrowserFragment : CategoriesFragment<VideosModel>() {
override fun onCreate(savedInstanceState: Bundle?) {
val minGroupLengthValue = Integer.valueOf(preferences.getString("video_min_group_length", "6"))
viewModel = VideosModel.get(this, arguments?.getString(Constants.KEY_GROUP), minGroupLengthValue, Medialibrary.SORT_DEFAULT)
viewModel = VideosModel.get(requireContext(), this, arguments?.getString(Constants.KEY_GROUP), minGroupLengthValue, Medialibrary.SORT_DEFAULT)
viewModel.categories.observe(this, Observer { update(it) })
super.onCreate(savedInstanceState)
}
......
......@@ -15,7 +15,7 @@ class VideosFragment : MediaLibBrowserFragment<VideosModel>() {
val minGroupLengthValue = Integer.valueOf(preferences.getString("video_min_group_length", "6"))
val group = arguments?.getString(Constants.KEY_GROUP)
title = group ?: getString(R.string.videos)
model = VideosModel.get(this, group, minGroupLengthValue, Medialibrary.SORT_DEFAULT)
model = VideosModel.get(requireContext(), this, group, minGroupLengthValue, Medialibrary.SORT_DEFAULT)
model.dataset.observe(this, Observer { update(it!!) })
}
......
......@@ -85,7 +85,7 @@ public class VideoGridFragment extends MediaBrowserFragment<VideosModel> impleme
final boolean seenMarkVisible = preferences.getBoolean("media_seen", true);
mAdapter = new VideoListAdapter(this, seenMarkVisible);
final int minGroupLengthValue = Integer.valueOf(preferences.getString("video_min_group_length", "6"));
viewModel = ViewModelProviders.of(requireActivity(), new VideosModel.Factory(mGroup, minGroupLengthValue, Medialibrary.SORT_DEFAULT)).get(VideosModel.class);
viewModel = ViewModelProviders.of(requireActivity(), new VideosModel.Factory(requireContext(), mGroup, minGroupLengthValue, Medialibrary.SORT_DEFAULT)).get(VideosModel.class);
viewModel.getDataset().observe(this, this);
}
if (savedInstanceState != null) setGroup(savedInstanceState.getString(Constants.KEY_GROUP));
......
......@@ -22,6 +22,7 @@ package org.videolan.vlc.viewmodels
import android.arch.lifecycle.MediatorLiveData
import android.arch.lifecycle.ViewModel
import android.content.Context
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.channels.Channel
......@@ -33,7 +34,7 @@ import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.vlc.util.*
private const val TAG = "VLC/BaseModel"
abstract class BaseModel<T : MediaLibraryItem> : ViewModel(), RefreshModel {
abstract class BaseModel<T : MediaLibraryItem>(protected val context: Context) : ViewModel(), RefreshModel {
var sort = Medialibrary.SORT_ALPHA
var desc = false
......
......@@ -20,12 +20,15 @@
package org.videolan.vlc.viewmodels
import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import android.content.Context
import kotlinx.coroutines.experimental.withContext
import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.media.MediaWrapper
import org.videolan.vlc.util.VLCIO
class HistoryModel: BaseModel<MediaWrapper>() {
class HistoryModel(context: Context) : BaseModel<MediaWrapper>(context) {
override fun canSortByName() = false
......@@ -47,4 +50,11 @@ class HistoryModel: BaseModel<MediaWrapper>() {
fun clear() {
dataset.value = mutableListOf()
}
class Factory(private val context: Context): ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
@Suppress("UNCHECKED_CAST")
return HistoryModel(context.applicationContext) as T
}
}
}
\ No newline at end of file
......@@ -20,6 +20,7 @@
package org.videolan.vlc.viewmodels
import android.content.Context
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.launch
import org.videolan.medialibrary.Medialibrary
......@@ -29,7 +30,7 @@ import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.vlc.util.EmptyMLCallbacks
abstract class MedialibraryModel<T : MediaLibraryItem> : BaseModel<T>(), Medialibrary.OnMedialibraryReadyListener, MediaUpdatedCb by EmptyMLCallbacks, MediaAddedCb by EmptyMLCallbacks {
abstract class MedialibraryModel<T : MediaLibraryItem>(context: Context) : BaseModel<T>(context), Medialibrary.OnMedialibraryReadyListener, MediaUpdatedCb by EmptyMLCallbacks, MediaAddedCb by EmptyMLCallbacks {
val medialibrary = Medialibrary.getInstance()
......
......@@ -24,6 +24,7 @@ import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import android.arch.lifecycle.ViewModelProviders
import android.content.Context
import android.support.v4.app.Fragment
import kotlinx.coroutines.experimental.withContext
import org.videolan.medialibrary.Medialibrary
......@@ -32,10 +33,11 @@ import org.videolan.medialibrary.media.MediaWrapper
import org.videolan.vlc.R
import org.videolan.vlc.VLCApplication
import org.videolan.vlc.media.MediaGroup
import org.videolan.vlc.util.Settings
import org.videolan.vlc.util.Util
import org.videolan.vlc.util.VLCIO
open class VideosModel(private val group: String?, private val minGroupLen: Int, customSort : Int) : MedialibraryModel<MediaWrapper>(), MediaAddedCb {
open class VideosModel(context: Context, private val group: String?, private val minGroupLen: Int, customSort : Int) : MedialibraryModel<MediaWrapper>(context), MediaAddedCb {
override val sortKey = "${super.sortKey}_$group"
override fun canSortByFileNameName() = true
......@@ -47,8 +49,8 @@ open class VideosModel(private val group: String?, private val minGroupLen: Int,
init {
sort = if (customSort != Medialibrary.SORT_DEFAULT) customSort
else VLCApplication.getSettings().getInt(sortKey, Medialibrary.SORT_ALPHA)
desc = VLCApplication.getSettings().getBoolean(sortKey+"_desc", false)
else Settings.getInstance(context).getInt(sortKey, Medialibrary.SORT_ALPHA)
desc = Settings.getInstance(context).getBoolean(sortKey+"_desc", false)
Medialibrary.lastThumb.observeForever(thumbObs)
}
......@@ -95,16 +97,16 @@ open class VideosModel(private val group: String?, private val minGroupLen: Int,
Medialibrary.lastThumb.removeObserver(thumbObs)
}
class Factory(val group: String?, private val minGroupLen : Int, private val sort : Int): ViewModelProvider.NewInstanceFactory() {
class Factory(private val context: Context, val group: String?, private val minGroupLen : Int, private val sort : Int): ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
@Suppress("UNCHECKED_CAST")
return VideosModel(group, minGroupLen, sort) as T
return VideosModel(context.applicationContext, group, minGroupLen, sort) as T
}
}
companion object {
fun get(fragment: Fragment, group: String?, minGroupLen : Int, sort : Int) : VideosModel {
return ViewModelProviders.of(fragment, Factory(group, minGroupLen, sort)).get(VideosModel::class.java)
fun get(context: Context, fragment: Fragment, group: String?, minGroupLen : Int, sort : Int) : VideosModel {
return ViewModelProviders.of(fragment, Factory(context, group, minGroupLen, sort)).get(VideosModel::class.java)
}
}
}
......@@ -22,24 +22,25 @@ package org.videolan.vlc.viewmodels.audio
import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import android.content.Context
import kotlinx.coroutines.experimental.withContext
import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.media.Artist
import org.videolan.medialibrary.media.Genre
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.vlc.VLCApplication
import org.videolan.vlc.util.Settings
import org.videolan.vlc.util.VLCIO
class AlbumModel(val parent: MediaLibraryItem? = null): AudioModel(), Medialibrary.AlbumsAddedCb {
class AlbumModel(context: Context, val parent: MediaLibraryItem? = null): AudioModel(context), Medialibrary.AlbumsAddedCb {
override val sortKey = "${super.sortKey}_${parent?.javaClass?.simpleName}"
override fun canSortByDuration() = true
override fun canSortByReleaseDate() = true
init {
sort = VLCApplication.getSettings().getInt(sortKey, Medialibrary.SORT_ALPHA)
desc = VLCApplication.getSettings().getBoolean("${sortKey}_desc", false)
sort = Settings.getInstance(context).getInt(sortKey, Medialibrary.SORT_ALPHA)
desc = Settings.getInstance(context).getBoolean("${sortKey}_desc", false)
if (sort == Medialibrary.SORT_ALPHA && parent is Artist) sort = Medialibrary.SORT_RELEASEDATE
}
......@@ -67,10 +68,10 @@ class AlbumModel(val parent: MediaLibraryItem? = null): AudioModel(), Medialibra
medialibrary.setAlbumsAddedCb(null)
}
class Factory(val parent: MediaLibraryItem?): ViewModelProvider.NewInstanceFactory() {
class Factory(private val context: Context, val parent: MediaLibraryItem?): ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
@Suppress("UNCHECKED_CAST")
return AlbumModel(parent) as T
return AlbumModel(context.applicationContext, parent) as T
}
}
}
\ No newline at end of file
......@@ -22,18 +22,19 @@ package org.videolan.vlc.viewmodels.audio
import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import android.content.Context
import kotlinx.coroutines.experimental.withContext
import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.Medialibrary.ArtistsAddedCb
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.vlc.VLCApplication
import org.videolan.vlc.util.Settings
import org.videolan.vlc.util.VLCIO
class ArtistModel(private var showAll: Boolean = false): AudioModel(), ArtistsAddedCb {
class ArtistModel(context: Context, private var showAll: Boolean = false): AudioModel(context), ArtistsAddedCb {
init {
sort = VLCApplication.getSettings().getInt(sortKey, Medialibrary.SORT_ALPHA)
desc = VLCApplication.getSettings().getBoolean("${sortKey}_desc", false)
sort = Settings.getInstance(context).getInt(sortKey, Medialibrary.SORT_ALPHA)
desc = Settings.getInstance(context).getBoolean("${sortKey}_desc", false)
}
override fun onArtistsAdded() {
......@@ -62,10 +63,10 @@ class ArtistModel(private var showAll: Boolean = false): AudioModel(), ArtistsAd
medialibrary.setArtistsAddedCb(null)
}
class Factory(private val showAll: Boolean): ViewModelProvider.NewInstanceFactory() {
class Factory(private val context: Context, private val showAll: Boolean): ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
@Suppress("UNCHECKED_CAST")
return ArtistModel(showAll) as T
return ArtistModel(context, showAll) as T
}
}
}
\ No newline at end of file
......@@ -20,7 +20,8 @@
package org.videolan.vlc.viewmodels.audio
import android.content.Context
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.vlc.viewmodels.MedialibraryModel
open class AudioModel: MedialibraryModel<MediaLibraryItem>()
\ No newline at end of file
open class AudioModel(context: Context): MedialibraryModel<MediaLibraryItem>(context)
\ No newline at end of file
......@@ -20,18 +20,21 @@
package org.videolan.vlc.viewmodels.audio
import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import android.content.Context
import kotlinx.coroutines.experimental.withContext
import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.vlc.VLCApplication
import org.videolan.vlc.util.Settings
import org.videolan.vlc.util.VLCIO
class GenresModel: AudioModel() {
class GenresModel(context: Context): AudioModel(context) {
init {
sort = VLCApplication.getSettings().getInt(sortKey, Medialibrary.SORT_ALPHA)
desc = VLCApplication.getSettings().getBoolean("${sortKey}_desc", false)
sort = Settings.getInstance(context).getInt(sortKey, Medialibrary.SORT_ALPHA)
desc = Settings.getInstance(context).getBoolean("${sortKey}_desc", false)
}
@Suppress("UNCHECKED_CAST")
......@@ -40,4 +43,11 @@ class GenresModel: AudioModel() {
medialibrary.getGenres(sort, desc).toMutableList() as MutableList<MediaLibraryItem>
}
}
class Factory(private val context: Context): ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
@Suppress("UNCHECKED_CAST")
return GenresModel(context.applicationContext) as T
}
}
}
\ No newline at end of file
......@@ -20,12 +20,15 @@
package org.videolan.vlc.viewmodels.audio
import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import android.content.Context
import kotlinx.coroutines.experimental.withContext
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.vlc.util.VLCIO
class PlaylistsModel: AudioModel() {
class PlaylistsModel(context: Context): AudioModel(context) {
override fun canSortByDuration() = true
......@@ -33,4 +36,11 @@ class PlaylistsModel: AudioModel() {
override suspend fun updateList() {
dataset.value = withContext(VLCIO) { medialibrary.getPlaylists(sort, desc).toMutableList() as MutableList<MediaLibraryItem> }
}
class Factory(private val context: Context): ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
@Suppress("UNCHECKED_CAST")
return PlaylistsModel(context.applicationContext) as T
}
}
}
\ No newline at end of file
......@@ -22,21 +22,22 @@ package org.videolan.vlc.viewmodels.audio
import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import android.content.Context
import kotlinx.coroutines.experimental.withContext
import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.media.*
import org.videolan.vlc.VLCApplication
import org.videolan.vlc.util.Settings
import org.videolan.vlc.util.VLCIO
class TracksModel(val parent: MediaLibraryItem? = null): AudioModel() {
class TracksModel(context: Context, val parent: MediaLibraryItem? = null): AudioModel(context) {
override val sortKey = "${super.sortKey}_${parent?.javaClass?.simpleName}"
override fun canSortByDuration() = true
override fun canSortByAlbum() = parent !== null
init {
sort = VLCApplication.getSettings().getInt(sortKey, Medialibrary.SORT_ALPHA)
desc = VLCApplication.getSettings().getBoolean("${sortKey}_desc", false)
sort = Settings.getInstance(context).getInt(sortKey, Medialibrary.SORT_ALPHA)
desc = Settings.getInstance(context).getBoolean("${sortKey}_desc", false)
if (sort == Medialibrary.SORT_ALPHA) sort = when (parent) {
is Artist -> Medialibrary.SORT_ALBUM
is Album -> Medialibrary.SORT_DEFAULT
......@@ -64,10 +65,10 @@ class TracksModel(val parent: MediaLibraryItem? = null): AudioModel() {
}
}
class Factory(val parent: MediaLibraryItem?): ViewModelProvider.NewInstanceFactory() {
class Factory(private val context: Context, private val parent: MediaLibraryItem?): ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
@Suppress("UNCHECKED_CAST")
return TracksModel(parent) as T
return TracksModel(context.applicationContext, parent) as T
}
}
}
\ No newline at end of file
......@@ -39,7 +39,7 @@ const val TYPE_NETWORK = 1
const val TYPE_PICKER = 2
const val TYPE_STORAGE = 3
open class BrowserModel(val context: Context, val url: String?, type: Int, showHiddenFiles: Boolean) : BaseModel<MediaLibraryItem>() {
open class BrowserModel(context: Context, val url: String?, type: Int, showHiddenFiles: Boolean) : BaseModel<MediaLibraryItem>(context) {
protected val provider: BrowserProvider = when (type) {
TYPE_PICKER -> FilePickerProvider(context, dataset, url)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment