소스 검색

admin page in progress

dola 1 년 전
부모
커밋
786bc4a796

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

@@ -87,8 +87,8 @@ data class CreateMatchResponse(
     val resultCode: Int,
     @Json(name = "message")
     val message: ResponseMessage,
-    @Json(name = "data")
-    val data: MatchCreatedData?
+    //@Json(name = "data")
+    //val data: MatchCreatedData?
 )
 
 @JsonClass(generateAdapter = true)

+ 17 - 1
app/src/main/java/com/sambath/kunkhmer/remote/service/ApiService.kt

@@ -3,6 +3,8 @@ package com.sambath.kunkhmer.remote.service
 import com.sambath.kunkhmer.extension.CurrentUserResponse
 import com.sambath.kunkhmer.remote.*
 import io.reactivex.Single
+import okhttp3.MultipartBody
+import okhttp3.RequestBody
 import okhttp3.ResponseBody
 import retrofit2.http.*
 
@@ -39,8 +41,22 @@ interface ApiService {
     @POST("/v1/users/get")
     fun getCurrentUser(): Single<CurrentUserResponse>
 
+
+//    @POST("/v1/new/create")
+//    fun createNews(@Body createNewsRequest: CreateNewsRequest): Single<CreateNewsResponse>
+
+    @Multipart
+    @POST("/v1/new/create")
+    fun createNews(@Part("title") title: RequestBody,
+                   @Part("desc") desc: RequestBody,
+                   @Part file: MultipartBody.Part
+    ): Single<CreateNewsResponse>
+
+    @Multipart
     @POST("/v1/new/create")
-    fun createNews(@Body createNewsRequest: CreateNewsRequest): Single<CreateNewsResponse>
+    fun createNews(@Part("title") title: RequestBody,
+                   @Part("desc") desc: RequestBody
+    ): Single<CreateNewsResponse>
 
     @POST("/v1/match/create")
     fun createMatch(@Body createMatchRequest: CreateMatchRequest): Single<CreateMatchResponse>

+ 25 - 0
app/src/main/java/com/sambath/kunkhmer/screen/fighter/CreateFighterFragment.kt

@@ -0,0 +1,25 @@
+package com.sambath.kunkhmer.screen.fighter
+
+import android.os.Bundle
+import androidx.fragment.app.Fragment
+import android.view.LayoutInflater
+import android.view.View
+import android.view.ViewGroup
+import com.sambath.kunkhmer.R
+
+
+class CreateFighterFragment : Fragment() {
+    private var _root: View? = null
+    private val binding get() = _root!!
+
+    override fun onCreate(savedInstanceState: Bundle?) {
+        super.onCreate(savedInstanceState)
+    }
+
+    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
+        // Inflate the layout for this fragment
+        _root = inflater.inflate(R.layout.fragment_create_fighter, container, false)
+
+        return binding
+    }
+}

+ 10 - 4
app/src/main/java/com/sambath/kunkhmer/screen/fighter/FighterAdminFragment.kt

@@ -9,6 +9,7 @@ import android.view.View
 import android.view.ViewGroup
 import android.widget.Toast
 import androidx.annotation.RequiresApi
+import androidx.navigation.findNavController
 import com.sambath.kunkhmer.R
 import com.sambath.kunkhmer.app.App
 import kotlinx.android.synthetic.main.fragment_fighter_admin.et_date
@@ -17,6 +18,7 @@ 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
+import kotlinx.android.synthetic.main.fragment_fighter_admin.view.fighter_button
 import kotlinx.android.synthetic.main.fragment_fighter_admin.view.submitButton
 import java.text.SimpleDateFormat
 import java.time.LocalDateTime
@@ -50,12 +52,12 @@ class FighterAdminFragment : Fragment() {
             render(it)
         })
 
