|  | @@ -7,10 +7,12 @@ import com.bluelinelabs.conductor.Router
 | 
	
		
			
				|  |  |  import eu.kanade.tachiyomi.R
 | 
	
		
			
				|  |  |  import eu.kanade.tachiyomi.data.database.DatabaseHelper
 | 
	
		
			
				|  |  |  import eu.kanade.tachiyomi.data.database.models.Category
 | 
	
		
			
				|  |  | -import eu.kanade.tachiyomi.data.preference.PreferenceValues.DisplayMode
 | 
	
		
			
				|  |  |  import eu.kanade.tachiyomi.data.preference.PreferencesHelper
 | 
	
		
			
				|  |  |  import eu.kanade.tachiyomi.data.track.TrackManager
 | 
	
		
			
				|  |  |  import eu.kanade.tachiyomi.data.track.TrackService
 | 
	
		
			
				|  |  | +import eu.kanade.tachiyomi.ui.library.setting.DisplayModeSetting
 | 
	
		
			
				|  |  | +import eu.kanade.tachiyomi.ui.library.setting.SortDirectionSetting
 | 
	
		
			
				|  |  | +import eu.kanade.tachiyomi.ui.library.setting.SortModeSetting
 | 
	
		
			
				|  |  |  import eu.kanade.tachiyomi.widget.ExtendedNavigationView
 | 
	
		
			
				|  |  |  import eu.kanade.tachiyomi.widget.ExtendedNavigationView.Item.TriStateGroup.State
 | 
	
		
			
				|  |  |  import eu.kanade.tachiyomi.widget.sheet.TabbedBottomSheetDialog
 | 
	
	
		
			
				|  | @@ -45,6 +47,8 @@ class LibrarySettingsSheet(
 | 
	
		
			
				|  |  |       * @param currentCategory ID of currently shown category
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  |      fun show(currentCategory: Category) {
 | 
	
		
			
				|  |  | +        sort.currentCategory = currentCategory
 | 
	
		
			
				|  |  | +        sort.adjustDisplaySelection()
 | 
	
		
			
				|  |  |          display.currentCategory = currentCategory
 | 
	
		
			
				|  |  |          display.adjustDisplaySelection()
 | 
	
		
			
				|  |  |          super.show()
 | 
	
	
		
			
				|  | @@ -158,8 +162,16 @@ class LibrarySettingsSheet(
 | 
	
		
			
				|  |  |      inner class Sort @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
 | 
	
		
			
				|  |  |          Settings(context, attrs) {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +        private val sort = SortGroup()
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          init {
 | 
	
		
			
				|  |  | -            setGroups(listOf(SortGroup()))
 | 
	
		
			
				|  |  | +            setGroups(listOf(sort))
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // Refreshes Display Setting selections
 | 
	
		
			
				|  |  | +        fun adjustDisplaySelection() {
 | 
	
		
			
				|  |  | +            sort.initModels()
 | 
	
		
			
				|  |  | +            sort.items.forEach { adapter.notifyItemChanged(it) }
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          inner class SortGroup : Group {
 | 
	
	
		
			
				|  | @@ -179,29 +191,29 @@ class LibrarySettingsSheet(
 | 
	
		
			
				|  |  |              override val footer = null
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              override fun initModels() {
 | 
	
		
			
				|  |  | -                val sorting = preferences.librarySortingMode().get()
 | 
	
		
			
				|  |  | -                val order = if (preferences.librarySortingAscending().get()) {
 | 
	
		
			
				|  |  | +                val sorting = SortModeSetting.get(preferences, currentCategory)
 | 
	
		
			
				|  |  | +                val order = if (SortDirectionSetting.get(preferences, currentCategory) == SortDirectionSetting.ASCENDING) {
 | 
	
		
			
				|  |  |                      Item.MultiSort.SORT_ASC
 | 
	
		
			
				|  |  |                  } else {
 | 
	
		
			
				|  |  |                      Item.MultiSort.SORT_DESC
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |                  alphabetically.state =
 | 
	
		
			
				|  |  | -                    if (sorting == LibrarySort.ALPHA) order else Item.MultiSort.SORT_NONE
 | 
	
		
			
				|  |  | +                    if (sorting == SortModeSetting.ALPHABETICAL) order else Item.MultiSort.SORT_NONE
 | 
	
		
			
				|  |  |                  lastRead.state =
 | 
	
		
			
				|  |  | -                    if (sorting == LibrarySort.LAST_READ) order else Item.MultiSort.SORT_NONE
 | 
	
		
			
				|  |  | +                    if (sorting == SortModeSetting.LAST_READ) order else Item.MultiSort.SORT_NONE
 | 
	
		
			
				|  |  |                  lastChecked.state =
 | 
	
		
			
				|  |  | -                    if (sorting == LibrarySort.LAST_CHECKED) order else Item.MultiSort.SORT_NONE
 | 
	
		
			
				|  |  | +                    if (sorting == SortModeSetting.LAST_CHECKED) order else Item.MultiSort.SORT_NONE
 | 
	
		
			
				|  |  |                  unread.state =
 | 
	
		
			
				|  |  | -                    if (sorting == LibrarySort.UNREAD) order else Item.MultiSort.SORT_NONE
 | 
	
		
			
				|  |  | +                    if (sorting == SortModeSetting.UNREAD) order else Item.MultiSort.SORT_NONE
 | 
	
		
			
				|  |  |                  total.state =
 | 
	
		
			
				|  |  | -                    if (sorting == LibrarySort.TOTAL) order else Item.MultiSort.SORT_NONE
 | 
	
		
			
				|  |  | +                    if (sorting == SortModeSetting.TOTAL_CHAPTERS) order else Item.MultiSort.SORT_NONE
 | 
	
		
			
				|  |  |                  latestChapter.state =
 | 
	
		
			
				|  |  | -                    if (sorting == LibrarySort.LATEST_CHAPTER) order else Item.MultiSort.SORT_NONE
 | 
	
		
			
				|  |  | +                    if (sorting == SortModeSetting.LATEST_CHAPTER) order else Item.MultiSort.SORT_NONE
 | 
	
		
			
				|  |  |                  chapterFetchDate.state =
 | 
	
		
			
				|  |  | -                    if (sorting == LibrarySort.CHAPTER_FETCH_DATE) order else Item.MultiSort.SORT_NONE
 | 
	
		
			
				|  |  | +                    if (sorting == SortModeSetting.DATE_FETCHED) order else Item.MultiSort.SORT_NONE
 | 
	
		
			
				|  |  |                  dateAdded.state =
 | 
	
		
			
				|  |  | -                    if (sorting == LibrarySort.DATE_ADDED) order else Item.MultiSort.SORT_NONE
 | 
	
		
			
				|  |  | +                    if (sorting == SortModeSetting.DATE_ADDED) order else Item.MultiSort.SORT_NONE
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              override fun onItemClicked(item: Item) {
 | 
	
	
		
			
				|  | @@ -219,23 +231,50 @@ class LibrarySettingsSheet(
 | 
	
		
			
				|  |  |                      else -> throw Exception("Unknown state")
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -                preferences.librarySortingMode().set(
 | 
	
		
			
				|  |  | -                    when (item) {
 | 
	
		
			
				|  |  | -                        alphabetically -> LibrarySort.ALPHA
 | 
	
		
			
				|  |  | -                        lastRead -> LibrarySort.LAST_READ
 | 
	
		
			
				|  |  | -                        lastChecked -> LibrarySort.LAST_CHECKED
 | 
	
		
			
				|  |  | -                        unread -> LibrarySort.UNREAD
 | 
	
		
			
				|  |  | -                        total -> LibrarySort.TOTAL
 | 
	
		
			
				|  |  | -                        latestChapter -> LibrarySort.LATEST_CHAPTER
 | 
	
		
			
				|  |  | -                        chapterFetchDate -> LibrarySort.CHAPTER_FETCH_DATE
 | 
	
		
			
				|  |  | -                        dateAdded -> LibrarySort.DATE_ADDED
 | 
	
		
			
				|  |  | -                        else -> throw Exception("Unknown sorting")
 | 
	
		
			
				|  |  | -                    }
 | 
	
		
			
				|  |  | -                )
 | 
	
		
			
				|  |  | -                preferences.librarySortingAscending().set(item.state == Item.MultiSort.SORT_ASC)
 | 
	
		
			
				|  |  | +                setSortModePreference(item)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                setSortDirectionPrefernece(item)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |                  item.group.items.forEach { adapter.notifyItemChanged(it) }
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            private fun setSortDirectionPrefernece(item: Item.MultiStateGroup) {
 | 
	
		
			
				|  |  | +                val flag = if (item.state == Item.MultiSort.SORT_ASC) {
 | 
	
		
			
				|  |  | +                    SortDirectionSetting.ASCENDING
 | 
	
		
			
				|  |  | +                } else {
 | 
	
		
			
				|  |  | +                    SortDirectionSetting.DESCENDING
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                if (preferences.categorisedDisplaySettings().get() && currentCategory != null && currentCategory?.id != 0) {
 | 
	
		
			
				|  |  | +                    currentCategory?.sortDirection = flag.flag
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                    db.insertCategory(currentCategory!!).executeAsBlocking()
 | 
	
		
			
				|  |  | +                } else {
 | 
	
		
			
				|  |  | +                    preferences.librarySortingAscending().set(flag)
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            private fun setSortModePreference(item: Item) {
 | 
	
		
			
				|  |  | +                val flag = when (item) {
 | 
	
		
			
				|  |  | +                    alphabetically -> SortModeSetting.ALPHABETICAL
 | 
	
		
			
				|  |  | +                    lastRead -> SortModeSetting.LAST_READ
 | 
	
		
			
				|  |  | +                    lastChecked -> SortModeSetting.LAST_CHECKED
 | 
	
		
			
				|  |  | +                    unread -> SortModeSetting.UNREAD
 | 
	
		
			
				|  |  | +                    total -> SortModeSetting.TOTAL_CHAPTERS
 | 
	
		
			
				|  |  | +                    latestChapter -> SortModeSetting.LATEST_CHAPTER
 | 
	
		
			
				|  |  | +                    chapterFetchDate -> SortModeSetting.DATE_FETCHED
 | 
	
		
			
				|  |  | +                    dateAdded -> SortModeSetting.DATE_ADDED
 | 
	
		
			
				|  |  | +                    else -> throw NotImplementedError("Unknown display mode")
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                if (preferences.categorisedDisplaySettings().get() && currentCategory != null && currentCategory?.id != 0) {
 | 
	
		
			
				|  |  | +                    currentCategory?.sortMode = flag.flag
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                    db.insertCategory(currentCategory!!).executeAsBlocking()
 | 
	
		
			
				|  |  | +                } else {
 | 
	
		
			
				|  |  | +                    preferences.librarySortingMode().set(flag)
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -264,9 +303,9 @@ class LibrarySettingsSheet(
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          // Gets user preference of currently selected display mode at current category
 | 
	
		
			
				|  |  | -        private fun getDisplayModePreference(): DisplayMode {
 | 
	
		
			
				|  |  | +        private fun getDisplayModePreference(): DisplayModeSetting {
 | 
	
		
			
				|  |  |              return if (preferences.categorisedDisplaySettings().get() && currentCategory != null && currentCategory?.id != 0) {
 | 
	
		
			
				|  |  | -                DisplayMode.values()[currentCategory?.displayMode ?: 0]
 | 
	
		
			
				|  |  | +                DisplayModeSetting.fromFlag(currentCategory?.displayMode)
 | 
	
		
			
				|  |  |              } else {
 | 
	
		
			
				|  |  |                  preferences.libraryDisplayMode().get()
 | 
	
		
			
				|  |  |              }
 | 
	
	
		
			
				|  | @@ -300,33 +339,26 @@ class LibrarySettingsSheet(
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              // Sets display group selections based on given mode
 | 
	
		
			
				|  |  | -            fun setGroupSelections(mode: DisplayMode) {
 | 
	
		
			
				|  |  | -                compactGrid.checked = mode == DisplayMode.COMPACT_GRID
 | 
	
		
			
				|  |  | -                comfortableGrid.checked = mode == DisplayMode.COMFORTABLE_GRID
 | 
	
		
			
				|  |  | -                list.checked = mode == DisplayMode.LIST
 | 
	
		
			
				|  |  | +            fun setGroupSelections(mode: DisplayModeSetting) {
 | 
	
		
			
				|  |  | +                compactGrid.checked = mode == DisplayModeSetting.COMPACT_GRID
 | 
	
		
			
				|  |  | +                comfortableGrid.checked = mode == DisplayModeSetting.COMFORTABLE_GRID
 | 
	
		
			
				|  |  | +                list.checked = mode == DisplayModeSetting.LIST
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              private fun setDisplayModePreference(item: Item) {
 | 
	
		
			
				|  |  | -                if (preferences.categorisedDisplaySettings().get() && currentCategory != null && currentCategory?.id != 0) {
 | 
	
		
			
				|  |  | -                    val flag = when (item) {
 | 
	
		
			
				|  |  | -                        compactGrid -> Category.COMPACT_GRID
 | 
	
		
			
				|  |  | -                        comfortableGrid -> Category.COMFORTABLE_GRID
 | 
	
		
			
				|  |  | -                        list -> Category.LIST
 | 
	
		
			
				|  |  | -                        else -> throw NotImplementedError("Unknown display mode")
 | 
	
		
			
				|  |  | -                    }
 | 
	
		
			
				|  |  | +                val flag = when (item) {
 | 
	
		
			
				|  |  | +                    compactGrid -> DisplayModeSetting.COMPACT_GRID
 | 
	
		
			
				|  |  | +                    comfortableGrid -> DisplayModeSetting.COMFORTABLE_GRID
 | 
	
		
			
				|  |  | +                    list -> DisplayModeSetting.LIST
 | 
	
		
			
				|  |  | +                    else -> throw NotImplementedError("Unknown display mode")
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -                    currentCategory?.displayMode = flag
 | 
	
		
			
				|  |  | +                if (preferences.categorisedDisplaySettings().get() && currentCategory != null && currentCategory?.id != 0) {
 | 
	
		
			
				|  |  | +                    currentCategory?.displayMode = flag.flag
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |                      db.insertCategory(currentCategory!!).executeAsBlocking()
 | 
	
		
			
				|  |  |                  } else {
 | 
	
		
			
				|  |  | -                    preferences.libraryDisplayMode().set(
 | 
	
		
			
				|  |  | -                        when (item) {
 | 
	
		
			
				|  |  | -                            compactGrid -> DisplayMode.COMPACT_GRID
 | 
	
		
			
				|  |  | -                            comfortableGrid -> DisplayMode.COMFORTABLE_GRID
 | 
	
		
			
				|  |  | -                            list -> DisplayMode.LIST
 | 
	
		
			
				|  |  | -                            else -> throw NotImplementedError("Unknown display mode")
 | 
	
		
			
				|  |  | -                        }
 | 
	
		
			
				|  |  | -                    )
 | 
	
		
			
				|  |  | +                    preferences.libraryDisplayMode().set(flag)
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |          }
 |