1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| package com.akelato.myapplication.Controller;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.ImageButton;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Bundle;
import android.content.DialogInterface;
import android.content.Intent;
import com.akelato.myapplication.Model.Preference;
import com.akelato.myapplication.View.VerticalViewPager;
import com.akelato.myapplication.R;
import com.akelato.myapplication.View.CustomSwipeAdapter;
import com.akelato.myapplication.Model.Serialize;
import com.akelato.myapplication.Model.Mood;
import org.joda.time.LocalDate;
import org.joda.time.Period;
import java.io.File;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ImageButton mAddNote, mHistory;
private CustomSwipeAdapter mAdapter;
private EditText mEditText;
private File mFolder, moodFile;
private int mCurrentMood;
private List<Mood> moodLog = new ArrayList<>();
private Mood newMood;
private ObjectInputStream mInputStream;
private ObjectOutputStream mOutputStream;
private String mCurrentMoodNote, mNote, mCurrentDay, mCurrentMoodDay;
private VerticalViewPager mViewPager;
private Serialize serial;
private Preference userPref;
private ImageButton mShare;
public static final String BUNDLE_STATE_MOOD = "usersMood";
public static final String BUNDLE_STATE_NOTE = "usersNote";
public static final String BUNDLE_STATE_DAY = "MoodsDay";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (VerticalViewPager) findViewById(R.id.view_pager);
mAdapter = new CustomSwipeAdapter(this);
mViewPager.setAdapter(mAdapter);
mCurrentDay = new LocalDate().toString();
userPref = new Preference(this);
if(savedInstanceState != null) {
mCurrentMood = savedInstanceState.getInt(BUNDLE_STATE_MOOD);
mCurrentMoodNote = savedInstanceState.getString(BUNDLE_STATE_NOTE);
mCurrentMoodDay = savedInstanceState.getString(BUNDLE_STATE_DAY);
} else {
mCurrentMood = userPref.getMoodPref();
mCurrentMoodNote = userPref.getNotePref();
mCurrentMoodDay = userPref.getDayPref();
}
System.out.println("CurrentMoodDay: "+mCurrentMoodDay +" CurrentDay: "+mCurrentDay);
LocalDate sDate = new LocalDate(LocalDate.parse(mCurrentMoodDay));
LocalDate cDate = new LocalDate(LocalDate.parse(mCurrentDay));
Period period = new Period(sDate, cDate);
int deltaDays = period.toStandardDays().getDays();
if (deltaDays != 0){
mFolder = new File(getFilesDir() + "/mood");
if (!mFolder.exists()){
mFolder.mkdir();
}
moodFile = new File(mFolder.getAbsolutePath() + "/moodLog1.dat");
serial = new Serialize();
moodLog = serial.deserialize(moodFile);
newMood = new Mood(mCurrentMood, deltaDays, mCurrentMoodNote);
serial.serialize(moodLog, newMood, moodFile);
mCurrentMood = 2;
mCurrentMoodNote = null;
mCurrentMoodDay = mCurrentDay;
}
mAddNote =(ImageButton)findViewById(R.id.add_note);
mAddNote.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View mView = getLayoutInflater().inflate(R.layout.note_layout, null);
mEditText = (EditText) mView.findViewById(R.id.note_text);
builder.setView(mView);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mNote = mEditText.getText().toString();
if(!mNote.isEmpty()){
Toast.makeText(MainActivity.this,
"Enregistrée",
Toast.LENGTH_SHORT).show();
}
dialog.cancel();
}
});
builder.setNegativeButton("Annuler", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
mHistory = (ImageButton)findViewById(R.id.history_display);
mHistory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent historyActivity = new Intent(MainActivity.this, HistoryActivity.class);
startActivity(historyActivity);
}
});
mShare = (ImageButton) findViewById(R.id.share_ic);
mShare.setOnClickListener(new ImageButton.OnClickListener() {
@Override
public void onClick(View v) {
Intent shareActivity = new Intent(MainActivity.this, ShareActivity.class);
startActivity(shareActivity);
}
});
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt(BUNDLE_STATE_MOOD, mCurrentMood);
outState.putString(BUNDLE_STATE_NOTE, mCurrentMoodNote);
outState.putString(BUNDLE_STATE_DAY, mCurrentMoodDay);
super.onSaveInstanceState(outState);
}
@Override
protected void onStop() {
int moodItem = mViewPager.getCurrentItem();
userPref.setMoodPref(moodItem);
String date = new LocalDate().toString();
userPref.setDayPref(date);
userPref.setNotePref(mNote);
super.onStop();
}
} |