dola hace 1 año
padre
commit
06d1ddad76

+ 51 - 25
app/src/main/java/com/sambath/kunkhmer/adapter/FighterAdapter.kt

@@ -1,6 +1,7 @@
 package com.sambath.kunkhmer.adapter
 
 import android.graphics.drawable.Drawable
+import android.util.Patterns
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
@@ -8,8 +9,11 @@ import android.widget.ImageView
 import android.widget.TextView
 import androidx.recyclerview.widget.RecyclerView
 import com.sambath.kunkhmer.R
+import com.sambath.kunkhmer.remote.Matches
+import com.squareup.picasso.Picasso
 
-class FighterAdapter (private val cardList: List<FighterCardItem>) : RecyclerView.Adapter<FighterAdapter.ViewHolder>()  {
+
+class FighterAdapter (private val cardList: List<Matches>) : RecyclerView.Adapter<FighterAdapter.ViewHolder>()  {
     class CardViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
 
     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FighterAdapter.ViewHolder {
@@ -20,34 +24,56 @@ class FighterAdapter (private val cardList: List<FighterCardItem>) : RecyclerVie
     override fun onBindViewHolder(holder: ViewHolder, position: Int) {
         val currentItem = cardList[position]
 
-        holder.imageLeft.setImageResource(currentItem.imageLeft)
-        holder.imageLeftTitle.text = currentItem.imageLeftTitle
-        holder.imageRight.setImageResource(currentItem.imageRight)
-        holder.imageRightTitle.text = currentItem.imageRightTitle
-        holder.date.text = currentItem.date
-        holder.title.text = currentItem.title
-
-        if(currentItem.favorite) {
-            val drawable: Drawable? = holder.itemView.context.getDrawable(R.drawable.ic_star_filled_50)
-            holder.favorite.setImageDrawable(drawable)
+        val imageUrl = currentItem.fighters[0].filename
+        if (Patterns.WEB_URL.matcher(imageUrl).matches()) {
+            // Load image from URL
+            Picasso.get()
+                .load(currentItem.fighters[0].filename)
+                .into(holder.imageLeft);
         } else {
-            val drawable: Drawable? = holder.itemView.context.getDrawable(R.drawable.ic_star_50)
-            holder.favorite.setImageDrawable(drawable)
+            // Load default image
+            Picasso.get()
+                .load(R.drawable.ic_user_white)
+                .into(holder.imageLeft)
         }
 
-        holder.favorite.setOnClickListener{
-            if (!currentItem.favorite) {
-                // Perform bookmarking action
-                val drawable: Drawable? = holder.itemView.context.getDrawable(R.drawable.ic_star_filled_50)
-                holder.favorite.setImageDrawable(drawable)
-                currentItem.favorite = true
-            } else {
-                // Perform unbookmarking action
-                val drawable: Drawable? = holder.itemView.context.getDrawable(R.drawable.ic_star_50)
-                holder.favorite.setImageDrawable(drawable)
-                currentItem.favorite = false
-            }
+        holder.imageLeftTitle.text = currentItem.fighters[0]?.name
+        if (Patterns.WEB_URL.matcher(imageUrl).matches()) {
+            // Load image from URL
+            Picasso.get()
+                .load(currentItem.fighters[0].filename)
+                .into(holder.imageRight);
+        } else {
+            // Load default image
+            Picasso.get()
+                .load(R.drawable.ic_user_white)
+                .into(holder.imageRight)
         }
+        holder.imageRightTitle.text = currentItem.fighters[1]?.name
+        holder.date.text = currentItem.matchInfo.date
+        holder.title.text = currentItem.matchInfo.title
+
+//        if(currentItem.favorite) {
+//            val drawable: Drawable? = holder.itemView.context.getDrawable(R.drawable.ic_star_filled_50)
+//            holder.favorite.setImageDrawable(drawable)
+//        } else {
+//            val drawable: Drawable? = holder.itemView.context.getDrawable(R.drawable.ic_star_50)
+//            holder.favorite.setImageDrawable(drawable)
+//        }
+
+//        holder.favorite.setOnClickListener{
+//            if (!currentItem.favorite) {
+//                // Perform bookmarking action
+//                val drawable: Drawable? = holder.itemView.context.getDrawable(R.drawable.ic_star_filled_50)
+//                holder.favorite.setImageDrawable(drawable)
+//                currentItem.favorite = true
+//            } else {
+//                // Perform unbookmarking action
+//                val drawable: Drawable? = holder.itemView.context.getDrawable(R.drawable.ic_star_50)
+//                holder.favorite.setImageDrawable(drawable)
+//                currentItem.favorite = false
+//            }
+//        }
 
     }
 

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

@@ -0,0 +1,68 @@
+package com.sambath.kunkhmer.remote
+
+import com.squareup.moshi.Json
+import com.squareup.moshi.JsonClass
+
+@JsonClass(generateAdapter = true)
+data class MatchesResponse(
+    @Json(name = "code")
+    val resultCode: Int,
+    @Json(name = "message")
+    val message: ResponseMessage,
+    @Json(name = "data")
+    val data: MatchesData?
+)
+
+@JsonClass(generateAdapter = true)
+data class ResponseMessage(
+    @Json(name = "code")
+    val code: String,
+    @Json(name = "description")
+    val description: String = "NA",
+)
+
+@JsonClass(generateAdapter = true)
+data class MatchesData(
+    @Json(name = "object")
+    val obj: List<Matches>,
+)
+
+@JsonClass(generateAdapter = true)
+data class Matches(
+    @Json(name = "_id")
+    val matchInfo: MatchInfo,
+    @Json(name = "fighters")
+    val fighters: List<Fighters>,
+)
+
+@JsonClass(generateAdapter = true)
+data class MatchInfo(
+    @Json(name = "_id")
+    val id: String,
+    @Json(name = "desc")
+    val desc: String,
+    @Json(name = "date")
+    val date: String,
+    @Json(name = "title")
+    val title: String,
+    @Json(name = "createdAt")
+    val createdAt: String,
+)
+
+@JsonClass(generateAdapter = true)
+data class Fighters(
+    @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 high: String,
+    @Json(name = "nationality")
+    val nationality: String,
+    @Json(name = "filename")
+    val filename: String,
+)

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

@@ -10,15 +10,25 @@ interface ApiService {
     @Headers("No-Authentication: true")
     fun login(@Body loginRequest: LoginRequest): Single<LoginResponse>
 
+    @GET("/v1/match/getAll")
+    fun getMatches(): Single<MatchesResponse>
+
+
+
+
+
+
+    ///---------------------------------------------------------
+
     @POST("/v1/api/user/changepassword")
     fun changePassword(@Body changePwd: ChangePwd): Single<ChangePwdResponse>
 
-    @GET("/v1/api/channel")
-    fun getChannel(): Single<ChannelResponse>
-
     @GET("/v1/api/user")
     fun getUser(): Single<UserResponse>
 
+    @GET("/v1/api/channel")
+    fun getChannel(): Single<ChannelResponse>
+
     @GET("/v1/api/weight")
     fun getWeight(): Single<WeightResponse>
 

+ 33 - 11
app/src/main/java/com/sambath/kunkhmer/screen/fighter/FighterFragment.kt

@@ -1,5 +1,6 @@
 package com.sambath.kunkhmer.screen.fighter
 
+import android.content.Intent
 import android.os.Bundle
 import android.view.LayoutInflater
 import android.view.View
@@ -10,33 +11,54 @@ import com.google.android.material.tabs.TabLayout
 import com.sambath.kunkhmer.R
 import com.sambath.kunkhmer.adapter.FighterAdapter
 import com.sambath.kunkhmer.adapter.FighterCardItem
+import com.sambath.kunkhmer.app.App
+import com.sambath.kunkhmer.app.MainActivity
+import com.sambath.kunkhmer.remote.MatchesData
+import com.sambath.kunkhmer.screen.login.LoginViewModel
+import com.sambath.kunkhmer.screen.login.LoginViewState
+import kotlinx.android.synthetic.main.activity_login.tv_error
 import kotlinx.android.synthetic.main.fragment_fighter.view.recyclerViewFighter
 import kotlinx.android.synthetic.main.fragment_fighter.view.selectedTabTextView
 import kotlinx.android.synthetic.main.fragment_fighter.view.tabLayout
+import java.util.Observer
 
 class FighterFragment : Fragment() {
     private var _root: View? = null
-
     private val binding get() = _root!!
 
     // Inside your Fragment:
     var tabSelectionListener: TabSelectionListener? = null
+    private lateinit var fighterViewModel: FighterViewModel
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
     }
 
-    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, container, false)
 
-        setTab()
+        fighterViewModel = FighterViewModel(App.injectApiService(), App.injectPrefHelper())
+        fighterViewModel.getMatches()
+        fighterViewModel.state.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
+            render(it)
+        })
+
         return binding
     }
 
-    private fun setTab() {
+    private fun render(state: FighterViewState) {
+        if (state.isProgress) {
+        }
+        if (state.isLoginSuccess) {
+            if(state.matchesData != null) {
+                setTab(state.matchesData)
+            }
+        }
+        if (state.error != null) {
+        }
+    }
+
+    private fun setTab(data: MatchesData) {
         val tabTitles = listOf("Fri 18 Aug", "Yesterday", "Today", "Tomorrow", "Tue 22 Aug")
 
         // Populate TabLayout with dynamic tabs
@@ -51,7 +73,7 @@ class FighterFragment : Fragment() {
                 tab?.let {
                     val selectedTitle = it.text.toString()
                     binding.selectedTabTextView.text = "Selected Tab: $selectedTitle"
-                    setFighters()
+                    setFighters(data)
                 }
             }
 
@@ -67,10 +89,10 @@ class FighterFragment : Fragment() {
         binding.tabLayout.getTabAt(2)?.select()
     }
 
-    private fun setFighters() {
+    private fun setFighters(data: MatchesData) {
         val recyclerView = binding.recyclerViewFighter
-        val cardList = createCardList() // Create your card data list here
-        val cardAdapter = FighterAdapter(cardList)
+        //val cardList = createCardList() // Create your card data list here
+        val cardAdapter = FighterAdapter(data.obj)
 
         recyclerView.layoutManager =
             LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)

+ 52 - 0
app/src/main/java/com/sambath/kunkhmer/screen/fighter/FighterViewModel.kt

@@ -0,0 +1,52 @@
+package com.sambath.kunkhmer.screen.fighter
+
+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.MatchesData
+import com.sambath.kunkhmer.remote.service.ApiService
+import com.sambath.kunkhmer.util.PrefHelper
+import io.reactivex.android.schedulers.AndroidSchedulers
+import io.reactivex.schedulers.Schedulers
+import java.util.concurrent.TimeUnit
+
+
+
+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!!
+
+    fun getMatches() {
+        if (_state.value!!.isProgress) return
+        _state.value = prev().copy(isProgress = true)
+
+        disposables.add(
+            apiService.getMatches()
+                .timeout(10, TimeUnit.SECONDS)
+                .subscribeOn(Schedulers.io())
+                .observeOn(AndroidSchedulers.mainThread())
+                .subscribe({
+                    if (it.resultCode == 1) {
+                        _state.value = prev().copy(isProgress = false, isLoginSuccess = true, matchesData = it.data!!)
+                    } else {
+                        _state.value =
+                            prev().copy(isProgress = false, error = "[${it.message.description}]")
+                    }
+                }, {
+                    val message: String = "ប្រតិបត្តិការមិនជោគជ័យ " + it.getErrorCode()
+                    _state.value = prev().copy(isProgress = false, error = message)
+                })
+        )
+    }
+}
+
+data class FighterViewState(
+    val initial: Boolean = false,
+    val isProgress: Boolean = false,
+    val isLoginSuccess: Boolean = false,
+    val error: String? = null,
+    val matchesData: MatchesData ? = null
+)

+ 0 - 6
app/src/main/java/com/sambath/kunkhmer/screen/login/LoginActivity.kt

@@ -59,25 +59,19 @@ class LoginActivity : AppCompatActivity() {
 
     private fun render(state: LoginViewState) {
         if (state.isProgress) {
-            //Toast.makeText(this@LoginActivity, "Loading", Toast.LENGTH_LONG).show()
             tv_error.text = ""
         }
         if (state.isLoginSuccess) {
-            //Toast.makeText(this@LoginActivity, "ចូលប្រព័ន្ធជោគជ័យ", Toast.LENGTH_LONG).show()
             val intent = Intent(this, MainActivity::class.java)
             startActivity(intent)
             finishAffinity()
         }
         if (state.error != null) {
-            //Toast.makeText(this@LoginActivity, "Error", Toast.LENGTH_LONG).show()
             tv_error.text = state.error
         }
     }
 
     override fun onDestroy() {
-        // loginViewModel.state.removeObservers(this)
         super.onDestroy()
-        //Toast.makeText(this@LoginActivity, "On Destroy Login", Toast.LENGTH_LONG).show()
-
     }
 }

+ 1 - 2
app/src/main/java/com/sambath/kunkhmer/screen/login/LoginViewModel.kt

@@ -17,6 +17,7 @@ import java.util.concurrent.TimeUnit
 class LoginViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : BaseViewModel() {
     private val _state = MutableLiveData<LoginViewState>(LoginViewState())
     val state: LiveData<LoginViewState> = _state
+
     fun login(username: String, password: String) {
         if (_state.value!!.isProgress) return
         val request = LoginRequest(username, password)
@@ -46,6 +47,4 @@ class LoginViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : B
     }
 
     private fun prev() = _state.value!!
-
-
 }

+ 0 - 1
app/src/main/java/com/sambath/kunkhmer/screen/main/MainViewModel.kt

@@ -37,7 +37,6 @@ class MainViewModel(val apiService: ApiService, val prefHelper: PrefHelper): Bas
                             prevChannel().copy(isUserPro = false, errorUser = "[${it.message.description}]")
                     }
                 }, {
-//                    Log.d("dataxzsd", it.getErrorCode())
                     val message: String = "ប្រតិបត្តិការមិនជោគជ័យ\n" + it.getErrorCode()
                     _stateChannel.value = prevChannel().copy(isUserPro = false, errorUser = message)
                 })

+ 6 - 0
app/src/main/res/layout/app_bar_boxing.xml

@@ -42,6 +42,7 @@
                 android:background="?attr/selectableItemBackgroundBorderless"
                 android:clickable="true"
                 android:focusable="true"
+                android:visibility="gone"
                 android:scaleType="fitXY"
                 android:src="@drawable/ic_user_white"
                 app:layout_constraintBottom_toBottomOf="@+id/iv_logo"
@@ -56,6 +57,7 @@
                 android:layout_marginEnd="8dp"
                 android:adjustViewBounds="true"
                 android:clickable="true"
+                android:visibility="gone"
                 android:focusable="true"
                 android:scaleType="fitXY"
                 android:src="@drawable/ic_star_filled_50"
@@ -71,6 +73,7 @@
                 android:layout_marginEnd="8dp"
                 android:adjustViewBounds="true"
                 android:clickable="true"
+                android:visibility="gone"
                 android:focusable="true"
                 android:scaleType="fitXY"
                 android:src="@drawable/ic_calendar_64"
@@ -88,6 +91,7 @@
                 android:clickable="true"
                 android:focusable="true"
                 android:scaleType="fitXY"
+                android:visibility="gone"
                 android:src="@drawable/ic_search_50"
                 app:layout_constraintBottom_toBottomOf="@+id/iv_logo"
                 app:layout_constraintEnd_toStartOf="@+id/iv_calendar"
@@ -102,6 +106,7 @@
                 android:adjustViewBounds="true"
                 android:clickable="true"
                 android:focusable="true"
+                android:visibility="gone"
                 android:scaleType="fitXY"
                 android:src="@drawable/ic_bell_96"
                 app:layout_constraintBottom_toBottomOf="@+id/iv_logo"
@@ -113,6 +118,7 @@
                 android:layout_width="26dp"
                 android:layout_height="wrap_content"
                 android:layout_marginTop="2dp"
+                android:visibility="gone"
                 android:layout_marginEnd="8dp"
                 android:adjustViewBounds="true"
                 android:clickable="true"

+ 1 - 0
app/src/main/res/layout/fragment_fighter.xml

@@ -12,6 +12,7 @@
         android:id="@+id/tabLayout"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
+        android:visibility="gone"
         android:background="@color/color_black"
         app:tabGravity="fill"
         app:tabMode="scrollable"

+ 1 - 0
app/src/main/res/layout/layout_fighter_card.xml

@@ -17,6 +17,7 @@
         <ImageView
             android:id="@+id/bookmarkToggleButton"
             android:layout_width="30dp"
+            android:visibility="gone"
             android:layout_height="30dp"
             android:layout_margin="5dp"
             android:src="@drawable/ic_star_50" />