Skip to content

Commit

Permalink
add: option to toggle chart labels in analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
Hamza417 committed Jan 3, 2025
1 parent d042cf4 commit 76a4d59
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 20 deletions.
1 change: 1 addition & 0 deletions app/src/main/assets/html/changelogs.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ <h4>Analytics</h4>
<li>Updated longpress behavior legend to highlight the pie slices and clicking should lead to
the respective data panels on any chart.
</li>
<li>Added option to toggle chart labels in <b>Analytics</b> panel.</li>
</ul>

<br/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.formatter.PercentFormatter;

import androidx.annotation.NonNull;
import app.simple.inure.R;
Expand Down Expand Up @@ -52,21 +54,21 @@ private void initProps() {
setNoDataTextColor(ThemeManager.INSTANCE.getTheme().getTextViewTheme().getSecondaryTextColor());
setNoDataTextTypeface(TypeFace.INSTANCE.getMediumTypeFace(getContext()));
}

setHoleColor(Color.TRANSPARENT);
setUsePercentValues(false);
setDragDecelerationFrictionCoef(0.95F);
setHighlightPerTapEnabled(true);
getDescription().setEnabled(false);
setDrawCenterText(false);

if (StatusBarHeight.isLandscape(getContext())) {
setExtraOffsets(chartOffset * 2F, chartOffset * 2F, chartOffset * 2F, chartOffset * 2F);
setExtraRightOffset(chartOffset * 4F);
} else {
setExtraOffsets(chartOffset, chartOffset, chartOffset, chartOffset);
}

