(db) insert + read

main
torrentcome 7 years ago
parent 464296c9af
commit 78b0003cc7
  1. 58
      app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java
  2. 14
      app/src/main/java/com/keylesspalace/tusky/TuskyApplication.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<Void, Void, List<TootEntity>>() {
@Override
protected List<TootEntity> doInBackground(Void... params) {
return tootDao.loadAll();
}
@Override
protected void onPostExecute(List<TootEntity> 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<Void, Void, Long>() {
@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<Account> implements Filterable {
private ArrayList<Account> resultList;
private @LayoutRes int layoutId;
private
@LayoutRes
int layoutId;
MentionAutoCompleteAdapter(Context context, @LayoutRes int resource) {
super(context, resource);

@ -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;
}
}
Loading…
Cancel
Save