冲刺day5

SubTimerSortActivity.java

package timeline.lizimumu.com.t.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Chronometer;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.fancy.androidutils.utils.DateUtils;
import com.fancy.androidutils.utils.ToastUtils;

import dev.xesam.android.toolbox.timer.CountTimer;
import timeline.lizimumu.com.t.R;
import timeline.lizimumu.com.t.data.TimerEntity;
import timeline.lizimumu.com.t.util.DateViewUtils;

public class SubTimerSortActivity extends AppCompatActivity {

    private TextView tvUsedTime;
    private boolean isFirst = true;
    private long startTime;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub_timer_sort);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setTitle(getIntent().getStringExtra("title"));
        }

        initView();
        initData();
    }


    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.home) {
            this.finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initView() {
        tvUsedTime = findViewById(R.id.tv_used_time);
        TextView tvStart = findViewById(R.id.tv_start);
        TextView tvPause = findViewById(R.id.tv_pause);
        TextView tvEnd = findViewById(R.id.tv_end);


        timer.start();
        startTime = System.currentTimeMillis();

        tvStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                timer.resume();
            }
        });


        tvPause.setOnClickListener(view -> timer.pause());

        tvEnd.setOnClickListener(view -> {
            long endTime = System.currentTimeMillis();
            TimerEntity timerEntity = new TimerEntity();
            timerEntity.setStartTime(DateUtils.formatDateTime(startTime, DateUtils.DF_HH_MM_SS));
            timerEntity.setEndTime(DateUtils.formatDateTime(endTime, DateUtils.DF_HH_MM_SS));

            int totalSec = (int) ((endTime - startTime) / 1000);
            Log.e("fanlcly", totalSec + "");
            int min = totalSec / 60;
            int hour = min / 60;
            int hour_sec = totalSec % (60 * 60);
            int sec = hour_sec % 60;

            timerEntity.setTotalTime(hour + ":" + min + ":" + sec);
            timerEntity.setEventName(getIntent().getStringExtra("title"));

            Intent intent = new Intent();
            intent.putExtra("entity", timerEntity);
            setResult(2022, intent);
            finish();
        });

    }

    private void initData() {

    }


    CountTimer timer = new

            CountTimer(100) {

                @Override
                public void onTick(long millisFly) { // millisFly is the Elapsed time at *Running State*
                    tvUsedTime.setText(((int) millisFly / 1000) + "");
                    Log.d("onTick", millisFly + "");
                }
            };


}

 

TimerActivity.java

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);
    }

}

 

posted @ 2022-04-20 19:56  椅念琼  阅读(7)  评论(0编辑  收藏  举报