[Library] Implemented Better saving of GridStyle
We used to store layout ID's of grid styles before which caused all sorts of trouble whenever an update was pushed. Layout ID's are generated at compile time according to alphabetical order ig, so if a layout is added or renamed, it will cause layout ID's to change, which resulted in either a Different layout than the one user stored, or totally random layout or a crash. So to counter this, 0-6 constant ID's are used to save Grid Styles which should result in stable updates.
This commit is contained in:
parent
e1364ed8a4
commit
63bd71b7e6
5 changed files with 65 additions and 23 deletions
|
@ -0,0 +1,16 @@
|
||||||
|
package code.name.monkey.retromusic.fragments
|
||||||
|
|
||||||
|
import androidx.annotation.LayoutRes
|
||||||
|
import code.name.monkey.retromusic.R
|
||||||
|
|
||||||
|
enum class GridStyle constructor(
|
||||||
|
@param:LayoutRes @field:LayoutRes val layoutResId: Int,
|
||||||
|
val id: Int
|
||||||
|
) {
|
||||||
|
Grid(R.layout.item_grid, 0),
|
||||||
|
Card(R.layout.item_card, 1),
|
||||||
|
ColoredCard(R.layout.item_card_color, 2),
|
||||||
|
Circular(R.layout.item_grid_circle, 3),
|
||||||
|
Image(R.layout.image, 4),
|
||||||
|
GradientImage(R.layout.item_image_gradient, 5)
|
||||||
|
}
|
|
@ -26,6 +26,7 @@ import code.name.monkey.retromusic.R
|
||||||
import code.name.monkey.retromusic.adapter.album.AlbumAdapter
|
import code.name.monkey.retromusic.adapter.album.AlbumAdapter
|
||||||
import code.name.monkey.retromusic.extensions.navigate
|
import code.name.monkey.retromusic.extensions.navigate
|
||||||
import code.name.monkey.retromusic.extensions.surfaceColor
|
import code.name.monkey.retromusic.extensions.surfaceColor
|
||||||
|
import code.name.monkey.retromusic.fragments.GridStyle
|
||||||
import code.name.monkey.retromusic.fragments.ReloadType
|
import code.name.monkey.retromusic.fragments.ReloadType
|
||||||
import code.name.monkey.retromusic.fragments.base.AbsRecyclerViewCustomGridSizeFragment
|
import code.name.monkey.retromusic.fragments.base.AbsRecyclerViewCustomGridSizeFragment
|
||||||
import code.name.monkey.retromusic.helper.MusicPlayerRemote
|
import code.name.monkey.retromusic.helper.MusicPlayerRemote
|
||||||
|
@ -131,11 +132,13 @@ class AlbumsFragment : AbsRecyclerViewCustomGridSizeFragment<AlbumAdapter, GridL
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun loadLayoutRes(): Int {
|
override fun loadLayoutRes(): Int {
|
||||||
return PreferenceUtil.albumGridStyle
|
return PreferenceUtil.albumGridStyle.layoutResId
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun saveLayoutRes(layoutRes: Int) {
|
override fun saveLayoutRes(layoutRes: Int) {
|
||||||
PreferenceUtil.albumGridStyle = layoutRes
|
PreferenceUtil.albumGridStyle = GridStyle.values().first { gridStyle ->
|
||||||
|
gridStyle.layoutResId == layoutRes
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
@ -307,9 +310,9 @@ class AlbumsFragment : AbsRecyclerViewCustomGridSizeFragment<AlbumAdapter, GridL
|
||||||
R.id.action_layout_circular -> R.layout.item_grid_circle
|
R.id.action_layout_circular -> R.layout.item_grid_circle
|
||||||
R.id.action_layout_image -> R.layout.image
|
R.id.action_layout_image -> R.layout.image
|
||||||
R.id.action_layout_gradient_image -> R.layout.item_image_gradient
|
R.id.action_layout_gradient_image -> R.layout.item_image_gradient
|
||||||
else -> PreferenceUtil.albumGridStyle
|
else -> PreferenceUtil.albumGridStyle.layoutResId
|
||||||
}
|
}
|
||||||
if (layoutRes != PreferenceUtil.albumGridStyle) {
|
if (layoutRes != PreferenceUtil.albumGridStyle.layoutResId) {
|
||||||
item.isChecked = true
|
item.isChecked = true
|
||||||
setAndSaveLayoutRes(layoutRes)
|
setAndSaveLayoutRes(layoutRes)
|
||||||
return true
|
return true
|
||||||
|
|
|
@ -25,8 +25,8 @@ import code.name.monkey.retromusic.EXTRA_ARTIST_ID
|
||||||
import code.name.monkey.retromusic.EXTRA_ARTIST_NAME
|
import code.name.monkey.retromusic.EXTRA_ARTIST_NAME
|
||||||
import code.name.monkey.retromusic.R
|
import code.name.monkey.retromusic.R
|
||||||
import code.name.monkey.retromusic.adapter.artist.ArtistAdapter
|
import code.name.monkey.retromusic.adapter.artist.ArtistAdapter
|
||||||
import code.name.monkey.retromusic.extensions.navigate
|
|
||||||
import code.name.monkey.retromusic.extensions.surfaceColor
|
import code.name.monkey.retromusic.extensions.surfaceColor
|
||||||
|
import code.name.monkey.retromusic.fragments.GridStyle
|
||||||
import code.name.monkey.retromusic.fragments.ReloadType
|
import code.name.monkey.retromusic.fragments.ReloadType
|
||||||
import code.name.monkey.retromusic.fragments.base.AbsRecyclerViewCustomGridSizeFragment
|
import code.name.monkey.retromusic.fragments.base.AbsRecyclerViewCustomGridSizeFragment
|
||||||
import code.name.monkey.retromusic.helper.MusicPlayerRemote
|
import code.name.monkey.retromusic.helper.MusicPlayerRemote
|
||||||
|
@ -132,11 +132,13 @@ class ArtistsFragment : AbsRecyclerViewCustomGridSizeFragment<ArtistAdapter, Gri
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun loadLayoutRes(): Int {
|
override fun loadLayoutRes(): Int {
|
||||||
return PreferenceUtil.artistGridStyle
|
return PreferenceUtil.artistGridStyle.layoutResId
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun saveLayoutRes(layoutRes: Int) {
|
override fun saveLayoutRes(layoutRes: Int) {
|
||||||
PreferenceUtil.artistGridStyle = layoutRes
|
PreferenceUtil.artistGridStyle = GridStyle.values().first { gridStyle ->
|
||||||
|
gridStyle.layoutResId == layoutRes
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
@ -312,9 +314,9 @@ class ArtistsFragment : AbsRecyclerViewCustomGridSizeFragment<ArtistAdapter, Gri
|
||||||
R.id.action_layout_circular -> R.layout.item_grid_circle
|
R.id.action_layout_circular -> R.layout.item_grid_circle
|
||||||
R.id.action_layout_image -> R.layout.image
|
R.id.action_layout_image -> R.layout.image
|
||||||
R.id.action_layout_gradient_image -> R.layout.item_image_gradient
|
R.id.action_layout_gradient_image -> R.layout.item_image_gradient
|
||||||
else -> PreferenceUtil.artistGridStyle
|
else -> PreferenceUtil.artistGridStyle.layoutResId
|
||||||
}
|
}
|
||||||
if (layoutRes != PreferenceUtil.artistGridStyle) {
|
if (layoutRes != PreferenceUtil.artistGridStyle.layoutResId) {
|
||||||
item.isChecked = true
|
item.isChecked = true
|
||||||
setAndSaveLayoutRes(layoutRes)
|
setAndSaveLayoutRes(layoutRes)
|
||||||
return true
|
return true
|
||||||
|
|
|
@ -21,8 +21,8 @@ import androidx.annotation.LayoutRes
|
||||||
import androidx.recyclerview.widget.GridLayoutManager
|
import androidx.recyclerview.widget.GridLayoutManager
|
||||||
import code.name.monkey.retromusic.R
|
import code.name.monkey.retromusic.R
|
||||||
import code.name.monkey.retromusic.adapter.song.SongAdapter
|
import code.name.monkey.retromusic.adapter.song.SongAdapter
|
||||||
import code.name.monkey.retromusic.extensions.navigate
|
|
||||||
import code.name.monkey.retromusic.extensions.surfaceColor
|
import code.name.monkey.retromusic.extensions.surfaceColor
|
||||||
|
import code.name.monkey.retromusic.fragments.GridStyle
|
||||||
import code.name.monkey.retromusic.fragments.ReloadType
|
import code.name.monkey.retromusic.fragments.ReloadType
|
||||||
import code.name.monkey.retromusic.fragments.base.AbsRecyclerViewCustomGridSizeFragment
|
import code.name.monkey.retromusic.fragments.base.AbsRecyclerViewCustomGridSizeFragment
|
||||||
import code.name.monkey.retromusic.helper.MusicPlayerRemote
|
import code.name.monkey.retromusic.helper.MusicPlayerRemote
|
||||||
|
@ -114,11 +114,13 @@ class SongsFragment : AbsRecyclerViewCustomGridSizeFragment<SongAdapter, GridLay
|
||||||
|
|
||||||
@LayoutRes
|
@LayoutRes
|
||||||
override fun loadLayoutRes(): Int {
|
override fun loadLayoutRes(): Int {
|
||||||
return PreferenceUtil.songGridStyle
|
return PreferenceUtil.songGridStyle.layoutResId
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun saveLayoutRes(@LayoutRes layoutRes: Int) {
|
override fun saveLayoutRes(@LayoutRes layoutRes: Int) {
|
||||||
PreferenceUtil.songGridStyle = layoutRes
|
PreferenceUtil.songGridStyle = GridStyle.values().first { gridStyle ->
|
||||||
|
gridStyle.layoutResId == layoutRes
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun setSortOrder(sortOrder: String) {
|
override fun setSortOrder(sortOrder: String) {
|
||||||
|
@ -307,9 +309,9 @@ class SongsFragment : AbsRecyclerViewCustomGridSizeFragment<SongAdapter, GridLay
|
||||||
R.id.action_layout_circular -> R.layout.item_grid_circle
|
R.id.action_layout_circular -> R.layout.item_grid_circle
|
||||||
R.id.action_layout_image -> R.layout.image
|
R.id.action_layout_image -> R.layout.image
|
||||||
R.id.action_layout_gradient_image -> R.layout.item_image_gradient
|
R.id.action_layout_gradient_image -> R.layout.item_image_gradient
|
||||||
else -> PreferenceUtil.songGridStyle
|
else -> PreferenceUtil.songGridStyle.layoutResId
|
||||||
}
|
}
|
||||||
if (layoutRes != PreferenceUtil.songGridStyle) {
|
if (layoutRes != PreferenceUtil.songGridStyle.layoutResId) {
|
||||||
item.isChecked = true
|
item.isChecked = true
|
||||||
setAndSaveLayoutRes(layoutRes)
|
setAndSaveLayoutRes(layoutRes)
|
||||||
return true
|
return true
|
||||||
|
|
|
@ -12,6 +12,7 @@ import code.name.monkey.retromusic.*
|
||||||
import code.name.monkey.retromusic.extensions.getIntRes
|
import code.name.monkey.retromusic.extensions.getIntRes
|
||||||
import code.name.monkey.retromusic.extensions.getStringOrDefault
|
import code.name.monkey.retromusic.extensions.getStringOrDefault
|
||||||
import code.name.monkey.retromusic.fragments.AlbumCoverStyle
|
import code.name.monkey.retromusic.fragments.AlbumCoverStyle
|
||||||
|
import code.name.monkey.retromusic.fragments.GridStyle
|
||||||
import code.name.monkey.retromusic.fragments.NowPlayingScreen
|
import code.name.monkey.retromusic.fragments.NowPlayingScreen
|
||||||
import code.name.monkey.retromusic.fragments.folder.FoldersFragment
|
import code.name.monkey.retromusic.fragments.folder.FoldersFragment
|
||||||
import code.name.monkey.retromusic.helper.SortOrder.*
|
import code.name.monkey.retromusic.helper.SortOrder.*
|
||||||
|
@ -344,22 +345,40 @@ object PreferenceUtil {
|
||||||
putInt(LYRICS_OPTIONS, value)
|
putInt(LYRICS_OPTIONS, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
var songGridStyle
|
var songGridStyle: GridStyle
|
||||||
get() = sharedPreferences.getInt(SONG_GRID_STYLE, R.layout.item_grid)
|
get() {
|
||||||
|
val id: Int = sharedPreferences.getInt(SONG_GRID_STYLE, 0)
|
||||||
|
// We can directly use "first" kotlin extension function here but
|
||||||
|
// there maybe layout id stored in this so to avoid a crash we use
|
||||||
|
// "firstOrNull"
|
||||||
|
return GridStyle.values().firstOrNull { gridStyle ->
|
||||||
|
gridStyle.id == id
|
||||||
|
} ?: GridStyle.Grid
|
||||||
|
}
|
||||||
set(value) = sharedPreferences.edit {
|
set(value) = sharedPreferences.edit {
|
||||||
putInt(SONG_GRID_STYLE, value)
|
putInt(SONG_GRID_STYLE, value.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
var albumGridStyle
|
var albumGridStyle: GridStyle
|
||||||
get() = sharedPreferences.getInt(ALBUM_GRID_STYLE, R.layout.item_grid)
|
get() {
|
||||||
|
val id: Int = sharedPreferences.getInt(ALBUM_GRID_STYLE, 0)
|
||||||
|
return GridStyle.values().firstOrNull { gridStyle ->
|
||||||
|
gridStyle.id == id
|
||||||
|
} ?: GridStyle.Grid
|
||||||
|
}
|
||||||
set(value) = sharedPreferences.edit {
|
set(value) = sharedPreferences.edit {
|
||||||
putInt(ALBUM_GRID_STYLE, value)
|
putInt(ALBUM_GRID_STYLE, value.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
var artistGridStyle
|
var artistGridStyle: GridStyle
|
||||||
get() = sharedPreferences.getInt(ARTIST_GRID_STYLE, R.layout.item_grid_circle)
|
get() {
|
||||||
|
val id: Int = sharedPreferences.getInt(ARTIST_GRID_STYLE, 4)
|
||||||
|
return GridStyle.values().firstOrNull { gridStyle ->
|
||||||
|
gridStyle.id == id
|
||||||
|
} ?: GridStyle.Circular
|
||||||
|
}
|
||||||
set(value) = sharedPreferences.edit {
|
set(value) = sharedPreferences.edit {
|
||||||
putInt(ARTIST_GRID_STYLE, value)
|
putInt(ARTIST_GRID_STYLE, value.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
val filterLength get() = sharedPreferences.getInt(FILTER_SONG, 20)
|
val filterLength get() = sharedPreferences.getInt(FILTER_SONG, 20)
|
||||||
|
|
Loading…
Reference in a new issue