Skip to content

Commit

Permalink
Refactor of timer logic,
Browse files Browse the repository at this point in the history
initial progress made on SleepLog functionality(storing, displaying sleep data)
Back button now works returning from settings to main view
  • Loading branch information
Billsong19 committed Jun 19, 2020
1 parent 6fed0b8 commit a8601d7
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 44 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ android {

defaultConfig {
applicationId "com.example.sleeplog"
minSdkVersion 16
minSdkVersion 28
targetSdkVersion 29
versionCode 1
versionName "1.0"
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/com/example/sleeplog/LogEntry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.sleeplog;

public class LogEntry {
private SleepSession _sleepSession;

public LogEntry(SleepSession sleepSession) {
_sleepSession = sleepSession;
}

public SleepSession get_sleepSession() {
return _sleepSession;
}

//Outputs the ByteArray needed to write to SleepLog.txt
// public byte[] toByteArray(){
// return _sleepSession.get();
// }
}
91 changes: 50 additions & 41 deletions app/src/main/java/com/example/sleeplog/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.example.sleeplog;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.SystemClock;
//import android.support.v7.app.AppCompactActivity;
import android.os.Bundle;
import android.view.View;
Expand All @@ -13,21 +11,45 @@
import androidx.appcompat.app.AppCompatActivity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.Duration;
import java.time.LocalDateTime;

//TODO make unit tests for clock? Would maybe have to encapsulate clock logic for
// tests.

public class MainActivity extends AppCompatActivity {

TextView timer ;
Button startButton, pauseButton, resetButton, stopAndLog, settingsButton;
long MillisecondTime, StartTime, TimeBuff, UpdateTime = 0L ;
Button startButton, pauseButton, resetButton, stopAndLog, settingsButton, sleepLogButton;
long MillisecondTime, TimeBuff, UpdateTime = 0L ;
LocalDateTime _startTime;
Duration _timeElapsed;
File _sleepLogFile;

Handler handler;
int Seconds, Minutes, Hours ;
long Seconds, Minutes, _hours;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get internal file directory, points to/creates SleepLog file
//AppCompatActivity extends Context so we use it in File constructor param

_sleepLogFile = new File(this.getFilesDir(), "SleepLog.txt");

// FileOutputStream stream;
// try {
// stream = new FileOutputStream(_sleepLogFile);
// stream.write("HELLO WORLD".getBytes());
// stream.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
//read

File _sleepLog = new File(this.getFilesDir(), "SleepLog");



Expand All @@ -37,14 +59,16 @@ protected void onCreate(Bundle savedInstanceState) {
resetButton = (Button)findViewById(R.id.Reset);
stopAndLog = (Button)findViewById(R.id.FinishedSleeping);
settingsButton = (Button)findViewById(R.id.Settings);
sleepLogButton = (Button)findViewById(R.id.SleepLog);


handler = new Handler() ;

startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

StartTime = SystemClock.uptimeMillis();
_startTime = LocalDateTime.now();
handler.postDelayed(runnable, 0);

resetButton.setEnabled(false);
Expand All @@ -55,8 +79,6 @@ public void onClick(View view) {
@Override
public void onClick(View view) {

TimeBuff += MillisecondTime;

handler.removeCallbacks(runnable);

resetButton.setEnabled(true);
Expand All @@ -67,49 +89,36 @@ public void onClick(View view) {
resetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

MillisecondTime = 0L ;
StartTime = 0L ;
TimeBuff = 0L ;
UpdateTime = 0L ;
Seconds = 0 ;
Minutes = 0 ;

timer.setText("00:00:00");

SleepSession sleepSession = new SleepSession(_startTime, LocalDateTime.now());
FileOutputStream stream;
try {
stream = new FileOutputStream(_sleepLogFile);
stream.write(sleepSession.get_byteArray());
System.out.println("Wrote " + sleepSession.get_byteArray().toString() + " to SleepLog.txt");
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
final Intent settings = new Intent(MainActivity.this, SettingsActivity.class);
settingsButton.setOnClickListener(view -> startActivity(settings));
// settingsButton.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view)
// {
// startActivity(settings);
//
// }
// });

final Intent _settings = new Intent(MainActivity.this, SettingsActivity.class);
settingsButton.setOnClickListener(view -> startActivity(_settings));

final Intent _sleepLog = new Intent(MainActivity.this, SleepLogActivity.class);
sleepLogButton.setOnClickListener(view -> startActivity(_sleepLog));
}

public Runnable runnable = new Runnable() {

public void run() {

MillisecondTime = SystemClock.uptimeMillis() - StartTime;

UpdateTime = TimeBuff + MillisecondTime;

Seconds = (int) (UpdateTime / 1000);

Hours = Seconds / 3600;

Minutes = Seconds / 60;

Seconds = Seconds % 60;
SleepSession sleepSession = new SleepSession(_startTime, LocalDateTime.now());

timer.setText("" + String.format("%02d", Hours) + ":" + String.format("%02d", Minutes)
+ ":" + String.format("%02d", Seconds));
timer.setText(sleepSession.toString());

handler.postDelayed(this, 0);
}
};}
};}
16 changes: 16 additions & 0 deletions app/src/main/java/com/example/sleeplog/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import androidx.appcompat.app.AppCompatActivity;
import android.widget.Button;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class SettingsActivity extends AppCompatActivity {

Button button2, button3, button4;
Expand All @@ -14,7 +18,19 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.settings_main);

button2 = (Button)findViewById(R.id.button2);

File _sleepLogFile = new File(this.getFilesDir(), "SleepLog.txt");
try {
Scanner myReader = new Scanner(_sleepLogFile);
String str = myReader.nextLine();
System.out.println(str);
button2.setText(str);
} catch (FileNotFoundException e) {
e.printStackTrace();
}

button3 = (Button)findViewById(R.id.button3);
button4 = (Button)findViewById(R.id.button4);

}
}
15 changes: 15 additions & 0 deletions app/src/main/java/com/example/sleeplog/SleepLogActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.sleeplog;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
/*TODO implement SleepLogActivity, make it list "bedtime -> waketime: duration date of the night"
parse lines in txt, pass into new SleepSessions, send to Buttons in recyclerview.
*/
public class SleepLogActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sleeplog_main);
}
}
64 changes: 64 additions & 0 deletions app/src/main/java/com/example/sleeplog/SleepSession.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.example.sleeplog;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;

