|
@@ -34,273 +34,282 @@ private const val VERSION_CLICK_COUNT_ON = 12
|
|
|
|
|
|
|
|
class MainActivity : AppCompatActivity(), Router {
|
|
class MainActivity : AppCompatActivity(), Router {
|
|
|
|
|
|
|
|
- private val config by lazy { ConfigurationImpl(applicationContext) as Configuration }
|
|
|
|
|
- private val api by lazy { app().baseApi.create(Endpoint::class.java) }
|
|
|
|
|
- private val auth by lazy { KAuthImpl(applicationContext, api) as KAuth }
|
|
|
|
|
- private val allBooks = mutableListOf<Book>()
|
|
|
|
|
- private val booksAdapter by lazy {
|
|
|
|
|
- BooksAdapter(BookCoverSearchImpl(
|
|
|
|
|
- app().imageApi.create(ImageEndpoint::class.java),
|
|
|
|
|
- BookCoverCacheImpl(applicationContext)
|
|
|
|
|
- ), api, auth, this)
|
|
|
|
|
- }
|
|
|
|
|
- private val allBooksAdapter by lazy { booksAdapter.build(allBooks) }
|
|
|
|
|
- private val navigation by lazy {
|
|
|
|
|
- findView<BottomNavigationView>(R.id.navigation).apply {
|
|
|
|
|
- visibility = if (config.isDevMode()) View.VISIBLE else View.GONE
|
|
|
|
|
|
|
+ private val config by lazy { ConfigurationImpl(applicationContext) as Configuration }
|
|
|
|
|
+ private val api by lazy { app().baseApi.create(Endpoint::class.java) }
|
|
|
|
|
+ private val auth by lazy { KAuthImpl(applicationContext, api) as KAuth }
|
|
|
|
|
+ private val allBooks = mutableListOf<Book>()
|
|
|
|
|
+ private val booksAdapter by lazy {
|
|
|
|
|
+ BooksAdapter(
|
|
|
|
|
+ BookCoverSearchImpl(
|
|
|
|
|
+ app().imageApi.create(ImageEndpoint::class.java),
|
|
|
|
|
+ BookCoverCacheImpl(applicationContext)
|
|
|
|
|
+ ), api, auth, this
|
|
|
|
|
+ )
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
- private var needUpdate = false
|
|
|
|
|
- private lateinit var loginOption: MenuItem
|
|
|
|
|
- private lateinit var currentTab: CurrentTab
|
|
|
|
|
-
|
|
|
|
|
- override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
|
- if (config.isDevMode()) {
|
|
|
|
|
- setTheme(R.style.DevTheme)
|
|
|
|
|
|
|
+ private val allBooksAdapter by lazy { booksAdapter.build(allBooks) }
|
|
|
|
|
+ private val navigation by lazy {
|
|
|
|
|
+ findView<BottomNavigationView>(R.id.navigation).apply {
|
|
|
|
|
+ visibility = if (config.isDevMode()) View.VISIBLE else View.GONE
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
- super.onCreate(savedInstanceState)
|
|
|
|
|
- setContentView(R.layout.activity_main)
|
|
|
|
|
- initRecyclerView(booksRecyclerView)
|
|
|
|
|
- initNavigationView()
|
|
|
|
|
- initToolbar(toolbar)
|
|
|
|
|
- addBookButton.setOnClickListener {
|
|
|
|
|
- startActivityForResult(createNewBookIntent(), BOOK_REQUEST_CODE)
|
|
|
|
|
|
|
+ private var needUpdate = false
|
|
|
|
|
+ private lateinit var loginOption: MenuItem
|
|
|
|
|
+ private lateinit var currentTab: CurrentTab
|
|
|
|
|
+
|
|
|
|
|
+ override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
|
+ if (config.isDevMode()) {
|
|
|
|
|
+ setTheme(R.style.DevTheme)
|
|
|
|
|
+ }
|
|
|
|
|
+ super.onCreate(savedInstanceState)
|
|
|
|
|
+ setContentView(R.layout.activity_main)
|
|
|
|
|
+ initRecyclerView(booksRecyclerView)
|
|
|
|
|
+ initNavigationView()
|
|
|
|
|
+ initToolbar(toolbar)
|
|
|
|
|
+ addBookButton.setOnClickListener {
|
|
|
|
|
+ startActivityForResult(createNewBookIntent(), BOOK_REQUEST_CODE)
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
- override fun onStart() {
|
|
|
|
|
- super.onStart()
|
|
|
|
|
- refreshOptionsMenu()
|
|
|
|
|
- auth.requestAccessToken {
|
|
|
|
|
- refreshOptionsMenu()
|
|
|
|
|
- if (needUpdate) {
|
|
|
|
|
- refresh()
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ override fun onStart() {
|
|
|
|
|
+ super.onStart()
|
|
|
|
|
+ refreshOptionsMenu()
|
|
|
|
|
+ auth.requestAccessToken {
|
|
|
|
|
+ refreshOptionsMenu()
|
|
|
|
|
+ if (needUpdate) {
|
|
|
|
|
+ refresh()
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
- override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
|
|
|
|
- when (requestCode) {
|
|
|
|
|
- ULOGIN_REQUEST_CODE -> {
|
|
|
|
|
- if (resultCode == RESULT_OK && data != null) {
|
|
|
|
|
- auth.saveTokenResponse(data)
|
|
|
|
|
- needUpdate = true
|
|
|
|
|
|
|
+ override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
|
|
|
|
+ when (requestCode) {
|
|
|
|
|
+ ULOGIN_REQUEST_CODE -> {
|
|
|
|
|
+ if (resultCode == RESULT_OK && data != null) {
|
|
|
|
|
+ auth.saveTokenResponse(data)
|
|
|
|
|
+ needUpdate = true
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ BOOK_REQUEST_CODE -> {
|
|
|
|
|
+ needUpdate = resultCode == RESULT_OK
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
- BOOK_REQUEST_CODE -> {
|
|
|
|
|
- needUpdate = resultCode == RESULT_OK
|
|
|
|
|
- }
|
|
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
- override fun openEditBookScreen(book: Book) {
|
|
|
|
|
- startActivityForResult(createEditBookIntent(book.id, book is FinishedBook), BOOK_REQUEST_CODE)
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ override fun openEditBookScreen(book: Book) {
|
|
|
|
|
+ startActivityForResult(
|
|
|
|
|
+ createEditBookIntent(book.id, book is FinishedBook),
|
|
|
|
|
+ BOOK_REQUEST_CODE
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- private fun initNavigationView() {
|
|
|
|
|
- refresh(HOME_TAB)
|
|
|
|
|
- navigation.setOnNavigationItemSelectedListener { item ->
|
|
|
|
|
- setCurrentTab(CurrentTab.getByItemId(item.itemId))
|
|
|
|
|
- true
|
|
|
|
|
|
|
+ private fun initNavigationView() {
|
|
|
|
|
+ refresh(HOME_TAB)
|
|
|
|
|
+ navigation.setOnNavigationItemSelectedListener { item ->
|
|
|
|
|
+ setCurrentTab(CurrentTab.getByItemId(item.itemId))
|
|
|
|
|
+ true
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
- private fun initRecyclerView(recyclerView: RecyclerView): RecyclerView {
|
|
|
|
|
- recyclerView.layoutManager = LinearLayoutManager(this)
|
|
|
|
|
- return recyclerView
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ private fun initRecyclerView(recyclerView: RecyclerView): RecyclerView {
|
|
|
|
|
+ recyclerView.layoutManager = LinearLayoutManager(this)
|
|
|
|
|
+ return recyclerView
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- private fun initToolbar(toolbar: Toolbar) {
|
|
|
|
|
- toolbar.inflateMenu(R.menu.options)
|
|
|
|
|
- toolbar.setOnMenuItemClickListener { item ->
|
|
|
|
|
- when (item.itemId) {
|
|
|
|
|
- R.id.option_login -> {
|
|
|
|
|
- login()
|
|
|
|
|
- true
|
|
|
|
|
- }
|
|
|
|
|
- R.id.option_about -> {
|
|
|
|
|
- val dialogView = View.inflate(this, R.layout.about, null)
|
|
|
|
|
- val versionView = dialogView.aboutAppVersion
|
|
|
|
|
- val designerView = dialogView.aboutDesignerText
|
|
|
|
|
- versionView.text = BuildConfig.VERSION_NAME
|
|
|
|
|
- var count = 0
|
|
|
|
|
- val enabled = config.isDevMode()
|
|
|
|
|
- val max = if (enabled) {
|
|
|
|
|
- VERSION_CLICK_COUNT_OFF
|
|
|
|
|
- } else {
|
|
|
|
|
- VERSION_CLICK_COUNT_ON
|
|
|
|
|
- }
|
|
|
|
|
- if (enabled) {
|
|
|
|
|
- designerView.setCompoundDrawablesRelativeWithIntrinsicBounds(
|
|
|
|
|
- R.drawable.ic_about_designer_highlighted, 0, 0, 0
|
|
|
|
|
- )
|
|
|
|
|
- }
|
|
|
|
|
- versionView.setOnClickListener {
|
|
|
|
|
- if (++count == max) {
|
|
|
|
|
- enabled.not().let {
|
|
|
|
|
- if (it) toast(R.string.dev_mode_message)
|
|
|
|
|
- config.setDevMode(it)
|
|
|
|
|
- recreate()
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ private fun initToolbar(toolbar: Toolbar) {
|
|
|
|
|
+ toolbar.inflateMenu(R.menu.options)
|
|
|
|
|
+ toolbar.setOnMenuItemClickListener { item ->
|
|
|
|
|
+ when (item.itemId) {
|
|
|
|
|
+ R.id.option_login -> {
|
|
|
|
|
+ login()
|
|
|
|
|
+ true
|
|
|
|
|
+ }
|
|
|
|
|
+ R.id.option_about -> {
|
|
|
|
|
+ val dialogView = View.inflate(this, R.layout.about, null)
|
|
|
|
|
+ val versionView = dialogView.aboutAppVersion
|
|
|
|
|
+ val designerView = dialogView.aboutDesignerText
|
|
|
|
|
+ versionView.text = BuildConfig.VERSION_NAME
|
|
|
|
|
+ var count = 0
|
|
|
|
|
+ val enabled = config.isDevMode()
|
|
|
|
|
+ val max = if (enabled) {
|
|
|
|
|
+ VERSION_CLICK_COUNT_OFF
|
|
|
|
|
+ } else {
|
|
|
|
|
+ VERSION_CLICK_COUNT_ON
|
|
|
|
|
+ }
|
|
|
|
|
+ if (enabled) {
|
|
|
|
|
+ designerView.setCompoundDrawablesRelativeWithIntrinsicBounds(
|
|
|
|
|
+ R.drawable.ic_about_designer_highlighted, 0, 0, 0
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+ versionView.setOnClickListener {
|
|
|
|
|
+ if (++count == max) {
|
|
|
|
|
+ enabled.not().let {
|
|
|
|
|
+ if (it) toast(R.string.dev_mode_message)
|
|
|
|
|
+ config.setDevMode(it)
|
|
|
|
|
+ recreate()
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ AlertDialog.Builder(this).setView(dialogView).show()
|
|
|
|
|
+ true
|
|
|
|
|
+ }
|
|
|
|
|
+ R.id.option_refresh -> {
|
|
|
|
|
+ refresh()
|
|
|
|
|
+ true
|
|
|
|
|
+ }
|
|
|
|
|
+ else -> false
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
- AlertDialog.Builder(this).setView(dialogView).show()
|
|
|
|
|
- true
|
|
|
|
|
}
|
|
}
|
|
|
- R.id.option_refresh -> {
|
|
|
|
|
- refresh()
|
|
|
|
|
- true
|
|
|
|
|
- }
|
|
|
|
|
- else -> false
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ loginOption = toolbar.menu.findItem(R.id.option_login)
|
|
|
}
|
|
}
|
|
|
- loginOption = toolbar.menu.findItem(R.id.option_login)
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
- private fun login() {
|
|
|
|
|
- RxPermissions(this).requestEach(READ_PHONE_STATE).subscribe({
|
|
|
|
|
- when {
|
|
|
|
|
- it.granted -> {
|
|
|
|
|
- if (auth.isAuthorized()) {
|
|
|
|
|
- auth.logout()
|
|
|
|
|
- refresh()
|
|
|
|
|
- } else {
|
|
|
|
|
- startActivityForResult(auth.getTokenRequest(), ULOGIN_REQUEST_CODE)
|
|
|
|
|
- }
|
|
|
|
|
- refreshOptionsMenu()
|
|
|
|
|
- }
|
|
|
|
|
- it.shouldShowRequestPermissionRationale -> {
|
|
|
|
|
- AlertDialog.Builder(this)
|
|
|
|
|
- .setTitle(R.string.no_access)
|
|
|
|
|
- .setMessage(R.string.no_access_message)
|
|
|
|
|
- .setPositiveButton(R.string.no_access_retry_button) { _, _ ->
|
|
|
|
|
- login()
|
|
|
|
|
- }
|
|
|
|
|
- .setNegativeButton(R.string.dialog_cancel_button, null)
|
|
|
|
|
- .setCancelable(false)
|
|
|
|
|
- .show()
|
|
|
|
|
- }
|
|
|
|
|
- else -> {
|
|
|
|
|
- AlertDialog.Builder(this)
|
|
|
|
|
- .setTitle(R.string.no_permissions)
|
|
|
|
|
- .setMessage(R.string.no_permissions_message)
|
|
|
|
|
- .setPositiveButton(R.string.no_permissions_goto_settings_button) { _, _ ->
|
|
|
|
|
- startActivity(Intent(
|
|
|
|
|
- Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
|
|
|
|
|
- Uri.fromParts("package", packageName, null)
|
|
|
|
|
- ))
|
|
|
|
|
- }
|
|
|
|
|
- .setNegativeButton(R.string.dialog_cancel_button, null)
|
|
|
|
|
- .setCancelable(false)
|
|
|
|
|
- .show()
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }, {
|
|
|
|
|
- logError("cannot request permission", it)
|
|
|
|
|
- })
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private fun refreshOptionsMenu() {
|
|
|
|
|
- loginOption.isVisible = true
|
|
|
|
|
- if (auth.isAuthorized()) {
|
|
|
|
|
- loginOption.setTitle(R.string.option_logout)
|
|
|
|
|
- loginOption.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER)
|
|
|
|
|
- } else {
|
|
|
|
|
- loginOption.setTitle(R.string.option_login)
|
|
|
|
|
- loginOption.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
|
|
|
|
|
|
|
+ private fun login() {
|
|
|
|
|
+ RxPermissions(this).requestEach(READ_PHONE_STATE).subscribe({
|
|
|
|
|
+ when {
|
|
|
|
|
+ it.granted -> {
|
|
|
|
|
+ if (auth.isAuthorized()) {
|
|
|
|
|
+ auth.logout()
|
|
|
|
|
+ refresh()
|
|
|
|
|
+ } else {
|
|
|
|
|
+ startActivityForResult(auth.getTokenRequest(), ULOGIN_REQUEST_CODE)
|
|
|
|
|
+ }
|
|
|
|
|
+ refreshOptionsMenu()
|
|
|
|
|
+ }
|
|
|
|
|
+ it.shouldShowRequestPermissionRationale -> {
|
|
|
|
|
+ AlertDialog.Builder(this)
|
|
|
|
|
+ .setTitle(R.string.no_access)
|
|
|
|
|
+ .setMessage(R.string.no_access_message)
|
|
|
|
|
+ .setPositiveButton(R.string.no_access_retry_button) { _, _ ->
|
|
|
|
|
+ login()
|
|
|
|
|
+ }
|
|
|
|
|
+ .setNegativeButton(R.string.dialog_cancel_button, null)
|
|
|
|
|
+ .setCancelable(false)
|
|
|
|
|
+ .show()
|
|
|
|
|
+ }
|
|
|
|
|
+ else -> {
|
|
|
|
|
+ AlertDialog.Builder(this)
|
|
|
|
|
+ .setTitle(R.string.no_permissions)
|
|
|
|
|
+ .setMessage(R.string.no_permissions_message)
|
|
|
|
|
+ .setPositiveButton(R.string.no_permissions_goto_settings_button) { _, _ ->
|
|
|
|
|
+ startActivity(
|
|
|
|
|
+ Intent(
|
|
|
|
|
+ Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
|
|
|
|
|
+ Uri.fromParts("package", packageName, null)
|
|
|
|
|
+ )
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+ .setNegativeButton(R.string.dialog_cancel_button, null)
|
|
|
|
|
+ .setCancelable(false)
|
|
|
|
|
+ .show()
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }, {
|
|
|
|
|
+ logError("cannot request permission", it)
|
|
|
|
|
+ })
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
- private fun refresh(tab: CurrentTab = currentTab) {
|
|
|
|
|
- setCurrentTab(tab)
|
|
|
|
|
- navigation.selectedItemId = tab.itemId
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private fun setCurrentTab(tab: CurrentTab) {
|
|
|
|
|
- needUpdate = false
|
|
|
|
|
- addBookButton.hide()
|
|
|
|
|
- currentTab = tab
|
|
|
|
|
- when (tab) {
|
|
|
|
|
- HOME_TAB -> refreshHomeTab()
|
|
|
|
|
- USERS_TAB -> refreshUsersTab()
|
|
|
|
|
- NOTES_TAB -> refreshNotesTab()
|
|
|
|
|
|
|
+ private fun refreshOptionsMenu() {
|
|
|
|
|
+ loginOption.isVisible = true
|
|
|
|
|
+ if (auth.isAuthorized()) {
|
|
|
|
|
+ loginOption.setTitle(R.string.option_logout)
|
|
|
|
|
+ loginOption.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ loginOption.setTitle(R.string.option_login)
|
|
|
|
|
+ loginOption.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
- private fun refreshHomeTab() {
|
|
|
|
|
- if (booksProgressBar.alpha > 0) {
|
|
|
|
|
- return
|
|
|
|
|
|
|
+ private fun refresh(tab: CurrentTab = currentTab) {
|
|
|
|
|
+ setCurrentTab(tab)
|
|
|
|
|
+ navigation.selectedItemId = tab.itemId
|
|
|
}
|
|
}
|
|
|
- booksRecyclerView.adapter = allBooksAdapter
|
|
|
|
|
- allBooks.clear()
|
|
|
|
|
- Singles.zip(
|
|
|
|
|
- api.getPlannedBooks(auth.getAccessToken())
|
|
|
|
|
- .map { it.sortedByDescending(PlannedBook::priority) },
|
|
|
|
|
- api.getFinishedBooks(auth.getAccessToken())
|
|
|
|
|
- .map { it.sortedByDescending(FinishedBook::order) }
|
|
|
|
|
- .map { it.groupFinishedBooks() }
|
|
|
|
|
- ).map { (planned, finished) ->
|
|
|
|
|
- mutableListOf<Book>().apply {
|
|
|
|
|
- if (planned.isNotEmpty()) {
|
|
|
|
|
- add(BookHeader(getString(R.string.book_header_todo)))
|
|
|
|
|
- }
|
|
|
|
|
- addAll(planned)
|
|
|
|
|
- addAll(finished)
|
|
|
|
|
- }
|
|
|
|
|
- }.io2main()
|
|
|
|
|
- .doOnSubscribe {
|
|
|
|
|
- booksProgressBar.show()
|
|
|
|
|
- booksPlaceholder.hide()
|
|
|
|
|
|
|
+
|
|
|
|
|
+ private fun setCurrentTab(tab: CurrentTab) {
|
|
|
|
|
+ needUpdate = false
|
|
|
|
|
+ addBookButton.hide()
|
|
|
|
|
+ currentTab = tab
|
|
|
|
|
+ when (tab) {
|
|
|
|
|
+ HOME_TAB -> refreshHomeTab()
|
|
|
|
|
+ USERS_TAB -> refreshUsersTab()
|
|
|
|
|
+ NOTES_TAB -> refreshNotesTab()
|
|
|
}
|
|
}
|
|
|
- .doAfterTerminate {
|
|
|
|
|
- booksProgressBar.hide()
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private fun refreshHomeTab() {
|
|
|
|
|
+ if (booksProgressBar.alpha > 0) {
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
- .subscribe({ books ->
|
|
|
|
|
- if (books.isEmpty()) {
|
|
|
|
|
- booksPlaceholder.setText(R.string.error_no_books)
|
|
|
|
|
- booksPlaceholder.show()
|
|
|
|
|
- }
|
|
|
|
|
- allBooks.addAll(books)
|
|
|
|
|
- allBooksAdapter.notifyDataSetChanged()
|
|
|
|
|
- addBookButton.show()
|
|
|
|
|
- }, {
|
|
|
|
|
- logError("cannot load books", it)
|
|
|
|
|
- booksPlaceholder.setText(
|
|
|
|
|
- if (it is HttpException && it.code() == 401) {
|
|
|
|
|
- R.string.error_unauthorized
|
|
|
|
|
- } else {
|
|
|
|
|
- R.string.error_loading_books
|
|
|
|
|
- }
|
|
|
|
|
- )
|
|
|
|
|
- booksPlaceholder.show()
|
|
|
|
|
- })
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ booksRecyclerView.adapter = allBooksAdapter
|
|
|
|
|
+ allBooks.clear()
|
|
|
|
|
+ Singles.zip(
|
|
|
|
|
+ api.getPlannedBooks(auth.getAccessToken())
|
|
|
|
|
+ .map { it.sortedByDescending(PlannedBook::priority) },
|
|
|
|
|
+ api.getFinishedBooks(auth.getAccessToken())
|
|
|
|
|
+ .map { it.sortedByDescending(FinishedBook::order) }
|
|
|
|
|
+ .map { it.groupFinishedBooks() }
|
|
|
|
|
+ ).map { (planned, finished) ->
|
|
|
|
|
+ mutableListOf<Book>().apply {
|
|
|
|
|
+ if (planned.isNotEmpty()) {
|
|
|
|
|
+ add(BookHeader(getString(R.string.book_header_todo)))
|
|
|
|
|
+ }
|
|
|
|
|
+ addAll(planned)
|
|
|
|
|
+ addAll(finished)
|
|
|
|
|
+ }
|
|
|
|
|
+ }.io2main()
|
|
|
|
|
+ .doOnSubscribe {
|
|
|
|
|
+ booksProgressBar.show()
|
|
|
|
|
+ booksPlaceholder.hide()
|
|
|
|
|
+ }
|
|
|
|
|
+ .doAfterTerminate {
|
|
|
|
|
+ booksProgressBar.hide()
|
|
|
|
|
+ }
|
|
|
|
|
+ .subscribe({ books ->
|
|
|
|
|
+ if (books.isEmpty()) {
|
|
|
|
|
+ booksPlaceholder.setText(R.string.error_no_books)
|
|
|
|
|
+ booksPlaceholder.show()
|
|
|
|
|
+ }
|
|
|
|
|
+ allBooks.addAll(books)
|
|
|
|
|
+ allBooksAdapter.notifyDataSetChanged()
|
|
|
|
|
+ addBookButton.show()
|
|
|
|
|
+ }, {
|
|
|
|
|
+ logError("cannot load books", it)
|
|
|
|
|
+ booksPlaceholder.setText(
|
|
|
|
|
+ if (it is HttpException && it.code() == 401) {
|
|
|
|
|
+ R.string.error_unauthorized
|
|
|
|
|
+ } else {
|
|
|
|
|
+ R.string.error_loading_books
|
|
|
|
|
+ }
|
|
|
|
|
+ )
|
|
|
|
|
+ booksPlaceholder.show()
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- private fun refreshUsersTab() {
|
|
|
|
|
- // todo
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ private fun refreshUsersTab() {
|
|
|
|
|
+ // todo
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- private fun refreshNotesTab() {
|
|
|
|
|
- // todo
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ private fun refreshNotesTab() {
|
|
|
|
|
+ // todo
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- private fun List<FinishedBook>.groupFinishedBooks(): List<Book> {
|
|
|
|
|
- val groupedBooks = mutableListOf<Book>()
|
|
|
|
|
- var previousReadYear = Int.MAX_VALUE.toString()
|
|
|
|
|
- forEachIndexed { index, book ->
|
|
|
|
|
- val readYear = book.readYear
|
|
|
|
|
- if (previousReadYear != readYear) {
|
|
|
|
|
- groupedBooks.add(BookHeader(
|
|
|
|
|
- when {
|
|
|
|
|
- book.readYear.isEmpty() -> getString(R.string.book_header_done_other)
|
|
|
|
|
- index == 0 -> getString(R.string.book_header_done_first, readYear)
|
|
|
|
|
- else -> getString(R.string.book_header_done, readYear)
|
|
|
|
|
|
|
+ private fun List<FinishedBook>.groupFinishedBooks(): List<Book> {
|
|
|
|
|
+ val groupedBooks = mutableListOf<Book>()
|
|
|
|
|
+ var previousReadYear = Int.MAX_VALUE.toString()
|
|
|
|
|
+ forEachIndexed { index, book ->
|
|
|
|
|
+ val readYear = book.readYear
|
|
|
|
|
+ if (previousReadYear != readYear) {
|
|
|
|
|
+ groupedBooks.add(
|
|
|
|
|
+ BookHeader(
|
|
|
|
|
+ when {
|
|
|
|
|
+ book.readYear.isEmpty() -> getString(R.string.book_header_done_other)
|
|
|
|
|
+ index == 0 -> getString(R.string.book_header_done_first, readYear)
|
|
|
|
|
+ else -> getString(R.string.book_header_done, readYear)
|
|
|
|
|
+ }
|
|
|
|
|
+ )
|
|
|
|
|
+ )
|
|
|
}
|
|
}
|
|
|
- ))
|
|
|
|
|
- }
|
|
|
|
|
- groupedBooks.add(book)
|
|
|
|
|
- previousReadYear = book.readYear
|
|
|
|
|
|
|
+ groupedBooks.add(book)
|
|
|
|
|
+ previousReadYear = book.readYear
|
|
|
|
|
+ }
|
|
|
|
|
+ return groupedBooks
|
|
|
}
|
|
}
|
|
|
- return groupedBooks
|
|
|
|
|
- }
|
|
|
|
|
}
|
|
}
|