|
|
@@ -4,18 +4,26 @@ import android.os.Bundle
|
|
|
import android.support.v7.app.AppCompatActivity
|
|
|
import android.support.v7.widget.Toolbar
|
|
|
import android.view.View
|
|
|
-import android.view.View.*
|
|
|
+import android.view.View.GONE
|
|
|
+import android.view.View.VISIBLE
|
|
|
import android.widget.CheckBox
|
|
|
-import android.widget.EditText
|
|
|
import android.widget.ImageView
|
|
|
+import android.widget.TextView
|
|
|
import com.bumptech.glide.Glide
|
|
|
import com.bumptech.glide.request.RequestOptions
|
|
|
import me.vadik.knigopis.api.BookCoverSearch
|
|
|
import me.vadik.knigopis.api.BookCoverSearchImpl
|
|
|
+import me.vadik.knigopis.api.Endpoint
|
|
|
import me.vadik.knigopis.api.ImageEndpoint
|
|
|
+import me.vadik.knigopis.auth.KAuth
|
|
|
+import me.vadik.knigopis.auth.KAuthImpl
|
|
|
+import me.vadik.knigopis.model.FinishedBookToSend
|
|
|
+import me.vadik.knigopis.model.PlannedBookToSend
|
|
|
|
|
|
class BookActivity : AppCompatActivity() {
|
|
|
|
|
|
+ private val api by lazy { app().baseApi.create(Endpoint::class.java) }
|
|
|
+ private val auth by lazy { KAuthImpl(applicationContext, api) as KAuth }
|
|
|
private val imageSearch: BookCoverSearch by lazy {
|
|
|
BookCoverSearchImpl(
|
|
|
app().imageApi.create(ImageEndpoint::class.java),
|
|
|
@@ -23,14 +31,18 @@ class BookActivity : AppCompatActivity() {
|
|
|
)
|
|
|
}
|
|
|
private val toolbar by lazy { findView<Toolbar>(R.id.toolbar) }
|
|
|
- private val titleEditText by lazy { findView<EditText>(R.id.book_title_edit_text) }
|
|
|
- private val coverImageView by lazy { findView<ImageView>(R.id.cover_image_view) }
|
|
|
+ private val titleEditText by lazy { findView<TextView>(R.id.book_title_edit_text) }
|
|
|
+ private val authorEditText by lazy { findView<TextView>(R.id.book_author_edit_text) }
|
|
|
+ private val dayEditText by lazy { findView<TextView>(R.id.book_day_edit_text) }
|
|
|
+ private val monthEditText by lazy { findView<TextView>(R.id.book_month_edit_text) }
|
|
|
+ private val yearEditText by lazy { findView<TextView>(R.id.book_year_edit_text) }
|
|
|
private val readCheckbox by lazy { findView<CheckBox>(R.id.book_read_checkbox) }
|
|
|
+ private val coverImageView by lazy { findView<ImageView>(R.id.cover_image_view) }
|
|
|
private val dateInputViews by lazy {
|
|
|
arrayOf<View>(
|
|
|
- findView(R.id.book_year_input),
|
|
|
+ findView(R.id.book_day_input),
|
|
|
findView(R.id.book_month_input),
|
|
|
- findView(R.id.book_day_input)
|
|
|
+ findView(R.id.book_year_input)
|
|
|
)
|
|
|
}
|
|
|
|
|
|
@@ -45,7 +57,23 @@ class BookActivity : AppCompatActivity() {
|
|
|
toolbar.setOnMenuItemClickListener {
|
|
|
when (it.itemId) {
|
|
|
R.id.option_save_book -> {
|
|
|
- finish()
|
|
|
+ if (readCheckbox.isChecked) {
|
|
|
+ api.postFinishedBook(auth.getAccessToken(), FinishedBookToSend(
|
|
|
+ titleEditText.text.toString(),
|
|
|
+ authorEditText.text.toString(),
|
|
|
+ dayEditText.text.toString(),
|
|
|
+ monthEditText.text.toString(),
|
|
|
+ yearEditText.text.toString()
|
|
|
+ ))
|
|
|
+ } else {
|
|
|
+ api.postPlannedBook(auth.getAccessToken(), PlannedBookToSend(
|
|
|
+ titleEditText.text.toString(),
|
|
|
+ authorEditText.text.toString()
|
|
|
+ ))
|
|
|
+ }.io2main().subscribe(
|
|
|
+ { finish() },
|
|
|
+ { logError("cannot post planned book", it) }
|
|
|
+ )
|
|
|
true
|
|
|
}
|
|
|
else -> false
|
|
|
@@ -66,8 +94,8 @@ class BookActivity : AppCompatActivity() {
|
|
|
}
|
|
|
}
|
|
|
readCheckbox.setOnCheckedChangeListener { _, checked ->
|
|
|
- dateInputViews.forEach {
|
|
|
- it.visibility = if (checked) VISIBLE else GONE
|
|
|
+ dateInputViews.forEach { view ->
|
|
|
+ view.visibility = if (checked) VISIBLE else GONE
|
|
|
}
|
|
|
}
|
|
|
}
|