Explorar o código

admin page in progress

dola hai 1 ano
pai
achega
04e331f44f

+ 56 - 1
app/src/main/java/com/sambath/kunkhmer/remote/FighterDataModel.kt

@@ -67,4 +67,59 @@ data class Fighters(
     val filename: String,
 )
 
-//------------- Image
+//------------- Create Match Data Model
+
+@JsonClass(generateAdapter = true)
+class CreateMatchRequest(
+    @Json(name = "date")
+    val date: String,
+    @Json(name = "title")
+    val title: String,
+    @Json(name = "desc")
+    val desc: String,
+    @Json(name = "fighter_id")
+    val fighterId: List<String>
+)
+
+@JsonClass(generateAdapter = true)
+data class CreateMatchResponse(
+    @Json(name = "code")
+    val resultCode: Int,
+    @Json(name = "message")
+    val message: ResponseMessage,
+    @Json(name = "data")
+    val data: MatchCreatedData?
+)
+
+@JsonClass(generateAdapter = true)
+data class MatchCreatedData(
+    @Json(name = "match")
+    val obj: CreatedMatch,
+)
+
+@JsonClass(generateAdapter = true)
+data class CreatedMatch(
+    @Json(name = "desc")
+    val desc: String,
+
+    @Json(name = "fighter_id")
+    val fighterId: List<String>,
+
+    @Json(name = "_id")
+    val id: String,
+
+    @Json(name = "date")
+    val date: String,
+
+    @Json(name = "title")
+    val title: String,
+
+    @Json(name = "createdAt")
+    val createdAt: String,
+
+    @Json(name = "updatedAt")
+    val updatedAt: String,
+
+    @Json(name = "__v")
+    val _v: Int,
+)

+ 3 - 0
app/src/main/java/com/sambath/kunkhmer/remote/service/ApiService.kt

@@ -41,6 +41,9 @@ interface ApiService {
 
     @POST("/v1/new/create")
     fun createNews(@Body createNewsRequest: CreateNewsRequest): Single<CreateNewsResponse>
+
+    @POST("/v1/match/create")
+    fun createMatch(@Body createMatchRequest: CreateMatchRequest): Single<CreateMatchResponse>
     ///---------------------------------------------------------
 
     @POST("/v1/api/user/changepassword")

+ 36 - 40
app/src/main/java/com/sambath/kunkhmer/screen/fighter/FighterAdminFragment.kt

@@ -5,56 +5,52 @@ import androidx.fragment.app.Fragment
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
+import android.widget.Toast
 import com.sambath.kunkhmer.R
+import com.sambath.kunkhmer.app.App
+import kotlinx.android.synthetic.main.fragment_fighter_admin.view.et_date
+import kotlinx.android.synthetic.main.fragment_fighter_admin.view.et_desc
+import kotlinx.android.synthetic.main.fragment_fighter_admin.view.et_fighter_1
+import kotlinx.android.synthetic.main.fragment_fighter_admin.view.et_fighter_2
+import kotlinx.android.synthetic.main.fragment_fighter_admin.view.et_title
 
-// TODO: Rename parameter arguments, choose names that match
-// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
-private const val ARG_PARAM1 = "param1"
-private const val ARG_PARAM2 = "param2"
-
-/**
- * A simple [Fragment] subclass.
- * Use the [FighterAdminFragment.newInstance] factory method to
- * create an instance of this fragment.
- */
 class FighterAdminFragment : Fragment() {
-    // TODO: Rename and change types of parameters
-    private var param1: String? = null
-    private var param2: String? = null
+    private var _root: View? = null
+    private val binding get() = _root!!
+    private lateinit var fighterViewModel: FighterViewModel
+
+    private final val fighter1 = "650d97d08cc32f66d67c0b60"
+    private final val fighter2 = "650d98498cc32f66d67c0b65"
+    private final val desc = "Keo Rumchong VS Thiago Teixeira"
+    private final val title = "Keo Rumchong VS Thiago Teixeira"
+    private final val date = "2023-10-30T12:55:04.395+0000"
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
-        arguments?.let {
-            param1 = it.getString(ARG_PARAM1)
-            param2 = it.getString(ARG_PARAM2)
-        }
     }
 
-    override fun onCreateView(
-        inflater: LayoutInflater, container: ViewGroup?,
-        savedInstanceState: Bundle?
-    ): View? {
+    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
+        _root = inflater.inflate(R.layout.fragment_fighter_admin, container, false)
+
+        fighterViewModel = FighterViewModel(App.injectApiService(), App.injectPrefHelper())
+        fighterViewModel.state.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
+            render(it)
+        })
+
         // Inflate the layout for this fragment