-        binding.et_date.setOnClickListener{
+        binding.et_date.setOnClickListener {
             showDatePickerDialog()
         }
         setDefaultData()
 
-        binding.submitButton.setOnClickListener{
+        binding.submitButton.setOnClickListener {
             fighterViewModel.createMatch(
                 binding.et_date.text.toString(),
                 binding.et_title.text.toString(),
@@ -64,6 +66,10 @@ class FighterAdminFragment : Fragment() {
             )
         }
 
+        binding.fighter_button.setOnClickListener {
+            requireActivity().findNavController(R.id.nav_host_fragment_activity_main).navigate(R.id.navigation_create_fighter)
+        }
+
         // Inflate the layout for this fragment
         return binding
     }
@@ -71,8 +77,8 @@ class FighterAdminFragment : Fragment() {
     private fun setDefaultData() {
         binding.et_title.setText("Keo Rumchong VS Thiago Teixeira")
         binding.et_desc.setText("Keo Rumchong VS Thiago Teixeira")
-        binding.et_fighter_1.setText("650d97d08cc32f66d67c0b60")
-        binding.et_fighter_2.setText("650d98498cc32f66d67c0b65")
+        binding.et_fighter_1.setText("65542230c82a6853109bceed")
+        binding.et_fighter_2.setText("65542284c82a6829d69bcef2")
     }
 
     private fun showDatePickerDialog() {

+ 35 - 8
app/src/main/java/com/sambath/kunkhmer/screen/news/NewsAdminFragment.kt

@@ -4,6 +4,7 @@ import android.app.Activity
 import android.content.Intent
 import android.net.Uri
 import android.os.Bundle
+import android.provider.MediaStore
 import androidx.fragment.app.Fragment
 import android.view.LayoutInflater
 import android.view.View
@@ -21,6 +22,7 @@ import kotlinx.android.synthetic.main.fragment_news_admin.view.submitButton
 import kotlinx.android.synthetic.main.fragment_news_admin.view.uploadButton
 import kotlinx.android.synthetic.main.fragment_news_admin.view.uploadedImageView
 import androidx.navigation.findNavController
+import kotlinx.android.synthetic.main.fragment_news_admin.view.til_title
 
 class NewsAdminFragment : Fragment() {
     private var _root: View? = null
@@ -44,16 +46,29 @@ class NewsAdminFragment : Fragment() {
         }
 
         binding.submitButton.setOnClickListener{
-            // You can store the image URI as a string in your CreateNewsRequest object
-            val createNewsRequest = CreateNewsRequest(
-                binding.et_title.text.toString(),
-                binding.et_desc.text.toString(),
-                imageUri.toString()
-            )
-
-            newsViewModel.createNews(createNewsRequest.title, createNewsRequest.desc, createNewsRequest.upload)
+            val imagePath = imageUri?.let { it1 -> getImagePathFromUri(it1) }
+            if (imagePath != null) {
+                // You can store the image URI as a string in your CreateNewsRequest object
+                val createNewsRequest = CreateNewsRequest(
+                    binding.et_title.text.toString(),
+                    binding.et_desc.text.toString(),
+                    imagePath
+                )
+                newsViewModel.createNews(createNewsRequest.title, createNewsRequest.desc, createNewsRequest.upload)
+            } else {
+                // Handle the case where the image path is null
+                val createNewsRequest = CreateNewsRequest(
+                    binding.et_title.text.toString(),
+                    binding.et_desc.text.toString(),
+                    ""
+                )
+                newsViewModel.createNews(createNewsRequest.title, createNewsRequest.desc, createNewsRequest.upload)
+            }
         }
 
+        binding.et_title.setText("Theara predicts elbow-driven victory, claims world Kun Khmer title")
+        binding.et_desc.setText("Top fighter")
+
         return binding;
     }
 
@@ -87,4 +102,16 @@ class NewsAdminFragment : Fragment() {
         intent.type = "image/*"
         imagePicker.launch(intent)
     }
+
+    private fun getImagePathFromUri(uri: Uri): String? {
+        val contentResolver = requireContext().contentResolver
+        val cursor = contentResolver.query(uri, null, null, null, null)
+        cursor?.use {
+            it.moveToFirst()
+            val index = it.getColumnIndex(MediaStore.Images.ImageColumns.DATA)
+            return it.getString(index)
+        }
+        return null
+    }
+
 }

+ 71 - 21
app/src/main/java/com/sambath/kunkhmer/screen/news/NewsViewModel.kt

@@ -8,15 +8,19 @@ import com.sambath.kunkhmer.base.BaseViewModel
 import com.sambath.kunkhmer.remote.CreateNewsRequest
 import com.sambath.kunkhmer.remote.CreatedNews
 import com.sambath.kunkhmer.remote.HighlightData
-import com.sambath.kunkhmer.remote.LoginRequest
 import com.sambath.kunkhmer.remote.NewsData
 import com.sambath.kunkhmer.remote.service.ApiService
-import com.sambath.kunkhmer.util.Const
-import com.sambath.kunkhmer.util.ModelPreferencesManager
 import com.sambath.kunkhmer.util.PrefHelper
+import io.reactivex.Flowable
 import io.reactivex.android.schedulers.AndroidSchedulers
 import io.reactivex.schedulers.Schedulers
+import okhttp3.MediaType.Companion.toMediaTypeOrNull
+import okhttp3.MultipartBody
+import okhttp3.RequestBody.Companion.asRequestBody
+import okhttp3.RequestBody.Companion.toRequestBody
+import java.io.File
 import java.util.concurrent.TimeUnit
+import java.util.concurrent.TimeoutException
 
 data class NewsViewState(
     val initial: Boolean = false,
@@ -82,24 +86,70 @@ class NewsViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : Ba
     fun createNews(title: String, desc: String, upload: String) {
         if (_state.value!!.isProgress) return
         val request = CreateNewsRequest(title, desc, upload)
-        _state.value = prev().copy(isProgress = true)
 
-        disposables.add(
-            apiService.createNews(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()
-                    _state.value = prev().copy(isProgress = false, error = message)
-                })
-        )
+        val titlePart = request.title.toRequestBody("text/plain".toMediaTypeOrNull())
+        val descPart = request.desc.toRequestBody("text/plain".toMediaTypeOrNull())
+
+       if(!upload.isNullOrEmpty()) {
+           // Assuming 'upload' is a file path, you may need to modify this part based on your actual requirements
+           val file = File(request.upload)
+           //val filePart = MultipartBody.Part.createFormData("upload", file.name, file.asRequestBody("multipart/form-data".toMediaTypeOrNull()))
+           val filePart = MultipartBody.Part.createFormData("upload", file.name, file.asRequestBody())
+
+           _state.value = prev().copy(isProgress = true)
+
+           disposables.add(
+               apiService.createNews(titlePart, descPart, filePart)
+                   .timeout(10, TimeUnit.SECONDS)
+                   .subscribeOn(Schedulers.io())
+                   .observeOn(AndroidSchedulers.mainThread())
+//                   .retryWhen { errors ->
+//                       errors
+//                           .zipWith(Flowable.range(1, 3)) { throwable, retryCount -> Pair(throwable, retryCount) }
+//                           .flatMap { (throwable, retryCount) ->
+//                               if (throwable is TimeoutException && retryCount <= 3) {
+//                                   Flowable.timer(5, TimeUnit.SECONDS) // Retry after 5 seconds
+//                               } else {
+//                                   Flowable.error(throwable)
+//                               }
+//                           }
+//
+//                   }
+                   .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()
+                       _state.value = prev().copy(isProgress = false, error = message)
+                   })
+           )
+       } else {
+           _state.value = prev().copy(isProgress = true)
+
+           disposables.add(
+               apiService.createNews(titlePart, descPart)
+                   .timeout(10, TimeUnit.SECONDS)
+                   .doOnSubscribe { Log.d("Network", "Request started") }
+                   .doOnSuccess { Log.d("Network", "Request succeeded: $it") }
+                   .doOnError { Log.e("Network", "Request failed", it) }
+                   .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()
+                       _state.value = prev().copy(isProgress = false, error = message)
+                   })
+           )
+       }
     }
 }

