241 lines
9.5 KiB
Java
241 lines
9.5 KiB
Java
package com.example.tally;
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
import androidx.fragment.app.Fragment;
|
|
import androidx.viewpager.widget.ViewPager;
|
|
|
|
import android.content.Context;
|
|
import android.content.SharedPreferences;
|
|
import android.graphics.Color;
|
|
import android.os.Build;
|
|
import android.os.Bundle;
|
|
import android.util.Log;
|
|
import android.view.View;
|
|
import android.webkit.WebSettings;
|
|
import android.webkit.WebView;
|
|
import android.webkit.WebViewClient;
|
|
import android.widget.Button;
|
|
import android.widget.TextView;
|
|
|
|
import com.example.tally.adapter.ChartVPAdapter;
|
|
import com.example.tally.db.DBManager;
|
|
import com.example.tally.frag_chart.IncomeChartFragment;
|
|
import com.example.tally.frag_chart.OutcomeChartFragment;
|
|
import com.example.tally.utils.CalendarDialog;
|
|
import com.xuexiang.xhttp2.XHttp;
|
|
import com.xuexiang.xhttp2.XHttpSDK;
|
|
import com.xuexiang.xhttp2.callback.SimpleCallBack;
|
|
import com.xuexiang.xhttp2.exception.ApiException;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Calendar;
|
|
import java.util.List;
|
|
|
|
public class MonthChartActivity extends AppCompatActivity {
|
|
Button inBtn,outBtn;
|
|
TextView dateTv,inTv,outTv;
|
|
ViewPager chartVp;
|
|
int year;
|
|
int month;
|
|
int selectPos = -1,selectMonth =-1;
|
|
List<Fragment> chartFragList;
|
|
private IncomeChartFragment incomChartFragment;
|
|
private OutcomeChartFragment outcomChartFragment;
|
|
private ChartVPAdapter chartVPAdapter;
|
|
float inMoneyOneMonth=0;
|
|
float outMoneyOneMonth=0;
|
|
|
|
WebView webView;
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
setContentView(R.layout.activity_month_chart);
|
|
webView = findViewById(R.id.web_view);
|
|
webView.getSettings().setJavaScriptEnabled(true);
|
|
webView.setWebViewClient(new WebViewClient(){
|
|
|
|
@Override
|
|
public void onPageFinished(WebView view, String url) {
|
|
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
|
|
}
|
|
|
|
});
|
|
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
|
|
|
|
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
|
|
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
|
|
}
|
|
// webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
|
|
webView.loadUrl("http://192.168.0.8:8080/chat/PolylineChat");
|
|
initView();
|
|
initTime();
|
|
initStatistics(year,month);
|
|
initFrag();
|
|
setVPSelectListener();
|
|
}
|
|
private void setVPSelectListener() {
|
|
chartVp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
|
|
@Override
|
|
public void onPageSelected(int position) {
|
|
setButtonStyle(position);
|
|
}
|
|
});
|
|
}
|
|
|
|
private void initFrag() {
|
|
chartFragList = new ArrayList<>();
|
|
// 添加Fragment的对象
|
|
incomChartFragment = new IncomeChartFragment();
|
|
outcomChartFragment = new OutcomeChartFragment();
|
|
// 添加数据到Fragment当中
|
|
Bundle bundle = new Bundle();
|
|
bundle.putInt("year",year);
|
|
bundle.putInt("month",month);
|
|
incomChartFragment.setArguments(bundle);
|
|
outcomChartFragment.setArguments(bundle);
|
|
// 将Fragment添加到数据源当中
|
|
chartFragList.add(outcomChartFragment);
|
|
chartFragList.add(incomChartFragment);
|
|
// 使用适配器
|
|
chartVPAdapter = new ChartVPAdapter(getSupportFragmentManager(), chartFragList);
|
|
chartVp.setAdapter(chartVPAdapter);
|
|
// 将Fragment加载到Acitivy当中
|
|
}
|
|
|
|
/* 统计某年某月的收支情况数据*/
|
|
private void initStatistics(int year, int month) {
|
|
SharedPreferences sharedPreferences = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
|
|
String token = sharedPreferences.getString("token", "");
|
|
XHttpSDK.setSuccessCode(200);
|
|
XHttp.get("/app/Category/getSumMoneyOneDay")
|
|
.headers("Authorization", "Bearer " + token)
|
|
.params("year",year)
|
|
.params("month",month)
|
|
.params("kind",1)
|
|
.syncRequest(false) //异步请求
|
|
.onMainThread(true) //回到主线程
|
|
.execute(new SimpleCallBack<Float>() {
|
|
@Override
|
|
public void onSuccess(Float response) throws Throwable {
|
|
inMoneyOneMonth=response;
|
|
updateStatistics();
|
|
Log.e("YourClass", "getSumMoneyOneMonth incountItemOneMonth success, response: " + response.toString());
|
|
}
|
|
|
|
@Override
|
|
public void onError(ApiException e) {
|
|
e.printStackTrace();
|
|
Log.e("YourClass", "getSumMoneyOneMonth error: " + e.getMessage());
|
|
}
|
|
});
|
|
XHttpSDK.setSuccessCode(200);
|
|
XHttp.get("/app/Category/getSumMoneyOneDay")
|
|
.headers("Authorization", "Bearer " + token)
|
|
.params("year",year)
|
|
.params("month",month)
|
|
.params("kind",0)
|
|
.syncRequest(false) //异步请求
|
|
.onMainThread(true) //回到主线程
|
|
.execute(new SimpleCallBack<Float>() {
|
|
@Override
|
|
public void onSuccess(Float response) throws Throwable {
|
|
outMoneyOneMonth=response;
|
|
updateStatistics();
|
|
Log.e("YourClass", "getSumMoneyOneMonth outcountItemOneMonth success, response: " + response.toString());
|
|
}
|
|
@Override
|
|
public void onError(ApiException e) {
|
|
e.printStackTrace();
|
|
Log.e("YourClass", "getSumMoneyOneMonth error: " + e.getMessage());
|
|
}
|
|
});
|
|
// float inMoneyOneMonth = DBManager.getSumMoneyOneMonth(year, month, 1); //收入总钱数
|
|
// float outMoneyOneMonth = DBManager.getSumMoneyOneMonth(year, month, 0); //支出总钱数
|
|
int incountItemOneMonth = DBManager.getCountItemOneMonth(year, month, 1); //收入多少笔
|
|
int outcountItemOneMonth = DBManager.getCountItemOneMonth(year, month, 0); //支出多少笔
|
|
dateTv.setText(year+"年"+month+"月账单");
|
|
inTv.setText("共"+incountItemOneMonth+"笔收入, ¥ "+inMoneyOneMonth);
|
|
outTv.setText("共"+outcountItemOneMonth+"笔支出, ¥ "+outMoneyOneMonth);
|
|
}
|
|
|
|
private void updateStatistics() {
|
|
int incountItemOneMonth = DBManager.getCountItemOneMonth(year, month, 1);
|
|
int outcountItemOneMonth = DBManager.getCountItemOneMonth(year, month, 0);
|
|
|
|
// 更新UI
|
|
runOnUiThread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
dateTv.setText(year + "年" + month + "月账单");
|
|
inTv.setText("共" + incountItemOneMonth + "笔收入, ¥ " + inMoneyOneMonth);
|
|
outTv.setText("共" + outcountItemOneMonth + "笔支出, ¥ " + outMoneyOneMonth);
|
|
}
|
|
});
|
|
}
|
|
/** 初始化时间的方法*/
|
|
private void initTime() {
|
|
Calendar calendar = Calendar.getInstance();
|
|
year = calendar.get(Calendar.YEAR);
|
|
month = calendar.get(Calendar.MONTH)+1;
|
|
}
|
|
|
|
//初始化控件
|
|
private void initView() {
|
|
inBtn = findViewById(R.id.chart_btn_in);
|
|
outBtn = findViewById(R.id.chart_btn_out);
|
|
dateTv = findViewById(R.id.chart_tv_date);
|
|
inTv = findViewById(R.id.chart_tv_in);
|
|
outTv = findViewById(R.id.chart_tv_out);
|
|
chartVp = findViewById(R.id.chart_vp);
|
|
}
|
|
|
|
public void onClick(View view) {
|
|
switch (view.getId()) {
|
|
case R.id.chart_iv_back:
|
|
finish();
|
|
break;
|
|
case R.id.chart_iv_rili:
|
|
showCalendarDialog();
|
|
break;
|
|
case R.id.chart_btn_in:
|
|
setButtonStyle(1);
|
|
chartVp.setCurrentItem(1);
|
|
break;
|
|
case R.id.chart_btn_out:
|
|
setButtonStyle(0);
|
|
chartVp.setCurrentItem(0);
|
|
break;
|
|
}
|
|
}
|
|
/* 显示日历对话框*/
|
|
private void showCalendarDialog() {
|
|
CalendarDialog dialog = new CalendarDialog(this, selectPos, selectMonth);
|
|
dialog.show();
|
|
dialog.setDialogSize();
|
|
dialog.setOnRefreshListener(new CalendarDialog.OnRefreshListener() {
|
|
@Override
|
|
public void onRefresh(int selPos, int year, int month) {
|
|
MonthChartActivity.this.selectPos = selPos;
|
|
MonthChartActivity.this.selectMonth = month;
|
|
initStatistics(year,month);
|
|
incomChartFragment.setDate(year,month);
|
|
outcomChartFragment.setDate(year,month);
|
|
}
|
|
});
|
|
}
|
|
|
|
/* 设置按钮样式的改变 支出-0 收入-1*/
|
|
private void setButtonStyle(int kind){
|
|
if (kind == 0) {
|
|
outBtn.setBackgroundResource(R.drawable.main_recordbtn_bg);
|
|
outBtn.setTextColor(Color.WHITE);
|
|
inBtn.setBackgroundResource(R.drawable.dialog_btn_bg);
|
|
inBtn.setTextColor(Color.BLACK);
|
|
}else if (kind == 1){
|
|
inBtn.setBackgroundResource(R.drawable.main_recordbtn_bg);
|
|
inBtn.setTextColor(Color.WHITE);
|
|
outBtn.setBackgroundResource(R.drawable.dialog_btn_bg);
|
|
outBtn.setTextColor(Color.BLACK);
|
|
}
|
|
}
|
|
} |