Skip to content

Commit

Permalink
Adapt fragments to fg sync
Browse files Browse the repository at this point in the history
Closes #155
  • Loading branch information
alexandr7035 committed Nov 8, 2021
1 parent f9a4409 commit 547f27e
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 153 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package by.alexandr7035.gitstat.data

import androidx.lifecycle.LiveData
import by.alexandr7035.gitstat.core.AppPreferences
import by.alexandr7035.gitstat.core.Language
import by.alexandr7035.gitstat.data.local.dao.RepositoriesDao
Expand All @@ -13,6 +14,10 @@ class ReposRepository @Inject constructor(
private val appPreferences: AppPreferences,
private val gson: Gson) {

fun getRepositoriesLiveData(): LiveData<List<RepositoryEntity>>{
return dao.getRepositoriesLiveData()
}

suspend fun fetchAllRepositoriesFromDb(): List<RepositoryEntity> {
return dao.getRepositories()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ interface ContributionsDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertContributionYearsCache(years: List<ContributionsYearEntity>)


@Query("SELECT * FROM contribution_years")
fun getContributionYearsWithDaysLiveData(): LiveData<List<ContributionsYearWithDays>>

@Query("SELECT * FROM contribution_years where id = (:yearId)")
suspend fun getContributionYearWithDays(yearId: Int): ContributionsYearWithDays

@Query("DELETE FROM contribution_years")
fun clearContributionYears()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package by.alexandr7035.gitstat.data.local.dao

import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
Expand All @@ -11,6 +12,9 @@ interface RepositoriesDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertRepositories(repos: List<RepositoryEntity>)

@Query("select * from repositories")
fun getRepositoriesLiveData(): LiveData<List<RepositoryEntity>>

@Query("select * from repositories")
suspend fun getRepositories(): List<RepositoryEntity>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,70 +47,82 @@ class ContributionsFragment : Fragment() {
// Update data
viewModel.getContributionYearsLiveData().observe(viewLifecycleOwner, { years ->

if (years.isNotEmpty()) {
yearContributionsAdapter = YearContributionsAdapter(this)
yearContributionsAdapter.setItems(years)
binding?.yearsViewPager?.adapter = yearContributionsAdapter

// Set to last position
binding?.yearsViewPager?.setCurrentItem(years.size - 1, false)
binding?.currentYearView?.text = years[years.size-1].year.id.toString()

// Change year in card title when viewpager item changes
binding?.yearsViewPager?.registerOnPageChangeCallback(object: ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
super.onPageSelected(position)
Timber.d("Page changed callback")
binding?.currentYearView?.text = years[position].year.id.toString()
}
})

// Attach tablayout
TabLayoutMediator(binding!!.yearsTabLayout, binding!!.yearsViewPager) {
tab, position ->
}.attach()

if (years != null) {

if (years.isNotEmpty()) {
yearContributionsAdapter = YearContributionsAdapter(this)
yearContributionsAdapter.setItems(years)
binding?.yearsViewPager?.adapter = yearContributionsAdapter

// Set to last position
binding?.yearsViewPager?.setCurrentItem(years.size - 1, false)
binding?.currentYearView?.text = years[years.size - 1].year.id.toString()

// Change year in card title when viewpager item changes
binding?.yearsViewPager?.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
super.onPageSelected(position)
Timber.d("Page changed callback")
binding?.currentYearView?.text = years[position].year.id.toString()
}
})

// Attach tablayout
TabLayoutMediator(binding!!.yearsTabLayout, binding!!.yearsViewPager) { tab, position ->
}.attach()

}
}
})


viewModel.getContributionDaysLiveData().observe(viewLifecycleOwner, { contributions ->
val totalContributions = contributions.sumOf { it.count }
binding?.totalContributions?.text = totalContributions.toString()
if (contributions != null) {
val totalContributions = contributions.sumOf { it.count }
binding?.totalContributions?.text = totalContributions.toString()
}
})


