Home Fragment 2 ui
Custom pressable card with two different press type Hindi versioning for homefragment2
This commit is contained in:
@@ -5,20 +5,86 @@ import androidx.fragment.app.Fragment
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import com.woka.R
|
||||
import com.woka.WokaApp
|
||||
import com.woka.databinding.FragmentHome2Binding
|
||||
import com.woka.home.HomeViewModel
|
||||
import com.woka.mvvm.userDataModels.UserDataResponse
|
||||
import com.woka.networking.ApiResult
|
||||
import com.woka.utils.UserType
|
||||
import com.woka.utils.toast
|
||||
|
||||
class Home2Fragment : Fragment() {
|
||||
|
||||
private lateinit var binding: FragmentHome2Binding
|
||||
|
||||
private lateinit var viewModel: HomeViewModel
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater, container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View? {
|
||||
): View {
|
||||
binding = FragmentHome2Binding.inflate(inflater, container, false)
|
||||
|
||||
activity?.let {
|
||||
viewModel = ViewModelProvider(it)[HomeViewModel::class.java]
|
||||
}
|
||||
|
||||
setObservers()
|
||||
|
||||
clickEvents()
|
||||
|
||||
return binding.root
|
||||
}
|
||||
|
||||
private fun clickEvents(){
|
||||
binding.apply {
|
||||
webSeries.setOnClickListener {
|
||||
toast("toast")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun setObservers() {
|
||||
WokaApp.userPrefs?.userLiveData?.observe(viewLifecycleOwner) {
|
||||
if (it is ApiResult.Success) {
|
||||
updateUserData(it.data)
|
||||
}
|
||||
}
|
||||
|
||||
viewModel.localeChangeLiveData.observe(viewLifecycleOwner) {
|
||||
binding.apply {
|
||||
helloTxt.text = getString(R.string.hello)
|
||||
welcomeText.text = getString(R.string.welcome_to_wokaland)
|
||||
|
||||
webSeriesTxt.text = getString(R.string.web_series)
|
||||
liveTvTxt.text = getString(R.string.live_tv)
|
||||
shopTxt.text = getString(R.string.shop)
|
||||
karaokeTxt.text = getString(R.string.karaoke)
|
||||
audioBooksTxt.text = getString(R.string.audio_books)
|
||||
gamesTxt.text = getString(R.string.games)
|
||||
wokaFmTxt.text = getString(R.string.woka_fm)
|
||||
|
||||
exploreWokaTxt.text = getString(R.string.explore_woka)
|
||||
comingSoonTxt.text = getString(R.string.coming_soonn_on_woka)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateUserData(data: UserDataResponse?) {
|
||||
binding.apply {
|
||||
val name = if (WokaApp.userPrefs?.userType == UserType.GUEST) WokaApp.userPrefs?.guestUserName
|
||||
else data?.result?.fullname
|
||||
|
||||
if (!name.isNullOrEmpty()) {
|
||||
val text = ", ${name.split(" ")[0]}"
|
||||
userName.text = text
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun newInstance() = Home2Fragment()
|
||||
}
|
||||
|
||||
110
app/src/main/java/com/woka/utils/PressableCard.kt
Normal file
110
app/src/main/java/com/woka/utils/PressableCard.kt
Normal file
@@ -0,0 +1,110 @@
|
||||
package com.woka.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import android.util.Log
|
||||
import android.view.MotionEvent
|
||||
import android.view.MotionEvent.ACTION_CANCEL
|
||||
import android.view.MotionEvent.ACTION_DOWN
|
||||
import android.view.MotionEvent.ACTION_OUTSIDE
|
||||
import android.view.MotionEvent.ACTION_SCROLL
|
||||
import android.view.MotionEvent.ACTION_UP
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.FrameLayout
|
||||
import android.widget.ScrollView
|
||||
import androidx.core.view.ScrollingView
|
||||
import com.woka.R
|
||||
import kotlin.math.abs
|
||||
|
||||
class PressableCard : FrameLayout {
|
||||
|
||||
companion object {
|
||||
private const val DEFAULT_SCALE_WHEN_PRESS = 0.9f
|
||||
private const val DEFAULT_SCALE = 1f
|
||||
|
||||
private const val DEFAULT_ALPHA_WHEN_PRESS = 0.5f
|
||||
private const val DEFAULT_ALPHA = 1f
|
||||
|
||||
private const val MIN_MOMENT_AFTER_TOUCH = 25f
|
||||
}
|
||||
|
||||
enum class PressableType(value: Int){
|
||||
SCALE(0), FADE(1);
|
||||
}
|
||||
|
||||
constructor(context: Context?) : super(context!!)
|
||||
constructor(context: Context?, attrs: AttributeSet?) : super(
|
||||
context!!, attrs
|
||||
){
|
||||
setPressType(attrs)
|
||||
}
|
||||
|
||||
constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(
|
||||
context!!, attrs, defStyle
|
||||
) {
|
||||
setPressType(attrs)
|
||||
}
|
||||
|
||||
// attributes
|
||||
private var pressType: PressableType? = PressableType.SCALE
|
||||
|
||||
private fun setPressType(attrs: AttributeSet?){
|
||||
val attributes = context.obtainStyledAttributes(attrs, R.styleable.PressableCard)
|
||||
val value = attributes.getInt(R.styleable.PressableCard_pressType, 0)
|
||||
pressType = when(value){
|
||||
1 -> PressableType.FADE
|
||||
else -> PressableType.SCALE
|
||||
}
|
||||
attributes.recycle()
|
||||
}
|
||||
|
||||
private fun refresh() {
|
||||
if (isPressed) {
|
||||
if (pressType == PressableType.SCALE){
|
||||
scaleX = (DEFAULT_SCALE_WHEN_PRESS)
|
||||
scaleY = (DEFAULT_SCALE_WHEN_PRESS)
|
||||
}else if (pressType == PressableType.FADE){
|
||||
alpha = DEFAULT_ALPHA_WHEN_PRESS
|
||||
}
|
||||
invalidate()
|
||||
return
|
||||
}
|
||||
if (pressType == PressableType.SCALE){
|
||||
scaleX = (DEFAULT_SCALE)
|
||||
scaleY = (DEFAULT_SCALE)
|
||||
}else if (pressType == PressableType.FADE){
|
||||
alpha = DEFAULT_ALPHA
|
||||
}
|
||||
invalidate()
|
||||
}
|
||||
|
||||
override fun setPressed(pressed: Boolean) {
|
||||
super.setPressed(pressed)
|
||||
refresh()
|
||||
}
|
||||
|
||||
override fun onTouchEvent(event: MotionEvent): Boolean {
|
||||
|
||||
when (event.action){
|
||||
ACTION_DOWN -> setPressed(true)
|
||||
ACTION_UP -> {
|
||||
setPressed(false)
|
||||
val x = event.x
|
||||
val y = event.y
|
||||
val isInside = x > 0 && x < width && y > 0 && y < height
|
||||
if (isInside) {
|
||||
performClick()
|
||||
}
|
||||
}
|
||||
ACTION_CANCEL -> setPressed(false)
|
||||
ACTION_SCROLL -> setPressed(false)
|
||||
ACTION_OUTSIDE -> setPressed(false)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
override fun performClick(): Boolean {
|
||||
return super.performClick()
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.woka.home
|
||||
package com.woka.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
@@ -33,16 +33,20 @@ class PressableImageView : AppCompatImageView {
|
||||
}
|
||||
|
||||
override fun onTouchEvent(event: MotionEvent): Boolean {
|
||||
if (event.action == MotionEvent.ACTION_DOWN) {
|
||||
setPressed(true)
|
||||
} else if (event.action == MotionEvent.ACTION_UP) {
|
||||
setPressed(false)
|
||||
val x = event.x
|
||||
val y = event.y
|
||||
val isInside = x > 0 && x < width && y > 0 && y < height
|
||||
if (isInside) {
|
||||
performClick()
|
||||
when (event.action){
|
||||
MotionEvent.ACTION_DOWN -> setPressed(true)
|
||||
MotionEvent.ACTION_UP -> {
|
||||
setPressed(false)
|
||||
val x = event.x
|
||||
val y = event.y
|
||||
val isInside = x > 0 && x < width && y > 0 && y < height
|
||||
if (isInside) {
|
||||
performClick()
|
||||
}
|
||||
}
|
||||
MotionEvent.ACTION_CANCEL -> setPressed(false)
|
||||
MotionEvent.ACTION_SCROLL -> setPressed(false)
|
||||
MotionEvent.ACTION_OUTSIDE -> setPressed(false)
|
||||
}
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user