|  | @@ -11,6 +11,7 @@ import android.support.v7.widget.SearchView
 | 
											
												
													
														|  |  import android.view.*
 |  |  import android.view.*
 | 
											
												
													
														|  |  import com.afollestad.materialdialogs.MaterialDialog
 |  |  import com.afollestad.materialdialogs.MaterialDialog
 | 
											
												
													
														|  |  import com.f2prateek.rx.preferences.Preference
 |  |  import com.f2prateek.rx.preferences.Preference
 | 
											
												
													
														|  | 
 |  | +import eu.kanade.tachiyomi.Constants
 | 
											
												
													
														|  |  import eu.kanade.tachiyomi.R
 |  |  import eu.kanade.tachiyomi.R
 | 
											
												
													
														|  |  import eu.kanade.tachiyomi.data.database.models.Category
 |  |  import eu.kanade.tachiyomi.data.database.models.Category
 | 
											
												
													
														|  |  import eu.kanade.tachiyomi.data.database.models.Manga
 |  |  import eu.kanade.tachiyomi.data.database.models.Manga
 | 
											
										
											
												
													
														|  | @@ -83,6 +84,11 @@ class LibraryFragment : BaseRxFragment<LibraryPresenter>(), ActionMode.Callback
 | 
											
												
													
														|  |       */
 |  |       */
 | 
											
												
													
														|  |      var isFilterUnread = false
 |  |      var isFilterUnread = false
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | 
 |  | +    /**
 | 
											
												
													
														|  | 
 |  | +     * Sorting mode for library
 | 
											
												
													
														|  | 
 |  | +     */
 | 
											
												
													
														|  | 
 |  | +    var sortingMode = 0
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  |      /**
 |  |      /**
 | 
											
												
													
														|  |       * Number of manga per row in grid mode.
 |  |       * Number of manga per row in grid mode.
 | 
											
												
													
														|  |       */
 |  |       */
 | 
											
										
											
												
													
														|  | @@ -123,8 +129,9 @@ class LibraryFragment : BaseRxFragment<LibraryPresenter>(), ActionMode.Callback
 | 
											
												
													
														|  |      override fun onCreate(savedState: Bundle?) {
 |  |      override fun onCreate(savedState: Bundle?) {
 | 
											
												
													
														|  |          super.onCreate(savedState)
 |  |          super.onCreate(savedState)
 | 
											
												
													
														|  |          setHasOptionsMenu(true)
 |  |          setHasOptionsMenu(true)
 | 
											
												
													
														|  | -        isFilterDownloaded = preferences.filterDownloaded().get() as Boolean
 |  | 
 | 
											
												
													
														|  | -        isFilterUnread = preferences.filterUnread().get() as Boolean
 |  | 
 | 
											
												
													
														|  | 
 |  | +        isFilterDownloaded = preferences.filterDownloaded().getOrDefault()
 | 
											
												
													
														|  | 
 |  | +        isFilterUnread = preferences.filterUnread().getOrDefault()
 | 
											
												
													
														|  | 
 |  | +        sortingMode = preferences.librarySortingMode().getOrDefault()
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |      override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedState: Bundle?): View? {
 |  |      override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedState: Bundle?): View? {
 | 
											
										
											
												
													
														|  | @@ -179,12 +186,37 @@ class LibraryFragment : BaseRxFragment<LibraryPresenter>(), ActionMode.Callback
 | 
											
												
													
														|  |          super.onSaveInstanceState(outState)
 |  |          super.onSaveInstanceState(outState)
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
 |  | 
 | 
											
												
													
														|  | -        inflater.inflate(R.menu.library, menu)
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | 
 |  | +    /**
 | 
											
												
													
														|  | 
 |  | +     * Prepare the Fragment host's standard options menu to be displayed.  This is
 | 
											
												
													
														|  | 
 |  | +     * called right before the menu is shown, every time it is shown.  You can
 | 
											
												
													
														|  | 
 |  | +     * use this method to efficiently enable/disable items or otherwise
 | 
											
												
													
														|  | 
 |  | +     * dynamically modify the contents.
 | 
											
												
													
														|  | 
 |  | +     *
 | 
											
												
													
														|  | 
 |  | +     * @param menu The options menu as last shown or first initialized by
 | 
											
												
													
														|  | 
 |  | +     */
 | 
											
												
													
														|  | 
 |  | +    override fun onPrepareOptionsMenu(menu: Menu) {
 | 
											
												
													
														|  |          // Initialize search menu
 |  |          // Initialize search menu
 | 
											
												
													
														|  |          val filterDownloadedItem = menu.findItem(R.id.action_filter_downloaded)
 |  |          val filterDownloadedItem = menu.findItem(R.id.action_filter_downloaded)
 | 
											
												
													
														|  |          val filterUnreadItem = menu.findItem(R.id.action_filter_unread)
 |  |          val filterUnreadItem = menu.findItem(R.id.action_filter_unread)
 | 
											
												
													
														|  | 
 |  | +        val sortModeAlpha = menu.findItem(R.id.action_sort_alpha)
 | 
											
												
													
														|  | 
 |  | +        val sortModeLastRead = menu.findItem(R.id.action_sort_last_read)
 | 
											
												
													
														|  | 
 |  | +        val sortModeLastUpdated = menu.findItem(R.id.action_sort_last_updated)
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +        // Set correct checkbox filter
 | 
											
												
													
														|  | 
 |  | +        filterDownloadedItem.isChecked = isFilterDownloaded
 | 
											
												
													
														|  | 
 |  | +        filterUnreadItem.isChecked = isFilterUnread
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +        // Set correct radio button sort
 | 
											
												
													
														|  | 
 |  | +        when (sortingMode) {
 | 
											
												
													
														|  | 
 |  | +            Constants.SORT_LIBRARY_ALPHA -> sortModeAlpha.isChecked = true
 | 
											
												
													
														|  | 
 |  | +            Constants.SORT_LIBRARY_LAST_READ -> sortModeLastRead.isChecked = true
 | 
											
												
													
														|  | 
 |  | +            Constants.SORT_LIBRARY_LAST_UPDATED -> sortModeLastUpdated.isChecked = true
 | 
											
												
													
														|  | 
 |  | +        }
 | 
											
												
													
														|  | 
 |  | +    }
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
 | 
											
												
													
														|  | 
 |  | +        inflater.inflate(R.menu.library, menu)
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  |          val searchItem = menu.findItem(R.id.action_search)
 |  |          val searchItem = menu.findItem(R.id.action_search)
 | 
											
												
													
														|  |          val searchView = searchItem.actionView as SearchView
 |  |          val searchView = searchItem.actionView as SearchView
 | 
											
												
													
														|  |  
 |  |  
 | 
											
										
											
												
													
														|  | @@ -194,9 +226,6 @@ class LibraryFragment : BaseRxFragment<LibraryPresenter>(), ActionMode.Callback
 | 
											
												
													
														|  |              searchView.clearFocus()
 |  |              searchView.clearFocus()
 | 
											
												
													
														|  |          }
 |  |          }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -        filterDownloadedItem.isChecked = isFilterDownloaded
 |  | 
 | 
											
												
													
														|  | -        filterUnreadItem.isChecked = isFilterUnread
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  |          searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
 |  |          searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
 | 
											
												
													
														|  |              override fun onQueryTextSubmit(query: String): Boolean {
 |  |              override fun onQueryTextSubmit(query: String): Boolean {
 | 
											
												
													
														|  |                  onSearchTextChange(query)
 |  |                  onSearchTextChange(query)
 | 
											
										
											
												
													
														|  | @@ -219,7 +248,7 @@ class LibraryFragment : BaseRxFragment<LibraryPresenter>(), ActionMode.Callback
 | 
											
												
													
														|  |                  // Update settings.
 |  |                  // Update settings.
 | 
											
												
													
														|  |                  preferences.filterUnread().set(isFilterUnread)
 |  |                  preferences.filterUnread().set(isFilterUnread)
 | 
											
												
													
														|  |                  // Apply filter.
 |  |                  // Apply filter.
 | 
											
												
													
														|  | -                onFilterCheckboxChanged()
 |  | 
 | 
											
												
													
														|  | 
 |  | +                onFilterOrSortChanged()
 | 
											
												
													
														|  |              }
 |  |              }
 | 
											
												
													
														|  |              R.id.action_filter_downloaded -> {
 |  |              R.id.action_filter_downloaded -> {
 | 
											
												
													
														|  |                  // Change downloaded filter status.
 |  |                  // Change downloaded filter status.
 | 
											
										
											
												
													
														|  | @@ -227,7 +256,7 @@ class LibraryFragment : BaseRxFragment<LibraryPresenter>(), ActionMode.Callback
 | 
											
												
													
														|  |                  // Update settings.
 |  |                  // Update settings.
 | 
											
												
													
														|  |                  preferences.filterDownloaded().set(isFilterDownloaded)
 |  |                  preferences.filterDownloaded().set(isFilterDownloaded)
 | 
											
												
													
														|  |                  // Apply filter.
 |  |                  // Apply filter.
 | 
											
												
													
														|  | -                onFilterCheckboxChanged()
 |  | 
 | 
											
												
													
														|  | 
 |  | +                onFilterOrSortChanged()
 | 
											
												
													
														|  |              }
 |  |              }
 | 
											
												
													
														|  |              R.id.action_filter_empty -> {
 |  |              R.id.action_filter_empty -> {
 | 
											
												
													
														|  |                  // Remove filter status.
 |  |                  // Remove filter status.
 | 
											
										
											
												
													
														|  | @@ -237,7 +266,22 @@ class LibraryFragment : BaseRxFragment<LibraryPresenter>(), ActionMode.Callback
 | 
											
												
													
														|  |                  preferences.filterUnread().set(isFilterUnread)
 |  |                  preferences.filterUnread().set(isFilterUnread)
 | 
											
												
													
														|  |                  preferences.filterDownloaded().set(isFilterDownloaded)
 |  |                  preferences.filterDownloaded().set(isFilterDownloaded)
 | 
											
												
													
														|  |                  // Apply filter
 |  |                  // Apply filter
 | 
											
												
													
														|  | -                onFilterCheckboxChanged()
 |  | 
 | 
											
												
													
														|  | 
 |  | +                onFilterOrSortChanged()
 | 
											
												
													
														|  | 
 |  | +            }
 | 
											
												
													
														|  | 
 |  | +            R.id.action_sort_alpha -> {
 | 
											
												
													
														|  | 
 |  | +                sortingMode = Constants.SORT_LIBRARY_ALPHA
 | 
											
												
													
														|  | 
 |  | +                preferences.librarySortingMode().set(sortingMode)
 | 
											
												
													
														|  | 
 |  | +                onFilterOrSortChanged()
 | 
											
												
													
														|  | 
 |  | +            }
 | 
											
												
													
														|  | 
 |  | +            R.id.action_sort_last_read -> {
 | 
											
												
													
														|  | 
 |  | +                sortingMode = Constants.SORT_LIBRARY_LAST_READ
 | 
											
												
													
														|  | 
 |  | +                preferences.librarySortingMode().set(sortingMode)
 | 
											
												
													
														|  | 
 |  | +                onFilterOrSortChanged()
 | 
											
												
													
														|  | 
 |  | +            }
 | 
											
												
													
														|  | 
 |  | +            R.id.action_sort_last_updated -> {
 | 
											
												
													
														|  | 
 |  | +                sortingMode = Constants.SORT_LIBRARY_LAST_UPDATED
 | 
											
												
													
														|  | 
 |  | +                preferences.librarySortingMode().set(sortingMode)
 | 
											
												
													
														|  | 
 |  | +                onFilterOrSortChanged()
 | 
											
												
													
														|  |              }
 |  |              }
 | 
											
												
													
														|  |              R.id.action_library_display_mode -> swapDisplayMode()
 |  |              R.id.action_library_display_mode -> swapDisplayMode()
 | 
											
												
													
														|  |              R.id.action_update_library -> {
 |  |              R.id.action_update_library -> {
 | 
											
										
											
												
													
														|  | @@ -256,7 +300,7 @@ class LibraryFragment : BaseRxFragment<LibraryPresenter>(), ActionMode.Callback
 | 
											
												
													
														|  |      /**
 |  |      /**
 | 
											
												
													
														|  |       * Applies filter change
 |  |       * Applies filter change
 | 
											
												
													
														|  |       */
 |  |       */
 | 
											
												
													
														|  | -    private fun onFilterCheckboxChanged() {
 |  | 
 | 
											
												
													
														|  | 
 |  | +    private fun onFilterOrSortChanged() {
 | 
											
												
													
														|  |          presenter.resubscribeLibrary()
 |  |          presenter.resubscribeLibrary()
 | 
											
												
													
														|  |          activity.supportInvalidateOptionsMenu()
 |  |          activity.supportInvalidateOptionsMenu()
 | 
											
												
													
														|  |      }
 |  |      }
 |