/*
* Legend's props
*/
Expand All @@ -77,13 +79,13 @@ private void initProps() {
getLegend().setTextColor(ThemeManager.INSTANCE.getTheme().getTextViewTheme().getSecondaryTextColor());
getLegend().setXEntrySpace(20F);
getLegend().setYEntrySpace(5F);

if (!isInEditMode()) {
getLegend().setTypeface(TypeFace.INSTANCE.getMediumTypeFace(getContext()));
}

getLegend().setWordWrapEnabled(true);

if (StatusBarHeight.isLandscape(getContext())) {
getLegend().setXOffset(chartOffset * 3F);
getLegend().setOrientation(Legend.LegendOrientation.VERTICAL);
Expand Down Expand Up @@ -171,4 +173,44 @@ private void animateHoleRadius(float value) {
public void setAnimation(boolean animate) {
this.animate = animate;
}

public void enableChartLabels(PieDataSet dataSet) {
dataSet.setValueTextColor(ThemeManager.INSTANCE.getTheme().getTextViewTheme().getPrimaryTextColor());
dataSet.setValueTextSize(9F);
dataSet.setValueTypeface(TypeFace.INSTANCE.getRegularTypeFace(getContext()));
dataSet.setValueLineColor(ThemeManager.INSTANCE.getTheme().getTextViewTheme().getPrimaryTextColor());
dataSet.setValueLinePart1OffsetPercentage(80F);
setEntryLabelColor(ThemeManager.INSTANCE.getTheme().getTextViewTheme().getPrimaryTextColor());
setEntryLabelTextSize(9F);
setEntryLabelTypeface(TypeFace.INSTANCE.getRegularTypeFace(getContext()));
dataSet.setValueFormatter(new PercentFormatter(this));
dataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
dataSet.setDrawIcons(true);
setUsePercentValues(true);
notifyDataSetChanged();
invalidate();
}

public void disableChartLabels(PieDataSet dataSet) {
dataSet.setValueTextColor(Color.TRANSPARENT);
dataSet.setValueTextSize(0F);
dataSet.setValueTypeface(TypeFace.INSTANCE.getRegularTypeFace(getContext()));
dataSet.setValueLineColor(Color.TRANSPARENT);
dataSet.setValueLinePart1OffsetPercentage(80F);
setEntryLabelColor(Color.TRANSPARENT);
setEntryLabelTextSize(0F);
setEntryLabelTypeface(TypeFace.INSTANCE.getRegularTypeFace(getContext()));
dataSet.setValueFormatter(null);
dataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
dataSet.setDrawIcons(false);
setUsePercentValues(false);
notifyDataSetChanged();
invalidate();
}

public PieDataSet getExistingDataSet() {
return (PieDataSet) getData().getDataSet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.FragmentManager
import app.simple.inure.R
import app.simple.inure.decorations.ripple.DynamicRippleTextView
import app.simple.inure.decorations.toggles.Switch
import app.simple.inure.extensions.fragments.ScopedBottomSheetFragment
import app.simple.inure.popups.analytics.PopupSdkValue
import app.simple.inure.preferences.AnalyticsPreferences
Expand All @@ -15,13 +17,15 @@ class AnalyticsMenu : ScopedBottomSheetFragment() {

private lateinit var sdkValue: DynamicRippleTextView
private lateinit var pieHoleRadius: DynamicRippleTextView
private lateinit var chartLabelsSwitch: Switch
private lateinit var settings: DynamicRippleTextView

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.dialog_menu_analytics, container, false)

sdkValue = view.findViewById(R.id.sdk_value)
pieHoleRadius = view.findViewById(R.id.dialog_open_pie_hole)
chartLabelsSwitch = view.findViewById(R.id.chart_labels_switch)
settings = view.findViewById(R.id.dialog_open_apps_settings)

return view
Expand All @@ -31,6 +35,7 @@ class AnalyticsMenu : ScopedBottomSheetFragment() {
super.onViewCreated(view, savedInstanceState)

setSdkValue()
chartLabelsSwitch.isChecked = AnalyticsPreferences.getChartLabel()

sdkValue.setOnClickListener {
PopupSdkValue(it)
Expand All @@ -43,6 +48,10 @@ class AnalyticsMenu : ScopedBottomSheetFragment() {
}
}

chartLabelsSwitch.setOnSwitchCheckedChangeListener {
AnalyticsPreferences.setChartLabel(it)
}

settings.setOnClickListener {
openSettings()
}
Expand Down Expand Up @@ -71,5 +80,11 @@ class AnalyticsMenu : ScopedBottomSheetFragment() {
fragment.arguments = args
return fragment
}

const val TAG = "analytics_menu"

fun FragmentManager.showAnalyticsMenu() {
newInstance().show(this, TAG)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ object AnalyticsPreferences {
const val SDK_VALUE = "analytics_sdk_value"
const val PIE_HOLE_RADIUS = "pie_hole_radius"
const val APPLICATION_TYPE = "application_type_analytics"
const val CHART_LABEL = "analytics_chart_label"

// ---------------------------------------------------------------------------------------------------------- //

Expand Down Expand Up @@ -37,4 +38,14 @@ object AnalyticsPreferences {
fun getApplicationType(): String {
return SharedPreferences.getSharedPreferences().getString(APPLICATION_TYPE, SortConstant.BOTH)!!
}

// ---------------------------------------------------------------------------------------------------------- //

fun setChartLabel(value: Boolean) {
SharedPreferences.getSharedPreferences().edit().putBoolean(CHART_LABEL, value).apply()
}

fun getChartLabel(): Boolean {
return SharedPreferences.getSharedPreferences().getBoolean(CHART_LABEL, true)
}
}
32 changes: 20 additions & 12 deletions app/src/main/java/app/simple/inure/ui/panels/Analytics.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package app.simple.inure.ui.panels

import android.content.SharedPreferences
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
Expand All @@ -17,7 +16,7 @@ import app.simple.inure.decorations.padding.PaddingAwareNestedScrollView
import app.simple.inure.decorations.theme.ThemePieChart
import app.simple.inure.decorations.typeface.TypeFaceTextView
import app.simple.inure.decorations.views.LegendRecyclerView
import app.simple.inure.dialogs.analytics.AnalyticsMenu
import app.simple.inure.dialogs.analytics.AnalyticsMenu.Companion.showAnalyticsMenu
import app.simple.inure.dialogs.analytics.AnalyticsSort.Companion.showAnalyticsSort
import app.simple.inure.extensions.fragments.ScopedFragment
import app.simple.inure.popups.charts.PopupChartEntry
Expand Down Expand Up @@ -92,8 +91,7 @@ class Analytics : ScopedFragment() {
BottomMenuConstants.getAllAppsBottomMenuItems(), scrollView) { id, _ ->
when (id) {
R.drawable.ic_settings -> {
AnalyticsMenu.newInstance()
.show(childFragmentManager, "analytics_menu")
childFragmentManager.showAnalyticsMenu()
}
R.drawable.ic_search -> {
openFragmentSlide(Search.newInstance(true), "search")
Expand All @@ -115,8 +113,7 @@ class Analytics : ScopedFragment() {
PieDataSet(pieData.first, "").apply {
data = PieData(this)
colors = pieData.second
valueTextColor = Color.TRANSPARENT
setEntryLabelColor(Color.TRANSPARENT)
checkLabelState()
}

setOnChartValueSelectedListener(object : OnChartValueSelectedListener {
Expand Down Expand Up @@ -167,8 +164,7 @@ class Analytics : ScopedFragment() {
PieDataSet(it.first, "").apply {
data = PieData(this)
colors = it.second
valueTextColor = Color.TRANSPARENT
setEntryLabelColor(Color.TRANSPARENT)
checkLabelState()
}

setOnChartValueSelectedListener(object : OnChartValueSelectedListener {
Expand Down Expand Up @@ -216,8 +212,7 @@ class Analytics : ScopedFragment() {
PieDataSet(it.first, "").apply {
data = PieData(this)
colors = ColorTemplate.PASTEL_COLORS.toMutableList()
valueTextColor = Color.TRANSPARENT
setEntryLabelColor(Color.TRANSPARENT)
checkLabelState()
}

setOnChartValueSelectedListener(object : OnChartValueSelectedListener {
Expand Down Expand Up @@ -267,8 +262,7 @@ class Analytics : ScopedFragment() {
PieDataSet(it.first, "").apply {
data = PieData(this)
colors = it.second
valueTextColor = Color.TRANSPARENT
setEntryLabelColor(Color.TRANSPARENT)
checkLabelState()
}

setOnChartValueSelectedListener(object : OnChartValueSelectedListener {
Expand Down Expand Up @@ -342,6 +336,20 @@ class Analytics : ScopedFragment() {
analyticsViewModel.refreshPackageData()
setFilterStyle()
}
AnalyticsPreferences.CHART_LABEL -> {
minimumOsPie.checkLabelState()
targetOsPie.checkLabelState()
packageTypePie.checkLabelState()
installerPie.checkLabelState()
}
}
}

private fun ThemePieChart.checkLabelState() {
if (AnalyticsPreferences.getChartLabel()) {
enableChartLabels(existingDataSet)
} else {
disableChartLabels(existingDataSet)
}
}

Expand Down
33 changes: 31 additions & 2 deletions app/src/main/res/layout/dialog_menu_analytics.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
android:layout_height="wrap_content"
android:clipChildren="false"
android:clipToPadding="false"
android:layout_gravity="bottom"
android:padding="@dimen/dialog_padding">

<app.simple.inure.decorations.corners.DynamicCornerLinearLayout
Expand Down Expand Up @@ -60,16 +61,44 @@
android:padding="@dimen/dialog_padding"
android:text="@string/pie_hole_radius"
android:textSize="@dimen/info_text_small"
android:layout_marginTop="@dimen/popup_item_gap_length"
app:appFontStyle="bold"
app:drawableEndCompat="@drawable/ic_dot_tiny"
app:drawableTintStyle="accent"
app:textColorStyle="primary" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="false"
android:clipToPadding="false"
android:layout_marginTop="@dimen/popup_item_gap_length"
android:orientation="horizontal">

<app.simple.inure.decorations.typeface.TypeFaceTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:padding="@dimen/dialog_padding"
android:text="@string/chart_labels"
android:textSize="@dimen/info_text_small"
app:appFontStyle="bold"
app:textColorStyle="primary" />

<app.simple.inure.decorations.toggles.Switch
android:id="@+id/chart_labels_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginEnd="@dimen/dialog_padding" />

</LinearLayout>

<app.simple.inure.decorations.theme.ThemeDivider
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginHorizontal="@dimen/dialog_padding"
android:layout_marginVertical="@dimen/dialog_padding" />
android:layout_margin="@dimen/dialog_padding" />

<app.simple.inure.decorations.ripple.DynamicRippleTextView
android:id="@+id/dialog_open_apps_settings"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -765,4 +765,5 @@
<string name="launchable">Launchable</string>
<string name="stopped">Stopped</string>
<string name="provider">Provider</string>
<string name="chart_labels">Chart Labels</string>
</resources>

0 comments on commit 76a4d59

Please sign in to comment.