Remove more context dependencies

This commit is contained in:
tzugen 2021-05-09 10:25:04 +02:00
parent 2c5f8d9dd0
commit 76ba4c8ac9
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
25 changed files with 59 additions and 60 deletions

View File

@ -306,9 +306,9 @@ public class BookmarksFragment extends Fragment {
}
playNowButton.setVisibility(enabled && deleteEnabled ? View.VISIBLE : View.GONE);
pinButton.setVisibility((enabled && !ActiveServerProvider.Companion.isOffline(getContext()) && selection.size() > pinnedCount) ? View.VISIBLE : View.GONE);
pinButton.setVisibility((enabled && !ActiveServerProvider.Companion.isOffline() && selection.size() > pinnedCount) ? View.VISIBLE : View.GONE);
unpinButton.setVisibility(enabled && unpinEnabled ? View.VISIBLE : View.GONE);
downloadButton.setVisibility(enabled && !deleteEnabled && !ActiveServerProvider.Companion.isOffline(getContext()) ? View.VISIBLE : View.GONE);
downloadButton.setVisibility(enabled && !deleteEnabled && !ActiveServerProvider.Companion.isOffline() ? View.VISIBLE : View.GONE);
deleteButton.setVisibility(enabled && deleteEnabled ? View.VISIBLE : View.GONE);
}

View File

