Commit 74b107f1 authored by Thomas's avatar Thomas
Browse files

Charts

parent c04a51f3
Loading
Loading
Loading
Loading
+128 −114
Original line number Diff line number Diff line
@@ -15,34 +15,31 @@ package app.fedilab.android.mastodon.activities;
 * see <http://www.gnu.org/licenses>. */


import android.graphics.Canvas;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.TextView;

import androidx.appcompat.widget.LinearLayoutCompat;

import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.renderer.XAxisRenderer;
import com.github.mikephil.charting.utils.MPPointF;
import com.github.mikephil.charting.utils.Transformer;
import com.github.mikephil.charting.utils.Utils;
import com.github.mikephil.charting.utils.ViewPortHandler;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@@ -52,7 +49,6 @@ import app.fedilab.android.databinding.ActivityCheckHomeCachetBinding;
import app.fedilab.android.mastodon.client.entities.api.Status;
import app.fedilab.android.mastodon.client.entities.app.StatusCache;
import app.fedilab.android.mastodon.exception.DBException;
import app.fedilab.android.mastodon.helper.Helper;
import app.fedilab.android.mastodon.helper.ThemeHelper;
import es.dmoral.toasty.Toasty;

@@ -62,6 +58,7 @@ public class CheckHomeCacheActivity extends BaseBarActivity {

    private ActivityCheckHomeCachetBinding binding;
    private List<Status> statuses;
    private ArrayList<String> xVals;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
@@ -77,6 +74,18 @@ public class CheckHomeCacheActivity extends BaseBarActivity {
            finish();
            return;
        }

        new Thread(() -> {
            try {
                statuses = new StatusCache(this).getHome(MainActivity.currentAccount);
                if (statuses == null || statuses.size() < 2) {
                    runOnUiThread(() -> binding.noAction.setVisibility(View.VISIBLE));
                    return;
                }
                runOnUiThread(() -> {

                    binding.progress.setVisibility(View.GONE);
                    binding.chart.setVisibility(View.VISIBLE);
                    binding.chart.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                        @Override
                        public void onGlobalLayout() {
@@ -87,16 +96,7 @@ public class CheckHomeCacheActivity extends BaseBarActivity {
                            binding.chart.setVisibility(View.VISIBLE);
                        }
                    });

        new Thread(() -> {
            try {
                statuses = new StatusCache(this).getHome(MainActivity.currentAccount);
                if (statuses == null || statuses.size() < 2) {
                    runOnUiThread(() -> binding.noAction.setVisibility(View.VISIBLE));
                    return;
                }

                final ArrayList<String> xVals = new ArrayList<>();
                    xVals = new ArrayList<>();
                    String xDate;
                    int inc = 0;
                    //We loop through cache
@@ -153,6 +153,7 @@ public class CheckHomeCacheActivity extends BaseBarActivity {
                                return "";
                        }
                    };

                    binding.chart.setExtraBottomOffset(80);
                    //  binding.chart.getXAxis().setGranularity(1f);
                    binding.chart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
@@ -167,11 +168,15 @@ public class CheckHomeCacheActivity extends BaseBarActivity {
                    binding.chart.getAxisRight().setAxisMinimum(0f);
                    binding.chart.getXAxis().setLabelCount(10, true);
                    binding.chart.getLegend().setEnabled(false);

                    binding.chart.setTouchEnabled(true);
                    Description description = binding.chart.getDescription();
                    description.setEnabled(false);
                    CustomMarkerView mv = new CustomMarkerView(CheckHomeCacheActivity.this, R.layout.custom_marker_view_layout);
                    binding.chart.setMarkerView(mv);

                    binding.chart.invalidate();
                });


            } catch (DBException | NegativeArraySizeException e) {
                binding.noAction.setVisibility(View.VISIBLE);
@@ -182,38 +187,47 @@ public class CheckHomeCacheActivity extends BaseBarActivity {

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public static class LineChartXAxisValueFormatter extends IndexAxisValueFormatter {
    public class CustomMarkerView extends MarkerView {

        private final TextView tvContent;
        private MPPointF mOffset;

        public CustomMarkerView(Context context, int layoutResource) {
            super(context, layoutResource);

            // find your layout components
            tvContent = findViewById(R.id.tvContent);
        }

        // callbacks everytime the MarkerView is redrawn, can be used to update the
        // content (user-interface)
        @Override
        public String getFormattedValue(float value) {
        public void refreshContent(Entry e, Highlight highlight) {
            tvContent.setText(getString(R.string.messages, (int) e.getY()) + "\r\n" + xVals.get((int) e.getX()));

            // this will perform necessary layouting
            super.refreshContent(e, highlight);
        }

            long emissionsMilliSince1970Time = ((long) value);
            Log.v(Helper.TAG, "value: " + value);
            Date timeMilliseconds = new Date(emissionsMilliSince1970Time);
            DateFormat dateTimeFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
            return dateTimeFormat.format(timeMilliseconds);
        @Override
        public MPPointF getOffset() {
            if (mOffset == null) {
                // center the marker horizontally and vertically
                mOffset = new MPPointF(-(getWidth() / 2), -getHeight());
            }

            return mOffset;
        }

    public class CustomXAxisRenderer extends XAxisRenderer {
        public CustomXAxisRenderer(ViewPortHandler viewPortHandler, XAxis xAxis, Transformer trans) {
            super(viewPortHandler, xAxis, trans);
    }

    @Override
        protected void drawLabel(Canvas c, String formattedLabel, float x, float y, MPPointF anchor, float angleDegrees) {
            String[] line = formattedLabel.split("\n");
            Utils.drawXAxisValue(c, line[0], x, y, mAxisLabelPaint, anchor, angleDegrees);
            Utils.drawXAxisValue(c, line[1], x + mAxisLabelPaint.getTextSize(), y + mAxisLabelPaint.getTextSize(), mAxisLabelPaint, anchor, angleDegrees);
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
+8 −0
Original line number Diff line number Diff line
@@ -25,8 +25,16 @@
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ProgressBar
            android:id="@+id/progress"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:indeterminate="true" />

        <com.github.mikephil.charting.charts.LineChart
            android:id="@+id/chart"
            android:visibility="gone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

+21 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="60dp"
    android:background="@color/black">

    <TextView
        android:id="@+id/tvContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="7dp"
        android:layout_marginRight="5dp"
        android:gravity="center"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@android:color/white"
        android:textSize="12sp" />

</RelativeLayout>
 No newline at end of file
+1 −0
Original line number Diff line number Diff line
@@ -1931,4 +1931,5 @@
    <string name="set_autoplay_gif">Autoplay animated media</string>
    <string name="no_cached_messages">No Home cached messages!</string>
    <string name="check_home_cache">Check Home cache</string>
    <string name="messages">%1$d cached messages</string>
</resources>
 No newline at end of file