Skip to content

Instantly share code, notes, and snippets.

@bnasim
Created September 12, 2019 22:13
Show Gist options
  • Save bnasim/fa8728b85a3dcd3f116dc3791632a576 to your computer and use it in GitHub Desktop.
Save bnasim/fa8728b85a3dcd3f116dc3791632a576 to your computer and use it in GitHub Desktop.
package com.example.moodtracker;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
import androidx.core.view.GestureDetectorCompat;
import com.google.gson.Gson;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private Integer scrn = 3; // so normal will be the first screen
private static final String MyPREFERENCES = "MoodTracker" ;
private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd", Locale.US);
private Calendar rightNow;
private String todaysDate; // = "x";
private GestureDetectorCompat mGestureDetector;
private ConstraintLayout mainLayout;
private Mood currentMood = new Mood();
private Integer defaultState = 3;
private Gson gson = new Gson();
private SharedPreferences prefs;
private SharedPreferences.Editor prefsEdit;
public void swapDisplay(ConstraintLayout mainLayout, int scrn) {
int bkgColor = 0;
ImageView imgSmiley;
imgSmiley = findViewById(R.id.imageSmiley);
Drawable img;
switch (scrn)
{
case 1:
// change background color
bkgColor = ContextCompat.getColor(getBaseContext(), R.color.disappointed);
mainLayout.setBackgroundColor(bkgColor);
// change smiley
img = getDrawable(R.drawable.smiley_disappointed);
imgSmiley.setImageDrawable(img);
currentMood.setIndex(scrn);
saveMood();
break;
case 2:
// change background color
bkgColor = ContextCompat.getColor(getBaseContext(), R.color.sad);
mainLayout.setBackgroundColor(bkgColor);
// change smiley
img = getDrawable(R.drawable.smiley_sad);
imgSmiley.setImageDrawable(img);
currentMood.setIndex(scrn);
saveMood();
break;
case 3:
// change background color
bkgColor = ContextCompat.getColor(getBaseContext(), R.color.normal);
mainLayout.setBackgroundColor(bkgColor);
// change smiley
img = getDrawable(R.drawable.smiley_normal);
imgSmiley.setImageDrawable(img);
currentMood.setIndex(scrn);
saveMood();
break;
case 4:
// change background color
bkgColor = ContextCompat.getColor(getBaseContext(), R.color.happy);
mainLayout.setBackgroundColor(bkgColor);
// change smiley
img = getDrawable(R.drawable.smiley_happy);
imgSmiley.setImageDrawable(img);
currentMood.setIndex(scrn);
saveMood();
break;
case 5:
// change background color
bkgColor = ContextCompat.getColor(getBaseContext(), R.color.super_happy);
mainLayout.setBackgroundColor(bkgColor);
// change smiley
img = getDrawable(R.drawable.smiley_super_happy);
imgSmiley.setImageDrawable(img);
currentMood.setIndex(scrn);
saveMood();
break;
default: // do nothing if scrn is out of range
break;
}
}
private void showDialog(ConstraintLayout mainLayout) {
MainActivity mainActivity;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
LayoutInflater inflater = LayoutInflater.from(this);
//builder.setView(inflater.inflate(R.layout.dialog_get_comment, null))
View dialogview = inflater.inflate(R.layout.dialog_get_comment, null);
final EditText newNote = dialogview.findViewById(R.id.newComment);
builder.setView(dialogview).setMessage(getString(R.string.todsys_note));
// final MainActivity finalMainActivity = new MainActivity();
builder.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
//Mood currentMood = new Mood();
String retrieveNote = newNote.getText().toString().trim();
//MainActivity callingActivity = (MainActivity) getCallingActivity();
currentMood.setComment(retrieveNote);
saveMood();
// Toast.makeText(MainActivity.this,retrieveNote,Toast.LENGTH_LONG).show();
// TODO add the save routine here...set the date and note, then save 'em
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this,"You clicked CANCEL",Toast.LENGTH_LONG).show();
}
});
builder.show();
}
public void saveMood (){
// if (prefsEdit == null) {
// Toast.makeText(MainActivity.this, "saveMood: prefsEdit is not NULL", Toast.LENGTH_LONG).show();
// };
// serialize object, then use string for the following save
String moodStr = gson.toJson(currentMood);
rightNow = Calendar.getInstance();
//String moodStr = "fubar";
todaysDate = sdf.format(rightNow.getTime());
// prefsEdit.putString(todaysDate, moodStr); //"skdj");
// prefsEdit.apply();
prefs = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
prefsEdit = prefs.edit();
prefsEdit.putString(todaysDate, moodStr);
prefsEdit.apply();
Toast.makeText(MainActivity.this,moodStr,Toast.LENGTH_LONG).show();
// TEST
// int svd = prefs.getInt(todaysDate, 99);
// TextView textView=findViewById(R.id.textView);
// textView.setText(String.format(Locale.US, "Fubar: %d %s", svd, prefs.contains(todaysDate)));
// END TEST
}
private class GestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent m1, MotionEvent m2, float velocityX, float velocityY) {
if ( velocityX > velocityY ) {
if (++scrn > 5) {scrn = 1;} // roll over to min from max
swapDisplay(mainLayout, scrn);
Toast.makeText(MainActivity.this, "Fling up"+scrn.toString(), Toast.LENGTH_SHORT).show();}
if ( velocityY > velocityX ) {
if (--scrn < 1) {scrn = 5;} // roll over to max from min
swapDisplay(mainLayout, scrn);
Toast.makeText(MainActivity.this, "Fling down"+scrn.toString(), Toast.LENGTH_SHORT).show();}
return super.onFling(m1, m2, velocityX, velocityY);
}
}
public boolean onTouchEvent(MotionEvent event) {
mGestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// prefs = getSharedPreferences("MoodTracker", MODE_PRIVATE);
mainLayout = findViewById(R.id.layoutView);
int bkgColor = ContextCompat.getColor(getBaseContext(),R.color.normal);
mainLayout.setBackgroundColor(bkgColor);
// prefs = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
ImageButton btnAddNote = findViewById(R.id.btnAddNote);
btnAddNote.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(mainLayout);
// swapDisplay(mainLayout);
}
});
ImageButton btnShowHistory = findViewById(R.id.btnShowHistory);
btnShowHistory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), ShowHistory.class);
startActivity(intent);
}
});
mGestureDetector = new GestureDetectorCompat(this, new GestureListener());
currentMood.setIndex(defaultState);
currentMood.setComment("");
//return(mainLayout);
}
@Override
protected void onStart() {
super.onStart();
//todaysDate = sdf.format(new Date());
// rightNow = Calendar.getInstance();
// todaysDate = sdf.format(rightNow.getTime());
// cannot get sharedPreferences until onCreate is finished
// prefs = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
// prefsEdit = prefs.edit();
// prefsEdit.putString(todaysDate, ""); //"skdj");
// prefsEdit.apply();
// if ((prefs != null) && (prefsEdit != null)){
// Toast.makeText(MainActivity.this, "onStart: prefs OK "+todaysDate, Toast.LENGTH_LONG).show();
// }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment