Activity home fragment theme 1 background- differentiating grass, clouds animations, stars and moon in night background.
status bar color navigation bar color language setup
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package com.woka.home
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.content.res.ColorStateList
|
||||
import android.graphics.Color
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
@@ -11,6 +15,7 @@ import androidx.activity.enableEdgeToEdge
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.view.GravityCompat
|
||||
import androidx.core.view.ViewCompat
|
||||
import androidx.core.view.WindowCompat
|
||||
import androidx.core.view.WindowInsetsCompat
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
@@ -41,6 +46,9 @@ class HomeActivity : WokaBaseActivity(),
|
||||
|
||||
private lateinit var viewModel: HomeViewModel
|
||||
|
||||
private var minuteReceiver: BroadcastReceiver? = null
|
||||
private var currentBackground: TimePeriod? = null
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
enableEdgeToEdge()
|
||||
@@ -52,13 +60,26 @@ class HomeActivity : WokaBaseActivity(),
|
||||
insets
|
||||
}
|
||||
|
||||
window?.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
|
||||
window?.statusBarColor = Color.parseColor("#55cffe")
|
||||
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
|
||||
window.statusBarColor = Color.TRANSPARENT
|
||||
window.navigationBarColor = Color.WHITE
|
||||
WindowCompat.getInsetsController(window, window.decorView).isAppearanceLightStatusBars = false
|
||||
|
||||
viewModel = ViewModelProvider(this)[HomeViewModel::class.java]
|
||||
|
||||
userPrefs?.userLiveData?.observe(this, this)
|
||||
|
||||
minuteReceiver = object : BroadcastReceiver(){
|
||||
override fun onReceive(context: Context?, intent: Intent?) {
|
||||
// this function is called every minute
|
||||
if (intent?.action == Intent.ACTION_TIME_TICK){
|
||||
updateBackground()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
registerReceiver(minuteReceiver, IntentFilter(Intent.ACTION_TIME_TICK))
|
||||
|
||||
initViews()
|
||||
|
||||
clickEvents()
|
||||
@@ -72,6 +93,11 @@ class HomeActivity : WokaBaseActivity(),
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
unregisterReceiver(minuteReceiver)
|
||||
}
|
||||
|
||||
private fun initViews() {
|
||||
binding.apply {
|
||||
val versionName = "VER ${BuildConfig.VERSION_NAME}"
|
||||
@@ -199,9 +225,42 @@ class HomeActivity : WokaBaseActivity(),
|
||||
}
|
||||
}
|
||||
|
||||
updateBackground()
|
||||
viewModel.selectedBottomTab = tab
|
||||
}
|
||||
|
||||
// updating background
|
||||
private fun updateBackground(){
|
||||
when(binding.bottomNav.getSelectedTab()){
|
||||
HOME -> {
|
||||
if (userPrefs?.appTheme == Theme.THEME_ONE){
|
||||
val timePeriod = TimePeriod.getCurrentTimePeriod()
|
||||
if (currentBackground != timePeriod){
|
||||
// time period has changed
|
||||
binding.root.background = when(timePeriod){
|
||||
TimePeriod.MORNING -> {
|
||||
ContextCompat.getDrawable(this, R.drawable.grad_morning)
|
||||
}
|
||||
TimePeriod.AFTERNOON -> {
|
||||
ContextCompat.getDrawable(this, R.drawable.grad_afternoon)
|
||||
}
|
||||
TimePeriod.EVENING -> {
|
||||
ContextCompat.getDrawable(this, R.drawable.grad_evening)
|
||||
}
|
||||
TimePeriod.NIGHT -> {
|
||||
ContextCompat.getDrawable(this, R.drawable.grad_night)
|
||||
}
|
||||
}
|
||||
|
||||
currentBackground = timePeriod
|
||||
}
|
||||
}else{
|
||||
binding.root.backgroundTintList = ColorStateList.valueOf(getColor(R.color.color_primary))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// observer for userdata changes
|
||||
override fun onChanged(value: ApiResult<UserDataResponse>) {
|
||||
when(value){
|
||||
|
||||
24
app/src/main/java/com/woka/home/TimePeriod.kt
Normal file
24
app/src/main/java/com/woka/home/TimePeriod.kt
Normal file
@@ -0,0 +1,24 @@
|
||||
package com.woka.home
|
||||
|
||||
import java.util.Calendar
|
||||
|
||||
enum class TimePeriod{
|
||||
MORNING,
|
||||
AFTERNOON,
|
||||
EVENING,
|
||||
NIGHT;
|
||||
|
||||
companion object{
|
||||
fun getCurrentTimePeriod(): TimePeriod{
|
||||
val cal = Calendar.getInstance()
|
||||
val hrs = cal.get(Calendar.HOUR_OF_DAY)
|
||||
|
||||
return when (hrs) {
|
||||
in 6..10 -> MORNING
|
||||
in 11..15 -> AFTERNOON
|
||||
in 16..19 -> EVENING
|
||||
else -> NIGHT
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,63 +11,59 @@ import android.os.Bundle
|
||||
import android.util.DisplayMetrics
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.View.GONE
|
||||
import android.view.View.VISIBLE
|
||||
import android.view.ViewGroup
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import com.woka.R
|
||||
import com.woka.WokaApp.Companion.userPrefs
|
||||
import com.woka.databinding.FragmentHome1Binding
|
||||
import com.woka.utils.toast
|
||||
import java.util.Calendar
|
||||
import com.woka.home.HomeViewModel
|
||||
import com.woka.home.TimePeriod
|
||||
import com.woka.mvvm.userDataModels.UserData
|
||||
import com.woka.mvvm.userDataModels.UserDataResponse
|
||||
import com.woka.networking.ApiResult
|
||||
import com.woka.utils.UserType
|
||||
|
||||
class Home1Fragment : Fragment() {
|
||||
|
||||
enum class TimePeriod{
|
||||
MORNING,
|
||||
AFTERNOON,
|
||||
EVENING,
|
||||
NIGHT;
|
||||
|
||||
companion object{
|
||||
fun getCurrentTimePeriod(): TimePeriod{
|
||||
val cal = Calendar.getInstance()
|
||||
val hrs = cal.get(Calendar.HOUR_OF_DAY)
|
||||
|
||||
return when (hrs) {
|
||||
in 6..10 -> MORNING
|
||||
in 11..15 -> AFTERNOON
|
||||
in 16..19 -> EVENING
|
||||
else -> NIGHT
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private lateinit var binding: FragmentHome1Binding
|
||||
private lateinit var viewModel: HomeViewModel
|
||||
|
||||
private var currentBackground: TimePeriod? = null
|
||||
private var minuteReceiver: BroadcastReceiver? = null
|
||||
|
||||
private var valueAnimator: ValueAnimator? = null
|
||||
private var tvAnimator: ValueAnimator? = null
|
||||
private var cloud1Animator: ValueAnimator? = null
|
||||
private var cloud2Animator: ValueAnimator? = null
|
||||
private var star1Animator: ValueAnimator? = null
|
||||
private var star2Animator: ValueAnimator? = null
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater, container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View? {
|
||||
binding = FragmentHome1Binding.inflate(inflater, container, false)
|
||||
activity?.let {
|
||||
viewModel = ViewModelProvider(it)[HomeViewModel::class.java]
|
||||
}
|
||||
|
||||
updateBackground()
|
||||
|
||||
minuteReceiver = object : BroadcastReceiver(){
|
||||
minuteReceiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context?, intent: Intent?) {
|
||||
// this function is called every minute
|
||||
if (intent?.action == ACTION_TIME_TICK){
|
||||
if (intent?.action == ACTION_TIME_TICK) {
|
||||
updateBackground()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
activity?.registerReceiver(minuteReceiver, IntentFilter(ACTION_TIME_TICK))
|
||||
|
||||
setObservers()
|
||||
|
||||
clickEvents()
|
||||
|
||||
return binding.root
|
||||
@@ -75,24 +71,40 @@ class Home1Fragment : Fragment() {
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
if (valueAnimator == null) {
|
||||
binding.tvView.post {
|
||||
val endMargin: Float = 25f * (resources.displayMetrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT)
|
||||
valueAnimator = ObjectAnimator.ofFloat(binding.tvView, "translationX", resources.displayMetrics.widthPixels - binding.tvView.width - (2 * endMargin)).apply {
|
||||
duration = 12000
|
||||
repeatCount = ValueAnimator.INFINITE
|
||||
repeatMode = ValueAnimator.REVERSE
|
||||
start()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
valueAnimator?.resume()
|
||||
}
|
||||
handleAnimations()
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
valueAnimator?.pause()
|
||||
tvAnimator?.pause()
|
||||
cloud1Animator?.pause()
|
||||
cloud2Animator?.pause()
|
||||
star1Animator?.pause()
|
||||
star2Animator?.pause()
|
||||
}
|
||||
|
||||
private fun setObservers() {
|
||||
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)
|
||||
playTxt.text = getString(R.string.play)
|
||||
|
||||
moreTxt.text = getString(R.string.more)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun clickEvents() {
|
||||
@@ -106,32 +118,190 @@ class Home1Fragment : Fragment() {
|
||||
activity?.unregisterReceiver(minuteReceiver)
|
||||
}
|
||||
|
||||
private fun updateBackground(){
|
||||
toast("Background updated")
|
||||
private fun updateUserData(data: UserDataResponse?) {
|
||||
binding.apply {
|
||||
val name = if (userPrefs?.userType == UserType.GUEST) userPrefs?.guestUserName
|
||||
else data?.result?.fullname
|
||||
val text = ", $name"
|
||||
|
||||
userName.text = text
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleAnimations() {
|
||||
if (tvAnimator == null) {
|
||||
binding.tvView.post {
|
||||
val endMargin: Float =
|
||||
25f * (resources.displayMetrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT)
|
||||
tvAnimator = ObjectAnimator.ofFloat(
|
||||
binding.tvView,
|
||||
"translationX",
|
||||
resources.displayMetrics.widthPixels - binding.tvView.width - (2 * endMargin)
|
||||
).apply {
|
||||
duration = 12000
|
||||
repeatCount = ValueAnimator.INFINITE
|
||||
repeatMode = ValueAnimator.REVERSE
|
||||
start()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
tvAnimator?.resume()
|
||||
}
|
||||
|
||||
if (cloud1Animator == null) {
|
||||
binding.cloud1.post {
|
||||
val cloud1Width: Float =
|
||||
900f * (resources.displayMetrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT)
|
||||
cloud1Animator = ObjectAnimator.ofFloat(
|
||||
binding.cloud1,
|
||||
"translationX",
|
||||
-cloud1Width + resources.displayMetrics.widthPixels
|
||||
).apply {
|
||||
duration = 120_000
|
||||
repeatCount = ValueAnimator.INFINITE
|
||||
repeatMode = ValueAnimator.REVERSE
|
||||
start()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
cloud1Animator?.resume()
|
||||
}
|
||||
|
||||
if (cloud2Animator == null) {
|
||||
binding.cloud2.post {
|
||||
val cloud2Width: Float =
|
||||
900f * (resources.displayMetrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT)
|
||||
cloud2Animator = ObjectAnimator.ofFloat(
|
||||
binding.cloud2,
|
||||
"translationX",
|
||||
cloud2Width - resources.displayMetrics.widthPixels
|
||||
).apply {
|
||||
duration = 120_000
|
||||
repeatCount = ValueAnimator.INFINITE
|
||||
repeatMode = ValueAnimator.REVERSE
|
||||
start()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
cloud2Animator?.resume()
|
||||
}
|
||||
|
||||
if (currentBackground == TimePeriod.NIGHT && star1Animator == null) {
|
||||
binding.star1.post {
|
||||
star1Animator = ObjectAnimator.ofFloat(binding.star1, "alpha", 0.3f, 1f).apply {
|
||||
duration = 3000
|
||||
repeatCount = ValueAnimator.INFINITE
|
||||
repeatMode = ValueAnimator.REVERSE
|
||||
start()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
star1Animator?.resume()
|
||||
}
|
||||
|
||||
if (currentBackground == TimePeriod.NIGHT && star2Animator == null) {
|
||||
binding.star2.post {
|
||||
star2Animator = ObjectAnimator.ofFloat(binding.star2, "alpha", 0.3f, 1f).apply {
|
||||
duration = 3000
|
||||
repeatCount = ValueAnimator.INFINITE
|
||||
repeatMode = ValueAnimator.REVERSE
|
||||
start()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
star2Animator?.resume()
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateBackground() {
|
||||
val timePeriod = TimePeriod.getCurrentTimePeriod()
|
||||
if (currentBackground != timePeriod){
|
||||
if (currentBackground != timePeriod) {
|
||||
// time period has changed
|
||||
activity?.let {
|
||||
binding.root.background = when(timePeriod){
|
||||
when (timePeriod) {
|
||||
TimePeriod.MORNING -> {
|
||||
it.window.statusBarColor = ContextCompat.getColor(it, R.color.morning_status)
|
||||
ContextCompat.getDrawable(it, R.drawable.morning_bg)
|
||||
// text colors
|
||||
binding.userName.setTextColor(
|
||||
ContextCompat.getColor(
|
||||
it,
|
||||
R.color.color_primary
|
||||
)
|
||||
)
|
||||
binding.welcomeText.setTextColor(
|
||||
ContextCompat.getColor(
|
||||
it,
|
||||
R.color.color_primary
|
||||
)
|
||||
)
|
||||
binding.helloTxt.setTextColor(
|
||||
ContextCompat.getColor(
|
||||
it,
|
||||
R.color.color_primary
|
||||
)
|
||||
)
|
||||
|
||||
binding.star1.visibility = GONE
|
||||
binding.star2.visibility = GONE
|
||||
binding.moon.visibility = GONE
|
||||
|
||||
binding.grass.setImageResource(R.drawable.img_grass_d)
|
||||
}
|
||||
|
||||
TimePeriod.AFTERNOON -> {
|
||||
it.window.statusBarColor = ContextCompat.getColor(it, R.color.afternoon_status)
|
||||
ContextCompat.getDrawable(it, R.drawable.afternoon_bg)
|
||||
// text colors
|
||||
binding.userName.setTextColor(
|
||||
ContextCompat.getColor(
|
||||
it,
|
||||
R.color.color_primary
|
||||
)
|
||||
)
|
||||
binding.welcomeText.setTextColor(
|
||||
ContextCompat.getColor(
|
||||
it,
|
||||
R.color.color_primary
|
||||
)
|
||||
)
|
||||
binding.helloTxt.setTextColor(
|
||||
ContextCompat.getColor(
|
||||
it,
|
||||
R.color.color_primary
|
||||
)
|
||||
)
|
||||
|
||||
binding.star1.visibility = GONE
|
||||
binding.star2.visibility = GONE
|
||||
binding.moon.visibility = GONE
|
||||
|
||||
binding.grass.setImageResource(R.drawable.img_grass_d)
|
||||
}
|
||||
|
||||
TimePeriod.EVENING -> {
|
||||
it.window.statusBarColor = ContextCompat.getColor(it, R.color.evening_status)
|
||||
ContextCompat.getDrawable(it, R.drawable.evening_bg)
|
||||
// text colors
|
||||
binding.userName.setTextColor(ContextCompat.getColor(it, R.color.white))
|
||||
binding.welcomeText.setTextColor(ContextCompat.getColor(it, R.color.white))
|
||||
binding.helloTxt.setTextColor(ContextCompat.getColor(it, R.color.white))
|
||||
|
||||
binding.star1.visibility = GONE
|
||||
binding.star2.visibility = GONE
|
||||
binding.moon.visibility = GONE
|
||||
|
||||
binding.grass.setImageResource(R.drawable.img_grass_d)
|
||||
}
|
||||
|
||||
TimePeriod.NIGHT -> {
|
||||
// text colors
|
||||
binding.userName.setTextColor(ContextCompat.getColor(it, R.color.white))
|
||||
binding.welcomeText.setTextColor(ContextCompat.getColor(it, R.color.white))
|
||||
binding.helloTxt.setTextColor(ContextCompat.getColor(it, R.color.white))
|
||||
|
||||
it.window.statusBarColor = ContextCompat.getColor(it, R.color.night_status)
|
||||
ContextCompat.getDrawable(it, R.drawable.night_bg)
|
||||
binding.star1.visibility = VISIBLE
|
||||
binding.star2.visibility = VISIBLE
|
||||
binding.moon.visibility = VISIBLE
|
||||
|
||||
currentBackground = timePeriod
|
||||
handleAnimations()
|
||||
|
||||
binding.grass.setImageResource(R.drawable.img_grass_n)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user