Browse Source

admin page in progress

dola 1 year ago
parent
commit
256b455c84

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

@@ -61,4 +61,59 @@ data class EventInfoFighters(
     val status: Boolean,
     @Json(name = "filename")
     val filename: String,
-)
+)
+
+////////
+
+@JsonClass(generateAdapter = true)
+class CreateEventRequest(
+    @Json(name = "title")
+    val title: String,
+    @Json(name = "desc")
+    val desc: String,
+    @Json(name = "match_fighter_id")
+    val matchFighterId: String
+)
+
+@JsonClass(generateAdapter = true)
+data class CreateEventResponse(
+    @Json(name = "code")
+    val resultCode: Int,
+    @Json(name = "message")
+    val message: ResponseMessage,
+    @Json(name = "data")
+    val data: EventCreatedData?
+)
+
+@JsonClass(generateAdapter = true)
+data class EventCreatedData(
+    @Json(name = "event")
+    val obj: CreatedEvent,
+)
+
+@JsonClass(generateAdapter = true)
+data class CreatedEvent(
+    @Json(name = "status")
+    val status: Boolean,
+
+    @Json(name = "_id")
+    val id: String,
+
+    @Json(name = "title")
+    val title: String,
+
+    @Json(name = "desc")
+    val desc: String,
+
+    @Json(name = "match_fighter_id")
+    val matchFighterId: 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

@@ -44,6 +44,9 @@ interface ApiService {
 
     @POST("/v1/match/create")
     fun createMatch(@Body createMatchRequest: CreateMatchRequest): Single<CreateMatchResponse>
+
+    @POST("/v1/event/create")
+    fun createEvent(@Body createEventRequest: CreateEventRequest): Single<CreateEventResponse>
     ///---------------------------------------------------------
 
     @POST("/v1/api/user/changepassword")

+ 41 - 36
app/src/main/java/com/sambath/kunkhmer/screen/event/EventAdminFragment.kt

@@ -5,56 +5,61 @@ 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_event_admin.view.et_desc
+import kotlinx.android.synthetic.main.fragment_event_admin.view.et_match_fighter_id
+import kotlinx.android.synthetic.main.fragment_event_admin.view.et_title
+import kotlinx.android.synthetic.main.fragment_event_admin.view.submitButton
 
-// 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 [EventAdminFragment.newInstance] factory method to
- * create an instance of this fragment.
- */
 class EventAdminFragment : 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 eventViewModel: EventViewModel
 
     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? {
+        _root = inflater.inflate(R.layout.fragment_event_admin, container, false)
+
+        eventViewModel = EventViewModel(App.injectApiService(), App.injectPrefHelper())
+        eventViewModel.state.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
+            render(it)
+        })
+
+        setDefaultData()
+        binding.submitButton.setOnClickListener{
+            eventViewModel.createEvent(
+                binding.et_title.text.toString(),
+                binding.et_desc.text.toString(),
+                binding.et_match_fighter_id.text.toString()
+            )
+        }
+
         // Inflate the layout for this fragment
-        return inflater.inflate(R.layout.fragment_event_admin, container, false)
+        return binding
+    }
+
+    private fun setDefaultData() {
+        binding.et_match_fighter_id.setText("650ea3b6b66ab4183c1657c1")
+        binding.et_title.setText("Prum Samnang VS Thoeun Theara")
+        binding.et_desc.setText("Prum Samnang VS Thoeun Theara")
     }
 
-    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 EventAdminFragment.
-         */
-        // TODO: Rename and change types and number of parameters
-        @JvmStatic
-        fun newInstance(param1: String, param2: String) =
-            EventAdminFragment().apply {
-                arguments = Bundle().apply {
-                    putString(ARG_PARAM1, param1)
-                    putString(ARG_PARAM2, param2)
-                }
-            }
+    private fun render(state: EventViewModel.EventViewState) {
+        if (state.isLoginSuccess) {
+            Toast.makeText(context, "Create Success...", Toast.LENGTH_LONG).show()
+
+            binding.et_title.setText("")
+            binding.et_desc.setText("")
+            binding.et_match_fighter_id.setText("")
+        }
+        // Other state handling...
     }
 }

+ 25 - 0
app/src/main/java/com/sambath/kunkhmer/screen/event/EventViewModel.kt

@@ -5,6 +5,7 @@ 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.CreateEventRequest
 import com.sambath.kunkhmer.remote.EventData
 import com.sambath.kunkhmer.remote.EventInfo
 import com.sambath.kunkhmer.remote.MatchesData
@@ -49,4 +50,28 @@ class EventViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : B
                 })
         )
     }
+
+    fun createEvent(title: String, desc: String, matchFighterId: String){
+        if (_state.value!!.isProgress) return
+        _state.value = prev().copy(isProgress = true)
+        val request = CreateEventRequest(title, desc, matchFighterId)
+
+        disposables.add(
+            apiService.createEvent(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)
+                })
+        )
+    }
 }

+ 73 - 8
app/src/main/res/layout/fragment_event_admin.xml

@@ -1,14 +1,79 @@
 <?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.event.EventAdminFragment">
+    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_match_fighter_id"
+        style="@style/BoxInputLayout"
         android:layout_width="match_parent"
-        android:layout_height="match_parent"
-        android:text="@string/hello_blank_fragment" />
+        android:layout_height="48dp"
+        android:layout_below="@id/tv_username"
+        app:endIconMode="clear_text"
+        app:hintEnabled="false">
 
-</FrameLayout>
+        <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>
+
+    <com.google.android.material.textfield.TextInputLayout
+        android:id="@+id/til_title"
+        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_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_desc"
+        style="@style/BoxInputLayout"
+        android:layout_width="match_parent"
+        android:layout_height="200dp"
+        android:layout_below="@id/tv_username"
+        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>

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

@@ -103,4 +103,5 @@
     <string name="hint_date">Date</string>
     <string name="hint_fighter_1">Fighter 1</string>
     <string name="hint_fighter_2">Fighter 2</string>
+    <string name="hint_match_fighter_id">Match Fighter Id</string>
 </resources>