viewModel.getContributionYearsWithRatesLiveData().observe(viewLifecycleOwner, { rateYears ->

if (rateYears.isNotEmpty()) {
yearContributionsRateAdapter = YearContributionRatesAdapter(this)
yearContributionsRateAdapter.setItems(rateYears)
binding?.rateViewPager?.adapter = yearContributionsRateAdapter
if (rateYears != null) {

if (rateYears.isNotEmpty()) {
yearContributionsRateAdapter = YearContributionRatesAdapter(this)
yearContributionsRateAdapter.setItems(rateYears)
binding?.rateViewPager?.adapter = yearContributionsRateAdapter

TabLayoutMediator(binding!!.rateTabLayout, binding!!.rateViewPager) { tab, position ->
}.attach()
TabLayoutMediator(binding!!.rateTabLayout, binding!!.rateViewPager) { tab, position ->
}.attach()

// Set to last position
binding?.rateViewPager?.setCurrentItem(rateYears.size - 1, false)
// Set to last position
binding?.rateViewPager?.setCurrentItem(rateYears.size - 1, false)

// Set total contribution rate in header
binding?.contributionsRate?.text = viewModel.getLastTotalContributionRateForYear(rateYears[rateYears.size - 1]).toString()
// Set total contribution rate in header
binding?.contributionsRate?.text =
viewModel.getLastTotalContributionRateForYear(rateYears[rateYears.size - 1]).toString()
}
}
})


viewModel.getContributionsRatioLiveData().observe(viewLifecycleOwner, { ratios ->
Timber.d("ratios $ratios")

// FIXME
val adapter = RatioLegendAdapter()
binding?.ratioLegendRecycler?.layoutManager = FlexboxLayoutManager(requireContext())
binding?.ratioLegendRecycler?.adapter = adapter
if (ratios != null) {

Timber.d("ratios $ratios")

// FIXME
val adapter = RatioLegendAdapter()
binding?.ratioLegendRecycler?.layoutManager = FlexboxLayoutManager(requireContext())
binding?.ratioLegendRecycler?.adapter = adapter

if (binding?.ratioChart != null) {
val plot = ContributionsRatioPlot()
plot.setupPlot(binding!!.ratioChart, ratios)
adapter.setItems(plot.getRatioLegendItems(binding!!.ratioChart, ratios))
if (binding?.ratioChart != null) {
val plot = ContributionsRatioPlot()
plot.setupPlot(binding!!.ratioChart, ratios)
adapter.setItems(plot.getRatioLegendItems(binding!!.ratioChart, ratios))
}
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import by.alexandr7035.gitstat.R
import by.alexandr7035.gitstat.databinding.ViewPlotContributionsYearBinding
import by.alexandr7035.gitstat.extensions.debug
import by.alexandr7035.gitstat.view.contributions.ContributionsViewModel
import by.alexandr7035.gitstat.view.contributions.plots.LinePlotFill
import dagger.hilt.android.AndroidEntryPoint
import timber.log.Timber

@AndroidEntryPoint
class YearContributionsFragment: Fragment() {
Expand All @@ -31,40 +33,45 @@ class YearContributionsFragment: Fragment() {

viewModel.getContributionYearsWithDaysLiveData().observe(viewLifecycleOwner, { yearsData ->

// FIXME find better solution (obtain certain year from data layer)
val yearData = yearsData.findLast {
it.year.id == year
}!!

// Set contributions count to cart title
val contributionsCount = yearData.contributionDays.sumOf { it.count }
binding?.contributionsCountView?.text = contributionsCount.toString()

// Get contributions rate (for the year) and apply to the view
val contributionsRate = viewModel.getContributionRateForYear(yearData)
// Set same color to the rate view as for the plot
val bg = ContextCompat.getDrawable(requireContext(), R.drawable.background_rounded_shape)
val plotFill = LinePlotFill.getPlotFillForYear(requireContext(), yearData.year.id)
bg?.setTint(plotFill.lineColor)
binding?.contributionsRateView?.background = bg
binding?.contributionsRateView?.text = contributionsRate.toString()


// Show stub if no contributions for this year
if (contributionsCount == 0) {
binding?.emptyPlotStub?.visibility = View.VISIBLE

binding?.contributionsChart?.visibility = View.GONE
binding?.contributionsRateView?.visibility = View.GONE
binding?.contributionsCountView?.visibility = View.GONE
binding?.yearCRLabel?.visibility = View.GONE
}

// Set plot data
val contributionsCountPlot = ContributionsCountPlot()
if (binding?.contributionsChart != null) {
contributionsCountPlot.setupPLot(binding!!.contributionsChart)
contributionsCountPlot.setYearData(binding!!.contributionsChart, yearData)
Timber.debug("$year $yearsData")

if (! yearsData.isNullOrEmpty()) {

// FIXME find better solution (obtain certain year from data layer)
val yearData = yearsData.findLast {
it.year.id == year
}!!

// Set contributions count to cart title
val contributionsCount = yearData.contributionDays.sumOf { it.count }
binding?.contributionsCountView?.text = contributionsCount.toString()

// Get contributions rate (for the year) and apply to the view
val contributionsRate = viewModel.getContributionRateForYear(yearData)
// Set same color to the rate view as for the plot
val bg = ContextCompat.getDrawable(requireContext(), R.drawable.background_rounded_shape)
val plotFill = LinePlotFill.getPlotFillForYear(requireContext(), yearData.year.id)
bg?.setTint(plotFill.lineColor)
binding?.contributionsRateView?.background = bg
binding?.contributionsRateView?.text = contributionsRate.toString()


// Show stub if no contributions for this year
if (contributionsCount == 0) {
binding?.emptyPlotStub?.visibility = View.VISIBLE

binding?.contributionsChart?.visibility = View.GONE
binding?.contributionsRateView?.visibility = View.GONE
binding?.contributionsCountView?.visibility = View.GONE
binding?.yearCRLabel?.visibility = View.GONE
}

// Set plot data
val contributionsCountPlot = ContributionsCountPlot()
if (binding?.contributionsChart != null) {
contributionsCountPlot.setupPLot(binding!!.contributionsChart)
contributionsCountPlot.setYearData(binding!!.contributionsChart, yearData)
}
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,37 +32,40 @@ class YearContributionRatesFragment : Fragment() {
// Observe the result
viewModel.getContributionYearsWithRatesLiveData().observe(viewLifecycleOwner, { yearsData ->

// FIXME find better solution (obtain certain year from data layer)
val yearData = yearsData.findLast {
it.year.id == year
}!!

// Setup plot
if (binding?.rateChart != null) {
val plot = ContributionRatePlot()
plot.setupPLot(binding!!.rateChart)
plot.setYearData(binding!!.rateChart, yearData)
}

// Set year title
binding?.year?.text = yearData.year.id.toString()

// Background for contribution rate views
// Set same color as for the plot
val plotFill = LinePlotFill.getPlotFillForYear(requireContext(), yearData.year.id)
val bg = ContextCompat.getDrawable(requireContext(), R.drawable.background_rounded_shape)
bg?.setTint(plotFill.lineColor)

// Peak contribution rate
val maxContributionsRate = viewModel.getMaxContributionRateForYear(yearData)
binding?.peakCRView?.background = bg
binding?.peakCRView?.text = maxContributionsRate.toString()

// Last contribution rate (end of the year)
val lastContributionRate = viewModel.getLastTotalContributionRateForYear(yearData)
binding?.lastCRView?.background = bg
binding?.lastCRView?.text = lastContributionRate.toString()
if (! yearsData.isNullOrEmpty()) {

// FIXME find better solution (obtain certain year from data layer)
val yearData = yearsData.findLast {
it.year.id == year
}!!

// Setup plot
if (binding?.rateChart != null) {
val plot = ContributionRatePlot()
plot.setupPLot(binding!!.rateChart)
plot.setYearData(binding!!.rateChart, yearData)
}

// Set year title
binding?.year?.text = yearData.year.id.toString()

// Background for contribution rate views
// Set same color as for the plot
val plotFill = LinePlotFill.getPlotFillForYear(requireContext(), yearData.year.id)
val bg = ContextCompat.getDrawable(requireContext(), R.drawable.background_rounded_shape)
bg?.setTint(plotFill.lineColor)

// Peak contribution rate
val maxContributionsRate = viewModel.getMaxContributionRateForYear(yearData)
binding?.peakCRView?.background = bg
binding?.peakCRView?.text = maxContributionsRate.toString()

// Last contribution rate (end of the year)
val lastContributionRate = viewModel.getLastTotalContributionRateForYear(yearData)
binding?.lastCRView?.background = bg
binding?.lastCRView?.text = lastContributionRate.toString()

}
})

}
Expand Down
Loading

0 comments on commit 547f27e

Please sign in to comment.