From ad95f1fa3d19fe8827fdfee14ac2fcb3afb8d8ed Mon Sep 17 00:00:00 2001 From: ssd352 <27953063+ssd352@users.noreply.github.com> Date: Tue, 16 Jan 2018 18:47:02 -0700 Subject: [PATCH 1/4] Add abstract Tweet class --- .idea/compiler.xml | 22 + .idea/copyright/profiles_settings.xml | 3 + .idea/gradle.xml | 18 + .idea/misc.xml | 36 + .idea/modules.xml | 9 + .idea/runConfigurations.xml | 12 + .idea/vcs.xml | 6 + .idea/workspace.xml | 2021 +++++++++++++++++ app/app.iml | 35 +- .../cs/lonelytwitter/ImportantTweet.java | 21 + .../lonelytwitter/LonelyTwitterActivity.java | 43 + .../cs/lonelytwitter/NormalTweet.java | 21 + .../ca/ualberta/cs/lonelytwitter/Tweet.java | 47 + .../lonelytwitter/TweetTooLongException.java | 8 + .../ualberta/cs/lonelytwitter/Tweetable.java | 8 + gradle/wrapper/gradle-wrapper.properties | 6 + gradlew | 10 +- lonelyTwitter.iml | 4 +- 18 files changed, 2303 insertions(+), 27 deletions(-) create mode 100644 .idea/compiler.xml create mode 100644 .idea/copyright/profiles_settings.xml create mode 100644 .idea/gradle.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/runConfigurations.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml create mode 100644 app/src/main/java/ca/ualberta/cs/lonelytwitter/ImportantTweet.java create mode 100644 app/src/main/java/ca/ualberta/cs/lonelytwitter/NormalTweet.java create mode 100644 app/src/main/java/ca/ualberta/cs/lonelytwitter/Tweet.java create mode 100644 app/src/main/java/ca/ualberta/cs/lonelytwitter/TweetTooLongException.java create mode 100644 app/src/main/java/ca/ualberta/cs/lonelytwitter/Tweetable.java create mode 100644 gradle/wrapper/gradle-wrapper.properties 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/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000000..16c4ff56a2 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + \ 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..f1c7bbffc1 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,2021 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1516147497036 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/app.iml b/app/app.iml index 3569a011bc..29c314a8e1 100644 --- a/app/app.iml +++ b/app/app.iml @@ -1,5 +1,5 @@ - + @@ -9,13 +9,9 @@ - + @@ -48,43 +44,46 @@ - + + + + + + - - + + + + + + - - + - - - - - - - + + 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..fcec3d0a40 100644 --- a/app/src/main/java/ca/ualberta/cs/lonelytwitter/LonelyTwitterActivity.java +++ b/app/src/main/java/ca/ualberta/cs/lonelytwitter/LonelyTwitterActivity.java @@ -12,6 +12,7 @@ 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; @@ -28,12 +29,15 @@ public class LonelyTwitterActivity extends Activity { @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); Button saveButton = (Button) findViewById(R.id.save); oldTweetsList = (ListView) findViewById(R.id.oldTweetsList); + + saveButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { @@ -48,12 +52,45 @@ public void onClick(View v) { @Override protected void onStart() { + // TODO Auto-generated method stub super.onStart(); + Log.i("LifeCycle --->", "onStart is called"); String[] tweets = loadFromFile(); ArrayAdapter adapter = new ArrayAdapter(this, R.layout.list_item, tweets); oldTweetsList.setAdapter(adapter); + + NormalTweet normalTweet = new NormalTweet(""); + try { + normalTweet.setMessage("Hello World!"); +// normalTweet.setMessage("aaaaaaaa123904ifkdjfhirhtiorhtherihgtjkerhgjkhergtjkherjkh49ryhui4thg754tdfjlsdkhioerhgiohruioghejiogfuioerhgfiohrjkfhasdjkhgiuhgiorgjkdfhgidfgsdkljfkldsjfklsdjklfjdklsfjkldghsk"); + } + catch (TweetTooLongException e) { + Log.e("Error ---->", "Tweet message too long"); + + } + + + ImportantTweet importantTweet1 = new ImportantTweet("Hello World! This is important"); + ImportantTweet importantTweet2 = new ImportantTweet("This is another important tweet"); + + NormalTweet normalTweet1 = new NormalTweet("This is not that important"); + NormalTweet normalTweet2 = new NormalTweet("This is not that important either"); + + ArrayList tweetList = new ArrayList(); + tweetList.add(normalTweet); + tweetList.add(normalTweet1); + tweetList.add(normalTweet2); + tweetList.add(importantTweet1); + tweetList.add(importantTweet2); + + for (Tweet t: + tweetList) { + Log.d("Tweet Polymorphism", t.isImportant().toString()); + + } + } private String[] loadFromFile() { @@ -92,4 +129,10 @@ private void saveInFile(String text, Date date) { e.printStackTrace(); } } + + @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..f603a29ec1 --- /dev/null +++ b/app/src/main/java/ca/ualberta/cs/lonelytwitter/Tweet.java @@ -0,0 +1,47 @@ +package ca.ualberta.cs.lonelytwitter; + +import java.util.Date; + +/** + * Created by dezfuli on 1/16/18. + */ + +public abstract class Tweet { + 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(); +} 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..63971d853e --- /dev/null +++ b/app/src/main/java/ca/ualberta/cs/lonelytwitter/Tweetable.java @@ -0,0 +1,8 @@ +package ca.ualberta.cs.lonelytwitter; + +/** + * Created by dezfuli on 1/16/18. + */ + +public interface Tweetable { +} diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000000..95517a5d60 --- /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-2.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..f6c1aa1a90 100644 --- a/lonelyTwitter.iml +++ b/lonelyTwitter.iml @@ -1,5 +1,5 @@ - + @@ -13,7 +13,7 @@ - + \ No newline at end of file From bd026b01f3c1d2849a7d04c0dc791a64d58549cf Mon Sep 17 00:00:00 2001 From: ssd352 <27953063+ssd352@users.noreply.github.com> Date: Tue, 16 Jan 2018 19:11:26 -0700 Subject: [PATCH 2/4] Add interface --- .idea/workspace.xml | 62 +++++++++---------- .../lonelytwitter/LonelyTwitterActivity.java | 16 +++++ .../ca/ualberta/cs/lonelytwitter/Tweet.java | 2 +- .../ualberta/cs/lonelytwitter/Tweetable.java | 3 + 4 files changed, 51 insertions(+), 32 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index f1c7bbffc1..599d9dcbec 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -12,15 +12,10 @@ - - - - - - + - - + + @@ -57,28 +52,30 @@ - - + + - + - - - + + + + + - + - - + + @@ -1386,9 +1383,10 @@ @@ -1963,14 +1961,6 @@ - - - - - - - - @@ -2001,21 +1991,31 @@ - + - - + + - - + + + + + + + + + + + + \ No newline at end of file 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 fcec3d0a40..9cdfde4e79 100644 --- a/app/src/main/java/ca/ualberta/cs/lonelytwitter/LonelyTwitterActivity.java +++ b/app/src/main/java/ca/ualberta/cs/lonelytwitter/LonelyTwitterActivity.java @@ -18,6 +18,7 @@ import android.widget.Button; import android.widget.EditText; import android.widget.ListView; +import android.widget.Toast; public class LonelyTwitterActivity extends Activity { @@ -91,6 +92,21 @@ protected void onStart() { } + + ArrayList tweetableList = new ArrayList(); + tweetableList.add(normalTweet); + tweetableList.add(normalTweet1); + tweetableList.add(normalTweet2); + tweetableList.add(importantTweet1); + tweetableList.add(importantTweet2); + + String messageOnScreen = ""; + for (Tweetable t: + tweetableList) { + messageOnScreen += t.getMessage() + "\n"; + } + Toast.makeText(this, messageOnScreen, Toast.LENGTH_SHORT).show(); + } private String[] loadFromFile() { diff --git a/app/src/main/java/ca/ualberta/cs/lonelytwitter/Tweet.java b/app/src/main/java/ca/ualberta/cs/lonelytwitter/Tweet.java index f603a29ec1..f3778ed5e4 100644 --- a/app/src/main/java/ca/ualberta/cs/lonelytwitter/Tweet.java +++ b/app/src/main/java/ca/ualberta/cs/lonelytwitter/Tweet.java @@ -6,7 +6,7 @@ * Created by dezfuli on 1/16/18. */ -public abstract class Tweet { +public abstract class Tweet implements Tweetable { private String message; private Date date; diff --git a/app/src/main/java/ca/ualberta/cs/lonelytwitter/Tweetable.java b/app/src/main/java/ca/ualberta/cs/lonelytwitter/Tweetable.java index 63971d853e..0c42d332bb 100644 --- a/app/src/main/java/ca/ualberta/cs/lonelytwitter/Tweetable.java +++ b/app/src/main/java/ca/ualberta/cs/lonelytwitter/Tweetable.java @@ -3,6 +3,9 @@ /** * Created by dezfuli on 1/16/18. */ +import java.util.Date; public interface Tweetable { + public String getMessage(); + public Date getDate(); } From 1a094d239a6a2b28d2cf550286e52ccd5f13e45a Mon Sep 17 00:00:00 2001 From: ta301fall2017 Date: Tue, 23 Jan 2018 17:50:24 -0700 Subject: [PATCH 3/4] w2018 lab 3 --- .../com_google_code_gson_gson_2_2_4_jar.xml | 9 + .idea/misc.xml | 5 +- .idea/workspace.xml | 1395 +++++++++++------ app/app.iml | 52 +- app/build.gradle | 5 +- .../lonelytwitter/LonelyTwitterActivity.java | 114 +- .../ca/ualberta/cs/lonelytwitter/Tweet.java | 5 + build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 2 +- lonelyTwitter.iml | 2 +- 10 files changed, 1056 insertions(+), 535 deletions(-) create mode 100644 .idea/libraries/com_google_code_gson_gson_2_2_4_jar.xml 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 index 16c4ff56a2..d9794b00f8 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,8 +1,5 @@ - - - - + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 599d9dcbec..903006b96f 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -12,10 +12,16 @@ + + + + - + + + @@ -48,13 +54,16 @@ - + - - - + + + + + + @@ -62,20 +71,25 @@ - - + + + + - - - - - + + + + + + + + @@ -91,18 +105,23 @@ + + + tweets + + \ 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/LonelyTwitterActivity.java b/app/src/main/java/ca/ualberta/cs/lonelytwitter/LonelyTwitterActivity.java index 9cdfde4e79..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,11 +1,14 @@ 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; @@ -20,11 +23,17 @@ 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 @@ -37,16 +46,18 @@ public void onCreate(Bundle savedInstanceState) { Button saveButton = (Button) findViewById(R.id.save); oldTweetsList = (ListView) findViewById(R.id.oldTweetsList); - - saveButton.setOnClickListener(new View.OnClickListener() { 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(); } }); } @@ -57,92 +68,53 @@ protected void onStart() { // TODO Auto-generated method stub super.onStart(); Log.i("LifeCycle --->", "onStart is called"); - String[] tweets = loadFromFile(); - ArrayAdapter adapter = new ArrayAdapter(this, - R.layout.list_item, tweets); - oldTweetsList.setAdapter(adapter); - - NormalTweet normalTweet = new NormalTweet(""); - try { - normalTweet.setMessage("Hello World!"); -// normalTweet.setMessage("aaaaaaaa123904ifkdjfhirhtiorhtherihgtjkerhgjkhergtjkherjkh49ryhui4thg754tdfjlsdkhioerhgiohruioghejiogfuioerhgfiohrjkfhasdjkhgiuhgiorgjkdfhgidfgsdkljfkldsjfklsdjklfjdklsfjkldghsk"); - } - catch (TweetTooLongException e) { - Log.e("Error ---->", "Tweet message too long"); - - } - - - ImportantTweet importantTweet1 = new ImportantTweet("Hello World! This is important"); - ImportantTweet importantTweet2 = new ImportantTweet("This is another important tweet"); - - NormalTweet normalTweet1 = new NormalTweet("This is not that important"); - NormalTweet normalTweet2 = new NormalTweet("This is not that important either"); - - ArrayList tweetList = new ArrayList(); - tweetList.add(normalTweet); - tweetList.add(normalTweet1); - tweetList.add(normalTweet2); - tweetList.add(importantTweet1); - tweetList.add(importantTweet2); - - for (Tweet t: - tweetList) { - Log.d("Tweet Polymorphism", t.isImportant().toString()); - } + loadFromFile(); - - ArrayList tweetableList = new ArrayList(); - tweetableList.add(normalTweet); - tweetableList.add(normalTweet1); - tweetableList.add(normalTweet2); - tweetableList.add(importantTweet1); - tweetableList.add(importantTweet2); - - String messageOnScreen = ""; - for (Tweetable t: - tweetableList) { - messageOnScreen += t.getMessage() + "\n"; - } - Toast.makeText(this, messageOnScreen, Toast.LENGTH_SHORT).show(); + 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(); } } diff --git a/app/src/main/java/ca/ualberta/cs/lonelytwitter/Tweet.java b/app/src/main/java/ca/ualberta/cs/lonelytwitter/Tweet.java index f3778ed5e4..96362b9d0f 100644 --- a/app/src/main/java/ca/ualberta/cs/lonelytwitter/Tweet.java +++ b/app/src/main/java/ca/ualberta/cs/lonelytwitter/Tweet.java @@ -44,4 +44,9 @@ public void setDate(Date date){ } public abstract Boolean isImportant(); + + public String toString() { + return date.toString() + " | " + message; + } + } 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 index 95517a5d60..b1ef018f8f 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.2-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.2-all.zip diff --git a/lonelyTwitter.iml b/lonelyTwitter.iml index f6c1aa1a90..fbd182a91c 100644 --- a/lonelyTwitter.iml +++ b/lonelyTwitter.iml @@ -13,7 +13,7 @@ - + \ No newline at end of file From 5fac13650c78ef7a0bed79957520e0b31eb7f4f4 Mon Sep 17 00:00:00 2001 From: ta301fall2017 Date: Tue, 23 Jan 2018 17:53:26 -0700 Subject: [PATCH 4/4] w2018 lab 3 --- .idea/workspace.xml | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 903006b96f..bba039f783 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -11,18 +11,7 @@