sirekanyan 8 år sedan
förälder
incheckning
38b250b01c

+ 15 - 0
app/src/main/java/me/vadik/knigopis/CurrentTab.kt

@@ -0,0 +1,15 @@
+package me.vadik.knigopis
+
+import android.support.annotation.IdRes
+
+enum class CurrentTab(@IdRes val itemId: Int) {
+
+  HOME_TAB(R.id.navigation_home),
+  DONE_TAB(R.id.navigation_done),
+  TODO_TAB(R.id.navigation_todo);
+
+  companion object {
+    fun getByItemId(@IdRes itemId: Int) =
+        checkNotNull(values().find { it.itemId == itemId })
+  }
+}

+ 3 - 6
app/src/main/java/me/vadik/knigopis/Endpoint.kt

@@ -4,16 +4,13 @@ import me.vadik.knigopis.model.Book
 import me.vadik.knigopis.model.Credentials
 import me.vadik.knigopis.model.User
 import retrofit2.Call
-import retrofit2.http.Field
-import retrofit2.http.FormUrlEncoded
 import retrofit2.http.GET
-import retrofit2.http.POST
+import retrofit2.http.Query
 
 interface Endpoint {
 
-  @FormUrlEncoded
-  @POST("/user/get-credentials")
-  fun getCredentials(@Field("token") token: String): Call<Credentials>
+  @GET("/user/get-credentials")
+  fun getCredentials(@Query("token") token: String): Call<Credentials>
 
   @GET("users/latest")
   fun latestUsers(): Call<Map<String, User>>

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

@@ -6,6 +6,7 @@ import android.support.v7.app.AppCompatActivity
 import android.support.v7.widget.LinearLayoutManager
 import android.support.v7.widget.RecyclerView
 import android.support.v7.widget.Toolbar
+import me.vadik.knigopis.CurrentTab.*
 import me.vadik.knigopis.model.Book
 import me.vadik.knigopis.model.User
 import retrofit2.Call
@@ -15,21 +16,16 @@ import retrofit2.Response
 class MainActivity : AppCompatActivity() {
 
   private val api by lazy { app().retrofit.create(Endpoint::class.java) }
-  private val recyclerView by lazy { findViewById(R.id.recycler_view) as RecyclerView }
-  private val toolbar by lazy { findViewById(R.id.toolbar) as Toolbar }
   private val users = mutableListOf<User>()
+  private val adapter = UsersAdapter(users)
+  private lateinit var currentTab: CurrentTab
 
   override fun onCreate(savedInstanceState: Bundle?) {
     super.onCreate(savedInstanceState)
     setContentView(R.layout.activity_main)
-    val navigation = findViewById(R.id.navigation) as BottomNavigationView
-    navigation.setOnNavigationItemSelectedListener { item ->
-      toolbar.title = item.title
-      true
-    }
-    val adapter = UsersAdapter(users)
-    recyclerView.adapter = adapter
-    recyclerView.layoutManager = LinearLayoutManager(this)
+    initNavigationView(findView(R.id.navigation))
+    initRecyclerView(findView(R.id.recycler_view))
+    initToolbar(findView(R.id.toolbar))
     api.latestUsers().enqueue(object : Callback<Map<String, User>> {
       override fun onResponse(call: Call<Map<String, User>>?, response: Response<Map<String, User>>?) {
         users.clear()
@@ -55,4 +51,21 @@ class MainActivity : AppCompatActivity() {
       }
     })
   }
+
+  private fun initNavigationView(navigation: BottomNavigationView) {
+    currentTab = HOME_TAB
+    navigation.setOnNavigationItemSelectedListener { item ->
+      currentTab = CurrentTab.getByItemId(item.itemId)
+      true
+    }
+  }
+
+  private fun initRecyclerView(recyclerView: RecyclerView) {
+    recyclerView.adapter = adapter
+    recyclerView.layoutManager = LinearLayoutManager(this@MainActivity)
+  }
+
+  private fun initToolbar(toolbar: Toolbar) {
+    toolbar.inflateMenu(R.menu.options)
+  }
 }

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

@@ -1,6 +1,7 @@
 package me.vadik.knigopis
 
 import android.app.Activity
+import android.support.annotation.IdRes
 import android.support.annotation.LayoutRes
 import android.util.Log
 import android.view.LayoutInflater
@@ -11,6 +12,8 @@ private const val TAG = "Knigopis"
 
 fun Activity.app() = application as App
 
+fun <T : View> Activity.findView(@IdRes id: Int): T = findViewById(id)
+
 fun logw(message: String) = Log.w(TAG, message)
 
 fun log(message: String, throwable: Throwable?) = Log.e(TAG, message, throwable)

+ 2 - 1
app/src/main/res/layout/activity_main.xml

@@ -17,7 +17,8 @@
         <android.support.v7.widget.Toolbar
             android:id="@+id/toolbar"
             android:layout_width="match_parent"
-            android:layout_height="wrap_content"/>
+            android:layout_height="wrap_content"
+            app:title="@string/app_name"/>
 
     </android.support.design.widget.AppBarLayout>
 

+ 2 - 11
app/src/main/res/menu/options.xml

@@ -1,17 +1,8 @@
 <?xml version="1.0" encoding="utf-8"?>
-<menu xmlns:android="http://schemas.android.com/apk/res/android"
-      xmlns:app="http://schemas.android.com/apk/res-auto">
+<menu xmlns:android="http://schemas.android.com/apk/res/android">
 
     <item
         android:id="@+id/option_login"
-        android:title="@string/option_login"
-        android:visible="false"
-        app:showAsAction="ifRoom"/>
-
-    <item
-        android:id="@+id/option_logout"
-        android:title="@string/option_logout"
-        android:visible="false"
-        app:showAsAction="never"/>
+        android:visible="false"/>
 
 </menu>