Prepares charts

This commit is contained in:
tom79 2019-07-28 15:55:25 +02:00
parent 40a1baf5a7
commit 4c8945811f
9 changed files with 563 additions and 0 deletions

View File

@ -123,4 +123,5 @@ dependencies {
implementation 'com.github.duanhong169:colorpicker:1.1.6'
implementation 'com.github.pengfeizhou.android.animation:glide-plugin:0.2.16'
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
}

View File

@ -228,6 +228,12 @@
android:label="@string/app_name"
android:launchMode="singleTask"
/>
<activity android:name="app.fedilab.android.activities.OwnerChartsActivity"
android:windowSoftInputMode="stateAlwaysHidden"
android:configChanges="orientation|screenSize"
android:label="@string/app_name"
android:launchMode="singleTask"
/>
<activity android:name="app.fedilab.android.activities.LanguageActivity"
android:windowSoftInputMode="stateAlwaysHidden"
android:configChanges="orientation|screenSize"

View File

@ -0,0 +1,232 @@
/* Copyright 2019 Thomas Schneider
*
* This file is a part of Fedilab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
* see <http://www.gnu.org/licenses>. */
package app.fedilab.android.activities;
import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.github.mikephil.charting.charts.LineChart;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import app.fedilab.android.R;
import app.fedilab.android.asynctasks.RetrieveChartsAsyncTask;
import app.fedilab.android.asynctasks.RetrieveFeedsAsyncTask;
import app.fedilab.android.asynctasks.RetrieveStatsAsyncTask;
import app.fedilab.android.client.APIResponse;
import app.fedilab.android.client.Entities.Account;
import app.fedilab.android.client.Entities.Charts;
import app.fedilab.android.client.Entities.Statistics;
import app.fedilab.android.client.Entities.Status;
import app.fedilab.android.drawers.StatusListAdapter;
import app.fedilab.android.helper.FilterToots;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.interfaces.OnRetrieveChartsInterface;
import app.fedilab.android.interfaces.OnRetrieveFeedsInterface;
import app.fedilab.android.interfaces.OnRetrieveStatsInterface;
import app.fedilab.android.services.BackupStatusInDataBaseService;
import app.fedilab.android.sqlite.AccountDAO;
import app.fedilab.android.sqlite.Sqlite;
import app.fedilab.android.sqlite.StatusCacheDAO;
import es.dmoral.toasty.Toasty;
/**
* Created by Thomas on 28/07/2019.
* Charts for owner activity
*/
public class OwnerChartsActivity extends BaseActivity implements OnRetrieveChartsInterface {
LinearLayoutManager mLayoutManager;
private int style;
private Button settings_time_from, settings_time_to;
private Date dateIni, dateEnd;
private LineChart chart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
switch (theme){
case Helper.THEME_LIGHT:
setTheme(R.style.AppTheme_NoActionBar_Fedilab);
break;
case Helper.THEME_DARK:
setTheme(R.style.AppThemeDark_NoActionBar);
break;
case Helper.THEME_BLACK:
setTheme(R.style.AppThemeBlack_NoActionBar);
break;
default:
setTheme(R.style.AppThemeDark_NoActionBar);
}
setContentView(R.layout.activity_ower_charts);
Toolbar toolbar = findViewById(R.id.toolbar);
if( theme == Helper.THEME_BLACK)
toolbar.setBackgroundColor(ContextCompat.getColor(OwnerChartsActivity.this, R.color.black));
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if( actionBar != null ){
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
assert inflater != null;
@SuppressLint("InflateParams") View view = inflater.inflate(R.layout.simple_action_bar, null);
actionBar.setCustomView(view, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
ImageView close_toot = actionBar.getCustomView().findViewById(R.id.close_toot);
close_toot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
TextView toolbarTitle = actionBar.getCustomView().findViewById(R.id.toolbar_title);
ImageView pp_actionBar = actionBar.getCustomView().findViewById(R.id.pp_actionBar);
if (theme == Helper.THEME_LIGHT){
Helper.colorizeToolbar(actionBar.getCustomView().findViewById(R.id.toolbar), R.color.black, OwnerChartsActivity.this);
}
toolbarTitle.setText(getString(R.string.owner_charts));
}
chart = findViewById(R.id.chart);
settings_time_from = findViewById(R.id.settings_time_from);
settings_time_to = findViewById(R.id.settings_time_to);
if( theme == Helper.THEME_DARK){
style = R.style.DialogDark;
}else if( theme == Helper.THEME_BLACK){
style = R.style.DialogBlack;
}else {
style = R.style.Dialog;
}
SQLiteDatabase db = Sqlite.getInstance(OwnerChartsActivity.this, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
dateIni = new StatusCacheDAO(OwnerChartsActivity.this, db).getSmallerDate(StatusCacheDAO.ARCHIVE_CACHE);
dateEnd = new StatusCacheDAO(OwnerChartsActivity.this, db).getGreaterDate(StatusCacheDAO.ARCHIVE_CACHE);
if(dateIni == null){
dateIni = new Date();
}
if( dateEnd == null){
dateEnd = new Date();
}
String dateInitString = Helper.shortDateToString(dateIni);
String dateEndString = Helper.shortDateToString(dateEnd);
settings_time_from.setText(dateInitString);
settings_time_to.setText(dateEndString);
new RetrieveChartsAsyncTask(OwnerChartsActivity.this, dateIni, dateEnd, OwnerChartsActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
private DatePickerDialog.OnDateSetListener iniDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
Calendar c = Calendar.getInstance();
c.set(year, monthOfYear, dayOfMonth, 0, 0);
dateIni = new Date(c.getTimeInMillis());
settings_time_from.setText(Helper.shortDateToString(new Date(c.getTimeInMillis())));
}
};
private DatePickerDialog.OnDateSetListener endDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
Calendar c = Calendar.getInstance();
c.set(year, monthOfYear, dayOfMonth, 23, 59);
dateEnd = new Date(c.getTimeInMillis());
settings_time_to.setText(Helper.shortDateToString(new Date(c.getTimeInMillis())));
}
};
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onCharts(Charts charts) {
}
}

