package com.hdl.photovoltaic.ui.home; import static com.hdl.photovoltaic.ui.home.CustomStyleChartComposer.configureColorfulColumnChart; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import androidx.appcompat.content.res.AppCompatResources; import com.bigkoo.pickerview.builder.TimePickerBuilder; import com.bigkoo.pickerview.listener.OnTimeSelectListener; import com.bigkoo.pickerview.view.TimePickerView; import com.github.AAChartModel.AAChartCore.AAChartCreator.AAChartModel; import com.github.AAChartModel.AAChartCore.AAChartCreator.AAChartView; import com.github.AAChartModel.AAChartCore.AAChartCreator.AAMoveOverEventMessageModel; import com.hdl.photovoltaic.R; import com.hdl.photovoltaic.base.CustomBaseFragment; import com.hdl.photovoltaic.config.ConstantManage; import com.hdl.photovoltaic.config.UserConfigManage; import com.hdl.photovoltaic.databinding.FragmentHomePageBinding; import com.hdl.photovoltaic.enums.HomepageTitleTabSwitch; import com.hdl.photovoltaic.other.HdlDeviceLogic; import com.hdl.photovoltaic.other.HdlLogLogic; import com.hdl.photovoltaic.other.HdlThreadLogic; import com.hdl.photovoltaic.utils.TimeUtils; import com.hdl.sdk.link.core.bean.eventbus.BaseEventBus; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; import java.util.Calendar; import java.util.Date; /** * 首页模块 */ public class HomePageFragment extends CustomBaseFragment implements AAChartView.AAChartViewCallBack { FragmentHomePageBinding viewBinding; AAChartView aaChartView; AAChartModel aaChartModel; private TimeType timeType = TimeType.day;//时间类型(默认:天) @Override public Object getContentView() { viewBinding = FragmentHomePageBinding.inflate(getLayoutInflater()); return viewBinding.getRoot(); } @Override public void onBindView(Bundle savedInstanceState) { //初始化 initView(); //初始化界面监听器 initEvent(); } private void initEvent() { //时间选择器 viewBinding.yearMonthDayTv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { selectedTimePickerBuilder(); } }); //日 viewBinding.dayTv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { timeType = TimeType.day; viewBinding.dayTv.setBackground(AppCompatResources.getDrawable(_mActivity, R.drawable.time_selected_38c494)); viewBinding.dayTv.setTextColor(_mActivity.getColor(R.color.text_38C494)); viewBinding.monthTv.setBackground(AppCompatResources.getDrawable(_mActivity, R.color.text_00000000)); viewBinding.monthTv.setTextColor(_mActivity.getColor(R.color.text_40000000)); viewBinding.yearTv.setBackground(AppCompatResources.getDrawable(_mActivity, R.color.text_00000000)); viewBinding.yearTv.setTextColor(_mActivity.getColor(R.color.text_40000000)); viewBinding.lifeCycleTv.setBackground(AppCompatResources.getDrawable(_mActivity, R.color.text_00000000)); viewBinding.lifeCycleTv.setTextColor(_mActivity.getColor(R.color.text_40000000)); viewBinding.v1.setVisibility(View.GONE); viewBinding.v2.setVisibility(View.VISIBLE); viewBinding.v3.setVisibility(View.VISIBLE); viewBinding.homePageStationSelectTimeLl.setVisibility(View.VISIBLE); String dataStr = TimeUtils.getDateTimestamp(getCurrentTimestamp(), getTimeDateFormat()); viewBinding.yearMonthDayTv.setText(dataStr); setUpAAChartView(); } }); //月 viewBinding.monthTv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { timeType = TimeType.month; viewBinding.dayTv.setBackground(AppCompatResources.getDrawable(_mActivity, R.color.text_00000000)); viewBinding.dayTv.setTextColor(_mActivity.getColor(R.color.text_40000000)); viewBinding.monthTv.setBackground(AppCompatResources.getDrawable(_mActivity, R.drawable.time_selected_38c494)); viewBinding.monthTv.setTextColor(_mActivity.getColor(R.color.text_38C494)); viewBinding.yearTv.setBackground(AppCompatResources.getDrawable(_mActivity, R.color.text_00000000)); viewBinding.yearTv.setTextColor(_mActivity.getColor(R.color.text_40000000)); viewBinding.lifeCycleTv.setBackground(AppCompatResources.getDrawable(_mActivity, R.color.text_00000000)); viewBinding.lifeCycleTv.setTextColor(_mActivity.getColor(R.color.text_40000000)); viewBinding.v1.setVisibility(View.GONE); viewBinding.v2.setVisibility(View.GONE); viewBinding.v3.setVisibility(View.VISIBLE); viewBinding.homePageStationSelectTimeLl.setVisibility(View.VISIBLE); String dataStr = TimeUtils.getDateTimestamp(getCurrentTimestamp(), getTimeDateFormat()); viewBinding.yearMonthDayTv.setText(dataStr); setUpAAChartView(); } }); //年 viewBinding.yearTv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { timeType = TimeType.year; viewBinding.dayTv.setBackground(AppCompatResources.getDrawable(_mActivity, R.color.text_00000000)); viewBinding.dayTv.setTextColor(_mActivity.getColor(R.color.text_40000000)); viewBinding.monthTv.setBackground(AppCompatResources.getDrawable(_mActivity, R.color.text_00000000)); viewBinding.monthTv.setTextColor(_mActivity.getColor(R.color.text_40000000)); viewBinding.yearTv.setBackground(AppCompatResources.getDrawable(_mActivity, R.drawable.time_selected_38c494)); viewBinding.yearTv.setTextColor(_mActivity.getColor(R.color.text_38C494)); viewBinding.lifeCycleTv.setBackground(AppCompatResources.getDrawable(_mActivity, R.color.text_00000000)); viewBinding.lifeCycleTv.setTextColor(_mActivity.getColor(R.color.text_40000000)); viewBinding.v1.setVisibility(View.VISIBLE); viewBinding.v2.setVisibility(View.GONE); viewBinding.v3.setVisibility(View.GONE); viewBinding.homePageStationSelectTimeLl.setVisibility(View.VISIBLE); String dataStr = TimeUtils.getDateTimestamp(getCurrentTimestamp(), getTimeDateFormat()); viewBinding.yearMonthDayTv.setText(dataStr); setUpAAChartView(); } }); //生命期 viewBinding.lifeCycleTv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { timeType = TimeType.lifecycle; viewBinding.dayTv.setBackground(AppCompatResources.getDrawable(_mActivity, R.color.text_00000000)); viewBinding.dayTv.setTextColor(_mActivity.getColor(R.color.text_40000000)); viewBinding.monthTv.setBackground(AppCompatResources.getDrawable(_mActivity, R.color.text_00000000)); viewBinding.monthTv.setTextColor(_mActivity.getColor(R.color.text_40000000)); viewBinding.yearTv.setBackground(AppCompatResources.getDrawable(_mActivity, R.color.text_00000000)); viewBinding.yearTv.setTextColor(_mActivity.getColor(R.color.text_40000000)); viewBinding.lifeCycleTv.setBackground(AppCompatResources.getDrawable(_mActivity, R.drawable.time_selected_38c494)); viewBinding.lifeCycleTv.setTextColor(_mActivity.getColor(R.color.text_38C494)); viewBinding.v1.setVisibility(View.VISIBLE); viewBinding.v2.setVisibility(View.VISIBLE); viewBinding.v3.setVisibility(View.GONE); viewBinding.homePageStationSelectTimeLl.setVisibility(View.GONE); setUpAAChartView(); } }); //上一步 viewBinding.lastStepClickLl.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String currDate = viewBinding.yearMonthDayTv.getText().toString(); if (TextUtils.isEmpty(currDate)) { return; } String timeDateFormat = getTimeDateFormat(); Calendar calendar = TimeUtils.stringToCalendar(currDate, timeDateFormat); String time = TimeUtils.subtractDayMonthYearFromDate(calendar, 1, timeDateFormat, timeType); viewBinding.yearMonthDayTv.setText(time); } }); //下一步 viewBinding.nextStepClickLl.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String currDate = viewBinding.yearMonthDayTv.getText().toString(); if (TextUtils.isEmpty(currDate)) { return; } String timeDateFormat = getTimeDateFormat(); Calendar calendar = TimeUtils.stringToCalendar(currDate, timeDateFormat); String time = TimeUtils.addDayMonthYearToDate(calendar, 1, timeDateFormat, timeType); if (TimeUtils.stringDateToTimestamp(time, timeDateFormat) > getCurrentTimestamp()) { HdlThreadLogic.toast(_mActivity, getString(R.string.the_query_date_is_exceeded)); return; } viewBinding.yearMonthDayTv.setText(time); } }); } private void initView() { String dataStr = TimeUtils.getDateTimestamp(getCurrentTimestamp(), getTimeDateFormat()); viewBinding.yearMonthDayTv.setText(dataStr); String installed_capacity = getString(R.string.installed_capacity) + "(" + HdlDeviceLogic.kWp + ")"; viewBinding.homePageStationInfoRl1Text2Tv.setText(installed_capacity); String generated_power = getString(R.string.generated_power) + "(" + HdlDeviceLogic.kW + ")"; viewBinding.homePageStationInfoRl1Text4Tv.setText(generated_power); String generation = getString(R.string.generation) + "(" + HdlDeviceLogic.kWp + ")"; viewBinding.homePageStationInfoRl1TitleTv.setText(generation); viewBinding.homePageStationInfoRl2DayIc.dataTypeTv.setText(R.string.day_power_generation); viewBinding.homePageStationInfoRl2MonIc.dataTypeTv.setText(R.string.month_power_generation); viewBinding.homePageStationInfoRl2YearIc.dataTypeTv.setText(R.string.year_power_generation); viewBinding.homePageStationInfoRl2CumulativeIc.dataTypeTv.setText(R.string.cumulative_power_generation); viewBinding.dayTv.setBackground(AppCompatResources.getDrawable(_mActivity, R.drawable.time_selected_38c494)); viewBinding.dayTv.setTextColor(_mActivity.getColor(R.color.text_38C494)); viewBinding.monthTv.setBackground(AppCompatResources.getDrawable(_mActivity, R.color.text_00000000)); viewBinding.monthTv.setTextColor(_mActivity.getColor(R.color.text_40000000)); viewBinding.yearTv.setBackground(AppCompatResources.getDrawable(_mActivity, R.color.text_00000000)); viewBinding.yearTv.setTextColor(_mActivity.getColor(R.color.text_40000000)); viewBinding.lifeCycleTv.setBackground(AppCompatResources.getDrawable(_mActivity, R.color.text_00000000)); viewBinding.lifeCycleTv.setTextColor(_mActivity.getColor(R.color.text_40000000)); viewBinding.v1.setVisibility(View.GONE); viewBinding.v2.setVisibility(View.VISIBLE); viewBinding.v3.setVisibility(View.VISIBLE); viewBinding.homePageStationSelectTimeLl.setVisibility(View.VISIBLE); setUpAAChartView();//图表加载 } /** * 时间选择器 */ private void selectedTimePickerBuilder() { String currDate = viewBinding.yearMonthDayTv.getText().toString(); if (TextUtils.isEmpty(currDate)) { return; } //时间选择器 Calendar selectedDate = TimeUtils.stringToCalendar(currDate, getTimeDateFormat()); Calendar startDate = Calendar.getInstance(); Calendar endDate = TimeUtils.timestampToCalendar(getCurrentTimestamp()); //设置日期【开始】和【结束】 startDate.set(2023, 1, 1); endDate.set(endDate.get(Calendar.YEAR), endDate.get(Calendar.MONTH), endDate.get(Calendar.DAY_OF_MONTH)); boolean day = true; boolean month = true; boolean year = true; if (timeType == TimeType.month) { day = false; } else if (timeType == TimeType.year) { day = false; month = false; } TimePickerView pvTime = new TimePickerBuilder(_mActivity, new OnTimeSelectListener() { @Override public void onTimeSelect(Date date, View v) {//选中事件回调 String timeDateFormat = getTimeDateFormat(); String time = TimeUtils.dateToString(date, timeDateFormat); viewBinding.yearMonthDayTv.setText(time); } }).setType(new boolean[]{year, month, day, false, false, false})// 默认全部显示 .setCancelText(_mActivity.getString(R.string.loading_cancel))//取消按钮文字 .setSubmitText(_mActivity.getString(R.string.loading_affirm))//确认按钮文字 .setContentTextSize(18)//滚轮文字大小 .setTitleSize(18)//标题文字大小 .setTitleText(_mActivity.getString(R.string.time_selectd))//标题文字 .setOutSideCancelable(true)//点击屏幕,点在控件外部范围时,是否取消显示 .isCyclic(false)//是否循环滚动 .setTitleColor(_mActivity.getColor(R.color.text_90000000))//标题文字颜色 .setSubmitColor(_mActivity.getColor(R.color.text_38C494))//确定按钮文字颜色 .setCancelColor(_mActivity.getColor(R.color.text_40000000))//取消按钮文字颜色 .setTitleBgColor(_mActivity.getColor(R.color.text_FFFFFFFF))//标题背景颜色 Night mode .setBgColor(_mActivity.getColor(R.color.text_FFFFFFFF))//滚轮背景颜色 Night mode .setTextColorCenter(_mActivity.getColor(R.color.text_90000000)).setDate(selectedDate)// 如果不设置的话,默认是系统时间*/ .setRangDate(startDate, endDate)//起始终止年月日设定 .setLabel(_mActivity.getString(R.string.year), _mActivity.getString(R.string.month), _mActivity.getString(R.string.day), _mActivity.getString(R.string.hour), _mActivity.getString(R.string.minute), _mActivity.getString(R.string.seconds))//默认设置为年月日时分秒 .isCenterLabel(false) //是否只显示中间选中项的label文字,false则每项item全部都带有label。 .isDialog(false)//是否显示为对话框样式 .setItemVisibleCount(7)//对话框高度 .setDividerColor(_mActivity.getColor(R.color.text_E1E1E1)).setLineSpacingMultiplier(3f)// .build(); pvTime.show(); } /** * 图表加载 */ private void setUpAAChartView() { aaChartView = viewBinding.AAChartView; aaChartView.callBack = this; if (TimeType.day == timeType) { aaChartModel = BasicChartComposer.configureAreaChart(); } else if (TimeType.month == timeType || TimeType.year == timeType || TimeType.lifecycle == timeType) { aaChartModel = configureColorfulColumnChart(); } aaChartView.aa_drawChartWithChartModel(aaChartModel); } @Override public void chartViewDidFinishLoad(AAChartView aaChartView) { } @Override public void chartViewMoveOverEventMessage(AAChartView aaChartView, AAMoveOverEventMessageModel messageModel) { } @Subscribe(threadMode = ThreadMode.MAIN, sticky = true) public void onEventMessage(BaseEventBus eventBus) { super.onEventMessage(eventBus); if (eventBus == null) { return; } //接收外部点击事件 if (eventBus.getTopic().equals(ConstantManage.homepage_title_tab_switch)) { if (eventBus.getType().equals(HomepageTitleTabSwitch.homepage.toString())) { // 取消粘性事件 EventBus.getDefault().removeStickyEvent(eventBus); HdlLogLogic.print("正在点击【首页】"); } } } /** * 获取当前系统日期时间戳 * * @return 时间戳 */ private long getCurrentTimestamp() { return new Date().getTime(); } /** * 获取日期格式 (2024/2/09,yyyy/DD/dd) * * @return 日期格式 (2024/2/09,yyyy/DD/dd) */ private String getTimeDateFormat() { String timeDateFormat = TimeUtils.zhDateYearMonthDayFormat; switch (timeType) { case day: { timeDateFormat = TimeUtils.zhDateYearMonthDayFormat; if (!UserConfigManage.getInstance().isZh()) { timeDateFormat = TimeUtils.enDateYearMonthDayFormat; } } break; case month: { timeDateFormat = TimeUtils.zhDateYearMonthFormat; if (!UserConfigManage.getInstance().isZh()) { timeDateFormat = TimeUtils.enDateYearMonthFormat; } } break; case year: case lifecycle: { timeDateFormat = TimeUtils.zhDateYearFormat; if (!UserConfigManage.getInstance().isZh()) { timeDateFormat = TimeUtils.enDateYearFormat; } } break; } return timeDateFormat; } public enum TimeType { day, month, year, lifecycle } }