package com.example.quentin.expensemanager;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;

import com.facebook.stetho.Stetho;
import com.facebook.stetho.okhttp3.StethoInterceptor;

import okhttp3.OkHttpClient;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Stetho.initializeWithDefaults(this);
        new OkHttpClient.Builder()
                .addNetworkInterceptor(new StethoInterceptor())
                .build();
        if (!checkDataBase()){
            final String reg = "^[0-9]*$";
            final AlertDialog.Builder alert = new AlertDialog.Builder(this);
            alert.setTitle(getString(R.string.alert_title));
            alert.setMessage(getString(R.string.alert_message));
            final EditText input = new EditText(this);

            alert.setView(input);
            alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    String cycleLength = input.getText().toString();
                    if(cycleLength.matches(reg)&&!cycleLength.isEmpty()){
                        dialog.dismiss();
                    }
                    else{
                        Toast.makeText(MainActivity.this, getString(R.string.alert_invalid_input), Toast.LENGTH_LONG).show();
                    }

                }

            });
            alert.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            Toast.makeText(MainActivity.this, getString(R.string.alert_invalid_input) , Toast.LENGTH_LONG).show();
                        }
                    });
            alert.create().show();
        }

    }

    private boolean checkDataBase() {
        SQLiteDatabase checkDB = null;
        try {
            checkDB = SQLiteDatabase.openDatabase(getString(R.string.db_path), null,
                    SQLiteDatabase.OPEN_READONLY);
            checkDB.close();
        } catch (SQLiteException e) {
            // database doesn't exist yet.
        }
        return checkDB != null;
    }
}

@@ -367,16 +367,81 @@
 Toast;%0A%0A
+import com.example.quentin.expensemanager.SQLite.SQLiteDBHelper;%0A
 import c
@@ -467,16 +467,16 @@
 Stetho;%0A
-
 import c
@@ -607,16 +607,46 @@
 vity %7B%0A%0A
+    SQLiteDBHelper mDBHelper;%0A
     @Ove
@@ -1017,1318 +1017,91 @@
     
-final String reg = %22%5E%5B0-9%5D*$%22;%0A            final AlertDialog.Builder alert = new AlertDialog.Builder(this);%0A            alert.setTitle(getString(R.string.alert_title));%0A            alert.setMessage(getString(R.string.alert_message));%0A            final EditText input = new EditText(this);%0A%0A            alert.setView(input);%0A            alert.setPositiveButton(%22Ok%22, new DialogInterface.OnClickListener() %7B%0A                public void onClick(DialogInterface dialog, int whichButton) %7B%0A                    String cycleLength = input.getText().toString();%0A                    if(cycleLength.matches(reg)&&!cycleLength.isEmpty())%7B%0A                        dialog.dismiss();%0A                    %7D%0A                    else%7B%0A                        Toast.makeText(MainActivity.this, getString(R.string.alert_invalid_input), Toast.LENGTH_LONG).show();%0A                    %7D%0A%0A                %7D%0A%0A            %7D);%0A            alert.setNegativeButton(%22Cancel%22,%0A                    new DialogInterface.OnClickListener() %7B%0A                        public void onClick(DialogInterface dialog, int whichButton) %7B%0A                            Toast.makeText(MainActivity.this, getString(R.string.alert_invalid_input) , Toast.LENGTH_LONG).show();%0A                        %7D%0A                    %7D);%0A            alert.create().show();
+//TODO: If no database in app, ask user to initialize it with a budget cycle length
 %0A   

Took out the alert dialog, will find a better solution