public class SleepSession {
private Duration _sleepDuration;
private LocalDateTime _bedTime, _wakeTime;
private long _hours, _minutes, _seconds ;

public SleepSession(LocalDateTime bedTime, LocalDateTime wakeTime) {

_sleepDuration = Duration.between(bedTime, wakeTime);
_bedTime = bedTime;
_wakeTime = wakeTime;
_hours = (long)_sleepDuration.toHours();
_minutes = (long)_sleepDuration.minusHours(_seconds).toMinutes();
_seconds = (long)_sleepDuration.minusHours(_seconds).minusMinutes(_minutes).getSeconds();
}

public long get_hours() {
return _hours;
}

public long get_minutes() {
return _minutes;
}

public long get_seconds() {
return _seconds;
}

// public String get_bedTime() {
// return _bedTime.toString();
// }
//
// public String get_wakeTime() {
// return _wakeTime.toString();
// }

public byte[] get_bedBytes() {
return _bedTime.toString().getBytes();
}

public byte[] get_wakeBytes() {
return _wakeTime.toString().getBytes();
}

public byte[] get_byteArray() {
return (_bedTime.toString() + _wakeTime.toString()).getBytes();
}

@Override
public String toString() {
return String.format("%02d", _hours) + ":" + String.format("%02d", _minutes)
+ ":" + String.format("%02d", _seconds);
}

public static SleepSession parse(String text) {
//substring it
return new SleepSession(LocalDateTime.parse(text), LocalDateTime.parse(text));
}
}
12 changes: 11 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,17 @@
app:layout_constraintHorizontal_bias="0.95"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- tools:layout_editor_absoluteY="3dp" />-->

<Button
android:id="@+id/SleepLog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="SleepLog"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.05"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>
28 changes: 28 additions & 0 deletions app/src/main/res/layout/sleeplog_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView
android:layout_width="479dp"
android:layout_height="835dp"
tools:layout_editor_absoluteX="3dp"
tools:layout_editor_absoluteY="-1dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button" />
</LinearLayout>
</ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>
18 changes: 17 additions & 1 deletion app/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@id/FirstFragment">
app:startDestination="@id/mainActivity">

<fragment
android:id="@+id/FirstFragment"
Expand All @@ -25,4 +25,20 @@
android:id="@+id/action_SecondFragment_to_FirstFragment"
app:destination="@id/FirstFragment" />
</fragment>
<activity
android:id="@+id/mainActivity"
android:name="com.example.sleeplog.MainActivity"
android:label="MainActivity" >
<action
android:id = "@+id/action_mainActivity_to_settingsActivity"
app:destination="@+id/settingsActivity" />
</activity>
<activity
android:id="@+id/settingsActivity"
android:name="com.example.sleeplog.SettingsActivity"
android:label="SettingsActivity" >
<action
android:id = "@+id/action_settingsActivity_to_mainActivity"
app:destination="@+id/mainActivity" />
</activity>
</navigation>

0 comments on commit a8601d7

Please sign in to comment.