package com.haussteuerung;

import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.lifecycle.ProcessLifecycleOwner;

import java.util.Timer;
import java.util.TimerTask;

public class activityLog extends AppCompatActivity {
    private TextView textLog;
    final Handler myHandler = new Handler();
    final Timer myTimer = new Timer();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ProcessLifecycleOwner.get().getLifecycle().addObserver(new AppLifecycleTracker());

        androidx.appcompat.app.ActionBar actionBar = getSupportActionBar();
        assert actionBar != null;
        actionBar.setBackgroundDrawable(AppCompatResources.getDrawable(getApplicationContext(), R.color.black));

        setContentView(R.layout.activity_log);
        setTitle("Haussteuerung - Log");

        textLog = findViewById(R.id.textLog);
    }

    // Runnable method
    final Runnable myRunnable = new Runnable() {
        public void run() {
            textLog.setText(MqttClient.logContent);
        }
    };

    @Override
    protected void onPause() {
        super.onPause();
        myTimer.cancel();
    }

    @Override
    protected void onResume() {
        super.onResume();
        TimerTask myTask = new TimerTask() {
            public void run() {
                myHandler.post(myRunnable); // updateUI method
            }
        };
        myTimer.schedule(myTask,0,1000); // TimerTask, delay, period
    }

    @Override
    protected void onStop() {
        super.onStop();
        myTimer.cancel();
    }
}