Pārlūkot izejas kodu

admin page in progress

dola 1 gadu atpakaļ
vecāks
revīzija
7e0ea522c2

+ 21 - 1
app/src/main/java/com/sambath/kunkhmer/remote/LivesDataModel.kt

@@ -65,4 +65,24 @@ data class LivesInfo(
 //    val nationality: String,
 //    @Json(name = "filename")
 //    val filename: String,
-//)
+//)
+
+///////////////////////////////////////
+
+@JsonClass(generateAdapter = true)
+class CreateLivesRequest(
+    @Json(name = "match_fighter_id")
+    val matchFighterId: String,
+    @Json(name = "status")
+    val status: Boolean,
+    @Json(name = "url")
+    val url: String,
+)
+
+@JsonClass(generateAdapter = true)
+data class CreateLivesResponse(
+    @Json(name = "code")
+    val resultCode: Int,
+    @Json(name = "message")
+    val message: ResponseMessage,
+)

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

@@ -77,6 +77,9 @@ interface ApiService {
 
     @POST("/v1/product/create")
     fun createProduct(@Body createProductRequest: CreateProductRequest): Single<CreateProductResponse>
+
+    @POST("/v1/live/create")
+    fun createLives(@Body createLivesRequest: CreateLivesRequest): Single<CreateLivesResponse>
     ///---------------------------------------------------------
 
     @POST("/v1/api/user/changepassword")

+ 46 - 0
app/src/main/java/com/sambath/kunkhmer/screen/lives/LivesAdminFragment.kt

@@ -5,8 +5,16 @@ import androidx.fragment.app.Fragment
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
+import android.widget.RadioButton
+import android.widget.Toast
 import com.sambath.kunkhmer.R
 import com.sambath.kunkhmer.app.App
+import com.sambath.kunkhmer.remote.CreateLivesRequest
+import com.sambath.kunkhmer.remote.CreateProductRequest
+import kotlinx.android.synthetic.main.fragment_lives_admin.view.et_match_fighter_id
+import kotlinx.android.synthetic.main.fragment_lives_admin.view.et_url
+import kotlinx.android.synthetic.main.fragment_lives_admin.view.radio_group_status
+import kotlinx.android.synthetic.main.fragment_lives_admin.view.submitButton
 
 class LivesAdminFragment : Fragment() {
     private var _root: View? = null
@@ -21,8 +29,46 @@ class LivesAdminFragment : Fragment() {
         // Inflate the layout for this fragment
         _root = inflater.inflate(R.layout.fragment_lives_admin, container, false)
         livesViewModel = LivesViewModel(App.injectApiService(), App.injectPrefHelper())
+        livesViewModel.state.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
+            render(it)
+        })
+
+        setDefault()
+
+        binding.submitButton.setOnClickListener {
+            val selectedId = binding.radio_group_status.checkedRadioButtonId
+            var status = false
+            if (selectedId != -1) { // -1 indicates no radio button is checked
+                val selectedRadioButton: RadioButton = binding.findViewById(selectedId)
+                val selectedValue: String = selectedRadioButton.text.toString()
+                if (selectedValue == "True") {
+                    status = true
+                }
+            }
+
+            val createLivesRequest = CreateLivesRequest(
+                binding.et_match_fighter_id.text.toString(), status, binding.et_url.text.toString()
+            )
+
+            livesViewModel.createLives(createLivesRequest.matchFighterId, createLivesRequest.status, createLivesRequest.url)
+        }
 
         return binding
     }
 
+    private fun setDefault() {
+        binding.et_match_fighter_id.setText("650ea3b6b66ab4183c1657c1")
+        binding.et_url.setText("https://youtu.be/xZtadif15WU")
+    }
+
+
+    private fun render(state: LivesViewModel.LivesViewState) {
+        if (state.isLoginSuccess) {
+            Toast.makeText(context, "Create Success...", Toast.LENGTH_LONG).show()
+
+            binding.et_match_fighter_id.setText("")
+            binding.et_url.setText("")
+        }
+        // Other state handling...
+    }
 }

+ 27 - 0
app/src/main/java/com/sambath/kunkhmer/screen/lives/LivesViewModel.kt