@ -118,7 +118,7 @@ public class MainFragment extends Fragment {
final MergeAdapter adapter = new MergeAdapter();
adapter.addViews(Collections.singletonList(serverButton), true);
if (!ActiveServerProvider.Companion.isOffline(this.getContext()))
if (!ActiveServerProvider.Companion.isOffline())
{
adapter.addView(musicTitle, false);
adapter.addViews(asList(artistsButton, albumsButton, genresButton), true);

View File

@ -709,7 +709,7 @@ public class PlayerFragment extends Fragment implements GestureDetector.OnGestur
MenuItem bookmarkRemoveMenuItem = menu.findItem(R.id.menu_item_bookmark_delete);
if (ActiveServerProvider.Companion.isOffline(getContext()))
if (ActiveServerProvider.Companion.isOffline())
{
if (shareMenuItem != null)
{
@ -834,7 +834,7 @@ public class PlayerFragment extends Fragment implements GestureDetector.OnGestur
}
}
if (ActiveServerProvider.Companion.isOffline(getContext()) || !Util.getShouldUseId3Tags())
if (ActiveServerProvider.Companion.isOffline() || !Util.getShouldUseId3Tags())
{
MenuItem menuItem = menu.findItem(R.id.menu_show_artist);
@ -844,7 +844,7 @@ public class PlayerFragment extends Fragment implements GestureDetector.OnGestur
}
}
if (ActiveServerProvider.Companion.isOffline(getContext()))
if (ActiveServerProvider.Companion.isOffline())
{
MenuItem menuItem = menu.findItem(R.id.menu_lyrics);

View File

@ -130,7 +130,7 @@ public class PlaylistsFragment extends Fragment {
MusicService musicService = MusicServiceFactory.getMusicService(getContext());
List<Playlist> playlists = musicService.getPlaylists(refresh, getContext());
if (!ActiveServerProvider.Companion.isOffline(getContext()))
if (!ActiveServerProvider.Companion.isOffline())
new CacheCleaner(getContext()).cleanPlaylists(playlists);
return playlists;
}
@ -151,14 +151,14 @@ public class PlaylistsFragment extends Fragment {
super.onCreateContextMenu(menu, view, menuInfo);
MenuInflater inflater = getActivity().getMenuInflater();
if (ActiveServerProvider.Companion.isOffline(getContext())) inflater.inflate(R.menu.select_playlist_context_offline, menu);
if (ActiveServerProvider.Companion.isOffline()) inflater.inflate(R.menu.select_playlist_context_offline, menu);
else inflater.inflate(R.menu.select_playlist_context, menu);
MenuItem downloadMenuItem = menu.findItem(R.id.album_menu_download);
if (downloadMenuItem != null)
{
downloadMenuItem.setVisible(!ActiveServerProvider.Companion.isOffline(getContext()));
downloadMenuItem.setVisible(!ActiveServerProvider.Companion.isOffline());
}
}

View File

@ -280,10 +280,10 @@ public class SearchFragment extends Fragment {
if (downloadMenuItem != null)
{
downloadMenuItem.setVisible(!ActiveServerProvider.Companion.isOffline(getContext()));
downloadMenuItem.setVisible(!ActiveServerProvider.Companion.isOffline());
}
if (ActiveServerProvider.Companion.isOffline(getContext()) || isArtist)
if (ActiveServerProvider.Companion.isOffline() || isArtist)
{
if (shareButton != null)
{

View File

@ -160,7 +160,7 @@ public class JukeboxMediaPlayer
try
{
if (!ActiveServerProvider.Companion.isOffline(context))
if (!ActiveServerProvider.Companion.isOffline())
{
task = tasks.take();
JukeboxStatus status = task.execute();

View File

@ -18,7 +18,7 @@ public class Scrobbler
public void scrobble(final Context context, final DownloadFile song, final boolean submission)
{
if (song == null || !ActiveServerProvider.Companion.isScrobblingEnabled(context)) return;
if (song == null || !ActiveServerProvider.Companion.isScrobblingEnabled()) return;
final String id = song.getSong().getId();
if (id == null) return;

View File

@ -97,7 +97,7 @@ public class ShufflePlayBuffer
// Check if active server has changed.
clearBufferIfNecessary();
if (buffer.size() > REFILL_THRESHOLD || (!Util.isNetworkConnected(context) && !ActiveServerProvider.Companion.isOffline(context)))
if (buffer.size() > REFILL_THRESHOLD || (!Util.isNetworkConnected(context) && !ActiveServerProvider.Companion.isOffline()))
{
return;
}
@ -124,9 +124,9 @@ public class ShufflePlayBuffer
{
synchronized (buffer)
{
if (currentServer != ActiveServerProvider.Companion.getActiveServerId(context))
if (currentServer != ActiveServerProvider.Companion.getActiveServerId())
{
currentServer = ActiveServerProvider.Companion.getActiveServerId(context);
currentServer = ActiveServerProvider.Companion.getActiveServerId();
buffer.clear();
}
}

View File

@ -1065,7 +1065,7 @@ public class Util
public static boolean getShouldShowArtistPicture(Context context)
{
SharedPreferences preferences = getPreferences();
boolean isOffline = ActiveServerProvider.Companion.isOffline(context);
boolean isOffline = ActiveServerProvider.Companion.isOffline();
boolean isId3Enabled = preferences.getBoolean(Constants.PREFERENCES_KEY_ID3_TAGS, false);
boolean shouldShowArtistPicture = preferences.getBoolean(Constants.PREFERENCES_KEY_SHOW_ARTIST_PICTURE, false);
return (!isOffline) && isId3Enabled && shouldShowArtistPicture;

View File

@ -121,7 +121,7 @@ public class AlbumView extends UpdateView
viewHolder.artist.setVisibility(artist == null ? View.GONE : View.VISIBLE);
viewHolder.star.setImageDrawable(starred ? starDrawable : starHollowDrawable);
if (ActiveServerProvider.Companion.isOffline(this.context) || "-1".equals(album.getId()))
if (ActiveServerProvider.Companion.isOffline() || "-1".equals(album.getId()))
{
viewHolder.star.setVisibility(View.GONE);
}

View File

@ -368,7 +368,7 @@ class NavigationActivity : AppCompatActivity() {
}
private fun setMenuForServerSetting() {
val visibility = !isOffline(this)
val visibility = !isOffline()
chatMenuItem?.isVisible = visibility
bookmarksMenuItem?.isVisible = visibility
sharesMenuItem?.isVisible = visibility

View File

@ -1,12 +1,12 @@
package org.moire.ultrasonic.data
import android.content.Context
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import org.moire.ultrasonic.R
import org.moire.ultrasonic.app.UApp
import org.moire.ultrasonic.service.MusicServiceFactory.resetMusicService
import org.moire.ultrasonic.util.Constants
import org.moire.ultrasonic.util.Util
@ -17,8 +17,7 @@ import timber.log.Timber
* It caches the settings read up from the DB to improve performance.
*/
class ActiveServerProvider(
private val repository: ServerSettingDao,
private val context: Context
private val repository: ServerSettingDao
) {
private var cachedServer: ServerSetting? = null
@ -27,7 +26,7 @@ class ActiveServerProvider(
* @return The Active Server Settings
*/
fun getActiveServer(): ServerSetting {
val serverId = getActiveServerId(context)
val serverId = getActiveServerId()
if (serverId > 0) {
if (cachedServer != null && cachedServer!!.id == serverId) return cachedServer!!
@ -44,13 +43,13 @@ class ActiveServerProvider(
}
if (cachedServer != null) return cachedServer!!
setActiveServerId(context, 0)
setActiveServerId(0)
}
return ServerSetting(
id = -1,
index = 0,
name = context.getString(R.string.main_offline),
name = UApp.applicationContext().getString(R.string.main_offline),
url = "http://localhost",
userName = "",
password = "",
@ -70,13 +69,13 @@ class ActiveServerProvider(
Timber.d("setActiveServerByIndex $index")
if (index < 1) {
// Offline mode is selected
setActiveServerId(context, 0)
setActiveServerId(0)
return
}
GlobalScope.launch(Dispatchers.IO) {
val serverId = repository.findByIndex(index)?.id ?: 0
setActiveServerId(context, serverId)
setActiveServerId(serverId)
}
}
@ -132,14 +131,14 @@ class ActiveServerProvider(
* Queries if the Active Server is the "Offline" mode of Ultrasonic
* @return True, if the "Offline" mode is selected
*/
fun isOffline(context: Context?): Boolean {
return context == null || getActiveServerId(context) < 1
fun isOffline(): Boolean {
return getActiveServerId() < 1
}
/**
* Queries the Id of the Active Server
*/
fun getActiveServerId(context: Context): Int {
fun getActiveServerId(): Int {
val preferences = Util.getPreferences()
return preferences.getInt(Constants.PREFERENCES_KEY_SERVER_INSTANCE, -1)
}
@ -147,7 +146,7 @@ class ActiveServerProvider(
/**
* Sets the Id of the Active Server
*/
fun setActiveServerId(context: Context, serverId: Int) {
fun setActiveServerId(serverId: Int) {
resetMusicService()
val preferences = Util.getPreferences()
@ -159,8 +158,8 @@ class ActiveServerProvider(
/**
* Queries if Scrobbling is enabled
*/
fun isScrobblingEnabled(context: Context): Boolean {
if (isOffline(context)) {
fun isScrobblingEnabled(): Boolean {
if (isOffline()) {
return false
}
val preferences = Util.getPreferences()
@ -170,8 +169,8 @@ class ActiveServerProvider(
/**
* Queries if Server Scaling is enabled
*/
fun isServerScalingEnabled(context: Context): Boolean {
if (isOffline(context)) {
fun isServerScalingEnabled(): Boolean {
if (isOffline()) {
return false
}
val preferences = Util.getPreferences()

View File

@ -12,7 +12,7 @@ import org.moire.ultrasonic.util.ThemeChangedEventDistributor
* This Koin module contains the registration of general classes needed for Ultrasonic
*/
val applicationModule = module {
single { ActiveServerProvider(get(), androidContext()) }
single { ActiveServerProvider(get()) }
single { ImageLoaderProvider(androidContext()) }
single { PermissionUtil(androidContext()) }
single { NowPlayingEventDistributor() }

View File

@ -36,7 +36,7 @@ internal const val OFFLINE_MUSIC_SERVICE = "OfflineMusicService"
val musicServiceModule = module {
single(named("ServerInstance")) {
return@single ActiveServerProvider.getActiveServerId(androidContext())
return@single ActiveServerProvider.getActiveServerId()
}
single(named("ServerID")) {

View File

@ -79,7 +79,7 @@ class ArtistListModel(
private suspend fun loadFromServer(refresh: Boolean, swipe: SwipeRefreshLayout) =
withContext(Dispatchers.IO) {
val musicService = MusicServiceFactory.getMusicService(context)
val isOffline = ActiveServerProvider.isOffline(context)
val isOffline = ActiveServerProvider.isOffline()
val useId3Tags = Util.getShouldUseId3Tags()
try {

View File

@ -156,7 +156,7 @@ class ArtistRowAdapter(
inflater.inflate(R.menu.select_artist_context, popup.menu)
val downloadMenuItem = popup.menu.findItem(R.id.artist_menu_download)
downloadMenuItem?.isVisible = !isOffline(view.context)
downloadMenuItem?.isVisible = !isOffline()
popup.setOnMenuItemClickListener { menuItem ->
onContextMenuClick(menuItem, artistList[position])

View File

@ -131,7 +131,7 @@ class SelectAlbumFragment : Fragment() {
selectFolderHeader = SelectMusicFolderView(
requireContext(), view as ViewGroup
) { selectedFolderId ->
if (!isOffline(context)) {
if (!isOffline()) {
val serverSettingsModel: ServerSettingsModel by viewModel()
val currentSetting = activeServerProvider.getActiveServer()
currentSetting.musicFolderId = selectedFolderId
@ -298,7 +298,7 @@ class SelectAlbumFragment : Fragment() {
model.getRandom(albumListSize)
} else {
setTitle(name)
if (!isOffline(activity) && Util.getShouldUseId3Tags()) {
if (!isOffline() && Util.getShouldUseId3Tags()) {
if (isAlbum) {
model.getAlbum(refresh, id, name, parentId)
} else {
@ -327,12 +327,12 @@ class SelectAlbumFragment : Fragment() {
shareButton = menu.findItem(R.id.menu_item_share)
if (shareButton != null) {
shareButton!!.isVisible = !isOffline(context)
shareButton!!.isVisible = !isOffline()
}
val downloadMenuItem = menu.findItem(R.id.album_menu_download)
if (downloadMenuItem != null) {
downloadMenuItem.isVisible = !isOffline(context)
downloadMenuItem.isVisible = !isOffline()
}
}
@ -551,12 +551,12 @@ class SelectAlbumFragment : Fragment() {
playNowButton!!.visibility = if (enabled) View.VISIBLE else View.GONE
playNextButton!!.visibility = if (enabled) View.VISIBLE else View.GONE
playLastButton!!.visibility = if (enabled) View.VISIBLE else View.GONE
pinButton!!.visibility = if (enabled && !isOffline(context) && selection.size > pinnedCount)
pinButton!!.visibility = if (enabled && !isOffline() && selection.size > pinnedCount)
View.VISIBLE
else
View.GONE
unpinButton!!.visibility = if (enabled && unpinEnabled) View.VISIBLE else View.GONE
downloadButton!!.visibility = if (enabled && !deleteEnabled && !isOffline(context))
downloadButton!!.visibility = if (enabled && !deleteEnabled && !isOffline())
View.VISIBLE
else
View.GONE
@ -799,7 +799,7 @@ class SelectAlbumFragment : Fragment() {
)
playAllButtonVisible = !(isAlbumList || entries.isEmpty()) && !allVideos
shareButtonVisible = !isOffline(context) && songCount > 0
shareButtonVisible = !isOffline() && songCount > 0
albumListView!!.removeHeaderView(emptyView!!)
if (entries.isEmpty()) {

View File

@ -40,7 +40,7 @@ class SelectAlbumModel(application: Application) : AndroidViewModel(application)
suspend fun getMusicFolders(refresh: Boolean) {
withContext(Dispatchers.IO) {
if (!ActiveServerProvider.isOffline(context)) {
if (!ActiveServerProvider.isOffline()) {
val musicService = MusicServiceFactory.getMusicService(context)
musicFolders.postValue(musicService.getMusicFolders(refresh))
}
@ -284,7 +284,7 @@ class SelectAlbumModel(application: Application) : AndroidViewModel(application)
suspend fun getAlbumList(albumListType: String, size: Int, offset: Int) {
showHeader = false
showSelectFolderHeader = !ActiveServerProvider.isOffline(context) &&
showSelectFolderHeader = !ActiveServerProvider.isOffline() &&
!Util.getShouldUseId3Tags() && (
(albumListType == AlbumListType.SORTED_BY_NAME.toString()) ||
(albumListType == AlbumListType.SORTED_BY_ARTIST.toString())

View File

@ -59,13 +59,13 @@ class SelectArtistFragment : Fragment() {
artistListModel.refresh(refreshArtistListView!!)
}
if (!ActiveServerProvider.isOffline(this.context) &&
if (!ActiveServerProvider.isOffline() &&
!Util.getShouldUseId3Tags()
) {
selectFolderHeader = SelectMusicFolderView(
requireContext(), view as ViewGroup,
{ selectedFolderId ->
if (!ActiveServerProvider.isOffline(context)) {
if (!ActiveServerProvider.isOffline()) {
val currentSetting = activeServerProvider.getActiveServer()
currentSetting.musicFolderId = selectedFolderId
serverSettingsModel.updateItem(currentSetting)
@ -81,7 +81,7 @@ class SelectArtistFragment : Fragment() {
if (title == null) {
setTitle(
this,
if (ActiveServerProvider.isOffline(this.context))
if (ActiveServerProvider.isOffline())
R.string.music_library_label_offline
else R.string.music_library_label
)

View File

@ -601,7 +601,7 @@ class LocalMediaPlayer(
override fun execute() {
setPlayerState(PlayerState.DOWNLOADING)
while (!bufferComplete() && !isOffline(context)) {
while (!bufferComplete() && !isOffline()) {
Util.sleepQuietly(1000L)
if (isCancelled) {
return

View File

@ -33,7 +33,7 @@ import org.moire.ultrasonic.di.musicServiceModule
object MusicServiceFactory : KoinComponent {
@JvmStatic
fun getMusicService(context: Context): MusicService {
return if (ActiveServerProvider.isOffline(context)) {
return if (ActiveServerProvider.isOffline()) {
get(named(OFFLINE_MUSIC_SERVICE))
} else {
get(named(ONLINE_MUSIC_SERVICE))

View File

@ -211,7 +211,7 @@ open class RESTMusicService(
): SearchResult {
return try {
if (
!isOffline(context) &&
!isOffline() &&
Util.getShouldUseId3Tags()
) search3(criteria)
else search2(criteria)
@ -534,7 +534,7 @@ open class RESTMusicService(
synchronized(entry) {
// Use cached file, if existing.
var bitmap = FileUtil.getAlbumArtBitmap(context, entry, size, highQuality)
val serverScaling = isServerScalingEnabled(context)
val serverScaling = isServerScalingEnabled()
if (bitmap == null) {
Timber.d("Loading cover art for: %s", entry)

View File

@ -203,11 +203,11 @@ class DownloadHandler(
val musicService = getMusicService(activity)
val songs: MutableList<MusicDirectory.Entry> = LinkedList()
val root: MusicDirectory
if (!isOffline(activity) && isArtist && Util.getShouldUseId3Tags()) {
if (!isOffline() && isArtist && Util.getShouldUseId3Tags()) {
getSongsForArtist(id, songs)
} else {
if (isDirectory) {
root = if (!isOffline(activity) && Util.getShouldUseId3Tags())
root = if (!isOffline() && Util.getShouldUseId3Tags())
musicService.getAlbum(id, name, false)
else
musicService.getMusicDirectory(id, name, false, activity)
@ -251,7 +251,7 @@ class DownloadHandler(
)
) {
val root: MusicDirectory = if (
!isOffline(activity) &&
!isOffline() &&
Util.getShouldUseId3Tags()
) musicService.getAlbum(id1, title, false)
else musicService.getMusicDirectory(id1, title, false, activity)

View File

@ -12,7 +12,7 @@ class NetworkAndStorageChecker(val context: Context) {
fun warnIfNetworkOrStorageUnavailable() {
if (!Util.isExternalStoragePresent()) {
Util.toast(context, R.string.select_album_no_sdcard)
} else if (!isOffline(context) && !Util.isNetworkConnected(context)) {
} else if (!isOffline() && !Util.isNetworkConnected(context)) {
Util.toast(context, R.string.select_album_no_network)
}
}

View File

@ -161,7 +161,7 @@ class SongView(context: Context) : UpdateView(context), Checkable {
viewHolder?.check?.visibility = if (checkable && !song.isVideo) VISIBLE else GONE
viewHolder?.drag?.visibility = if (draggable) VISIBLE else GONE
if (isOffline(this.context)) {
if (isOffline()) {
viewHolder?.star?.visibility = GONE
viewHolder?.rating?.visibility = GONE
} else {