|
|
@@ -12,14 +12,12 @@ import kotlinx.android.synthetic.main.profile_activity.*
|
|
|
import me.vadik.knigopis.*
|
|
|
import me.vadik.knigopis.api.Endpoint
|
|
|
import me.vadik.knigopis.auth.KAuth
|
|
|
+import me.vadik.knigopis.common.clearAndAddAll
|
|
|
import me.vadik.knigopis.common.createTextShareIntent
|
|
|
+import me.vadik.knigopis.common.random
|
|
|
import me.vadik.knigopis.common.setCircleImage
|
|
|
-import me.vadik.knigopis.model.Book
|
|
|
-import me.vadik.knigopis.model.PlannedBook
|
|
|
-import me.vadik.knigopis.model.Profile
|
|
|
+import me.vadik.knigopis.model.*
|
|
|
import org.koin.android.ext.android.inject
|
|
|
-import java.util.*
|
|
|
-
|
|
|
|
|
|
fun Context.createProfileIntent() = Intent(this, ProfileActivity::class.java)
|
|
|
|
|
|
@@ -27,7 +25,6 @@ class ProfileActivity : AppCompatActivity() {
|
|
|
|
|
|
private val api by inject<Endpoint>()
|
|
|
private val auth by inject<KAuth>()
|
|
|
- private val random = Random()
|
|
|
private val todoList = mutableListOf<Book>()
|
|
|
private val doingList = mutableListOf<Book>()
|
|
|
private val doneList = mutableListOf<Book>()
|
|
|
@@ -83,44 +80,45 @@ class ProfileActivity : AppCompatActivity() {
|
|
|
}
|
|
|
|
|
|
private fun refreshProfile() {
|
|
|
- api.getProfile(auth.getAccessToken())
|
|
|
- .io2main()
|
|
|
- .subscribe({ user ->
|
|
|
- userId = user.id
|
|
|
- profileUrl = user.fixedProfile
|
|
|
- profileNickname.text = user.nickname.orEmpty()
|
|
|
- profileAvatar.setCircleImage(user.photo)
|
|
|
- editOption.isVisible = true
|
|
|
- }, {
|
|
|
+ api.getProfile(auth.getAccessToken()).io2main()
|
|
|
+ .subscribe(::onRefreshProfile, {
|
|
|
logError("cannot get profile", it)
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- @Suppress("USELESS_CAST")
|
|
|
+ private fun onRefreshProfile(user: Credentials.UserFull) {
|
|
|
+ userId = user.id
|
|
|
+ profileUrl = user.fixedProfile
|
|
|
+ profileNickname.text = user.nickname.orEmpty()
|
|
|
+ profileAvatar.setCircleImage(user.photo)
|
|
|
+ editOption.isVisible = true
|
|
|
+ }
|
|
|
+
|
|
|
private fun refreshCounters() {
|
|
|
- api.getFinishedBooks(auth.getAccessToken())
|
|
|
- .io2main()
|
|
|
- .subscribe({ finishedBooks ->
|
|
|
- doneList.clearAndAddAll(finishedBooks)
|
|
|
- profileDoneCount.text =
|
|
|
- getString(R.string.profile_caption_done, doneList.size as Int)
|
|
|
- }, {
|
|
|
+ api.getFinishedBooks(auth.getAccessToken()).io2main()
|
|
|
+ .subscribe(::onRefreshFinishedBooks, {
|
|
|
logError("cannot check finished books count", it)
|
|
|
})
|
|
|
- api.getPlannedBooks(auth.getAccessToken())
|
|
|
- .io2main()
|
|
|
- .subscribe({ plannedBooks ->
|
|
|
- doingList.clearAndAddAll(plannedBooks.filter { it.priority > 0 })
|
|
|
- profileDoingCount.text =
|
|
|
- getString(R.string.profile_caption_doing, doingList.size as Int)
|
|
|
- todoList.clearAndAddAll(plannedBooks.filter { it.priority == 0 })
|
|
|
- profileTodoCount.text =
|
|
|
- getString(R.string.profile_caption_todo, todoList.size as Int)
|
|
|
- }, {
|
|
|
+ api.getPlannedBooks(auth.getAccessToken()).io2main()
|
|
|
+ .subscribe(::onRefreshPlannedBooks, {
|
|
|
logError("cannot check planned books count", it)
|
|
|
})
|
|
|
}
|
|
|
|
|
|
+ @Suppress("USELESS_CAST")
|
|
|
+ private fun onRefreshFinishedBooks(finishedBooks: List<FinishedBook>) {
|
|
|
+ doneList.clearAndAddAll(finishedBooks)
|
|
|
+ profileDoneCount.text = getString(R.string.profile_caption_done, doneList.size as Int)
|
|
|
+ }
|
|
|
+
|
|
|
+ @Suppress("USELESS_CAST")
|
|
|
+ private fun onRefreshPlannedBooks(plannedBooks: List<PlannedBook>) {
|
|
|
+ doingList.clearAndAddAll(plannedBooks.filter { it.priority > 0 })
|
|
|
+ profileDoingCount.text = getString(R.string.profile_caption_doing, doingList.size as Int)
|
|
|
+ todoList.clearAndAddAll(plannedBooks.filter { it.priority == 0 })
|
|
|
+ profileTodoCount.text = getString(R.string.profile_caption_todo, todoList.size as Int)
|
|
|
+ }
|
|
|
+
|
|
|
private fun updateNicknameOrExitEditMode() {
|
|
|
if (profileNickname.text.toString() == profileNicknameEditText.text.toString()) {
|
|
|
quitEditMode()
|
|
|
@@ -209,14 +207,4 @@ class ProfileActivity : AppCompatActivity() {
|
|
|
private val isEditMode
|
|
|
get() = profileNicknameSwitcher.displayedChild == 1
|
|
|
|
|
|
- private fun <T> List<T>.random(): T? {
|
|
|
- if (size == 0) return null
|
|
|
- return get(random.nextInt(size))
|
|
|
- }
|
|
|
-
|
|
|
- private fun <T> MutableCollection<T>.clearAndAddAll(collection: Collection<T>) {
|
|
|
- clear()
|
|
|
- addAll(collection)
|
|
|
- }
|
|
|
-
|
|
|
}
|