diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000000..96cc43efa6 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000000..e7bedf3377 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000000..7ac24c777f --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/.idea/libraries/com_google_code_gson_gson_2_2_4_jar.xml b/.idea/libraries/com_google_code_gson_gson_2_2_4_jar.xml new file mode 100644 index 0000000000..3acd89f14d --- /dev/null +++ b/.idea/libraries/com_google_code_gson_gson_2_2_4_jar.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000000..d9794b00f8 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000..c15affbaa2 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000000..7f68460d8b --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000..35eb1ddfbb --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000000..bba039f783 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,2517 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + tweets + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1516147497036 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/app.iml b/app/app.iml index 3569a011bc..b1a373173c 100644 --- a/app/app.iml +++ b/app/app.iml @@ -1,5 +1,5 @@ - + @@ -9,13 +9,9 @@ - + - + + + + - + + + + + + + + + + + + + + + - + + + + + + + + - + - + + + - - - - - - - + + + + + + + - - - + + + + + + + + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 1cc9ef1961..eb93711aa9 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,5 +1,4 @@ apply plugin: 'com.android.application' - android { compileSdkVersion 18 buildToolsVersion "22.0.1" @@ -21,3 +20,7 @@ android { } } } + +dependencies { + implementation 'com.google.code.gson:gson:2.2.4' +} \ No newline at end of file diff --git a/app/src/main/java/ca/ualberta/cs/lonelytwitter/ImportantTweet.java b/app/src/main/java/ca/ualberta/cs/lonelytwitter/ImportantTweet.java new file mode 100644 index 0000000000..1b7c2c919e --- /dev/null +++ b/app/src/main/java/ca/ualberta/cs/lonelytwitter/ImportantTweet.java @@ -0,0 +1,21 @@ +package ca.ualberta.cs.lonelytwitter; + +/** + * Created by dezfuli on 1/16/18. + */ +import java.util.Date; + +public class ImportantTweet extends Tweet { + ImportantTweet(String message){ + super(message); + } + + ImportantTweet(String message, Date date){ + super(message, date); + } + + @Override + public Boolean isImportant() { + return Boolean.TRUE; + } +} diff --git a/app/src/main/java/ca/ualberta/cs/lonelytwitter/LonelyTwitterActivity.java b/app/src/main/java/ca/ualberta/cs/lonelytwitter/LonelyTwitterActivity.java index cd5feb6966..c6c899ec88 100644 --- a/app/src/main/java/ca/ualberta/cs/lonelytwitter/LonelyTwitterActivity.java +++ b/app/src/main/java/ca/ualberta/cs/lonelytwitter/LonelyTwitterActivity.java @@ -1,33 +1,45 @@ package ca.ualberta.cs.lonelytwitter; import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Date; import android.app.Activity; import android.content.Context; import android.os.Bundle; +import android.util.Log; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; +import android.widget.Toast; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; public class LonelyTwitterActivity extends Activity { - private static final String FILENAME = "file.sav"; + private static final String FILENAME = "tweets.sav"; private EditText bodyText; private ListView oldTweetsList; + + private ArrayList tweetList; + private ArrayAdapter adapter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + Log.i("LifeCycle ---->", "onCreate is called"); setContentView(R.layout.main); bodyText = (EditText) findViewById(R.id.body); @@ -39,57 +51,76 @@ public void onCreate(Bundle savedInstanceState) { public void onClick(View v) { setResult(RESULT_OK); String text = bodyText.getText().toString(); - saveInFile(text, new Date(System.currentTimeMillis())); - finish(); + Tweet tweet = new NormalTweet(text); + tweetList.add(tweet); + + adapter.notifyDataSetChanged(); + + saveInFile(); } }); } @Override protected void onStart() { + // TODO Auto-generated method stub super.onStart(); - String[] tweets = loadFromFile(); - ArrayAdapter adapter = new ArrayAdapter(this, - R.layout.list_item, tweets); + Log.i("LifeCycle --->", "onStart is called"); + + loadFromFile(); + + adapter = new ArrayAdapter(this, + R.layout.list_item, tweetList); oldTweetsList.setAdapter(adapter); + } - private String[] loadFromFile() { - ArrayList tweets = new ArrayList(); + private void loadFromFile() { + try { FileInputStream fis = openFileInput(FILENAME); BufferedReader in = new BufferedReader(new InputStreamReader(fis)); - String line = in.readLine(); - while (line != null) { - tweets.add(line); - line = in.readLine(); - } + + Gson gson = new Gson(); + + // Taken https://stackoverflow.com/questions/12384064/gson-convert-from-json-to-a-typed-arraylistt + // 2018-01-23 + Type listType = new TypeToken>(){}.getType(); + tweetList = gson.fromJson(in, listType); } catch (FileNotFoundException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + tweetList = new ArrayList(); } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + throw new RuntimeException(); } - return tweets.toArray(new String[tweets.size()]); + } - private void saveInFile(String text, Date date) { + private void saveInFile() { try { + FileOutputStream fos = openFileOutput(FILENAME, - Context.MODE_APPEND); - fos.write(new String(date.toString() + " | " + text) - .getBytes()); - fos.close(); + Context.MODE_PRIVATE); + BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fos)); + + Gson gson = new Gson(); + gson.toJson(tweetList, out); + out.flush(); + } catch (FileNotFoundException e) { // TODO Auto-generated catch block - e.printStackTrace(); + throw new RuntimeException(); } catch (IOException e) { // TODO Auto-generated catch block - e.printStackTrace(); + throw new RuntimeException(); } } + + @Override + protected void onDestroy() { + super.onDestroy(); + Log.i("Lifecycle", "onDestroy is called"); + } } \ No newline at end of file diff --git a/app/src/main/java/ca/ualberta/cs/lonelytwitter/NormalTweet.java b/app/src/main/java/ca/ualberta/cs/lonelytwitter/NormalTweet.java new file mode 100644 index 0000000000..7b06e75110 --- /dev/null +++ b/app/src/main/java/ca/ualberta/cs/lonelytwitter/NormalTweet.java @@ -0,0 +1,21 @@ +package ca.ualberta.cs.lonelytwitter; + +/** + * Created by dezfuli on 1/16/18. + */ +import java.util.Date; + +public class NormalTweet extends Tweet { + NormalTweet(String message){ + super(message); + } + + NormalTweet(String message, Date date){ + super(message, date); + } + + @Override + public Boolean isImportant() { + return Boolean.FALSE; + } +} diff --git a/app/src/main/java/ca/ualberta/cs/lonelytwitter/Tweet.java b/app/src/main/java/ca/ualberta/cs/lonelytwitter/Tweet.java new file mode 100644 index 0000000000..96362b9d0f --- /dev/null +++ b/app/src/main/java/ca/ualberta/cs/lonelytwitter/Tweet.java @@ -0,0 +1,52 @@ +package ca.ualberta.cs.lonelytwitter; + +import java.util.Date; + +/** + * Created by dezfuli on 1/16/18. + */ + +public abstract class Tweet implements Tweetable { + private String message; + private Date date; + + Tweet(String message){ + + this.message = message; + date = new Date(); +// message = message; + } + + Tweet(String message, Date date){ + this.message = message; + this.date = date; + } + + public String getMessage(){ + return message; + } + + public void setMessage(String message) throws TweetTooLongException{ + if (message.length() < 140){ + this.message = message; + } + else{ + throw new TweetTooLongException(); + } + } + + public Date getDate(){ + return date; + } + + public void setDate(Date date){ + this.date = date; + } + + public abstract Boolean isImportant(); + + public String toString() { + return date.toString() + " | " + message; + } + +} diff --git a/app/src/main/java/ca/ualberta/cs/lonelytwitter/TweetTooLongException.java b/app/src/main/java/ca/ualberta/cs/lonelytwitter/TweetTooLongException.java new file mode 100644 index 0000000000..5eda368654 --- /dev/null +++ b/app/src/main/java/ca/ualberta/cs/lonelytwitter/TweetTooLongException.java @@ -0,0 +1,8 @@ +package ca.ualberta.cs.lonelytwitter; + +/** + * Created by dezfuli on 1/16/18. + */ + +public class TweetTooLongException extends Exception { +} diff --git a/app/src/main/java/ca/ualberta/cs/lonelytwitter/Tweetable.java b/app/src/main/java/ca/ualberta/cs/lonelytwitter/Tweetable.java new file mode 100644 index 0000000000..0c42d332bb --- /dev/null +++ b/app/src/main/java/ca/ualberta/cs/lonelytwitter/Tweetable.java @@ -0,0 +1,11 @@ +package ca.ualberta.cs.lonelytwitter; + +/** + * Created by dezfuli on 1/16/18. + */ +import java.util.Date; + +public interface Tweetable { + public String getMessage(); + public Date getDate(); +} diff --git a/build.gradle b/build.gradle index 6a5c233c5d..e4467f8ede 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:1.3.0' + classpath 'com.android.tools.build:gradle:3.0.1' } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000000..b1ef018f8f --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Tue Jan 16 17:05:36 MST 2018 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-4.2-all.zip diff --git a/gradlew b/gradlew index 91a7e269e1..9d82f78915 100755 --- a/gradlew +++ b/gradlew @@ -42,11 +42,6 @@ case "`uname`" in ;; esac -# For Cygwin, ensure paths are in UNIX format before anything is touched. -if $cygwin ; then - [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` -fi - # Attempt to set APP_HOME # Resolve links: $0 may be a link PRG="$0" @@ -61,9 +56,9 @@ while [ -h "$PRG" ] ; do fi done SAVED="`pwd`" -cd "`dirname \"$PRG\"`/" >&- +cd "`dirname \"$PRG\"`/" >/dev/null APP_HOME="`pwd -P`" -cd "$SAVED" >&- +cd "$SAVED" >/dev/null CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar @@ -114,6 +109,7 @@ fi if $cygwin ; then APP_HOME=`cygpath --path --mixed "$APP_HOME"` CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` # We build the pattern for arguments to be converted via cygpath ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` diff --git a/lonelyTwitter.iml b/lonelyTwitter.iml index 8c831df98f..fbd182a91c 100644 --- a/lonelyTwitter.iml +++ b/lonelyTwitter.iml @@ -1,5 +1,5 @@ - + @@ -13,7 +13,7 @@ - + \ No newline at end of file