@@ -1,9 +1,12 @@
 package com.sambath.kunkhmer.screen.lives
 
+import android.util.Log
 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.CreateLivesRequest
+import com.sambath.kunkhmer.remote.CreateProductRequest
 import com.sambath.kunkhmer.remote.LivesInfo
 import com.sambath.kunkhmer.remote.service.ApiService
 import com.sambath.kunkhmer.util.PrefHelper
@@ -46,4 +49,28 @@ class LivesViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : B
                 })
         )
     }
+
+    fun createLives(match_fighter_id: String, status: Boolean, url: String) {
+        if (_state.value!!.isProgress) return
+        _state.value = prev().copy(isProgress = true)
+        val request = CreateLivesRequest(match_fighter_id, status, url)
+
+        disposables.add(
+            apiService.createLives(request)
+                .timeout(10, TimeUnit.SECONDS)
+                .subscribeOn(Schedulers.io())
+                .observeOn(AndroidSchedulers.mainThread())
+                .subscribe({
+                    Log.d("login", it.toString())
+                    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() + it.message
+                    _state.value = prev().copy(isProgress = false, error = message)
+                })
+        )
+    }
 }

+ 81 - 7
app/src/main/res/layout/fragment_lives_admin.xml

@@ -1,14 +1,88 @@
 <?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.lives.LivesAdminFragment">
+    android:background="@color/color_black"
+    android:orientation="vertical"
+    android:padding="10dp">
+
+    <com.google.android.material.textfield.TextInputLayout
+        android:id="@+id/til_match_fighter_id"
+        style="@style/BoxInputLayout"
+        android:layout_width="match_parent"
+        android:layout_height="48dp"
+        android:layout_below="@id/tv_username"
+        app:endIconMode="clear_text"
+        app:hintEnabled="false">
+
+        <androidx.appcompat.widget.AppCompatEditText
+            android:id="@+id/et_match_fighter_id"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:background="@color/color_white"
+            android:hint="@string/hint_match_fighter_id"
+            android:inputType="text"
+            android:maxLength="50"
+            android:padding="0dp" />
+    </com.google.android.material.textfield.TextInputLayout>
 
-    <!-- TODO: Update blank fragment layout -->
     <TextView
         android:layout_width="match_parent"
-        android:layout_height="match_parent"
-        android:text="@string/hello_blank_fragment" />
+        android:layout_height="wrap_content"
+        android:padding="5dp"
+        android:text="@string/hint_status"
+        android:textColor="@color/color_white" />
+
+    <RadioGroup
+        android:id="@+id/radio_group_status"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:orientation="horizontal">
+
+        <RadioButton
+            android:id="@+id/radioButtonTrue"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="True"
+            android:textColor="@color/color_white" />
+
+        <RadioButton
+            android:id="@+id/radioButtonFalse"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:checked="true"
+            android:text="False"
+            android:textColor="@color/color_white" />
+    </RadioGroup>
+
+    <com.google.android.material.textfield.TextInputLayout
+        android:id="@+id/til_url"
+        style="@style/BoxInputLayout"
+        android:layout_width="match_parent"
+        android:layout_height="48dp"
+        android:layout_below="@id/tv_username"
+        app:endIconMode="clear_text"
+        app:hintEnabled="false">
+
+        <androidx.appcompat.widget.AppCompatEditText
+            android:id="@+id/et_url"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:background="@color/color_white"
+            android:hint="@string/hint_price"
+            android:inputType="number"
+            android:maxLength="50"
+            android:padding="0dp" />
+    </com.google.android.material.textfield.TextInputLayout>
+
 
-</FrameLayout>
+    <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>

+ 6 - 0
app/src/main/res/values/arrays.xml

@@ -0,0 +1,6 @@
+<resources>
+    <string-array name="true_false_array">
+        <item>true</item>
+        <item>false</item>
+    </string-array>
+</resources>

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

@@ -111,4 +111,5 @@
     <string name="weight">Weight</string>
     <string name="height">Height</string>
     <string name="nationality">Nationality</string>
+    <string name="hint_status">Status</string>
 </resources>