View File

@ -0,0 +1,66 @@
/* Copyright 2019 Thomas Schneider
*
* This file is a part of Fedilab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
* see <http://www.gnu.org/licenses>. */
package app.fedilab.android.asynctasks;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import java.lang.ref.WeakReference;
import java.util.Date;
import app.fedilab.android.client.Entities.Charts;
import app.fedilab.android.interfaces.OnRetrieveChartsInterface;
import app.fedilab.android.sqlite.Sqlite;
import app.fedilab.android.sqlite.StatusCacheDAO;
/**
* Created by Thomas on 28/07/2019.
* Creates charts for an account
*/
public class RetrieveChartsAsyncTask extends AsyncTask<Void, Void, Void> {
private OnRetrieveChartsInterface listener;
private WeakReference<Context> contextReference;
private Charts charts;
private Date dateIni;
private Date dateEnd;
public RetrieveChartsAsyncTask(Context context, Date dateIni, Date dateEnd, OnRetrieveChartsInterface onRetrieveChartsInterface){
this.contextReference = new WeakReference<>(context);
this.listener = onRetrieveChartsInterface;
this.dateIni = dateIni;
this.dateEnd = dateEnd;
}
@Override
protected Void doInBackground(Void... params) {
SQLiteDatabase db = Sqlite.getInstance(contextReference.get(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
charts = new StatusCacheDAO(contextReference.get(), db).getCharts(dateIni, dateEnd);
return null;
}
@Override
protected void onPostExecute(Void result) {
listener.onCharts(charts);
}
}

View File

@ -0,0 +1,76 @@
package app.fedilab.android.client.Entities;
import java.util.List;
/* Copyright 2019 Thomas Schneider
*
* This file is a part of Fedilab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
* see <http://www.gnu.org/licenses>. */
public class Charts {
private List<String> xLabels;
private List<String> yLabels;
private List<Integer> xValues;
private List<Integer> statuses;
private List<Integer> boosts;
private List<Integer> replies;
public List<String> getxLabels() {
return xLabels;
}
public void setxLabels(List<String> xLabels) {
this.xLabels = xLabels;
}
public List<String> getyLabels() {
return yLabels;
}
public void setyLabels(List<String> yLabels) {
this.yLabels = yLabels;
}
public List<Integer> getxValues() {
return xValues;
}
public void setxValues(List<Integer> xValues) {
this.xValues = xValues;
}
public List<Integer> getStatuses() {
return statuses;
}
public void setStatuses(List<Integer> statuses) {
this.statuses = statuses;
}
public List<Integer> getBoosts() {
return boosts;
}
public void setBoosts(List<Integer> boosts) {
this.boosts = boosts;
}
public List<Integer> getReplies() {
return replies;
}
public void setReplies(List<Integer> replies) {
this.replies = replies;
}
}

View File

@ -0,0 +1,25 @@
/* Copyright 2019 Thomas Schneider
*
* This file is a part of Fedilab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
* see <http://www.gnu.org/licenses>. */
package app.fedilab.android.interfaces;
import app.fedilab.android.client.Entities.Charts;
/**
* Created by Thomas on 28/07/2019.
* Interface when retrieving charts
*/
public interface OnRetrieveChartsInterface {
void onCharts(Charts charts);
}

View File

@ -20,12 +20,15 @@ import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;
import app.fedilab.android.client.Entities.Charts;
import app.fedilab.android.client.Entities.Statistics;
import app.fedilab.android.client.Entities.Status;
import app.fedilab.android.client.Entities.Tag;
@ -434,6 +437,80 @@ public class StatusCacheDAO {
}
public Charts getCharts(Date dateIni, Date dateEnd){
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
String instance = Helper.getLiveInstance(context);
Charts charts = new Charts();
dateIni = new Date(dateIni.getYear(), dateIni.getMonth(), dateIni.getDay(), 0, 0, 0);
Calendar start = Calendar.getInstance();
start.setTime(dateIni);
start.set(Calendar.HOUR,0);
start.set(Calendar.MINUTE,0);
start.set(Calendar.SECOND,0);
Calendar end = Calendar.getInstance();
end.setTime(dateEnd);
end.set(Calendar.HOUR,23);
end.set(Calendar.MINUTE,59);
end.set(Calendar.SECOND,59);
StringBuilder selection = new StringBuilder(Sqlite.COL_CACHED_ACTION + " = '" + ARCHIVE_CACHE + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "' AND " + Sqlite.COL_USER_ID + " = '" + userId + "'");
selection.append(" AND " + Sqlite.COL_CREATED_AT + " >= '").append(Helper.dateToString(start.getTime())).append("'");
selection.append(" AND " + Sqlite.COL_CREATED_AT + " <= '").append(Helper.dateToString(end.getTime())).append("'");
List<Status> data = new ArrayList<>();
try {
Cursor c = db.query(Sqlite.TABLE_STATUSES_CACHE, null, selection.toString(), null, null, null, Sqlite.COL_CREATED_AT + " ASC");
data = cursorToListStatuses(c);
} catch (Exception e) {
e.printStackTrace();
}
int inc = 0;
List<String> xLabel = new ArrayList<>();
List<Integer> xValues = new ArrayList<>();
List<Integer> statuses = new ArrayList<>();
List<Integer> boosts = new ArrayList<>();
List<Integer> replies = new ArrayList<>();
if( data != null && data.size() > 0) {
while (!start.after(end)) {
Date targetDay = start.getTime();
Date dateLimite = new Date(targetDay.getTime() - TimeUnit.DAYS.toMillis(1));
xLabel.add(Helper.shortDateToString(targetDay));
xValues.add(inc);
int boostsCount = 0;
int repliesCount = 0;
int statusesCount = 0;
for(Status status: data){
if(status.getCreated_at().after(targetDay) && status.getCreated_at().before(dateLimite)){
if( status.getReblog() != null){
boostsCount++;
}else if( status.getIn_reply_to_id() != null){
repliesCount++;
}else {
statusesCount++;
}
}else{
inc++;
break;
}
}
boosts.add(boostsCount);
replies.add(repliesCount);
statuses.add(statusesCount);
start.add(Calendar.DATE, 1);
}
}
charts.setxLabels(xLabel);
charts.setxValues(xValues);
charts.setBoosts(boosts);
charts.setReplies(replies);
charts.setStatuses(statuses);
return charts;
}
/**
* Returns a cached status by id in db
* @return stored status StoredStatus

View File

@ -0,0 +1,79 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="app.fedilab.android.activities.OwnerStatusActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="?attr/popupOverlay"/>
</com.google.android.material.appbar.AppBarLayout>
<RelativeLayout
android:id="@+id/main_app_container"
android:paddingLeft="@dimen/fab_margin"
android:paddingRight="@dimen/fab_margin"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="app.fedilab.android.activities.OwnerStatusActivity"
>
<LinearLayout
style="?attr/shapeBorder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:paddingBottom="10dp"
android:orientation="horizontal">
<TextView
android:text="@string/settings_time_from"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_marginStart="10dp"
android:focusableInTouchMode="false"
android:id="@+id/settings_time_from"
style="?attr/borderlessColored"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginStart="10dp"
android:text="@string/settings_time_to"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_marginStart="10dp"
android:focusableInTouchMode="false"
style="?attr/borderlessColored"
android:id="@+id/settings_time_to"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.drawerlayout.widget.DrawerLayout>

View File

@ -1140,6 +1140,7 @@
<string name="action_groups">Groups</string>
<string name="no_messages">No groups!</string>
<string name="set_disable_animated_emoji">Disable custom animated emojis</string>
<string name="owner_charts">Charts</string>
<plurals name="number_of_vote">
<item quantity="one">%d vote</item>
<item quantity="other">%d votes</item>