From 094c0d2757ca50f61894c73aef8bd55ba782c27c Mon Sep 17 00:00:00 2001 From: tzugen Date: Tue, 4 May 2021 09:37:10 +0200 Subject: [PATCH 1/3] Cache requireArguments() in a local val --- .../fragment/SelectAlbumFragment.kt | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/SelectAlbumFragment.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/SelectAlbumFragment.kt index bc059dfa..6c024294 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/SelectAlbumFragment.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/SelectAlbumFragment.kt @@ -230,31 +230,32 @@ class SelectAlbumFragment : Fragment() { } private fun updateDisplay(refresh: Boolean) { - val id = requireArguments().getString(Constants.INTENT_EXTRA_NAME_ID) - val isAlbum = requireArguments().getBoolean(Constants.INTENT_EXTRA_NAME_IS_ALBUM, false) - val name = requireArguments().getString(Constants.INTENT_EXTRA_NAME_NAME) - val parentId = requireArguments().getString(Constants.INTENT_EXTRA_NAME_PARENT_ID) - val playlistId = requireArguments().getString(Constants.INTENT_EXTRA_NAME_PLAYLIST_ID) - val podcastChannelId = requireArguments().getString( + val args = requireArguments() + val id = args.getString(Constants.INTENT_EXTRA_NAME_ID) + val isAlbum = args.getBoolean(Constants.INTENT_EXTRA_NAME_IS_ALBUM, false) + val name = args.getString(Constants.INTENT_EXTRA_NAME_NAME) + val parentId = args.getString(Constants.INTENT_EXTRA_NAME_PARENT_ID) + val playlistId = args.getString(Constants.INTENT_EXTRA_NAME_PLAYLIST_ID) + val podcastChannelId = args.getString( Constants.INTENT_EXTRA_NAME_PODCAST_CHANNEL_ID ) - val playlistName = requireArguments().getString(Constants.INTENT_EXTRA_NAME_PLAYLIST_NAME) - val shareId = requireArguments().getString(Constants.INTENT_EXTRA_NAME_SHARE_ID) - val shareName = requireArguments().getString(Constants.INTENT_EXTRA_NAME_SHARE_NAME) - val albumListType = requireArguments().getString( + val playlistName = args.getString(Constants.INTENT_EXTRA_NAME_PLAYLIST_NAME) + val shareId = args.getString(Constants.INTENT_EXTRA_NAME_SHARE_ID) + val shareName = args.getString(Constants.INTENT_EXTRA_NAME_SHARE_NAME) + val albumListType = args.getString( Constants.INTENT_EXTRA_NAME_ALBUM_LIST_TYPE ) - val genreName = requireArguments().getString(Constants.INTENT_EXTRA_NAME_GENRE_NAME) - val albumListTitle = requireArguments().getInt( + val genreName = args.getString(Constants.INTENT_EXTRA_NAME_GENRE_NAME) + val albumListTitle = args.getInt( Constants.INTENT_EXTRA_NAME_ALBUM_LIST_TITLE, 0 ) - val getStarredTracks = requireArguments().getInt(Constants.INTENT_EXTRA_NAME_STARRED, 0) - val getVideos = requireArguments().getInt(Constants.INTENT_EXTRA_NAME_VIDEOS, 0) - val getRandomTracks = requireArguments().getInt(Constants.INTENT_EXTRA_NAME_RANDOM, 0) - val albumListSize = requireArguments().getInt( + val getStarredTracks = args.getInt(Constants.INTENT_EXTRA_NAME_STARRED, 0) + val getVideos = args.getInt(Constants.INTENT_EXTRA_NAME_VIDEOS, 0) + val getRandomTracks = args.getInt(Constants.INTENT_EXTRA_NAME_RANDOM, 0) + val albumListSize = args.getInt( Constants.INTENT_EXTRA_NAME_ALBUM_LIST_SIZE, 0 ) - val albumListOffset = requireArguments().getInt( + val albumListOffset = args.getInt( Constants.INTENT_EXTRA_NAME_ALBUM_LIST_OFFSET, 0 ) From 9ca15deef29e280711ed465e8348d68fdcdf02ed Mon Sep 17 00:00:00 2001 From: tzugen Date: Tue, 4 May 2021 09:46:44 +0200 Subject: [PATCH 2/3] Fix a copy & paste mistake (Wrongly inserted if clause) --- .../ultrasonic/fragment/SelectAlbumModel.kt | 285 ++++++++---------- 1 file changed, 132 insertions(+), 153 deletions(-) diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/SelectAlbumModel.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/SelectAlbumModel.kt index 1cbdc1c6..7c402bf8 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/SelectAlbumModel.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/SelectAlbumModel.kt @@ -54,50 +54,49 @@ class SelectAlbumModel(application: Application) : AndroidViewModel(application) parentId: String? ) { withContext(Dispatchers.IO) { - if (!ActiveServerProvider.isOffline(context)) { - val service = MusicServiceFactory.getMusicService(context) - var root = MusicDirectory() + val service = MusicServiceFactory.getMusicService(context) - if (allSongsId == id) { - val musicDirectory = service.getMusicDirectory( - parentId, name, refresh, context - ) + var root = MusicDirectory() - val songs: MutableList = LinkedList() - getSongsRecursively(musicDirectory, songs) + if (allSongsId == id) { + val musicDirectory = service.getMusicDirectory( + parentId, name, refresh, context + ) - for (song in songs) { - if (!song.isDirectory) { - root.addChild(song) - } - } - } else { - val musicDirectory = service.getMusicDirectory(id, name, refresh, context) + val songs: MutableList = LinkedList() + getSongsRecursively(musicDirectory, songs) - if (Util.getShouldShowAllSongsByArtist(context) && - musicDirectory.findChild(allSongsId) == null && - hasOnlyFolders(musicDirectory) - ) { - val allSongs = MusicDirectory.Entry() - - allSongs.isDirectory = true - allSongs.artist = name - allSongs.parent = id - allSongs.id = allSongsId - allSongs.title = String.format( - context.resources.getString(R.string.select_album_all_songs), name - ) - - root.addChild(allSongs) - root.addAll(musicDirectory.getChildren()) - } else { - root = musicDirectory + for (song in songs) { + if (!song.isDirectory) { + root.addChild(song) } } + } else { + val musicDirectory = service.getMusicDirectory(id, name, refresh, context) - currentDirectory.postValue(root) + if (Util.getShouldShowAllSongsByArtist(context) && + musicDirectory.findChild(allSongsId) == null && + hasOnlyFolders(musicDirectory) + ) { + val allSongs = MusicDirectory.Entry() + + allSongs.isDirectory = true + allSongs.artist = name + allSongs.parent = id + allSongs.id = allSongsId + allSongs.title = String.format( + context.resources.getString(R.string.select_album_all_songs), name + ) + + root.addChild(allSongs) + root.addAll(musicDirectory.getChildren()) + } else { + root = musicDirectory + } } + + currentDirectory.postValue(root) } } @@ -128,107 +127,99 @@ class SelectAlbumModel(application: Application) : AndroidViewModel(application) suspend fun getArtist(refresh: Boolean, id: String?, name: String?) { withContext(Dispatchers.IO) { - if (!ActiveServerProvider.isOffline(context)) { - val service = MusicServiceFactory.getMusicService(context) + val service = MusicServiceFactory.getMusicService(context) - var root = MusicDirectory() + var root = MusicDirectory() - val musicDirectory = service.getArtist(id, name, refresh, context) + val musicDirectory = service.getArtist(id, name, refresh, context) - if (Util.getShouldShowAllSongsByArtist(context) && - musicDirectory.findChild(allSongsId) == null && - hasOnlyFolders(musicDirectory) - ) { - val allSongs = MusicDirectory.Entry() + if (Util.getShouldShowAllSongsByArtist(context) && + musicDirectory.findChild(allSongsId) == null && + hasOnlyFolders(musicDirectory) + ) { + val allSongs = MusicDirectory.Entry() - allSongs.isDirectory = true - allSongs.artist = name - allSongs.parent = id - allSongs.id = allSongsId - allSongs.title = String.format( - context.resources.getString(R.string.select_album_all_songs), name - ) + allSongs.isDirectory = true + allSongs.artist = name + allSongs.parent = id + allSongs.id = allSongsId + allSongs.title = String.format( + context.resources.getString(R.string.select_album_all_songs), name + ) - root.addFirst(allSongs) - root.addAll(musicDirectory.getChildren()) - } else { - root = musicDirectory - } - currentDirectory.postValue(root) + root.addFirst(allSongs) + root.addAll(musicDirectory.getChildren()) + } else { + root = musicDirectory } + currentDirectory.postValue(root) } } suspend fun getAlbum(refresh: Boolean, id: String?, name: String?, parentId: String?) { withContext(Dispatchers.IO) { - if (!ActiveServerProvider.isOffline(context)) { - val service = MusicServiceFactory.getMusicService(context) + val service = MusicServiceFactory.getMusicService(context) - val musicDirectory: MusicDirectory + val musicDirectory: MusicDirectory - musicDirectory = if (allSongsId == id) { - val root = MusicDirectory() + musicDirectory = if (allSongsId == id) { + val root = MusicDirectory() - val songs: MutableCollection = LinkedList() - val artist = service.getArtist(parentId, "", false, context) + val songs: MutableCollection = LinkedList() + val artist = service.getArtist(parentId, "", false, context) - for ((id1) in artist.getChildren()) { - if (allSongsId != id1) { - val albumDirectory = service.getAlbum( - id1, "", false, context - ) + for ((id1) in artist.getChildren()) { + if (allSongsId != id1) { + val albumDirectory = service.getAlbum( + id1, "", false, context + ) - for (song in albumDirectory.getChildren()) { - if (!song.isVideo) { - songs.add(song) - } + for (song in albumDirectory.getChildren()) { + if (!song.isVideo) { + songs.add(song) } } } - - for (song in songs) { - if (!song.isDirectory) { - root.addChild(song) - } - } - root - } else { - service.getAlbum(id, name, refresh, context) } - currentDirectory.postValue(musicDirectory) + + for (song in songs) { + if (!song.isDirectory) { + root.addChild(song) + } + } + root + } else { + service.getAlbum(id, name, refresh, context) } + currentDirectory.postValue(musicDirectory) } } suspend fun getSongsForGenre(genre: String, count: Int, offset: Int) { withContext(Dispatchers.IO) { - if (!ActiveServerProvider.isOffline(context)) { - val service = MusicServiceFactory.getMusicService(context) - val musicDirectory = service.getSongsByGenre(genre, count, offset, context) - songsForGenre.postValue(musicDirectory) - } + val service = MusicServiceFactory.getMusicService(context) + val musicDirectory = service.getSongsByGenre(genre, count, offset, context) + songsForGenre.postValue(musicDirectory) } } suspend fun getStarred() { withContext(Dispatchers.IO) { - if (!ActiveServerProvider.isOffline(context)) { - val service = MusicServiceFactory.getMusicService(context) - val musicDirectory: MusicDirectory - val context = context + val service = MusicServiceFactory.getMusicService(context) + val musicDirectory: MusicDirectory + val context = context - if (Util.getShouldUseId3Tags(context)) { - musicDirectory = Util.getSongsFromSearchResult(service.getStarred2(context)) - } else { - musicDirectory = Util.getSongsFromSearchResult(service.getStarred(context)) - } - - currentDirectory.postValue(musicDirectory) + if (Util.getShouldUseId3Tags(context)) { + musicDirectory = Util.getSongsFromSearchResult(service.getStarred2(context)) + } else { + musicDirectory = Util.getSongsFromSearchResult(service.getStarred(context)) } + + currentDirectory.postValue(musicDirectory) } } @@ -236,68 +227,58 @@ class SelectAlbumModel(application: Application) : AndroidViewModel(application) showHeader = false withContext(Dispatchers.IO) { - if (!ActiveServerProvider.isOffline(context)) { - val service = MusicServiceFactory.getMusicService(context) - currentDirectory.postValue(service.getVideos(refresh, context)) - } + val service = MusicServiceFactory.getMusicService(context) + currentDirectory.postValue(service.getVideos(refresh, context)) } } suspend fun getRandom(size: Int) { withContext(Dispatchers.IO) { - if (!ActiveServerProvider.isOffline(context)) { - val service = MusicServiceFactory.getMusicService(context) - val musicDirectory = service.getRandomSongs(size, context) + val service = MusicServiceFactory.getMusicService(context) + val musicDirectory = service.getRandomSongs(size, context) - currentDirectoryIsSortable = false - currentDirectory.postValue(musicDirectory) - } + currentDirectoryIsSortable = false + currentDirectory.postValue(musicDirectory) } } suspend fun getPlaylist(playlistId: String, playlistName: String?) { withContext(Dispatchers.IO) { - if (!ActiveServerProvider.isOffline(context)) { - val service = MusicServiceFactory.getMusicService(context) - val musicDirectory = service.getPlaylist(playlistId, playlistName, context) + val service = MusicServiceFactory.getMusicService(context) + val musicDirectory = service.getPlaylist(playlistId, playlistName, context) - currentDirectory.postValue(musicDirectory) - } + currentDirectory.postValue(musicDirectory) } } suspend fun getPodcastEpisodes(podcastChannelId: String) { withContext(Dispatchers.IO) { - if (!ActiveServerProvider.isOffline(context)) { - val service = MusicServiceFactory.getMusicService(context) - val musicDirectory = service.getPodcastEpisodes(podcastChannelId, context) - currentDirectory.postValue(musicDirectory) - } + val service = MusicServiceFactory.getMusicService(context) + val musicDirectory = service.getPodcastEpisodes(podcastChannelId, context) + currentDirectory.postValue(musicDirectory) } } suspend fun getShare(shareId: String) { withContext(Dispatchers.IO) { - if (!ActiveServerProvider.isOffline(context)) { - val service = MusicServiceFactory.getMusicService(context) - val musicDirectory = MusicDirectory() + val service = MusicServiceFactory.getMusicService(context) + val musicDirectory = MusicDirectory() - val shares = service.getShares(true, context) + val shares = service.getShares(true, context) - for (share in shares) { - if (share.id == shareId) { - for (entry in share.getEntries()) { - musicDirectory.addChild(entry) - } - break + for (share in shares) { + if (share.id == shareId) { + for (entry in share.getEntries()) { + musicDirectory.addChild(entry) } + break } - currentDirectory.postValue(musicDirectory) } + currentDirectory.postValue(musicDirectory) } } @@ -311,30 +292,28 @@ class SelectAlbumModel(application: Application) : AndroidViewModel(application) ) withContext(Dispatchers.IO) { - if (!ActiveServerProvider.isOffline(context)) { - val service = MusicServiceFactory.getMusicService(context) - val musicDirectory: MusicDirectory - val musicFolderId = if (showSelectFolderHeader) { - activeServerProvider.getActiveServer().musicFolderId - } else { - null - } - - if (Util.getShouldUseId3Tags(context)) { - musicDirectory = service.getAlbumList2( - albumListType, size, - offset, musicFolderId, context - ) - } else { - musicDirectory = service.getAlbumList( - albumListType, size, - offset, musicFolderId, context - ) - } - - currentDirectoryIsSortable = sortableCollection(albumListType) - albumList.postValue(musicDirectory) + val service = MusicServiceFactory.getMusicService(context) + val musicDirectory: MusicDirectory + val musicFolderId = if (showSelectFolderHeader) { + activeServerProvider.getActiveServer().musicFolderId + } else { + null } + + if (Util.getShouldUseId3Tags(context)) { + musicDirectory = service.getAlbumList2( + albumListType, size, + offset, musicFolderId, context + ) + } else { + musicDirectory = service.getAlbumList( + albumListType, size, + offset, musicFolderId, context + ) + } + + currentDirectoryIsSortable = sortableCollection(albumListType) + albumList.postValue(musicDirectory) } } From a6c914cf7f0a5efbf4abdaa0e549bf65cbe04388 Mon Sep 17 00:00:00 2001 From: tzugen Date: Tue, 4 May 2021 09:49:47 +0200 Subject: [PATCH 3/3] Make the server setting model a local val --- .../kotlin/org/moire/ultrasonic/fragment/SelectAlbumFragment.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/SelectAlbumFragment.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/SelectAlbumFragment.kt index 6c024294..63694ff9 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/SelectAlbumFragment.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/SelectAlbumFragment.kt @@ -93,7 +93,6 @@ class SelectAlbumFragment : Fragment() { private var cancellationToken: CancellationToken? = null private val activeServerProvider: ActiveServerProvider by inject() - private val serverSettingsModel: ServerSettingsModel by viewModel() private val model: SelectAlbumModel by viewModels() private val random: Random = SecureRandom() @@ -133,6 +132,7 @@ class SelectAlbumFragment : Fragment() { requireContext(), view as ViewGroup ) { selectedFolderId -> if (!isOffline(context)) { + val serverSettingsModel: ServerSettingsModel by viewModel() val currentSetting = activeServerProvider.getActiveServer() currentSetting.musicFolderId = selectedFolderId serverSettingsModel.updateItem(currentSetting)