+ 126 - 0
app/src/main/res/layout/fragment_create_fighter.xml

@@ -0,0 +1,126 @@
+<?xml version="1.0" encoding="utf-8"?>
+<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:background="@color/color_black"
+    android:orientation="vertical"
+    android:padding="10dp"
+    tools:context=".screen.fighter.CreateFighterFragment">
+
+    <com.google.android.material.textfield.TextInputLayout
+        android:id="@+id/til_name"
+        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_name"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:background="@color/color_white"
+            android:hint="@string/name"
+            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="48dp"
+        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:hint="@string/description"
+            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_weight"
+        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_weight"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:background="@color/color_white"
+            android:hint="@string/weight"
+            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_height"
+        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_height"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:background="@color/color_white"
+            android:hint="@string/height"
+            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_nationality"
+        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_nationality"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:background="@color/color_white"
+            android:hint="@string/nationality"
+            android:inputType="text"
+            android:maxLength="50"
+            android:padding="0dp" />
+    </com.google.android.material.textfield.TextInputLayout>
+
+    <Button
+        android:id="@+id/uploadButton"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="center"
+        android:layout_marginBottom="16dp"
+        android:paddingStart="16dp"
+        android:paddingEnd="16dp"
+        android:text="Upload Image" />
+
+    <ImageView
+        android:id="@+id/uploadedImageView"
+        android:layout_width="200dp"
+        android:layout_height="200dp"
+        android:layout_gravity="center"
+        android:layout_marginBottom="16dp"
+        android:background="@color/color_gray_300"
+        android:scaleType="centerCrop" />
+
+</androidx.appcompat.widget.LinearLayoutCompat>

