瀏覽代碼

Image in fighter

dola 1 年之前
父節點
當前提交
dc0746b03c

+ 31 - 53
app/src/main/java/com/sambath/kunkhmer/adapter/FighterAdapter.kt

@@ -1,7 +1,8 @@
 package com.sambath.kunkhmer.adapter
 
-import android.graphics.drawable.Drawable
-import android.util.Patterns
+import android.graphics.Bitmap
+import android.graphics.BitmapFactory
+import android.util.Log
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
@@ -9,78 +10,55 @@ import android.widget.ImageView
 import android.widget.TextView
 import androidx.recyclerview.widget.RecyclerView
 import com.sambath.kunkhmer.R
+import com.sambath.kunkhmer.adapter.FighterAdapter.ViewHolder
+import com.sambath.kunkhmer.config.Config
+import com.sambath.kunkhmer.remote.Fighters
 import com.sambath.kunkhmer.remote.Matches
+import com.sambath.kunkhmer.remote.service.ApiService
 import com.squareup.picasso.Picasso
+import io.reactivex.android.schedulers.AndroidSchedulers
+import io.reactivex.disposables.CompositeDisposable
+import io.reactivex.schedulers.Schedulers
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.launch
+import okhttp3.ResponseBody
 
 
-class FighterAdapter (private val cardList: List<Matches>) : RecyclerView.Adapter<FighterAdapter.ViewHolder>()  {
+class FighterAdapter(private val cardList: List<Matches>, private val apiService: ApiService) : RecyclerView.Adapter<ViewHolder>() {
+    private val disposables = CompositeDisposable()
+
     class CardViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
 
-    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FighterAdapter.ViewHolder {
+    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
         val itemView = LayoutInflater.from(parent.context).inflate(R.layout.layout_fighter_card, parent, false)
-        return FighterAdapter.ViewHolder(itemView)
+        return ViewHolder(itemView)
     }
 
     override fun onBindViewHolder(holder: ViewHolder, position: Int) {
         val currentItem = cardList[position]
 
-        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 {
-            // Load default image
-            Picasso.get()
-                .load(R.drawable.ic_user_white)
-                .into(holder.imageLeft)
-        }
-
         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
+        Picasso.get()
+            .load(Config.BASE_URL + "/v1/fighter/image/" + currentItem.fighters[0].filename)
+            .into(holder.imageLeft);
 
-//        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)
-//        }
+        //------------------------------Right
+        holder.imageRightTitle.text = currentItem.fighters[1]?.name
+        Picasso.get()
+            .load(Config.BASE_URL + "/v1/fighter/image/" + currentItem.fighters[1].filename)
+            .into(holder.imageRight);
 
-//        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.date.text = currentItem.matchInfo.date
+        holder.title.text = currentItem.matchInfo.title
     }
 
     override fun getItemCount() = cardList.size
 
     // Holds the views for adding it to image and text
-    class ViewHolder(ItemView: View) : RecyclerView.ViewHolder(ItemView) {
+    inner class ViewHolder(ItemView: View) : RecyclerView.ViewHolder(ItemView) {
         val imageLeft: ImageView = itemView.findViewById(R.id.imageLeft)
         val imageLeftTitle: TextView = itemView.findViewById(R.id.titleLeft)
         val imageRight: ImageView = itemView.findViewById(R.id.imageRight)

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

@@ -65,4 +65,6 @@ data class Fighters(
     val nationality: String,
     @Json(name = "filename")
     val filename: String,
-)
+)
+
+//------------- Image

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

@@ -2,6 +2,7 @@ package com.sambath.kunkhmer.remote.service
 
 import com.sambath.kunkhmer.remote.*
 import io.reactivex.Single
+import okhttp3.ResponseBody
 import retrofit2.http.*
 
 interface ApiService {
@@ -13,9 +14,8 @@ interface ApiService {
     @GET("/v1/match/getAll")
     fun getMatches(): Single<MatchesResponse>
 
-
-
-
+    @GET("fighter/image/{imageId}.jpeg")
+    fun getFighterImage(@Path("imageId") imageId: String): Single<ResponseBody>
 
 
     ///---------------------------------------------------------

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

@@ -92,7 +92,7 @@ class FighterFragment : Fragment() {
     private fun setFighters(data: MatchesData) {
         val recyclerView = binding.recyclerViewFighter
         //val cardList = createCardList() // Create your card data list here
-        val cardAdapter = FighterAdapter(data.obj)
+        val cardAdapter = FighterAdapter(data.obj, App.injectApiService())
 
         recyclerView.layoutManager =
             LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)

+ 28 - 1
app/src/main/java/com/sambath/kunkhmer/screen/fighter/FighterViewModel.kt

@@ -1,5 +1,7 @@
 package com.sambath.kunkhmer.screen.fighter
 
+import android.graphics.Bitmap
+import android.graphics.BitmapFactory
 import android.util.Log
 import androidx.lifecycle.LiveData
 import androidx.lifecycle.MutableLiveData
@@ -10,6 +12,7 @@ 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 okhttp3.ResponseBody
 import java.util.concurrent.TimeUnit
 
 
@@ -41,6 +44,29 @@ class FighterViewModel(val apiService: ApiService, val prefHelper: PrefHelper) :
                 })
         )
     }
+
+    fun getFighterImage(imageUrl: String) : LiveData<Bitmap?> {
+        val imageLiveData = MutableLiveData<Bitmap?>()
+        disposables.add(apiService.getFighterImage(imageUrl)
+            .map { responseBody -> processResponseIntoImage(responseBody) }
+            .subscribeOn(Schedulers.io())
+            .observeOn(AndroidSchedulers.mainThread())
+            .subscribe({ bitmap ->
+                imageLiveData.value = bitmap
+            }, { throwable ->
+                Log.e("FighterViewModel", "Error getting image", throwable)
+            })
+        )
+        return imageLiveData
+    }
+
+    private fun processResponseIntoImage(responseBody: ResponseBody): Bitmap? {
+        return if (responseBody.contentLength() > 0) {
+            BitmapFactory.decodeStream(responseBody.byteStream())
+        } else {
+            null
+        }
+    }
 }
 
 data class FighterViewState(
@@ -48,5 +74,6 @@ data class FighterViewState(
     val isProgress: Boolean = false,
     val isLoginSuccess: Boolean = false,
     val error: String? = null,
-    val matchesData: MatchesData ? = null
+    val matchesData: MatchesData ? = null,
+    val fighterImage: Bitmap? = null
 )

+ 137 - 137
app/src/main/java/com/sambath/kunkhmer/screen/main/MainFragment.kt

@@ -28,143 +28,143 @@ import com.sambath.kunkhmer.view.SettingButtonView
  * A simple [Fragment] subclass as the default destination in the navigation.
  */
 class MainFragment : BaseFragment(R.layout.fragment_main) {
-    private val TAB_TITLES = arrayOf(
-        R.string.current_bets,
-        R.string.today_report,
-    )
-    private lateinit var mainViewModel: MainViewModel
-    private val shareViewModel = App.injectShareViewModel()
-    private val sharePref = App.injectPrefHelper()
-    private lateinit var user: User
-    private lateinit var btnPlay: SettingButtonView
-    private lateinit var btnResult: SettingButtonView
-    private lateinit var betTouch: MediaPlayer
-    lateinit var result: MediaPlayer
-    private var anim: AlphaAnimation? = null
-    override fun onCreate(savedInstanceState: Bundle?) {
-//        mainViewModel = MainViewModel(App.injectApiService(), App.injectPrefHelper())
-        super.onCreate(savedInstanceState)
-        //companyInfoId =arguments?.getInt(COM_INTO_ID_KEY) ?: 0
-        (requireActivity() as ShareActivity).apply {
-            user = userShare
-        }
-        anim = AlphaAnimation(0.0f, 1.0f)
-        anim?.duration = 50 //You can manage the blinking time with this parameter
-        anim?.startOffset = 50
-        anim?.repeatMode = Animation.REVERSE
-        anim?.repeatCount = Animation.INFINITE
-    }
-
-    @SuppressLint("CheckResult")
-    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
-        super.onViewCreated(view, savedInstanceState)
-//        mainViewModel.state.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
-//            getMainData(it)
-//        })
-//        mainViewModel.getMainData()
+//    private val TAB_TITLES = arrayOf(
+//        R.string.current_bets,
+//        R.string.today_report,
+//    )
+//    private lateinit var mainViewModel: MainViewModel
+//    private val shareViewModel = App.injectShareViewModel()
+//    private val sharePref = App.injectPrefHelper()
+//    private lateinit var user: User
+//    private lateinit var btnPlay: SettingButtonView
+//    private lateinit var btnResult: SettingButtonView
+//    private lateinit var betTouch: MediaPlayer
+//    lateinit var result: MediaPlayer
+//    private var anim: AlphaAnimation? = null
+//    override fun onCreate(savedInstanceState: Bundle?) {
+////        mainViewModel = MainViewModel(App.injectApiService(), App.injectPrefHelper())
+//        super.onCreate(savedInstanceState)
+//        //companyInfoId =arguments?.getInt(COM_INTO_ID_KEY) ?: 0
 //        (requireActivity() as ShareActivity).apply {
-//            userNameTextView.text = user.userName
+//            user = userShare
 //        }
-        val mainPagerAdapter = MainPagerAdapter(requireActivity() as ShareActivity)
-        val viewPager: ViewPager2 = view.findViewById(R.id.view_pager)
-        viewPager.adapter = mainPagerAdapter
-        val tabs: TabLayout = view.findViewById(R.id.tabLayout)
-        TabLayoutMediator(
-            tabs, viewPager
-        ) { tab, position -> // Styling each tab here
-            tab.text = getString(TAB_TITLES[position])
-        }.attach()
-//        shareViewModel.getConnectivityStates.observe(viewLifecycleOwner, Observer {
-//            getSocketConectionStatus(it)
-//        })
-        btnPlay = view.findViewById(R.id.btn_play)
-        btnPlay.setOnClickListener {
-//            if (!sharePref.getIsMute()) betTouch.start()
-            it.btnClick().subscribe {
-//                btnPlay.isEnabled = false
-                (requireActivity() as? ShareActivity)?.apply {
-                    supportFragmentManager.beginTransaction()
-                        .setCustomAnimations(
-                            R.anim.slide_in,
-                            R.anim.slide_out,
-                            R.anim.slide_in_exit,
-                            R.anim.slide_out_exit
-                        )
-                        .add(R.id.container, CfBetFragment().apply {
-                            arguments =
-                                Bundle().apply {
-                                    // putInt(Fragment7.COM_INFO_ID_KEY, companyInfoId)
-                                }
-                        })
-                        .addToBackStack(null)
-                        .commit()
-                }
-            }
-        }
-        btnResult = view.findViewById(R.id.btn_result)
-        btnResult.setOnClickListener {
-//            if (!sharePref.getIsMute()) betTouch.start()
-            it.btnClick().subscribe {
-//                btnResult.isEnabled = false
-                (requireActivity() as? ShareActivity)?.apply {
-                    supportFragmentManager.beginTransaction()
-                        .setCustomAnimations(
-                            R.anim.slide_in,
-                            R.anim.slide_out,
-                            R.anim.slide_in_exit,
-                            R.anim.slide_out_exit
-                        )
-                        .add(R.id.container, CfResultFragment().apply {
-                            arguments =
-                                Bundle().apply {
-                                    // putInt(Fragment7.COM_INFO_ID_KEY, companyInfoId)
-                                }
-                        })
-                        .addToBackStack(null)
-                        .commit()
-                }
-            }
-        }
-        (requireActivity() as? ShareActivity)?.apply {
-            groupToolbarIcons.isVisible = true
-            layCfStatus.isVisible = true
-            settingImageView.setOnClickListener {
-//                if (!sharePref.getIsMute()) betTouch.start()
-                it.btnClick().subscribe {
-                    supportFragmentManager.beginTransaction()
-                        .setCustomAnimations(
-                            R.anim.slide_in,
-                            R.anim.slide_out,
-                            R.anim.slide_in_exit,
-                            R.anim.slide_out_exit
-                        )
-                        .add(R.id.container, SettingFragment())
-                        .addToBackStack(null)
-                        .commit()
-                }
-            }
-
-            reportImageView.setOnClickListener {
-//                if (!sharePref.getIsMute()) betTouch.start()
-                layCfStatus.isVisible = true
-                it.btnClick().subscribe {
-                    supportFragmentManager.beginTransaction()
-                        .setCustomAnimations(
-                            R.anim.slide_in,
-                            R.anim.slide_out,
-                            R.anim.slide_in_exit,
-                            R.anim.slide_out_exit
-                        )
-                        .add(R.id.container, StatementFragment().apply {
-                            arguments =
-                                Bundle().apply {
-                                    // putInt(Fragment7.COM_INFO_ID_KEY, companyInfoId)
-                                }
-                        })
-                        .addToBackStack(null)
-                        .commit()
-                }
-            }
-        }
-    }
+//        anim = AlphaAnimation(0.0f, 1.0f)
+//        anim?.duration = 50 //You can manage the blinking time with this parameter
+//        anim?.startOffset = 50
+//        anim?.repeatMode = Animation.REVERSE
+//        anim?.repeatCount = Animation.INFINITE
+//    }
+//
+//    @SuppressLint("CheckResult")
+//    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
+//        super.onViewCreated(view, savedInstanceState)
+////        mainViewModel.state.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
+////            getMainData(it)
+////        })
+////        mainViewModel.getMainData()
+////        (requireActivity() as ShareActivity).apply {
+////            userNameTextView.text = user.userName
+////        }
+//        val mainPagerAdapter = MainPagerAdapter(requireActivity() as ShareActivity)
+//        val viewPager: ViewPager2 = view.findViewById(R.id.view_pager)
+//        viewPager.adapter = mainPagerAdapter
+//        val tabs: TabLayout = view.findViewById(R.id.tabLayout)
+//        TabLayoutMediator(
+//            tabs, viewPager
+//        ) { tab, position -> // Styling each tab here
+//            tab.text = getString(TAB_TITLES[position])
+//        }.attach()
+////        shareViewModel.getConnectivityStates.observe(viewLifecycleOwner, Observer {
+////            getSocketConectionStatus(it)
+////        })
+//        btnPlay = view.findViewById(R.id.btn_play)
+//        btnPlay.setOnClickListener {
+////            if (!sharePref.getIsMute()) betTouch.start()
+//            it.btnClick().subscribe {
+////                btnPlay.isEnabled = false
+//                (requireActivity() as? ShareActivity)?.apply {
+//                    supportFragmentManager.beginTransaction()
+//                        .setCustomAnimations(
+//                            R.anim.slide_in,
+//                            R.anim.slide_out,
+//                            R.anim.slide_in_exit,
+//                            R.anim.slide_out_exit
+//                        )
+//                        .add(R.id.container, CfBetFragment().apply {
+//                            arguments =
+//                                Bundle().apply {
+//                                    // putInt(Fragment7.COM_INFO_ID_KEY, companyInfoId)
+//                                }
+//                        })
+//                        .addToBackStack(null)
+//                        .commit()
+//                }
+//            }
+//        }
+//        btnResult = view.findViewById(R.id.btn_result)
+//        btnResult.setOnClickListener {
+////            if (!sharePref.getIsMute()) betTouch.start()
+//            it.btnClick().subscribe {
+////                btnResult.isEnabled = false
+//                (requireActivity() as? ShareActivity)?.apply {
+//                    supportFragmentManager.beginTransaction()
+//                        .setCustomAnimations(
+//                            R.anim.slide_in,
+//                            R.anim.slide_out,
+//                            R.anim.slide_in_exit,
+//                            R.anim.slide_out_exit
+//                        )
+//                        .add(R.id.container, CfResultFragment().apply {
+//                            arguments =
+//                                Bundle().apply {
+//                                    // putInt(Fragment7.COM_INFO_ID_KEY, companyInfoId)
+//                                }
+//                        })
+//                        .addToBackStack(null)
+//                        .commit()
+//                }
+//            }
+//        }
+//        (requireActivity() as? ShareActivity)?.apply {
+//            groupToolbarIcons.isVisible = true
+//            layCfStatus.isVisible = true
+//            settingImageView.setOnClickListener {
+////                if (!sharePref.getIsMute()) betTouch.start()
+//                it.btnClick().subscribe {
+//                    supportFragmentManager.beginTransaction()
+//                        .setCustomAnimations(
+//                            R.anim.slide_in,
+//                            R.anim.slide_out,
+//                            R.anim.slide_in_exit,
+//                            R.anim.slide_out_exit
+//                        )
+//                        .add(R.id.container, SettingFragment())
+//                        .addToBackStack(null)
+//                        .commit()
+//                }
+//            }
+//
+//            reportImageView.setOnClickListener {
+////                if (!sharePref.getIsMute()) betTouch.start()
+//                layCfStatus.isVisible = true
+//                it.btnClick().subscribe {
+//                    supportFragmentManager.beginTransaction()
+//                        .setCustomAnimations(
+//                            R.anim.slide_in,
+//                            R.anim.slide_out,
+//                            R.anim.slide_in_exit,
+//                            R.anim.slide_out_exit
+//                        )
+//                        .add(R.id.container, StatementFragment().apply {
+//                            arguments =
+//                                Bundle().apply {
+//                                    // putInt(Fragment7.COM_INFO_ID_KEY, companyInfoId)
+//                                }
+//                        })
+//                        .addToBackStack(null)
+//                        .commit()
+//                }
+//            }
+//        }
+//    }
 }

+ 1 - 1
app/src/main/java/com/sambath/kunkhmer/screen/main/cflive/currentbets/CurrentBetsFragment.kt

@@ -53,7 +53,7 @@ class CurrentBetsFragment : BaseFragment(R.layout.fragment_current_bets) {
         rv_current_bet.adapter = adapter
         adapter.printButtonListener = { fightNo, betDate, betType, amount, payout, payoutPrint, id ->
         }
-        label_amount.text = "Amount ($currency)"
+        //label_amount.text = "Amount ($currency)"
         currentBetsViewModel.getCurrentBetsData()
         currentBetsViewModel.state.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
             getCurrentBets(it)

+ 1 - 1
app/src/main/java/com/sambath/kunkhmer/screen/splash/SplashScreenActivity.kt

@@ -59,7 +59,7 @@ class SplashScreenActivity : BaseActivity() {
                     startActivity(intent)
 
                     if (prefHelper.getToken().isNotEmpty()) {
-                        val intent = Intent(this@SplashScreenActivity, ShareActivity::class.java)
+                        val intent = Intent(this@SplashScreenActivity, MainActivity::class.java)
                         startActivity(intent)
                     }else{
                         val intent = Intent(this@SplashScreenActivity, LoginActivity::class.java)