Commit 71ff2bfe authored by Geoffrey Métais's avatar Geoffrey Métais

Make DB repositories singletons

parent 7ea8e692
......@@ -178,7 +178,7 @@ val migration_26_27 = object:Migration(26, 27) {
}
fun populateDB(context: Context) = launch(VLCIO) {
val favRepo = BrowserFavRepository(MediaDatabase.getDatabase(context).browserFavDao())
val favRepo = BrowserFavRepository.getInstance(context)
val uris = listOf(AndroidDevices.MediaFolders.EXTERNAL_PUBLIC_MOVIES_DIRECTORY_URI,
AndroidDevices.MediaFolders.EXTERNAL_PUBLIC_MUSIC_DIRECTORY_URI,
AndroidDevices.MediaFolders.EXTERNAL_PUBLIC_PODCAST_DIRECTORY_URI,
......
......@@ -106,7 +106,7 @@ abstract class BaseBrowserFragment : MediaBrowserFragment<BrowserModel>(), IRefr
}
showHiddenFiles = PreferenceManager.getDefaultSharedPreferences(requireContext()).getBoolean("browser_show_hidden_files", false)
isRootDirectory = defineIsRoot()
browserFavRepository = BrowserFavRepository(MediaDatabase.getDatabase(requireContext()).browserFavDao())
browserFavRepository = BrowserFavRepository.getInstance(requireContext())
}
override fun onPrepareOptionsMenu(menu: Menu?) {
......
......@@ -78,7 +78,7 @@ public class NetworkServerDialog extends DialogFragment implements AdapterView.O
public void onAttach(Activity activity) {
super.onAttach(activity);
mActivity = activity;
mBrowserFavRepository = new BrowserFavRepository( MediaDatabase.Companion.getDatabase(requireContext()).browserFavDao());
if (mBrowserFavRepository == null) mBrowserFavRepository = BrowserFavRepository.Companion.getInstance(activity);
}
@Override
......
......@@ -108,7 +108,7 @@ class MainTvFragment : BrowseSupportFragment(), OnItemViewSelectedListener, OnIt
backgroundManager = BackgroundManager.getInstance(requireActivity()).apply { attach(requireActivity().window) }
nowPlayingDelegate = NowPlayingDelegate(this)
browserFavRepository = BrowserFavRepository(MediaDatabase.getDatabase(requireContext()).browserFavDao())
browserFavRepository = BrowserFavRepository.getInstance(requireContext())
favorites = browserFavRepository.networkFavorites
favorites.observe(this, Observer{
it?.let{
......
......@@ -82,7 +82,7 @@ public class MediaItemDetailsFragment extends DetailsFragment implements Playbac
super.onCreate(savedInstanceState);
mBackgroundManager = BackgroundManager.getInstance(getActivity());
mBackgroundManager.setAutoReleaseOnStop(false);
mBrowserFavRepository = new BrowserFavRepository(MediaDatabase.Companion.getDatabase(VLCApplication.getAppContext()).browserFavDao());
mBrowserFavRepository = BrowserFavRepository.Companion.getInstance(getActivity());
buildDetails();
}
......
......@@ -850,7 +850,7 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVLCVout.C
if(data.hasExtra(FilePickerFragment.EXTRA_MRL)) {
mService.addSubtitleTrack(Uri.parse(data.getStringExtra(FilePickerFragment.EXTRA_MRL)), true);
new SlaveRepository(MediaDatabase.Companion.getDatabase(getApplicationContext()).slaveDao()).saveSlave(mService.getCurrentMediaLocation(), Media.Slave.Type.Subtitle, 2, data.getStringExtra(FilePickerFragment.EXTRA_MRL));
SlaveRepository.Companion.getInstance(this).saveSlave(mService.getCurrentMediaLocation(), Media.Slave.Type.Subtitle, 2, data.getStringExtra(FilePickerFragment.EXTRA_MRL));
} else if (BuildConfig.DEBUG) Log.d(TAG, "Subtitle selection dialog was cancelled");
}
......@@ -2883,7 +2883,7 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVLCVout.C
final MediaWrapper mw = mService.getCurrentMediaWrapper();
if (mw != null) mediaTitle = FileUtils.getFileNameFromPath(mService.getCurrentMediaWrapper().getLocation());
}
if (mediaTitle != null) prefsList.addAll(new ExternalSubRepository(MediaDatabase.Companion.getDatabase(getApplicationContext()).externalSubDao()).getSubtitles(mediaTitle));
if (mediaTitle != null) prefsList.addAll(ExternalSubRepository.Companion.getInstance(VideoPlayerActivity.this).getSubtitles(mediaTitle));
return prefsList;
}
......
......@@ -28,7 +28,7 @@ class PlayerController : IVLCVout.Callback, MediaPlayer.EventListener {
private val playerContext by lazy(LazyThreadSafetyMode.NONE) { newSingleThreadContext("vlc-player") }
private val settings by lazy(LazyThreadSafetyMode.NONE) { VLCApplication.getSettings() }
val progress by lazy(LazyThreadSafetyMode.NONE) { MutableLiveData<Progress>().apply { value = Progress() } }
private val slaveRepository by lazy { SlaveRepository(MediaDatabase.getDatabase(VLCApplication.getAppContext()).slaveDao()) }
private val slaveRepository by lazy { SlaveRepository.getInstance(VLCApplication.getAppContext()) }
private var mediaplayer = newMediaPlayer()
var switchToVideo = false
......
......@@ -54,7 +54,7 @@ open class FileBrowserProvider(
private var storagePosition = -1
private var otgPosition = -1
private val showFavorites : Boolean
private val favorites = if (url == null && !filePicker) BrowserFavRepository(MediaDatabase.getDatabase(context).browserFavDao()).localFavorites else null
private val favorites = if (url == null && !filePicker) BrowserFavRepository.getInstance(context).localFavorites else null
private val favoritesObserver by lazy { Observer<List<BrowserFav>> {
val favs = convertFavorites(it)
......
......@@ -34,7 +34,7 @@ import org.videolan.vlc.util.LiveDataset
class NetworkProvider(context: Context, dataset: LiveDataset<MediaLibraryItem>, url: String? = null, showHiddenFiles: Boolean): BrowserProvider(context, dataset, url, showHiddenFiles), Observer<List<MediaWrapper>> {
private val favorites = if (url == null) BrowserFavRepository(MediaDatabase.getDatabase(context).browserFavDao()).networkFavorites else null
private val favorites = if (url == null) BrowserFavRepository.getInstance(context).networkFavorites else null
init {
favorites?.observeForever(this)
......
......@@ -20,14 +20,18 @@
package org.videolan.vlc.repository
import android.annotation.SuppressLint
import android.arch.lifecycle.MediatorLiveData
import android.content.Context
import android.net.Uri
import android.support.annotation.WorkerThread
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.launch
import org.videolan.medialibrary.media.MediaWrapper
import org.videolan.tools.SingletonHolder
import org.videolan.vlc.ExternalMonitor
import org.videolan.vlc.database.BrowserFavDao
import org.videolan.vlc.database.MediaDatabase
import org.videolan.vlc.database.models.BrowserFav
import org.videolan.vlc.util.Constants.TYPE_LOCAL_FAV
import org.videolan.vlc.util.Constants.TYPE_NETWORK_FAV
......@@ -80,4 +84,6 @@ class BrowserFavRepository(private val browserFavDao: BrowserFavDao) {
else -> this
}
}
companion object : SingletonHolder<BrowserFavRepository, Context>({ BrowserFavRepository(MediaDatabase.getDatabase(it.applicationContext).browserFavDao()) })
}
......@@ -20,14 +20,17 @@
package org.videolan.vlc.repository
import android.content.Context
import android.net.Uri
import android.support.annotation.WorkerThread
import org.videolan.tools.SingletonHolder
import org.videolan.vlc.database.models.ExternalSub
import org.videolan.vlc.database.ExternalSubDao
import org.videolan.vlc.database.MediaDatabase
import java.io.File
class ExternalSubRepository(val externalSubDao: ExternalSubDao ) {
class ExternalSubRepository(private val externalSubDao: ExternalSubDao ) {
fun saveSubtitle(path: String, mediaName: String) {
externalSubDao.insert(ExternalSub(path, mediaName))
}
......@@ -38,12 +41,11 @@ class ExternalSubRepository(val externalSubDao: ExternalSubDao ) {
val existExternalSubs: MutableList<String> = mutableListOf()
externalSubs.map {
if (File(Uri.decode(it.uri)).exists()) {
existExternalSubs.add(it.uri)
} else {
externalSubDao.delete(it)
}
if (File(Uri.decode(it.uri)).exists()) existExternalSubs.add(it.uri)
else externalSubDao.delete(it)
}
return existExternalSubs
}
companion object : SingletonHolder<ExternalSubRepository, Context>({ ExternalSubRepository(MediaDatabase.getDatabase(it.applicationContext).externalSubDao()) })
}
\ No newline at end of file
......@@ -27,13 +27,14 @@ import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.experimental.withContext
import org.videolan.libvlc.Media
import org.videolan.medialibrary.media.MediaWrapper
import org.videolan.tools.SingletonHolder
import org.videolan.vlc.database.MediaDatabase
import org.videolan.vlc.database.SlaveDao
import org.videolan.vlc.database.models.Slave
import org.videolan.vlc.util.VLCIO
class SlaveRepository(val slaveDao:SlaveDao){
class SlaveRepository(private val slaveDao:SlaveDao){
fun saveSlave(mediaPath: String, type: Int, priority: Int, uriString: String): Job {
return launch(VLCIO) {
......@@ -61,4 +62,6 @@ class SlaveRepository(val slaveDao:SlaveDao){
mediaSlaves
}
}
companion object : SingletonHolder<SlaveRepository, Context>({ SlaveRepository(MediaDatabase.getDatabase(it.applicationContext).slaveDao()) })
}
......@@ -346,7 +346,7 @@ public class SubtitlesDownloader {
while ((length = gzIS.read(buffer)) != -1) {
f.write(buffer, 0, length);
}
new ExternalSubRepository(MediaDatabase.Companion.getDatabase(VLCApplication.getAppContext()).externalSubDao()).saveSubtitle(srtUrl, fileName);
ExternalSubRepository.Companion.getInstance(mContext).saveSubtitle(srtUrl, fileName);
} catch (Throwable e) { //for various service outages
if (BuildConfig.DEBUG) Log.e(TAG, "download fail", e);
} finally {
......
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