-        return inflater.inflate(R.layout.fragment_fighter_admin, container, false)
+        return binding
     }
 
-    companion object {
-        /**
-         * Use this factory method to create a new instance of
-         * this fragment using the provided parameters.
-         *
-         * @param param1 Parameter 1.
-         * @param param2 Parameter 2.
-         * @return A new instance of fragment FighterAdminFragment.
-         */
-        // TODO: Rename and change types and number of parameters
-        @JvmStatic
-        fun newInstance(param1: String, param2: String) =
-            FighterAdminFragment().apply {
-                arguments = Bundle().apply {
-                    putString(ARG_PARAM1, param1)
-                    putString(ARG_PARAM2, param2)
-                }
-            }
+    private fun render(state: FighterViewState) {
+        if (state.isLoginSuccess) {
+            Toast.makeText(context, "Create Success...", Toast.LENGTH_LONG).show()
+
+            binding.et_date.setText("")
+            binding.et_title.setText("")
+            binding.et_desc.setText("")
+            binding.et_fighter_1.setText("")
+            binding.et_fighter_2.setText("")
+        }
+        // Other state handling...
     }
 }

+ 29 - 3
app/src/main/java/com/sambath/kunkhmer/screen/fighter/FighterViewModel.kt

@@ -7,6 +7,8 @@ import androidx.lifecycle.LiveData
 import androidx.lifecycle.MutableLiveData
 import com.sambath.kunkhmer.app.getErrorCode
 import com.sambath.kunkhmer.base.BaseViewModel
+import com.sambath.kunkhmer.remote.CreateMatchRequest
+import com.sambath.kunkhmer.remote.CreateNewsRequest
 import com.sambath.kunkhmer.remote.MatchesData
 import com.sambath.kunkhmer.remote.service.ApiService
 import com.sambath.kunkhmer.util.PrefHelper
@@ -15,7 +17,7 @@ import io.reactivex.schedulers.Schedulers
 import okhttp3.ResponseBody
 import java.util.concurrent.TimeUnit
 
-    class FighterViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : BaseViewModel() {
+class FighterViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : BaseViewModel() {
     private val _state = MutableLiveData<FighterViewState>(FighterViewState())
     val state: LiveData<FighterViewState> = _state
     private fun prev() = _state.value!!
@@ -43,7 +45,31 @@ import java.util.concurrent.TimeUnit
         )
     }
 
