Procházet zdrojové kódy

Added deleting books functionality

sirekanyan před 8 roky
rodič
revize
6e7be9ac56

+ 1 - 1
app/src/main/java/me/vadik/knigopis/MainActivity.kt

@@ -32,7 +32,7 @@ class MainActivity : AppCompatActivity() {
     BooksAdapter(BookCoverSearchImpl(
         app().imageApi.create(ImageEndpoint::class.java),
         getSharedPreferences("knigopis", MODE_PRIVATE)
-    ))
+    ), api, auth)
   }
   private val allBooksAdapter by lazy { booksAdapter.build(allBooks) }
   private val finishedBooksAdapter by lazy { booksAdapter.build(finishedBooks) }

+ 13 - 4
app/src/main/java/me/vadik/knigopis/api/Endpoint.kt

@@ -3,10 +3,7 @@ package me.vadik.knigopis.api
 import io.reactivex.Completable
 import io.reactivex.Single
 import me.vadik.knigopis.model.*
-import retrofit2.http.Body
-import retrofit2.http.GET
-import retrofit2.http.POST
-import retrofit2.http.Query
+import retrofit2.http.*
 
 interface Endpoint {
 
@@ -22,6 +19,12 @@ interface Endpoint {
       @Body book: FinishedBookToSend
   ): Completable
 
+  @DELETE("books/{id}")
+  fun deleteFinishedBook(
+      @Path("id") id: String,
+      @Query("access-token") accessToken: String
+  ): Completable
+
   @GET("wishes")
   fun getPlannedBooks(@Query("access-token") accessToken: String): Single<List<PlannedBook>>
 
@@ -31,6 +34,12 @@ interface Endpoint {
       @Body book: PlannedBookToSend
   ): Completable
 
+  @DELETE("wishes/{id}")
+  fun deletePlannedBook(
+      @Path("id") id: String,
+      @Query("access-token") accessToken: String
+  ): Completable
+
   @GET("users/latest")
   fun getLatestUsers(): Single<Map<String, User>>
 

+ 6 - 0
app/src/main/java/me/vadik/knigopis/extensions.kt

@@ -1,12 +1,14 @@
 package me.vadik.knigopis
 
 import android.app.Activity
+import android.content.Context
 import android.support.annotation.IdRes
 import android.support.annotation.LayoutRes
 import android.util.Log
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
+import android.widget.Toast
 import io.reactivex.Completable
 import io.reactivex.Flowable
 import io.reactivex.Single
@@ -15,6 +17,8 @@ import io.reactivex.schedulers.Schedulers
 
 private const val TAG = "Knigopis"
 
+fun Context.toast(message: String) = Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
+
 fun Activity.app() = application as App
 
 fun <T : View> Activity.findView(@IdRes id: Int): T = findViewById(id)
@@ -34,3 +38,5 @@ fun <T> Flowable<T>.io2main(): Flowable<T> =
 
 fun Completable.io2main(): Completable =
     subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
+
+fun String.orDefault(default: String) = if (isEmpty()) default else this

+ 4 - 0
app/src/main/java/me/vadik/knigopis/model/Book.kt

@@ -1,7 +1,11 @@
 package me.vadik.knigopis.model
 
+import me.vadik.knigopis.orDefault
+
 interface Book {
   val id: String
   val title: String
   val author: String
+  val titleOrDefault get() = title.orDefault("(без названия)")
+  val authorOrDefault get() = author.orDefault("(автор не указан)")
 }

+ 1 - 0
app/src/main/res/layout/book.xml

@@ -2,6 +2,7 @@
 <FrameLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
+    android:id="@+id/book_item_container"
     android:layout_width="match_parent"
     android:layout_height="72dp">