package comcondrint.github.sleepdata;


import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import java.text.*;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity {


    public static SharedPreferences pref;
    public SharedPreferences.Editor dataEditor;

    EditText RateText;
    EditText WakeText;
    EditText SleepText;

    AlertDialog saveAlert;
    AlertDialog formatAlert;

    public float[] extractData() {

        //Get rating 1-5 to use as value for storage
        float Rating = Float.parseFloat(RateText.getText().toString());

        //Extract difference between sleep/wake time to use as key for storage
        String WakeTime = WakeText.getText().toString();
        String SleepTime = SleepText.getText().toString();

        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        float[] pair = new float [2];
        try {
            Date date1 = format.parse(WakeTime);
            Date date2 = format.parse(SleepTime);
            float difference = Math.abs((date2.getTime() - date1.getTime()) / 3600000);
            pair[0] = difference;
            pair[1] = Rating;

        } catch (ParseException e){
            ; //should never get here because I check the format before calling extractdata()
        };
        return pair;
    }



    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
        dataEditor = pref.edit();


        SleepText = (EditText)findViewById(R.id.SleepText);
        WakeText = (EditText)findViewById(R.id.WakeText);
        RateText = (EditText)findViewById(R.id.RateText);

        saveAlert = new AlertDialog.Builder(MainActivity.this).create();


        formatAlert = new AlertDialog.Builder(MainActivity.this).create();
        formatAlert.setMessage("Wrong input format");
    }

    public void goToRatings(View view)
    {
        Intent intent = new Intent(MainActivity.this, RatingChart.class);
        startActivity(intent);
    }

    public void goToData(View view)
    {

        Intent intent = new Intent(MainActivity.this, SleepDataActivity.class);
        startActivity(intent);
    }

    public void SaveData(View view)
    {
        //amt of sleep is key, rating of ease of waking is value
        if (checkFormat(view)) {
            float[] pair = extractData();
            String amtSleep = Float.toString(pair[0]);
            dataEditor.putInt(amtSleep , Math.round(pair[1]));
            dataEditor.apply();
            RateText.setText("");
            WakeText.setText("");
            SleepText.setText("");
            saveAlert.setMessage("Data saved" + ": " + amtSleep + " / " + Integer.toString((Math.round(pair[1]))));
            saveAlert.show();
        } else{
            //wrong format
            formatAlert.show();
        }



    }

    public boolean checkFormat(View view)
    {
        //check if inputs are empty before proceeding
        boolean notEmpty = (!WakeText.getText().toString().isEmpty()) && (!SleepText.getText().toString().isEmpty()) && (!RateText.getText().toString().isEmpty());

        if (notEmpty){
            //check 1-5 for rating, and 24 hr formatted times for sleep/wake
            int Rating = Integer.parseInt(RateText.getText().toString());
            boolean ratingFormat = (Rating >= 1 && Rating <= 5);

            //check 24hr format
            Pattern pattern;
            Matcher matcher;
            pattern = Pattern.compile("([01]?[0-9]|2[0-3]):[0-5][0-9]");
            String WakeTime = WakeText.getText().toString();
            String SleepTime = SleepText.getText().toString();

            return ratingFormat && pattern.matcher(WakeTime).matches() && pattern.matcher(SleepTime).matches();
        } else {
            return false;
        }

    }

}

@@ -333,24 +333,47 @@
 util.Date;%0D%0A
+import java.util.Map;%0D%0A
 import java.
@@ -393,16 +393,16 @@
 tcher;%0D%0A
-
 import j
@@ -712,24 +712,53 @@
 atAlert;%0D%0A%0D%0A
+    Integer currentValue;%0D%0A%0D%0A
     public f
@@ -1942,24 +1942,96 @@
 .edit();%0D%0A%0D%0A
+        dataEditor.clear(); //for testing%0D%0A        dataEditor.apply();%0D%0A
 %0D%0A        Sl
@@ -2273,26 +2273,24 @@
 reate();%0D%0A%0D%0A
-%0D%0A
         form
@@ -2404,24 +2404,302 @@
  format%22);%0D%0A
+%0D%0A        Map %3CString,?%3E prefs = pref.getAll();%0D%0A        Integer max = -1;%0D%0A        for (String key : prefs.keySet()) %7B%0D%0A            if (Integer.parseInt(key) %3E max) %7B%0D%0A                max = Integer.parseInt(key);%0D%0A            %7D%0D%0A        %7D%0D%0A%0D%0A        currentValue = max + 1;%0D%0A%0D%0A
     %7D%0D%0A%0D%0A   
@@ -3299,23 +3299,83 @@
 .put
-Int(amtSleep , 
+String(Integer.toString(currentValue), amtSleep + %22 / %22 + Integer.toString(
 Math
@@ -3390,16 +3390,17 @@
 air%5B1%5D))
+)
 ;%0D%0A     
@@ -3592,32 +3592,38 @@
 %22 + amtSleep + %22
+ Hours
  / %22 + Integer.t
@@ -4333,32 +4333,32 @@
 ck 24hr format%0D%0A
+
             Patt
@@ -4375,38 +4375,8 @@
 n;%0D%0A
-            Matcher matcher;%0D%0A
     

Add files via upload