Created
February 16, 2022 06:42
-
-
Save rohitjakhar/9a2620574d170f5d226800fba6daa9dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.rohit.healthOn.ui.homeScreen.dashboardScreen | |
import android.os.Bundle | |
import android.view.View | |
import androidx.fragment.app.Fragment | |
import androidx.fragment.app.viewModels | |
import androidx.lifecycle.lifecycleScope | |
import com.google.android.material.tabs.TabLayoutMediator | |
import com.rohit.healthOn.R | |
import com.rohit.healthOn.databinding.FragmentDashboardBinding | |
import com.rohit.healthOn.util.getFirstName | |
import com.rohit.healthOn.util.setMarginTopForFullScreen | |
import com.rohit.healthOn.util.viewBinding | |
import dagger.hilt.android.AndroidEntryPoint | |
import kotlinx.coroutines.flow.collect | |
@AndroidEntryPoint | |
class DashboardFragment : Fragment(R.layout.fragment_dashboard) { | |
private lateinit var dashboardPagerAdapter: DashboardPagerAdapter | |
private val binding by viewBinding(FragmentDashboardBinding::bind) | |
private val viewModel by viewModels<DashboardViewModel>() | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
binding.headerText.setMarginTopForFullScreen() | |
dashboardPagerAdapter = DashboardPagerAdapter(requireActivity()) | |
collectUserInfo() | |
initViewPager() | |
} | |
private fun collectUserInfo() = lifecycleScope.launchWhenStarted { | |
viewModel.user.collect { | |
it?.let { user -> | |
binding.headerText.text = "Hey ${user.username.getFirstName()} !" | |
} | |
} | |
} | |
private fun initViewPager() { | |
binding.viewpager.adapter = dashboardPagerAdapter | |
TabLayoutMediator(binding.tableLayout, binding.viewpager) { tab, position -> | |
when (position) { | |
0 -> { | |
tab.text = "Water" | |
} | |
1 -> { | |
tab.text = "Sleep" | |
} | |
} | |
}.attach() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment