|
@@ -14,7 +14,9 @@ import android.support.v7.widget.Toolbar
|
|
|
import android.text.format.DateUtils
|
|
import android.text.format.DateUtils
|
|
|
import android.view.MenuItem
|
|
import android.view.MenuItem
|
|
|
import android.view.View
|
|
import android.view.View
|
|
|
|
|
+import android.widget.TextView
|
|
|
import com.tbruyelle.rxpermissions2.RxPermissions
|
|
import com.tbruyelle.rxpermissions2.RxPermissions
|
|
|
|
|
+import io.reactivex.Single
|
|
|
import kotlinx.android.synthetic.main.about.view.*
|
|
import kotlinx.android.synthetic.main.about.view.*
|
|
|
import kotlinx.android.synthetic.main.activity_main.*
|
|
import kotlinx.android.synthetic.main.activity_main.*
|
|
|
import kotlinx.android.synthetic.main.books_page.*
|
|
import kotlinx.android.synthetic.main.books_page.*
|
|
@@ -359,15 +361,7 @@ class MainActivity : AppCompatActivity(), Router {
|
|
|
private fun refreshHomeTab() {
|
|
private fun refreshHomeTab() {
|
|
|
bookRepository.loadBooks()
|
|
bookRepository.loadBooks()
|
|
|
.io2main()
|
|
.io2main()
|
|
|
- .doOnSubscribe {
|
|
|
|
|
- if (!swipeRefresh.isRefreshing) {
|
|
|
|
|
- booksProgressBar.show()
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- .doFinally {
|
|
|
|
|
- booksProgressBar.hide()
|
|
|
|
|
- swipeRefresh.isRefreshing = false
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ .showProgressBar()
|
|
|
.subscribe({ books ->
|
|
.subscribe({ books ->
|
|
|
booksPlaceholder.show(books.isEmpty())
|
|
booksPlaceholder.show(books.isEmpty())
|
|
|
booksErrorPlaceholder.hide()
|
|
booksErrorPlaceholder.hide()
|
|
@@ -376,27 +370,14 @@ class MainActivity : AppCompatActivity(), Router {
|
|
|
allBooksAdapter.notifyDataSetChanged()
|
|
allBooksAdapter.notifyDataSetChanged()
|
|
|
}, {
|
|
}, {
|
|
|
logError("cannot load books", it)
|
|
logError("cannot load books", it)
|
|
|
- if (booksPlaceholder.isVisible || allBooksAdapter.itemCount > 0) {
|
|
|
|
|
- toast(it.messageRes)
|
|
|
|
|
- } else {
|
|
|
|
|
- booksErrorPlaceholder.setText(it.messageRes)
|
|
|
|
|
- booksErrorPlaceholder.show()
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ handleError(it, booksPlaceholder, booksErrorPlaceholder, allBooksAdapter)
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private fun refreshUsersTab() {
|
|
private fun refreshUsersTab() {
|
|
|
api.getSubscriptions(auth.getAccessToken())
|
|
api.getSubscriptions(auth.getAccessToken())
|
|
|
.io2main()
|
|
.io2main()
|
|
|
- .doOnSubscribe {
|
|
|
|
|
- if (!swipeRefresh.isRefreshing) {
|
|
|
|
|
- booksProgressBar.show()
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- .doFinally {
|
|
|
|
|
- booksProgressBar.hide()
|
|
|
|
|
- swipeRefresh.isRefreshing = false
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ .showProgressBar()
|
|
|
.subscribe({ subscriptions ->
|
|
.subscribe({ subscriptions ->
|
|
|
usersPlaceholder.show(subscriptions.isEmpty())
|
|
usersPlaceholder.show(subscriptions.isEmpty())
|
|
|
usersErrorPlaceholder.hide()
|
|
usersErrorPlaceholder.hide()
|
|
@@ -405,27 +386,14 @@ class MainActivity : AppCompatActivity(), Router {
|
|
|
usersAdapter.notifyDataSetChanged()
|
|
usersAdapter.notifyDataSetChanged()
|
|
|
}, {
|
|
}, {
|
|
|
logError("cannot load users", it)
|
|
logError("cannot load users", it)
|
|
|
- if (usersPlaceholder.isVisible || usersAdapter.itemCount > 0) {
|
|
|
|
|
- toast(it.messageRes)
|
|
|
|
|
- } else {
|
|
|
|
|
- usersErrorPlaceholder.setText(it.messageRes)
|
|
|
|
|
- usersErrorPlaceholder.show()
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ handleError(it, usersPlaceholder, usersErrorPlaceholder, usersAdapter)
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private fun refreshNotesTab() {
|
|
private fun refreshNotesTab() {
|
|
|
api.getLatestBooksWithNotes()
|
|
api.getLatestBooksWithNotes()
|
|
|
.io2main()
|
|
.io2main()
|
|
|
- .doOnSubscribe {
|
|
|
|
|
- if (!swipeRefresh.isRefreshing) {
|
|
|
|
|
- booksProgressBar.show()
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- .doFinally {
|
|
|
|
|
- booksProgressBar.hide()
|
|
|
|
|
- swipeRefresh.isRefreshing = false
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ .showProgressBar()
|
|
|
.subscribe({ notes ->
|
|
.subscribe({ notes ->
|
|
|
notesPlaceholder.show(notes.isEmpty())
|
|
notesPlaceholder.show(notes.isEmpty())
|
|
|
notesErrorPlaceholder.hide()
|
|
notesErrorPlaceholder.hide()
|
|
@@ -434,15 +402,34 @@ class MainActivity : AppCompatActivity(), Router {
|
|
|
notesAdapter.notifyDataSetChanged()
|
|
notesAdapter.notifyDataSetChanged()
|
|
|
}, {
|
|
}, {
|
|
|
logError("cannot load notes", it)
|
|
logError("cannot load notes", it)
|
|
|
- if (notesPlaceholder.isVisible || notesAdapter.itemCount > 0) {
|
|
|
|
|
- toast(it.messageRes)
|
|
|
|
|
- } else {
|
|
|
|
|
- notesErrorPlaceholder.setText(it.messageRes)
|
|
|
|
|
- notesErrorPlaceholder.show()
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ handleError(it, notesPlaceholder, notesErrorPlaceholder, notesAdapter)
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private fun <T> Single<T>.showProgressBar() =
|
|
|
|
|
+ doOnSubscribe {
|
|
|
|
|
+ if (!swipeRefresh.isRefreshing) {
|
|
|
|
|
+ booksProgressBar.show()
|
|
|
|
|
+ }
|
|
|
|
|
+ }.doFinally {
|
|
|
|
|
+ booksProgressBar.hide()
|
|
|
|
|
+ swipeRefresh.isRefreshing = false
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private fun handleError(
|
|
|
|
|
+ th: Throwable,
|
|
|
|
|
+ placeholder: View,
|
|
|
|
|
+ errPlaceholder: TextView,
|
|
|
|
|
+ adapter: RecyclerView.Adapter<*>
|
|
|
|
|
+ ) {
|
|
|
|
|
+ if (placeholder.isVisible || adapter.itemCount > 0) {
|
|
|
|
|
+ toast(th.messageRes)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ errPlaceholder.setText(th.messageRes)
|
|
|
|
|
+ errPlaceholder.show()
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private val Throwable.messageRes
|
|
private val Throwable.messageRes
|
|
|
get() = if (this is HttpException && code() == 401) {
|
|
get() = if (this is HttpException && code() == 401) {
|
|
|
R.string.error_unauthorized
|
|
R.string.error_unauthorized
|