package timeline.lizimumu.com.t.ui;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fancy.androidutils.utils.SpUtils;
import java.util.ArrayList;
import java.util.List;
import timeline.lizimumu.com.t.R;
import timeline.lizimumu.com.t.adapter.TimerListAdapter;
import timeline.lizimumu.com.t.data.TimerEntity;
import timeline.lizimumu.com.t.widget.AlertEditView;
public class TimerActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private TextView tvTime;
private List<TimerEntity> mDatas;
private TimerListAdapter timerListAdapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer);
initView();
initData();
}
private void initView() {
recyclerView = findViewById(R.id.recycler_view);
tvTime = findViewById(R.id.tv_time);
historyList = SpUtils.getList(this, "data");
recyclerView.setLayoutManager(new LinearLayoutManager(this));
tvTime.setOnClickListener(view -> {
AlertEditView alertEditView = new AlertEditView(TimerActivity.this);
alertEditView.show();
alertEditView.setResultListener((type, string) -> {
if (type == 1) {
Intent intent = new Intent(this, SubTimerActivity.class);
intent.putExtra("title", string);
startActivityForResult(intent, 1000);
} else if (type == 2) {
Intent intent = new Intent(this, SubTimerSortActivity.class);
intent.putExtra("title", string);
startActivityForResult(intent, 1000);
}
});
});
}
private void initData() {
mDatas = new ArrayList<>();
mDatas.add(new TimerEntity());
if (historyList != null) {
for (int i = 0; i < historyList.size(); i++) {
TimerEntity entity = JSONObject.parseObject(historyList.get(i), TimerEntity.class);
mDatas.add(entity);
}
}
timerListAdapter = new TimerListAdapter(mDatas);
recyclerView.setAdapter(timerListAdapter);
}
private List<String> historyList; // 历史数据
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1000 && resultCode == 2022) {
TimerEntity timerEntity = (TimerEntity) data.getSerializableExtra("entity");
saveCount((JSONObject) JSONObject.toJSON(timerEntity));
new Handler(getMainLooper()).postDelayed(new Runnable() {
@SuppressLint("NotifyDataSetChanged")
@Override
public void run() {
mDatas.clear();
mDatas.add(new TimerEntity());
if (historyList != null) {
for (int i = 0; i < historyList.size(); i++) {
TimerEntity entity = JSONObject.parseObject(historyList.get(i), TimerEntity.class);
mDatas.add(entity);
}
}
timerListAdapter.notifyDataSetChanged();
}
}, 300);
}
}
/**
* 保存帐号
*/
private void saveCount(JSONObject jsonObject) {
String jsonStr = JSON.toJSONString(jsonObject);
if (historyList == null) {
historyList = new ArrayList<>();
}
historyList.add(jsonStr);
if (historyList.size() > 5) {
historyList.remove(0);
}
SpUtils.putList(this, "data", historyList);
}
}