-    fun getFighterImage(imageUrl: String) : LiveData<Bitmap?> {
+    fun createMatch(date: String, title: String, desc: String, fighterId: List<String>) {
+        if (_state.value!!.isProgress) return
+        _state.value = prev().copy(isProgress = true)
+        val request = CreateMatchRequest(date, title, desc, fighterId)
+
+        disposables.add(
+            apiService.createMatch(request)
+                .timeout(10, TimeUnit.SECONDS)
+                .subscribeOn(Schedulers.io())
+                .observeOn(AndroidSchedulers.mainThread())
+                .subscribe({
+                    if (it.resultCode == 1) {
+                        _state.value = prev().copy(isProgress = false, isLoginSuccess = true)
+                    } else {
+                        _state.value =
+                            prev().copy(isProgress = false, error = "[${it.message.description}]")
+                    }
+                }, {
+                    val message: String = "ប្រតិបត្តិការមិនជោគជ័យ " + it.getErrorCode()
+                    _state.value = prev().copy(isProgress = false, error = message)
+                })
+        )
+    }
+
+    fun getFighterImage(imageUrl: String): LiveData<Bitmap?> {
         val imageLiveData = MutableLiveData<Bitmap?>()
         disposables.add(apiService.getFighterImage(imageUrl)
             .map { responseBody -> processResponseIntoImage(responseBody) }
@@ -72,6 +98,6 @@ data class FighterViewState(
     val isProgress: Boolean = false,
     val isLoginSuccess: Boolean = false,
     val error: String? = null,
-    val matchesData: MatchesData ? = null,
+    val matchesData: MatchesData? = null,
     val fighterImage: Bitmap? = null
 )

+ 108 - 8
app/src/main/res/layout/fragment_fighter_admin.xml

@@ -1,14 +1,114 @@
 <?xml version="1.0" encoding="utf-8"?>
-<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:tools="http://schemas.android.com/tools"
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
-    tools:context=".screen.fighter.FighterAdminFragment">
+    android:background="@color/color_black"
+    android:orientation="vertical"
+    android:padding="10dp">
 
-    <!-- TODO: Update blank fragment layout -->
-    <TextView
+    <com.google.android.material.textfield.TextInputLayout
+        android:id="@+id/til_date"
+        style="@style/BoxInputLayout"
         android:layout_width="match_parent"
-        android:layout_height="match_parent"
-        android:text="@string/hello_blank_fragment" />
+        android:layout_height="48dp"
+        app:endIconMode="clear_text"
+        app:hintEnabled="false">
 
-</FrameLayout>
+        <androidx.appcompat.widget.AppCompatEditText
+            android:id="@+id/et_date"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:background="@color/color_white"
+            android:hint="@string/hint_date"
+            android:inputType="text"
+            android:maxLength="50"
+            android:padding="0dp" />
+    </com.google.android.material.textfield.TextInputLayout>
+
+    <com.google.android.material.textfield.TextInputLayout
+        android:id="@+id/til_title"
+        style="@style/BoxInputLayout"
+        android:layout_width="match_parent"
+        android:layout_height="48dp"
+        app:endIconMode="clear_text"
+        app:hintEnabled="false">
+
+        <androidx.appcompat.widget.AppCompatEditText
+            android:id="@+id/et_title"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:background="@color/color_white"
+            android:hint="@string/hint_title"
+            android:inputType="text"
+            android:maxLength="50"
+            android:padding="0dp" />
+    </com.google.android.material.textfield.TextInputLayout>
+
+    <com.google.android.material.textfield.TextInputLayout
+        android:id="@+id/til_fighter_1"
+        style="@style/BoxInputLayout"
+        android:layout_width="match_parent"
+        android:layout_height="48dp"
+        app:endIconMode="clear_text"
+        app:hintEnabled="false">
+
+        <androidx.appcompat.widget.AppCompatEditText
+            android:id="@+id/et_fighter_1"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:background="@color/color_white"
+            android:hint="@string/hint_fighter_1"
+            android:inputType="text"
+            android:maxLength="50"
+            android:padding="0dp" />
+    </com.google.android.material.textfield.TextInputLayout>
+
+    <com.google.android.material.textfield.TextInputLayout
+        android:id="@+id/til_fighter_2"
+        style="@style/BoxInputLayout"
+        android:layout_width="match_parent"
+        android:layout_height="48dp"
+        app:endIconMode="clear_text"
+        app:hintEnabled="false">
+
+        <androidx.appcompat.widget.AppCompatEditText
+            android:id="@+id/et_fighter_2"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:background="@color/color_white"
+            android:hint="@string/hint_fighter_2"
+            android:inputType="text"
+            android:maxLength="50"
+            android:padding="0dp" />
+    </com.google.android.material.textfield.TextInputLayout>
+
+    <com.google.android.material.textfield.TextInputLayout
+        android:id="@+id/til_desc"
+        style="@style/BoxInputLayout"
+        android:layout_width="match_parent"
+        android:layout_height="200dp"
+        app:endIconMode="clear_text"
+        app:hintEnabled="false">
+
+        <androidx.appcompat.widget.AppCompatEditText
+            android:id="@+id/et_desc"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:background="@color/color_white"
+            android:gravity="top"
+            android:hint="@string/hint_desc"
+            android:inputType="text"
+            android:maxLength="50"
+            android:padding="0dp" />
+    </com.google.android.material.textfield.TextInputLayout>
+
+    <Button
+        android:id="@+id/submitButton"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="center"
+        android:paddingStart="16dp"
+        android:paddingEnd="16dp"
+        android:text="Submit" />
+</LinearLayout>

+ 3 - 0
app/src/main/res/values/strings.xml

@@ -100,4 +100,7 @@
     <string name="English">English</string>
     <string name="hint_title">Title</string>
     <string name="hint_desc">Description</string>
+    <string name="hint_date">Date</string>
+    <string name="hint_fighter_1">Fighter 1</string>
+    <string name="hint_fighter_2">Fighter 2</string>
 </resources>