From 3928febf5bdfbf0f085075347d0be234a859aa76 Mon Sep 17 00:00:00 2001 From: torrentcome Date: Thu, 29 Jun 2017 12:03:44 +0200 Subject: [PATCH] (clean) keep it simple , don't polluted ComposeActivity, commented and moving the code --- .../keylesspalace/tusky/ComposeActivity.java | 44 ++-------------- .../keylesspalace/tusky/db/AppDatabase.java | 2 +- .../keylesspalace/tusky/db/MediaEntity.java | 39 ++++++++++++-- .../keylesspalace/tusky/db/TootAction.java | 51 +++++++++++++++++++ .../keylesspalace/tusky/db/TootEntity.java | 4 +- 5 files changed, 91 insertions(+), 49 deletions(-) create mode 100644 app/src/main/java/com/keylesspalace/tusky/db/TootAction.java diff --git a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java index ed8d5eae..7a5b4057 100644 --- a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java @@ -76,8 +76,7 @@ import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.TextView; -import com.keylesspalace.tusky.db.TootDao; -import com.keylesspalace.tusky.db.TootEntity; +import com.keylesspalace.tusky.db.TootAction; import com.keylesspalace.tusky.entity.Account; import com.keylesspalace.tusky.entity.Media; import com.keylesspalace.tusky.entity.Status; @@ -155,7 +154,6 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFragm private int currentFlags; private Uri photoUploadUri; - private TootDao tootDao = TuskyApplication.getDB().tootDao(); /** @@ -181,7 +179,7 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFragm visibilityBtn = (ImageButton) findViewById(R.id.action_toggle_visibility); postProgress = (ProgressBar) findViewById(R.id.postProgress); - getTheToot(); + TootAction.getAllToot(); // Setup the toolbar. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); @@ -1216,47 +1214,11 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFragm @Override public void onBackPressed() { - saveTheToot(); + TootAction.saveTheToot(textEditor.getText().toString()); super.onBackPressed(); } - private void getTheToot() { - new AsyncTask>() { - @Override - protected List doInBackground(Void... params) { - return tootDao.loadAll(); - } - - @Override - protected void onPostExecute(List tootEntities) { - super.onPostExecute(tootEntities); - for (TootEntity t : tootEntities) { - Log.e("toot", "id=" + t.getUid() + "text=" + t.getText()); - } - } - }.execute(); - } - - private void saveTheToot() { - final TootEntity toot = new TootEntity(); - toot.setText(textEditor.getText().toString()); - new AsyncTask() { - @Override - protected Long doInBackground(Void... params) { - long tootId = tootDao.insert(toot); - if(!mediaQueued.isEmpty()){ - - } - return -1L; - } - @Override - protected void onPostExecute(Long aLong) { - super.onPostExecute(aLong); - - } - }.execute(); - } @Override public void onReceiveHeaderInfo(ParserUtils.HeaderInfo headerInfo) { diff --git a/app/src/main/java/com/keylesspalace/tusky/db/AppDatabase.java b/app/src/main/java/com/keylesspalace/tusky/db/AppDatabase.java index f39f29b7..00871429 100644 --- a/app/src/main/java/com/keylesspalace/tusky/db/AppDatabase.java +++ b/app/src/main/java/com/keylesspalace/tusky/db/AppDatabase.java @@ -4,7 +4,7 @@ import android.arch.persistence.room.Database; import android.arch.persistence.room.RoomDatabase; /** - * Created by cto3543 on 28/06/2017. + * DB version & declare DAO */ @Database(entities = {TootEntity.class}, version = 2, exportSchema = false) diff --git a/app/src/main/java/com/keylesspalace/tusky/db/MediaEntity.java b/app/src/main/java/com/keylesspalace/tusky/db/MediaEntity.java index b23e8f42..398b1d1c 100644 --- a/app/src/main/java/com/keylesspalace/tusky/db/MediaEntity.java +++ b/app/src/main/java/com/keylesspalace/tusky/db/MediaEntity.java @@ -2,17 +2,46 @@ package com.keylesspalace.tusky.db; import android.arch.persistence.room.ColumnInfo; import android.arch.persistence.room.Entity; +import android.arch.persistence.room.ForeignKey; import android.arch.persistence.room.PrimaryKey; /** - * Created by cto3543 on 28/06/2017. + * Media model */ -@Entity +@Entity(foreignKeys = @ForeignKey(entity = TootEntity.class, + parentColumns = "uid", + childColumns = "toot_id")) public class MediaEntity { - @PrimaryKey + @ColumnInfo(name = "toot_id") + private int toot_id; + @PrimaryKey(autoGenerate = true) private int uid; - @ColumnInfo(name = "url") - private String text; + private String url; + + // getter setter + public int getToot_id() { + return toot_id; + } + + public void setToot_id(int toot_id) { + this.toot_id = toot_id; + } + + public int getUid() { + return uid; + } + + public void setUid(int uid) { + this.uid = uid; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } } diff --git a/app/src/main/java/com/keylesspalace/tusky/db/TootAction.java b/app/src/main/java/com/keylesspalace/tusky/db/TootAction.java new file mode 100644 index 00000000..7232dfa4 --- /dev/null +++ b/app/src/main/java/com/keylesspalace/tusky/db/TootAction.java @@ -0,0 +1,51 @@ +package com.keylesspalace.tusky.db; + +import android.os.AsyncTask; +import android.text.TextUtils; +import android.util.Log; + +import com.keylesspalace.tusky.TuskyApplication; + +import java.util.List; + +/** + * Created by cto3543 on 29/06/2017. + * + */ + +public class TootAction { + + private static TootDao tootDao = TuskyApplication.getDB().tootDao(); + + public static void getAllToot() { + new AsyncTask>() { + @Override + protected List doInBackground(Void... params) { + return tootDao.loadAll(); + } + + @Override + protected void onPostExecute(List tootEntities) { + super.onPostExecute(tootEntities); + for (TootEntity t : tootEntities) { + Log.e("toot", "id=" + t.getUid() + "text=" + t.getText()); + } + } + }.execute(); + } + + public static void saveTheToot(String s) { + if (!TextUtils.isEmpty(s)) { + final TootEntity toot = new TootEntity(); + toot.setText(s); + new AsyncTask() { + @Override + protected Void doInBackground(Void... params) { + tootDao.insert(toot); + return null; + } + }.execute(); + } + } + +} diff --git a/app/src/main/java/com/keylesspalace/tusky/db/TootEntity.java b/app/src/main/java/com/keylesspalace/tusky/db/TootEntity.java index 4fc0f8e2..4323c957 100644 --- a/app/src/main/java/com/keylesspalace/tusky/db/TootEntity.java +++ b/app/src/main/java/com/keylesspalace/tusky/db/TootEntity.java @@ -5,7 +5,7 @@ import android.arch.persistence.room.Entity; import android.arch.persistence.room.PrimaryKey; /** - * Created by cto3543 on 28/06/2017. + * toot model */ @Entity @@ -16,7 +16,7 @@ public class TootEntity { @ColumnInfo(name = "text") private String text; - // set get + // getter setter public String getText() { return text; }