From 78b0003cc778aa5f91f8385206ee1345ad4ea191 Mon Sep 17 00:00:00 2001 From: torrentcome Date: Wed, 28 Jun 2017 19:33:54 +0200 Subject: [PATCH] (db) insert + read --- .../keylesspalace/tusky/ComposeActivity.java | 58 ++++++++++++++++++- .../keylesspalace/tusky/TuskyApplication.java | 14 ++++- 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java index 865c18ed..ed8d5eae 100644 --- a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java @@ -76,6 +76,8 @@ 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.entity.Account; import com.keylesspalace.tusky.entity.Media; import com.keylesspalace.tusky.entity.Status; @@ -153,6 +155,9 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFragm private int currentFlags; private Uri photoUploadUri; + private TootDao tootDao = TuskyApplication.getDB().tootDao(); + + /** * The Target object must be stored as a member field or method and cannot be an anonymous class otherwise this won't work as expected. The reason is that Picasso accepts this parameter as a weak memory reference. Because anonymous classes are eligible for garbage collection when there are no more references, the network request to fetch the image may finish after this anonymous class has already been reclaimed. See this Stack Overflow discussion for more details. */ @@ -176,6 +181,8 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFragm visibilityBtn = (ImageButton) findViewById(R.id.action_toggle_visibility); postProgress = (ProgressBar) findViewById(R.id.postProgress); + getTheToot(); + // Setup the toolbar. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); @@ -444,7 +451,7 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFragm } private void doErrorDialog(@StringRes int descriptionId, @StringRes int actionId, - View.OnClickListener listener) { + View.OnClickListener listener) { Snackbar bar = Snackbar.make(findViewById(R.id.activity_compose), getString(descriptionId), Snackbar.LENGTH_SHORT); bar.setAction(actionId, listener); @@ -1206,6 +1213,51 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFragm return super.onOptionsItemSelected(item); } + + @Override + public void onBackPressed() { + saveTheToot(); + 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) { if (!TextUtils.isEmpty(headerInfo.title)) { @@ -1352,7 +1404,9 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFragm private class MentionAutoCompleteAdapter extends ArrayAdapter implements Filterable { private ArrayList resultList; - private @LayoutRes int layoutId; + private + @LayoutRes + int layoutId; MentionAutoCompleteAdapter(Context context, @LayoutRes int resource) { super(context, resource); diff --git a/app/src/main/java/com/keylesspalace/tusky/TuskyApplication.java b/app/src/main/java/com/keylesspalace/tusky/TuskyApplication.java index 4b0996c9..877473fe 100644 --- a/app/src/main/java/com/keylesspalace/tusky/TuskyApplication.java +++ b/app/src/main/java/com/keylesspalace/tusky/TuskyApplication.java @@ -16,13 +16,18 @@ package com.keylesspalace.tusky; import android.app.Application; +import android.arch.persistence.room.Room; import android.net.Uri; +import com.jakewharton.picasso.OkHttp3Downloader; +import com.keylesspalace.tusky.db.AppDatabase; import com.keylesspalace.tusky.util.OkHttpUtils; import com.squareup.picasso.Picasso; -import com.jakewharton.picasso.OkHttp3Downloader; public class TuskyApplication extends Application { + + private static AppDatabase db; + @Override public void onCreate() { super.onCreate(); @@ -47,5 +52,12 @@ public class TuskyApplication extends Application { if (BuildConfig.DEBUG) { Picasso.with(this).setLoggingEnabled(true); } + + db = Room.databaseBuilder(getApplicationContext(), + AppDatabase.class, "tuskyDB").build(); + } + + public static AppDatabase getDB() { + return db; } } \ No newline at end of file