dola пре 1 година
родитељ
комит
7d56f91a11

+ 14 - 4
app/src/main/java/com/sambath/kunkhmer/adapter/LivesAdapter.kt

@@ -8,10 +8,12 @@ import android.widget.TextView
 import androidx.appcompat.app.AppCompatActivity
 import androidx.recyclerview.widget.RecyclerView
 import com.sambath.kunkhmer.R
+import com.sambath.kunkhmer.config.Config
+import com.sambath.kunkhmer.remote.LivesInfo
 import com.sambath.kunkhmer.screen.lives.LiveDetailFragment
 import com.squareup.picasso.Picasso
 
-class LivesAdapter(private val cardList: List<LivesCardItem>, private val listener: OnItemClickListener) : RecyclerView.Adapter<LivesAdapter.ViewHolder>() {
+class LivesAdapter(var cardList: List<LivesInfo>, private val listener: OnItemClickListener) : RecyclerView.Adapter<LivesAdapter.ViewHolder>() {
 
     interface OnItemClickListener {
         fun onItemClick(position: Int)
@@ -25,9 +27,17 @@ class LivesAdapter(private val cardList: List<LivesCardItem>, private val listen
     override fun onBindViewHolder(holder: LivesAdapter.ViewHolder, position: Int) {
         val currentItem = cardList[position]
 
-        Picasso.get().load(currentItem.imageResource).into(holder.imageView)
-        holder.subTitleTextView.text = currentItem.subTitle
-        holder.titleTextView.text = currentItem.title
+        if(currentItem.filename == null) {
+            Picasso.get().load(R.drawable.default_background).into(holder.imageView)
+        } else {
+            Picasso.get()
+                .load(Config.BASE_URL + "/v1/live/image/" + currentItem.filename)
+                .error(R.drawable.default_background) // Set the default image resource
+                .into(holder.imageView)
+        }
+
+        holder.subTitleTextView.text = currentItem.desc
+        holder.titleTextView.text = currentItem.name
     }
 
     override fun getItemCount() = cardList.size

+ 33 - 23
app/src/main/java/com/sambath/kunkhmer/remote/LivesDataModel.kt

@@ -21,38 +21,48 @@ data class LivesData(
 
 @JsonClass(generateAdapter = true)
 data class LivesInfo(
-    @Json(name = "_id")
-    val liveInfoId: LivesInfoId,
-    @Json(name = "fighters")
-    val fighters: MutableList<LiveInfoFighters>,
-)
-
-@JsonClass(generateAdapter = true)
-data class LivesInfoId(
     @Json(name = "_id")
     val id: String,
+    @Json(name = "desc")
+    val desc: String,
     @Json(name = "status")
     val status: Boolean,
     @Json(name = "fighter_id")
     val fighterId: String,
     @Json(name = "createdAt")
     val createdAt: String,
-)
-
-@JsonClass(generateAdapter = true)
-data class LiveInfoFighters(
-    @Json(name = "_id")
-    val id: String,
     @Json(name = "name")
     val name: String,
-    @Json(name = "desc")
-    val desc: String,
-    @Json(name = "weight")
-    val weight: String,
-    @Json(name = "high")
-    val height: String,
-    @Json(name = "nationality")
-    val nationality: String,
     @Json(name = "filename")
     val filename: String,
-)
+)
+
+//@JsonClass(generateAdapter = true)
+//data class LivesInfoId(
+//    @Json(name = "_id")
+//    val id: String,
+//    @Json(name = "status")
+//    val status: Boolean,
+//    @Json(name = "fighter_id")
+//    val fighterId: String,
+//    @Json(name = "createdAt")
+//    val createdAt: String,
+//)
+
+//@JsonClass(generateAdapter = true)
+//data class LiveInfoFighters(
+//    @Json(name = "_id")
+//    val id: String,
+//    @Json(name = "desc")
+//    val desc: String,
+//    @Json(name = "name")
+//    val name: String,
+//    @Json(name = "weight")
+//    val weight: String,
+//    @Json(name = "high")
+//    val height: String,
+//    @Json(name = "nationality")
+//    val nationality: String,
+//    @Json(name = "filename")
+//    val filename: String,
+//)

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

@@ -45,6 +45,10 @@ interface ApiService {
 //    @POST("/v1/new/create")
 //    fun createNews(@Body createNewsRequest: CreateNewsRequest): Single<CreateNewsResponse>
 
+    @Multipart
+    @POST("/v1/new/create")
+    fun createNews(@Part request: MultipartBody.Part): Single<CreateNewsResponse>
+
     @Multipart
     @POST("/v1/new/create")
     fun createNews(@Part("title") title: RequestBody,
@@ -52,6 +56,13 @@ interface ApiService {
                    @Part file: MultipartBody.Part
     ): Single<CreateNewsResponse>
 
+    @Multipart
+    @POST("/v1/new/create")
+    fun createNews(@Part("title") title: RequestBody,
+                   @Part("desc") desc: RequestBody,
+                   @Part("upload") upload: RequestBody
+    ): Single<CreateNewsResponse>
+
     @Multipart
     @POST("/v1/new/create")
     fun createNews(@Part("title") title: RequestBody,

+ 55 - 28
app/src/main/java/com/sambath/kunkhmer/screen/lives/LivesFragment.kt

@@ -12,13 +12,19 @@ import androidx.recyclerview.widget.RecyclerView
 import com.sambath.kunkhmer.R
 import com.sambath.kunkhmer.adapter.LivesAdapter
 import com.sambath.kunkhmer.adapter.LivesCardItem
+import com.sambath.kunkhmer.adapter.ProductAdapter
+import com.sambath.kunkhmer.app.App
+import com.sambath.kunkhmer.remote.LivesInfo
+import com.sambath.kunkhmer.screen.shop.ShopViewModel
 import kotlinx.android.synthetic.main.fragment_event.view.recyclerViewEvent
 import kotlinx.android.synthetic.main.fragment_lives.view.recyclerViewLives
 
 class LivesFragment : Fragment(), LivesAdapter.OnItemClickListener  {
     private var _root: View? = null
     private val binding get() = _root!!
-    private var cardList: List<LivesCardItem>? = null
+    private var cardList: List<LivesInfo>? = null
+    private lateinit var livesAdapter: LivesAdapter
+    private lateinit var livesViewModel: LivesViewModel
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
@@ -31,47 +37,68 @@ class LivesFragment : Fragment(), LivesAdapter.OnItemClickListener  {
         // Inflate the layout for this fragment
         _root = inflater.inflate(R.layout.fragment_lives, container, false)
 
-        setEvent()
+        livesAdapter = LivesAdapter(mutableListOf(), this)
+        livesViewModel = LivesViewModel(App.injectApiService(), App.injectPrefHelper())
+        livesViewModel.getLives()
+        livesViewModel.state.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
+            render(it)
+        })
 
-        return binding
-    }
-
-    private fun setEvent() {
         val recyclerView = binding.recyclerViewLives
-        cardList = createCardList() // Create your card data list here
-        val cardAdapter = LivesAdapter(cardList!!, this)
-
         recyclerView.layoutManager =
             LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
-        recyclerView.adapter = cardAdapter
+        recyclerView.adapter = livesAdapter
+
+        return binding
     }
 
-    private fun createCardList(): List<LivesCardItem> {
-        // Create and return your list of CardItems
-        return listOf(
-            LivesCardItem("https://i.ytimg.com/vi/Lh5O00zo_pc/hq720.jpg?sqp=-oaymwEcCOgCEMoBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLCEQ100buCWUuyBKWe_96UbevbIEA",
-                "ផ្សាយបន្តផ្ទាល់កម្មវិធីប្រដាល់ Krud គុនខ្មែរ", "ឃឹម បូរ៉ា (ខ្មែរ) Vs ពេជ្រ សម្បត្តិ"),
-            LivesCardItem("https://i.ytimg.com/vi/Lh5O00zo_pc/hq720.jpg?sqp=-oaymwEcCOgCEMoBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLCEQ100buCWUuyBKWe_96UbevbIEA",
-                "ផ្សាយបន្តផ្ទាល់កម្មវិធីប្រដាល់ Krud គុនខ្មែរ", "ឃឹម បូរ៉ា (ខ្មែរ) Vs ពេជ្រ សម្បត្តិ"),
-            LivesCardItem("https://i.ytimg.com/vi/Lh5O00zo_pc/hq720.jpg?sqp=-oaymwEcCOgCEMoBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLCEQ100buCWUuyBKWe_96UbevbIEA",
-                "ផ្សាយបន្តផ្ទាល់កម្មវិធីប្រដាល់ Krud គុនខ្មែរ", "ឃឹម បូរ៉ា (ខ្មែរ) Vs ពេជ្រ សម្បត្តិ"),
-            LivesCardItem("https://i.ytimg.com/vi/Lh5O00zo_pc/hq720.jpg?sqp=-oaymwEcCOgCEMoBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLCEQ100buCWUuyBKWe_96UbevbIEA",
-                "ផ្សាយបន្តផ្ទាល់កម្មវិធីប្រដាល់ Krud គុនខ្មែរ", "ឃឹម បូរ៉ា (ខ្មែរ) Vs ពេជ្រ សម្បត្តិ"),
-            LivesCardItem("https://i.ytimg.com/vi/Lh5O00zo_pc/hq720.jpg?sqp=-oaymwEcCOgCEMoBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLCEQ100buCWUuyBKWe_96UbevbIEA",
-                "ផ្សាយបន្តផ្ទាល់កម្មវិធីប្រដាល់ Krud គុនខ្មែរ", "ឃឹម បូរ៉ា (ខ្មែរ) Vs ពេជ្រ សម្បត្តិ"),
-            // Add more card items as needed
-        )
+    private fun render(state: LivesViewModel.LivesViewState) {
+        if (state.isLoginSuccess) {
+            if(state.livesData != null) {
+                livesAdapter.cardList = state.livesData
+                cardList = livesAdapter.cardList
+                livesAdapter.notifyDataSetChanged()
+            }
+        }
+        // Other state handling...
     }
 
+//    private fun setEvent() {
+//        val recyclerView = binding.recyclerViewLives
+//        cardList = createCardList() // Create your card data list here
+//        val cardAdapter = LivesAdapter(cardList!!, this)
+//
+//        recyclerView.layoutManager =
+//            LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
+//        recyclerView.adapter = cardAdapter
+//    }
+
+//    private fun createCardList(): List<LivesCardItem> {
+//        // Create and return your list of CardItems
+//        return listOf(
+//            LivesCardItem("https://i.ytimg.com/vi/Lh5O00zo_pc/hq720.jpg?sqp=-oaymwEcCOgCEMoBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLCEQ100buCWUuyBKWe_96UbevbIEA",
+//                "ផ្សាយបន្តផ្ទាល់កម្មវិធីប្រដាល់ Krud គុនខ្មែរ", "ឃឹម បូរ៉ា (ខ្មែរ) Vs ពេជ្រ សម្បត្តិ"),
+//            LivesCardItem("https://i.ytimg.com/vi/Lh5O00zo_pc/hq720.jpg?sqp=-oaymwEcCOgCEMoBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLCEQ100buCWUuyBKWe_96UbevbIEA",
+//                "ផ្សាយបន្តផ្ទាល់កម្មវិធីប្រដាល់ Krud គុនខ្មែរ", "ឃឹម បូរ៉ា (ខ្មែរ) Vs ពេជ្រ សម្បត្តិ"),
+//            LivesCardItem("https://i.ytimg.com/vi/Lh5O00zo_pc/hq720.jpg?sqp=-oaymwEcCOgCEMoBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLCEQ100buCWUuyBKWe_96UbevbIEA",
+//                "ផ្សាយបន្តផ្ទាល់កម្មវិធីប្រដាល់ Krud គុនខ្មែរ", "ឃឹម បូរ៉ា (ខ្មែរ) Vs ពេជ្រ សម្បត្តិ"),
+//            LivesCardItem("https://i.ytimg.com/vi/Lh5O00zo_pc/hq720.jpg?sqp=-oaymwEcCOgCEMoBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLCEQ100buCWUuyBKWe_96UbevbIEA",
+//                "ផ្សាយបន្តផ្ទាល់កម្មវិធីប្រដាល់ Krud គុនខ្មែរ", "ឃឹម បូរ៉ា (ខ្មែរ) Vs ពេជ្រ សម្បត្តិ"),
+//            LivesCardItem("https://i.ytimg.com/vi/Lh5O00zo_pc/hq720.jpg?sqp=-oaymwEcCOgCEMoBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLCEQ100buCWUuyBKWe_96UbevbIEA",
+//                "ផ្សាយបន្តផ្ទាល់កម្មវិធីប្រដាល់ Krud គុនខ្មែរ", "ឃឹម បូរ៉ា (ខ្មែរ) Vs ពេជ្រ សម្បត្តិ"),
+//            // Add more card items as needed
+//        )
+//    }
+
     override fun onItemClick(position: Int) {
         val clickedItem = cardList!![position]
         val liveDetailFragment = LiveDetailFragment()
 
         // You can pass data to the LiveDetailFragment using arguments if needed
         val bundle = Bundle()
-        bundle.putString("image_url", clickedItem.imageResource)
-        bundle.putString("title", clickedItem.title)
-        bundle.putString("description", clickedItem.title)
+        bundle.putString("image_url", clickedItem.filename)
+        bundle.putString("title", clickedItem.name)
+        bundle.putString("description", clickedItem.desc)
         liveDetailFragment.arguments = bundle
 
         // Replace the current fragment with the LiveDetailFragment

+ 62 - 46
app/src/main/java/com/sambath/kunkhmer/screen/news/NewsViewModel.kt

@@ -16,6 +16,7 @@ import io.reactivex.android.schedulers.AndroidSchedulers
 import io.reactivex.schedulers.Schedulers
 import okhttp3.MediaType.Companion.toMediaTypeOrNull
 import okhttp3.MultipartBody
+import okhttp3.RequestBody
 import okhttp3.RequestBody.Companion.asRequestBody
 import okhttp3.RequestBody.Companion.toRequestBody
 import java.io.File
@@ -90,19 +91,34 @@ class NewsViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : Ba
         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())
+        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)
+//            val file = File(request.upload)
+//            val fileRequestBody = RequestBody.create("multipart/form-data".toMediaTypeOrNull(), file)
+//            val filePart = MultipartBody.Part.createFormData("upload", file.name, fileRequestBody)
+//
+//            _state.value = prev().copy(isProgress = true)
+//
+//            val requestBody = MultipartBody.Builder()
+//                .setType(MultipartBody.FORM)
+//                .addPart(titlePart)
+//                .addPart(descPart)
+//                .addPart(filePart)
+//                .build()
+//
+//            val multipartBody = MultipartBody.Part.createFormData("request", "filename", requestBody)
+
+            _state.value = prev().copy(isProgress = true)
 
-           disposables.add(
-               apiService.createNews(titlePart, descPart, filePart)
-                   .timeout(10, TimeUnit.SECONDS)
-                   .subscribeOn(Schedulers.io())
-                   .observeOn(AndroidSchedulers.mainThread())
+            disposables.add(
+                apiService.createNews(titlePart, descPart, file.asRequestBody("multipart/form-data".toMediaTypeOrNull()))
+                    .timeout(60, TimeUnit.SECONDS)
+                    .subscribeOn(Schedulers.io())
+                    .observeOn(AndroidSchedulers.mainThread())
 //                   .retryWhen { errors ->
 //                       errors
 //                           .zipWith(Flowable.range(1, 3)) { throwable, retryCount -> Pair(throwable, retryCount) }
@@ -115,41 +131,41 @@ class NewsViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : Ba
 //                           }
 //
 //                   }
-                   .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)
+                    .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)
-                   })
-           )
-       }
+            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)
+                    })
+            )
+        }
     }
 }