+ 12 - 0
app/src/main/res/layout/fragment_fighter_admin.xml

@@ -111,4 +111,16 @@
         android:paddingStart="16dp"
         android:paddingEnd="16dp"
         android:text="Submit" />
+
+    <TextView
+        android:id="@+id/fighter_button"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="center"
+        android:layout_marginTop="10dp"
+        android:paddingTop="10dp"
+        android:paddingBottom="10dp"
+        android:text="@string/create_fighter"
+        android:textColor="@color/color_white"
+        android:textSize="14sp" />
 </LinearLayout>

+ 6 - 0
app/src/main/res/navigation/mobile_navigation.xml

@@ -41,4 +41,10 @@
         android:label="@string/account"
         tools:layout="@layout/fragment_account"/>
 
+    <fragment
+        android:id="@+id/navigation_create_fighter"
+        android:name="com.sambath.kunkhmer.screen.fighter.CreateFighterFragment"
+        android:label="@string/create_fighter"
+        tools:layout="@layout/fragment_create_fighter"/>
+
 </navigation>

+ 7 - 0
app/src/main/res/navigation/mobile_navigation_admin.xml

@@ -41,4 +41,11 @@
         android:label="@string/account"
         tools:layout="@layout/fragment_account"/>
 
+    <fragment
+        android:id="@+id/navigation_create_fighter"
+        android:name="com.sambath.kunkhmer.screen.fighter.CreateFighterFragment"
+        android:label="@string/create_fighter"
+        tools:layout="@layout/fragment_create_fighter"/>
+
+
 </navigation>

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

@@ -106,4 +106,9 @@
     <string name="hint_match_fighter_id">Match Fighter Id</string>
     <string name="hint_price">Price</string>
     <string name="hint_product_name">Product Name</string>
+    <string name="create_fighter">Create Fighter</string>
+    <string name="name">Name</string>
+    <string name="weight">Weight</string>
+    <string name="height">Height</string>
+    <string name="nationality">Nationality</string>
 </resources>