package com.zhen.accountbook;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.zhen.accountbook.adapter.AccountAdapter;
import com.zhen.accountbook.db.AccountBean;
import com.zhen.accountbook.db.DBManager;
import com.zhen.accountbook.utils.BeiZhuDialog;
import com.zhen.accountbook.utils.BudgetDialog;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
ListView todayLv;//展示今日收支情况
ImageView searchIv;
Button editBtn;
ImageButton moreBtn;
//声明数据源
List<AccountBean> mDatas;
AccountAdapter adapter;
//头布局相关的控件
TextView topOutTv, topInTv, topbudgetTv, topConTv;
ImageView topShowIv;
int year, month, day;
View headerView;
SharedPreferences preferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initTime();
extracted();
preferences = getSharedPreferences("budget", Context.MODE_PRIVATE);
addLVHeaderView();
mDatas = new ArrayList<>();
//设置适配器:加载每一行数据到列表当中
adapter = new AccountAdapter(this, mDatas);
todayLv.setAdapter(adapter);
}
private void extracted() {
todayLv = findViewById(R.id.main_lv);
editBtn = findViewById(R.id.main_btn_edit);
moreBtn = findViewById(R.id.main_btn_more);
searchIv = findViewById(R.id.main_iv_search);
editBtn.setOnClickListener(this);
moreBtn.setOnClickListener(this);
searchIv.setOnClickListener(this);
}
private void addLVHeaderView() {
headerView = getLayoutInflater().inflate(R.layout.item_mainlv_top, null);
todayLv.addHeaderView(headerView);
topOutTv = headerView.findViewById(R.id.item_mainlv_top_tv_out);
topInTv = headerView.findViewById(R.id.item_mainlv_top_tv_in);
topbudgetTv = headerView.findViewById(R.id.item_mainlv_top_tv_budget);
topConTv = headerView.findViewById(R.id.item_mainlv_top_tv_day);
topShowIv = headerView.findViewById(R.id.item_mainlv_top_iv_hide);
topbudgetTv.setOnClickListener(this);
headerView.setOnClickListener(this);
topShowIv.setOnClickListener(this);
}
private void initTime() {
Calendar calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH) + 1;
day = calendar.get(Calendar.DAY_OF_MONTH);
}
//当activity获取焦点时,会调用的方法
@Override
protected void onResume() {
super.onResume();
loadDBData();
setTopTvShow();
}
private void setTopTvShow() {
float outMonthCome = DBManager.getMoneyOneMonth(year, month, 0);
float inMonthCome = DBManager.getMoneyOneMonth(year, month, 1);
float outDayCome = DBManager.getMoneyOneDay(year, month, day, 0);
float inDayCome = DBManager.getMoneyOneDay(year, month, day, 1);
String infoOneDay = "今日支出 ¥" + outDayCome + " ,收入 ¥" + inDayCome;
topConTv.setText(infoOneDay);
topInTv.setText("¥ " + inMonthCome);
topOutTv.setText("¥ " + outMonthCome);
//设置预算剩余
float bMoney = preferences.getFloat("bmoney", 0);
if (bMoney == 0) {
topbudgetTv.setText(" ¥0");
} else {
float bMoney1 = bMoney - outMonthCome;
topbudgetTv.setText(" ¥" + bMoney1);
}
}
private void loadDBData() {
List<AccountBean> list = DBManager.getAccountListOneDayFromAccounttb(year, month, day);
mDatas.clear();
mDatas.addAll(list);
adapter.notifyDataSetChanged();
}
//button相关的点击事件
@Override
public void onClick(View view) {
if (view.getId() == R.id.main_iv_search) {
} else if (view.getId() == R.id.main_btn_edit) {
Intent it1 = new Intent(this, RecordActivity.class); //跳转界面
startActivity(it1);
} else if (view.getId() == R.id.main_btn_more) {
} else if (view.getId() == R.id.item_mainlv_top_tv_budget) {
showBudgetDialog();
} else if (view == headerView) {
//头布局被点击
} else if (view.getId() == R.id.item_mainlv_top_iv_hide) {
//切换明文和密文
toggleShow();
}
}
private void showBudgetDialog() {
//显示设置预算对话框
BudgetDialog dialog = new BudgetDialog(this);
dialog.show();
dialog.setOnEnsureListener(money -> {
//将金额写入共享参数中,进行存储
SharedPreferences.Editor editor = preferences.edit();
editor.putFloat("bmoney", money);
editor.commit();
//计算剩余金额
float outComeMonth = DBManager.getMoneyOneMonth(year, month, 0);
float sMoney = money - outComeMonth;//预算剩余
topbudgetTv.setText("¥ " + sMoney);
});
}
boolean isShow = true;
//点击眼睛时,如果是明文就加密,如果是密文,就变成明文
private void toggleShow() {
if (isShow) {
PasswordTransformationMethod passwordMethod = PasswordTransformationMethod.getInstance();
topInTv.setTransformationMethod(passwordMethod);
topOutTv.setTransformationMethod(passwordMethod);
topbudgetTv.setTransformationMethod(passwordMethod);
topShowIv.setImageResource(R.mipmap.ih_hide);
isShow = false;
} else {
HideReturnsTransformationMethod hideReturnsTransformationMethod = HideReturnsTransformationMethod.getInstance();
topInTv.setTransformationMethod(hideReturnsTransformationMethod);
topOutTv.setTransformationMethod(hideReturnsTransformationMethod);
topbudgetTv.setTransformationMethod(hideReturnsTransformationMethod);
topShowIv.setImageResource(R.mipmap.ih_show);
isShow = true;
}
}
}