From e649f412d3b7b0285985835e0ca4e96179ca848c Mon Sep 17 00:00:00 2001 From: torrentcome Date: Wed, 28 Jun 2017 19:32:50 +0200 Subject: [PATCH 01/19] (room) add in gradle config --- app/build.gradle | 3 +++ build.gradle | 1 + 2 files changed, 4 insertions(+) diff --git a/app/build.gradle b/app/build.gradle index 20c3efab..69dae628 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -54,4 +54,7 @@ dependencies { exclude module: 'support-v4' } testCompile 'junit:junit:4.12' + compile "android.arch.persistence.room:runtime:1.0.0-alpha3" + annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha3" + } diff --git a/build.gradle b/build.gradle index 0a26ece5..eb17a91d 100644 --- a/build.gradle +++ b/build.gradle @@ -16,6 +16,7 @@ allprojects { repositories { jcenter() maven { url "https://jitpack.io" } + maven { url 'https://maven.google.com' } } } From 2642c4432c061fbf0cecbd143637b445db052d0b Mon Sep 17 00:00:00 2001 From: torrentcome Date: Wed, 28 Jun 2017 19:33:20 +0200 Subject: [PATCH 02/19] (db) create CRUD model --- .../keylesspalace/tusky/db/AppDatabase.java | 13 +++++++ .../keylesspalace/tusky/db/MediaEntity.java | 18 ++++++++++ .../com/keylesspalace/tusky/db/TootDao.java | 30 ++++++++++++++++ .../keylesspalace/tusky/db/TootEntity.java | 35 +++++++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 app/src/main/java/com/keylesspalace/tusky/db/AppDatabase.java create mode 100644 app/src/main/java/com/keylesspalace/tusky/db/MediaEntity.java create mode 100644 app/src/main/java/com/keylesspalace/tusky/db/TootDao.java create mode 100644 app/src/main/java/com/keylesspalace/tusky/db/TootEntity.java diff --git a/app/src/main/java/com/keylesspalace/tusky/db/AppDatabase.java b/app/src/main/java/com/keylesspalace/tusky/db/AppDatabase.java new file mode 100644 index 00000000..f39f29b7 --- /dev/null +++ b/app/src/main/java/com/keylesspalace/tusky/db/AppDatabase.java @@ -0,0 +1,13 @@ +package com.keylesspalace.tusky.db; + +import android.arch.persistence.room.Database; +import android.arch.persistence.room.RoomDatabase; + +/** + * Created by cto3543 on 28/06/2017. + */ + +@Database(entities = {TootEntity.class}, version = 2, exportSchema = false) +abstract public class AppDatabase extends RoomDatabase { + public abstract TootDao tootDao(); +} diff --git a/app/src/main/java/com/keylesspalace/tusky/db/MediaEntity.java b/app/src/main/java/com/keylesspalace/tusky/db/MediaEntity.java new file mode 100644 index 00000000..b23e8f42 --- /dev/null +++ b/app/src/main/java/com/keylesspalace/tusky/db/MediaEntity.java @@ -0,0 +1,18 @@ +package com.keylesspalace.tusky.db; + +import android.arch.persistence.room.ColumnInfo; +import android.arch.persistence.room.Entity; +import android.arch.persistence.room.PrimaryKey; + +/** + * Created by cto3543 on 28/06/2017. + */ + +@Entity +public class MediaEntity { + @PrimaryKey + private int uid; + + @ColumnInfo(name = "url") + private String text; +} diff --git a/app/src/main/java/com/keylesspalace/tusky/db/TootDao.java b/app/src/main/java/com/keylesspalace/tusky/db/TootDao.java new file mode 100644 index 00000000..39ea0e5e --- /dev/null +++ b/app/src/main/java/com/keylesspalace/tusky/db/TootDao.java @@ -0,0 +1,30 @@ +package com.keylesspalace.tusky.db; + +import android.arch.persistence.room.Dao; +import android.arch.persistence.room.Delete; +import android.arch.persistence.room.Insert; +import android.arch.persistence.room.Query; + +import java.util.List; + +/** + * Created by cto3543 on 28/06/2017. + */ + +@Dao +public interface TootDao { + @Query("SELECT * FROM TootEntity") + List loadAll(); + + @Query("SELECT * FROM TootEntity WHERE uid IN (:uid)") + List loadAllByUserId(int... uid); + + @Insert + long insert(TootEntity users); + + @Insert + void insertAll(TootEntity... users); + + @Delete + void delete(TootEntity user); +} diff --git a/app/src/main/java/com/keylesspalace/tusky/db/TootEntity.java b/app/src/main/java/com/keylesspalace/tusky/db/TootEntity.java new file mode 100644 index 00000000..4fc0f8e2 --- /dev/null +++ b/app/src/main/java/com/keylesspalace/tusky/db/TootEntity.java @@ -0,0 +1,35 @@ +package com.keylesspalace.tusky.db; + +import android.arch.persistence.room.ColumnInfo; +import android.arch.persistence.room.Entity; +import android.arch.persistence.room.PrimaryKey; + +/** + * Created by cto3543 on 28/06/2017. + */ + +@Entity +public class TootEntity { + @PrimaryKey(autoGenerate = true) + private int uid; + + @ColumnInfo(name = "text") + private String text; + + // set get + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public int getUid() { + return uid; + } + + public void setUid(int uid) { + this.uid = uid; + } +} From fec8e5c47c0d05224aa5eed519f5f0a3a4aab492 Mon Sep 17 00:00:00 2001 From: torrentcome Date: Wed, 28 Jun 2017 19:33:54 +0200 Subject: [PATCH 03/19] (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 From 9d21c36758f1a778a79e1dca8429574d4cd76bea Mon Sep 17 00:00:00 2001 From: torrentcome Date: Thu, 29 Jun 2017 12:01:14 +0200 Subject: [PATCH 04/19] (dao) crud interface --- .../com/keylesspalace/tusky/db/TootDao.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/db/TootDao.java b/app/src/main/java/com/keylesspalace/tusky/db/TootDao.java index 39ea0e5e..373f7b94 100644 --- a/app/src/main/java/com/keylesspalace/tusky/db/TootDao.java +++ b/app/src/main/java/com/keylesspalace/tusky/db/TootDao.java @@ -4,27 +4,33 @@ import android.arch.persistence.room.Dao; import android.arch.persistence.room.Delete; import android.arch.persistence.room.Insert; import android.arch.persistence.room.Query; +import android.arch.persistence.room.Update; import java.util.List; /** * Created by cto3543 on 28/06/2017. + * crud interface on this Toot DB */ @Dao public interface TootDao { + // c + @Insert + long insert(TootEntity users); + + // r @Query("SELECT * FROM TootEntity") List loadAll(); @Query("SELECT * FROM TootEntity WHERE uid IN (:uid)") - List loadAllByUserId(int... uid); + List loadAllByTootId(int... uid); - @Insert - long insert(TootEntity users); - - @Insert - void insertAll(TootEntity... users); + // u + @Update + void updateToot(TootEntity... toot); + // d @Delete void delete(TootEntity user); } From f79445f50aa158ac4cb1c448c3af1f5179e17018 Mon Sep 17 00:00:00 2001 From: torrentcome Date: Thu, 29 Jun 2017 12:03:44 +0200 Subject: [PATCH 05/19] (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; } From 946f58439c3a27b7540f36461e5fe5953389e3e9 Mon Sep 17 00:00:00 2001 From: torrentcome Date: Thu, 29 Jun 2017 17:55:39 +0200 Subject: [PATCH 06/19] (saved_toot) add part of saved toot activity --- app/src/main/AndroidManifest.xml | 4 + .../tusky/SavedTootActivity.java | 84 +++++++++++ .../tusky/adapter/SavedTootAdapter.java | 137 ++++++++++++++++++ .../main/res/layout/activity_saved_toot.xml | 28 ++++ app/src/main/res/layout/item_saved_toot.xml | 24 +++ app/src/main/res/values/strings.xml | 4 + 6 files changed, 281 insertions(+) create mode 100644 app/src/main/java/com/keylesspalace/tusky/SavedTootActivity.java create mode 100644 app/src/main/java/com/keylesspalace/tusky/adapter/SavedTootAdapter.java create mode 100644 app/src/main/res/layout/activity_saved_toot.xml create mode 100644 app/src/main/res/layout/item_saved_toot.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1bb3c925..bafc4abd 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -26,6 +26,10 @@ + + diff --git a/app/src/main/java/com/keylesspalace/tusky/SavedTootActivity.java b/app/src/main/java/com/keylesspalace/tusky/SavedTootActivity.java new file mode 100644 index 00000000..f33ef5b3 --- /dev/null +++ b/app/src/main/java/com/keylesspalace/tusky/SavedTootActivity.java @@ -0,0 +1,84 @@ +/* Copyright 2017 Andrew Dawson + * + * This file is a part of Tusky. + * + * This program is free software; you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation; either version 3 of the + * License, or (at your option) any later version. + * + * Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even + * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. + * + * You should have received a copy of the GNU General Public License along with Tusky; if not, + * see . */ + +package com.keylesspalace.tusky; + +import android.graphics.drawable.Drawable; +import android.os.AsyncTask; +import android.os.Bundle; +import android.support.v7.widget.DividerItemDecoration; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.Toolbar; +import android.util.Log; + +import com.keylesspalace.tusky.adapter.SavedTootAdapter; +import com.keylesspalace.tusky.db.TootDao; +import com.keylesspalace.tusky.db.TootEntity; +import com.keylesspalace.tusky.util.ThemeUtils; + +import java.util.List; + +public class SavedTootActivity extends BaseActivity { + + // dao + private static TootDao tootDao = TuskyApplication.getDB().tootDao(); + + // ui + private SavedTootAdapter adapter; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_saved_toot); + + Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); + setSupportActionBar(toolbar); + + RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); + recyclerView.setHasFixedSize(true); + LinearLayoutManager layoutManager = new LinearLayoutManager(this); + recyclerView.setLayoutManager(layoutManager); + DividerItemDecoration divider = new DividerItemDecoration( + this, layoutManager.getOrientation()); + Drawable drawable = ThemeUtils.getDrawable(this, R.attr.status_divider_drawable, + R.drawable.status_divider_dark); + divider.setDrawable(drawable); + recyclerView.addItemDecoration(divider); + adapter = new SavedTootAdapter(); + recyclerView.setAdapter(adapter); + + getAllToot(); + } + + public 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()); + } + adapter.addItems(tootEntities); + } + }.execute(); + } + +} diff --git a/app/src/main/java/com/keylesspalace/tusky/adapter/SavedTootAdapter.java b/app/src/main/java/com/keylesspalace/tusky/adapter/SavedTootAdapter.java new file mode 100644 index 00000000..c8005347 --- /dev/null +++ b/app/src/main/java/com/keylesspalace/tusky/adapter/SavedTootAdapter.java @@ -0,0 +1,137 @@ +/* Copyright 2017 Andrew Dawson + * + * This file is a part of Tusky. + * + * This program is free software; you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation; either version 3 of the + * License, or (at your option) any later version. + * + * Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even + * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. + * + * You should have received a copy of the GNU General Public License along with Tusky; if not, + * see . */ + +package com.keylesspalace.tusky.adapter; + +import android.support.annotation.Nullable; +import android.support.v7.widget.RecyclerView; +import android.text.TextUtils; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageButton; +import android.widget.TextView; + +import com.keylesspalace.tusky.R; +import com.keylesspalace.tusky.db.TootEntity; + +import java.util.ArrayList; +import java.util.List; + +public class SavedTootAdapter extends RecyclerView.Adapter { + private List list; + + public SavedTootAdapter() { + super(); + list = new ArrayList<>(); + } + + @Override + public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + View view = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.item_saved_toot, parent, false); + return new TootViewHolder(view); + } + + @Override + public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) { + TootViewHolder holder = (TootViewHolder) viewHolder; + holder.bind(getItem(position)); + } + + @Override + public int getItemCount() { + return list.size() + 1; + } + + public void update(List newToot) { + if (newToot == null || newToot.isEmpty()) { + return; + } + if (list.isEmpty()) { + list = newToot; + } else { + int index = list.indexOf(newToot.get(newToot.size() - 1)); + for (int i = 0; i < index; i++) { + list.remove(0); + } + int newIndex = newToot.indexOf(list.get(0)); + if (newIndex == -1) { + list.addAll(0, newToot); + } else { + list.addAll(0, newToot.subList(0, newIndex)); + } + } + notifyDataSetChanged(); + } + + public void addItems(List newToot) { + int end = list.size(); + list.addAll(newToot); + notifyItemRangeInserted(end, newToot.size()); + } + + @Nullable + public TootEntity removeItem(int position) { + if (position < 0 || position >= list.size()) { + return null; + } + TootEntity toot = list.remove(position); + notifyItemRemoved(position); + return toot; + } + + public void addItem(TootEntity toot, int position) { + if (position < 0 || position > list.size()) { + return; + } + list.add(position, toot); + notifyItemInserted(position); + } + + public TootEntity getItem(int position) { + if (position >= 0 && position < list.size()) { + return list.get(position); + } + return null; + } + + public static class ViewHolder extends RecyclerView.ViewHolder { + TextView mTextView; + + public ViewHolder(TextView v) { + super(v); + mTextView = v; + } + } + + private static class TootViewHolder extends RecyclerView.ViewHolder { + public TextView content; + public ImageButton suppr; + + TootViewHolder(View view) { + super(view); + content = (TextView) view.findViewById(R.id.content); + suppr = (ImageButton) view.findViewById(R.id.suppr); + } + + public void bind(TootEntity item) { + if (item != null && !TextUtils.isEmpty(item.getText())) + content.setText(item.getText()); + else + content.setText(""); + } + } +} diff --git a/app/src/main/res/layout/activity_saved_toot.xml b/app/src/main/res/layout/activity_saved_toot.xml new file mode 100644 index 00000000..a4575d17 --- /dev/null +++ b/app/src/main/res/layout/activity_saved_toot.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_saved_toot.xml b/app/src/main/res/layout/item_saved_toot.xml new file mode 100644 index 00000000..089a993f --- /dev/null +++ b/app/src/main/res/layout/item_saved_toot.xml @@ -0,0 +1,24 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d77e6152..b1bc44b7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -101,6 +101,7 @@ Accept Reject Search + Saved Toot Share toot URL to… Share toot to… @@ -194,4 +195,7 @@ Video Follow requested + + %1$s %2$s + From 2022705282cf58bc51e37fb4586b997f8a4fe590 Mon Sep 17 00:00:00 2001 From: torrentcome Date: Thu, 29 Jun 2017 17:56:36 +0200 Subject: [PATCH 07/19] (menu) change menu array to list laster we will put some logic for show or not the savedToot part --- .../com/keylesspalace/tusky/MainActivity.java | 43 +++++++++++-------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/MainActivity.java b/app/src/main/java/com/keylesspalace/tusky/MainActivity.java index 4b2958a1..7423d0f4 100644 --- a/app/src/main/java/com/keylesspalace/tusky/MainActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/MainActivity.java @@ -65,7 +65,6 @@ import retrofit2.Response; public class MainActivity extends BaseActivity { private static final String TAG = "MainActivity"; // logging tag - protected static int COMPOSE_RESULT = 1; private static final long DRAWER_ITEM_EDIT_PROFILE = 0; private static final long DRAWER_ITEM_FAVOURITES = 1; private static final long DRAWER_ITEM_MUTED_USERS = 2; @@ -75,7 +74,10 @@ public class MainActivity extends BaseActivity { private static final long DRAWER_ITEM_ABOUT = 6; private static final long DRAWER_ITEM_LOG_OUT = 7; private static final long DRAWER_ITEM_FOLLOW_REQUESTS = 8; + private static final long DRAWER_ITEM_SAVED_TOOT = 9; + protected static int COMPOSE_RESULT = 1; + public FloatingActionButton composeButton; private String loggedInAccountId; private String loggedInAccountUsername; private Stack pageHistory; @@ -83,8 +85,6 @@ public class MainActivity extends BaseActivity { private Drawer drawer; private ViewPager viewPager; - public FloatingActionButton composeButton; - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -179,7 +179,8 @@ public class MainActivity extends BaseActivity { } @Override - public void onTabReselected(TabLayout.Tab tab) {} + public void onTabReselected(TabLayout.Tab tab) { + } }); Intent intent = getIntent(); @@ -289,23 +290,28 @@ public class MainActivity extends BaseActivity { R.drawable.ic_mute_24dp, getTheme()); ThemeUtils.setDrawableTint(this, muteDrawable, R.attr.toolbar_icon_tint); + List listItem = new ArrayList<>(); + listItem.add(new PrimaryDrawerItem().withIdentifier(DRAWER_ITEM_EDIT_PROFILE).withName(getString(R.string.action_edit_profile)).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_person)); + listItem.add(new PrimaryDrawerItem().withIdentifier(DRAWER_ITEM_FAVOURITES).withName(getString(R.string.action_view_favourites)).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_star)); + listItem.add(new PrimaryDrawerItem().withIdentifier(DRAWER_ITEM_MUTED_USERS).withName(getString(R.string.action_view_mutes)).withSelectable(false).withIcon(muteDrawable)); + listItem.add(new PrimaryDrawerItem().withIdentifier(DRAWER_ITEM_BLOCKED_USERS).withName(getString(R.string.action_view_blocks)).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_block)); + listItem.add(new PrimaryDrawerItem().withIdentifier(DRAWER_ITEM_SEARCH).withName(getString(R.string.action_search)).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_search)); + listItem.add(new PrimaryDrawerItem().withIdentifier(DRAWER_ITEM_SAVED_TOOT).withName(getString(R.string.action_access_saved_toot)).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_save)); + listItem.add(new DividerDrawerItem()); + listItem.add(new SecondaryDrawerItem().withIdentifier(DRAWER_ITEM_PREFERENCES).withName(getString(R.string.action_view_preferences)).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_settings)); + listItem.add(new SecondaryDrawerItem().withIdentifier(DRAWER_ITEM_ABOUT).withName(getString(R.string.about_title_activity)).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_info)); + listItem.add(new SecondaryDrawerItem().withIdentifier(DRAWER_ITEM_LOG_OUT).withName(getString(R.string.action_logout)).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_exit_to_app)); + + IDrawerItem[] array = new IDrawerItem[listItem.size()]; + listItem.toArray(array); // fill the array + drawer = new DrawerBuilder() .withActivity(this) //.withToolbar(toolbar) .withAccountHeader(headerResult) .withHasStableIds(true) .withSelectedItem(-1) - .addDrawerItems( - new PrimaryDrawerItem().withIdentifier(DRAWER_ITEM_EDIT_PROFILE).withName(getString(R.string.action_edit_profile)).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_person), - new PrimaryDrawerItem().withIdentifier(DRAWER_ITEM_FAVOURITES).withName(getString(R.string.action_view_favourites)).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_star), - new PrimaryDrawerItem().withIdentifier(DRAWER_ITEM_MUTED_USERS).withName(getString(R.string.action_view_mutes)).withSelectable(false).withIcon(muteDrawable), - new PrimaryDrawerItem().withIdentifier(DRAWER_ITEM_BLOCKED_USERS).withName(getString(R.string.action_view_blocks)).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_block), - new PrimaryDrawerItem().withIdentifier(DRAWER_ITEM_SEARCH).withName(getString(R.string.action_search)).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_search), - new DividerDrawerItem(), - new SecondaryDrawerItem().withIdentifier(DRAWER_ITEM_PREFERENCES).withName(getString(R.string.action_view_preferences)).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_settings), - new SecondaryDrawerItem().withIdentifier(DRAWER_ITEM_ABOUT).withName(getString(R.string.about_title_activity)).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_info), - new SecondaryDrawerItem().withIdentifier(DRAWER_ITEM_LOG_OUT).withName(getString(R.string.action_logout)).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_exit_to_app) - ) + .addDrawerItems(array) .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() { @Override public boolean onItemClick(View view, int position, IDrawerItem drawerItem) { @@ -341,6 +347,9 @@ public class MainActivity extends BaseActivity { Intent intent = new Intent(MainActivity.this, AccountListActivity.class); intent.putExtra("type", AccountListActivity.Type.FOLLOW_REQUESTS); startActivity(intent); + } else if (drawerItemIdentifier == DRAWER_ITEM_SAVED_TOOT) { + Intent intent = new Intent(MainActivity.this, SavedTootActivity.class); + startActivity(intent); } } @@ -469,9 +478,9 @@ public class MainActivity extends BaseActivity { @Override public void onBackPressed() { - if(drawer != null && drawer.isDrawerOpen()) { + if (drawer != null && drawer.isDrawerOpen()) { drawer.closeDrawer(); - } else if(pageHistory.size() < 2) { + } else if (pageHistory.size() < 2) { super.onBackPressed(); } else { pageHistory.pop(); From b0f848b0f71fb00d8161f5e065ee4e8a6f39b8ce Mon Sep 17 00:00:00 2001 From: torrentcome Date: Thu, 29 Jun 2017 17:56:58 +0200 Subject: [PATCH 08/19] (db) insert toot with long click --- .../keylesspalace/tusky/ComposeActivity.java | 37 ++++++++++++++++--- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java index 7a5b4057..04935f58 100644 --- a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java @@ -75,8 +75,10 @@ import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.TextView; +import android.widget.Toast; -import com.keylesspalace.tusky.db.TootAction; +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; @@ -128,6 +130,7 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFragm private static final int PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1; private static final int COMPOSE_SUCCESS = -1; private static final int THUMBNAIL_SIZE = 128; // pixels + private static TootDao tootDao = TuskyApplication.getDB().tootDao(); private EditTextTyped textEditor; private LinearLayout mediaPreviewBar; @@ -155,7 +158,6 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFragm private Uri photoUploadUri; - /** * 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. */ @@ -179,8 +181,6 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFragm visibilityBtn = (ImageButton) findViewById(R.id.action_toggle_visibility); postProgress = (ProgressBar) findViewById(R.id.postProgress); - TootAction.getAllToot(); - // Setup the toolbar. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); @@ -201,6 +201,16 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFragm onSendClicked(); } }); + floatingBtn.setOnLongClickListener(new View.OnLongClickListener() { + @Override + public boolean onLongClick(View v) { + boolean b = saveTheToot(textEditor.getText().toString()); + if (b) { + Toast.makeText(ComposeActivity.this, "Toot saved !", Toast.LENGTH_SHORT).show(); + } + return b; + } + }); pickBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -500,6 +510,23 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFragm } } + public boolean saveTheToot(String s) { + if (TextUtils.isEmpty(s)) { + return false; + } else { + final TootEntity toot = new TootEntity(); + toot.setText(s); + new AsyncTask() { + @Override + protected Void doInBackground(Void... params) { + tootDao.insert(toot); + return null; + } + }.execute(); + return true; + } + } + private void setStatusVisibility(String visibility) { statusVisibility = visibility; switch (visibility) { @@ -1214,12 +1241,10 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFragm @Override public void onBackPressed() { - TootAction.saveTheToot(textEditor.getText().toString()); super.onBackPressed(); } - @Override public void onReceiveHeaderInfo(ParserUtils.HeaderInfo headerInfo) { if (!TextUtils.isEmpty(headerInfo.title)) { From 6ce3b98615dfc91511d5a9adf41bdeb6070cc5b9 Mon Sep 17 00:00:00 2001 From: torrentcome Date: Thu, 29 Jun 2017 17:58:01 +0200 Subject: [PATCH 09/19] (rm) tootaction ...for keep easier action , we will distribute every result inside the activity --- .../keylesspalace/tusky/db/TootAction.java | 51 ------------------- 1 file changed, 51 deletions(-) delete mode 100644 app/src/main/java/com/keylesspalace/tusky/db/TootAction.java diff --git a/app/src/main/java/com/keylesspalace/tusky/db/TootAction.java b/app/src/main/java/com/keylesspalace/tusky/db/TootAction.java deleted file mode 100644 index 7232dfa4..00000000 --- a/app/src/main/java/com/keylesspalace/tusky/db/TootAction.java +++ /dev/null @@ -1,51 +0,0 @@ -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(); - } - } - -} From 451229779f1a98e2b76388e04345a5a4896a4f27 Mon Sep 17 00:00:00 2001 From: torrentcome Date: Wed, 5 Jul 2017 16:34:59 +0200 Subject: [PATCH 10/19] (room) add room gradle file --- app/build.gradle | 3 +++ .../java/com/keylesspalace/tusky/TuskyApplication.java | 10 +++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 69dae628..6a078918 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -54,7 +54,10 @@ dependencies { exclude module: 'support-v4' } testCompile 'junit:junit:4.12' + + //room compile "android.arch.persistence.room:runtime:1.0.0-alpha3" annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha3" + compile "android.arch.persistence.room:rxjava2:1.0.0-alpha2" } diff --git a/app/src/main/java/com/keylesspalace/tusky/TuskyApplication.java b/app/src/main/java/com/keylesspalace/tusky/TuskyApplication.java index 877473fe..3a1b0b44 100644 --- a/app/src/main/java/com/keylesspalace/tusky/TuskyApplication.java +++ b/app/src/main/java/com/keylesspalace/tusky/TuskyApplication.java @@ -28,6 +28,10 @@ public class TuskyApplication extends Application { private static AppDatabase db; + public static AppDatabase getDB() { + return db; + } + @Override public void onCreate() { super.onCreate(); @@ -54,10 +58,6 @@ public class TuskyApplication extends Application { } db = Room.databaseBuilder(getApplicationContext(), - AppDatabase.class, "tuskyDB").build(); - } - - public static AppDatabase getDB() { - return db; + AppDatabase.class, "tuskyDB").allowMainThreadQueries().build(); } } \ No newline at end of file From 03769f0644b489a79b48a4e0c94b62fec0cec3c0 Mon Sep 17 00:00:00 2001 From: torrentcome Date: Wed, 5 Jul 2017 16:35:15 +0200 Subject: [PATCH 11/19] (crud) change delete stuff --- app/src/main/java/com/keylesspalace/tusky/db/TootDao.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/db/TootDao.java b/app/src/main/java/com/keylesspalace/tusky/db/TootDao.java index 373f7b94..19461984 100644 --- a/app/src/main/java/com/keylesspalace/tusky/db/TootDao.java +++ b/app/src/main/java/com/keylesspalace/tusky/db/TootDao.java @@ -32,5 +32,5 @@ public interface TootDao { // d @Delete - void delete(TootEntity user); + int delete(TootEntity user); } From 1e597fb7b199a38d268866bbac3763b89eac8997 Mon Sep 17 00:00:00 2001 From: torrentcome Date: Wed, 5 Jul 2017 16:35:33 +0200 Subject: [PATCH 12/19] (ui) more ui with list saved toot --- app/src/main/res/layout/activity_saved_toot.xml | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/app/src/main/res/layout/activity_saved_toot.xml b/app/src/main/res/layout/activity_saved_toot.xml index a4575d17..718273bc 100644 --- a/app/src/main/res/layout/activity_saved_toot.xml +++ b/app/src/main/res/layout/activity_saved_toot.xml @@ -6,7 +6,7 @@ android:layout_height="match_parent" tools:context="com.keylesspalace.tusky.AccountListActivity"> - @@ -18,11 +18,21 @@ android:background="?attr/toolbar_background_color" android:elevation="4dp" /> + + + android:layout_height="match_parent" + android:layout_below="@+id/toolbar" /> - + + \ No newline at end of file From 7a7bca565ea1c9a7083b66ab53527939af5680c0 Mon Sep 17 00:00:00 2001 From: torrentcome Date: Wed, 5 Jul 2017 16:35:41 +0200 Subject: [PATCH 13/19] (string) add string --- app/src/main/res/values/strings.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b1bc44b7..a88b42a8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -32,6 +32,7 @@ Blocked users Follow Requests Edit your profile + Saved Toot \@%s %s boosted @@ -197,5 +198,6 @@ Follow requested %1$s %2$s + no content From 87c1edb3d48fb4a5590eeb6fd537b67ed8ef1c3f Mon Sep 17 00:00:00 2001 From: torrentcome Date: Wed, 5 Jul 2017 16:36:14 +0200 Subject: [PATCH 14/19] (medias) try to add medias --- .../keylesspalace/tusky/ComposeActivity.java | 29 +++++++ .../tusky/SavedTootActivity.java | 58 ++++++++++++- .../tusky/adapter/SavedTootAdapter.java | 81 +++++++++---------- .../keylesspalace/tusky/db/AppDatabase.java | 5 +- .../keylesspalace/tusky/db/TootEntity.java | 11 +++ app/src/main/res/layout/item_saved_toot.xml | 2 + 6 files changed, 140 insertions(+), 46 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java index 04935f58..ef75d068 100644 --- a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java @@ -77,6 +77,8 @@ import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; import com.keylesspalace.tusky.db.TootDao; import com.keylesspalace.tusky.db.TootEntity; import com.keylesspalace.tusky.entity.Account; @@ -296,6 +298,22 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFragm startingContentWarning = intent.getStringExtra("content_warning"); } } + + /* If come from SavedTootActivity + * */ + String savedTootText = intent.getStringExtra("saved_toot_text"); + if (!TextUtils.isEmpty(savedTootText)) { + textEditor.append(savedTootText); + } + + String savedJsonUrls = intent.getStringExtra("saved_json_urls"); + if (!TextUtils.isEmpty(savedJsonUrls)) { + // try to redo a list of media + ArrayList playersList = new Gson().fromJson(savedJsonUrls, + new TypeToken>() { + }.getType()); + + } } /* If the currently logged in account is locked, its posts should default to private. This @@ -516,6 +534,17 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFragm } else { final TootEntity toot = new TootEntity(); toot.setText(s); + if (mediaQueued != null && mediaQueued.size() > 0) { + List list = new ArrayList<>(); + for (QueuedMedia q : + mediaQueued) { + Log.d("list", "" + q.uri); + list.add(q.uri.toString()); + } + String json = new Gson().toJson(list); + toot.setUrls(json); + } + new AsyncTask() { @Override protected Void doInBackground(Void... params) { diff --git a/app/src/main/java/com/keylesspalace/tusky/SavedTootActivity.java b/app/src/main/java/com/keylesspalace/tusky/SavedTootActivity.java index f33ef5b3..b7e0f924 100644 --- a/app/src/main/java/com/keylesspalace/tusky/SavedTootActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/SavedTootActivity.java @@ -15,14 +15,19 @@ package com.keylesspalace.tusky; +import android.content.Intent; import android.graphics.drawable.Drawable; import android.os.AsyncTask; import android.os.Bundle; +import android.support.v7.app.ActionBar; import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.util.Log; +import android.view.MenuItem; +import android.view.View; +import android.widget.TextView; import com.keylesspalace.tusky.adapter.SavedTootAdapter; import com.keylesspalace.tusky.db.TootDao; @@ -31,13 +36,14 @@ import com.keylesspalace.tusky.util.ThemeUtils; import java.util.List; -public class SavedTootActivity extends BaseActivity { +public class SavedTootActivity extends BaseActivity implements SavedTootAdapter.SavedTootAction { // dao private static TootDao tootDao = TuskyApplication.getDB().tootDao(); // ui private SavedTootAdapter adapter; + private TextView noContent; @Override protected void onCreate(Bundle savedInstanceState) { @@ -46,8 +52,15 @@ public class SavedTootActivity extends BaseActivity { Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); + ActionBar bar = getSupportActionBar(); + if (bar != null) { + bar.setTitle(getString(R.string.title_saved_toot)); + bar.setDisplayHomeAsUpEnabled(true); + bar.setDisplayShowHomeEnabled(true); + } RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); + noContent = (TextView) findViewById(R.id.no_content); recyclerView.setHasFixedSize(true); LinearLayoutManager layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); @@ -57,12 +70,24 @@ public class SavedTootActivity extends BaseActivity { R.drawable.status_divider_dark); divider.setDrawable(drawable); recyclerView.addItemDecoration(divider); - adapter = new SavedTootAdapter(); + adapter = new SavedTootAdapter(this); recyclerView.setAdapter(adapter); + // req getAllToot(); } + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: { + onBackPressed(); + return true; + } + } + return super.onOptionsItemSelected(item); + } + public void getAllToot() { new AsyncTask>() { @Override @@ -76,9 +101,38 @@ public class SavedTootActivity extends BaseActivity { for (TootEntity t : tootEntities) { Log.e("toot", "id=" + t.getUid() + "text=" + t.getText()); } + + // set ui + setNoContent(tootEntities.size()); adapter.addItems(tootEntities); } }.execute(); } + private void setNoContent(int size) { + if (size == 0) { + noContent.setVisibility(View.VISIBLE); + } else { + noContent.setVisibility(View.INVISIBLE); + } + } + + @Override + public void delete(int position, TootEntity item) { + // update DB + tootDao.delete(item); + // update adapter + if (adapter != null) { + adapter.removeItem(position); + setNoContent(adapter.getItemCount()); + } + } + + @Override + public void click(int position, TootEntity item) { + Intent intent = new Intent(this, ComposeActivity.class); + intent.putExtra("saved_toot_text", item.getText()); + intent.putExtra("saved_json_urls", item.getUrls()); + startActivity(intent); + } } diff --git a/app/src/main/java/com/keylesspalace/tusky/adapter/SavedTootAdapter.java b/app/src/main/java/com/keylesspalace/tusky/adapter/SavedTootAdapter.java index c8005347..3db08bc0 100644 --- a/app/src/main/java/com/keylesspalace/tusky/adapter/SavedTootAdapter.java +++ b/app/src/main/java/com/keylesspalace/tusky/adapter/SavedTootAdapter.java @@ -15,6 +15,7 @@ package com.keylesspalace.tusky.adapter; +import android.content.Context; import android.support.annotation.Nullable; import android.support.v7.widget.RecyclerView; import android.text.TextUtils; @@ -32,10 +33,12 @@ import java.util.List; public class SavedTootAdapter extends RecyclerView.Adapter { private List list; + private SavedTootAction handler; - public SavedTootAdapter() { + public SavedTootAdapter(Context context) { super(); list = new ArrayList<>(); + handler = (SavedTootAction) context; } @Override @@ -48,33 +51,12 @@ public class SavedTootAdapter extends RecyclerView.Adapter { @Override public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) { TootViewHolder holder = (TootViewHolder) viewHolder; - holder.bind(getItem(position)); + holder.bind(position, getItem(position)); } @Override public int getItemCount() { - return list.size() + 1; - } - - public void update(List newToot) { - if (newToot == null || newToot.isEmpty()) { - return; - } - if (list.isEmpty()) { - list = newToot; - } else { - int index = list.indexOf(newToot.get(newToot.size() - 1)); - for (int i = 0; i < index; i++) { - list.remove(0); - } - int newIndex = newToot.indexOf(list.get(0)); - if (newIndex == -1) { - list.addAll(0, newToot); - } else { - list.addAll(0, newToot.subList(0, newIndex)); - } - } - notifyDataSetChanged(); + return list.size(); } public void addItems(List newToot) { @@ -93,14 +75,6 @@ public class SavedTootAdapter extends RecyclerView.Adapter { return toot; } - public void addItem(TootEntity toot, int position) { - if (position < 0 || position > list.size()) { - return; - } - list.add(position, toot); - notifyItemInserted(position); - } - public TootEntity getItem(int position) { if (position >= 0 && position < list.size()) { return list.get(position); @@ -108,6 +82,13 @@ public class SavedTootAdapter extends RecyclerView.Adapter { return null; } + // handler saved toot + public interface SavedTootAction { + void delete(int position, TootEntity item); + + void click(int position, TootEntity item); + } + public static class ViewHolder extends RecyclerView.ViewHolder { TextView mTextView; @@ -117,21 +98,37 @@ public class SavedTootAdapter extends RecyclerView.Adapter { } } - private static class TootViewHolder extends RecyclerView.ViewHolder { - public TextView content; - public ImageButton suppr; + private class TootViewHolder extends RecyclerView.ViewHolder { + View view; + TextView content; + ImageButton suppr; TootViewHolder(View view) { super(view); - content = (TextView) view.findViewById(R.id.content); - suppr = (ImageButton) view.findViewById(R.id.suppr); + this.view = view; + this.content = (TextView) view.findViewById(R.id.content); + this.suppr = (ImageButton) view.findViewById(R.id.suppr); } - public void bind(TootEntity item) { - if (item != null && !TextUtils.isEmpty(item.getText())) - content.setText(item.getText()); - else - content.setText(""); + void bind(final int position, final TootEntity item) { + if (item != null) { + if (!TextUtils.isEmpty(item.getText())) + content.setText(item.getText()); + else + content.setText(""); + suppr.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + handler.delete(position, item); + } + }); + view.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + handler.click(position, item); + } + }); + } } } } 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 00871429..0a4936d2 100644 --- a/app/src/main/java/com/keylesspalace/tusky/db/AppDatabase.java +++ b/app/src/main/java/com/keylesspalace/tusky/db/AppDatabase.java @@ -7,7 +7,8 @@ import android.arch.persistence.room.RoomDatabase; * DB version & declare DAO */ -@Database(entities = {TootEntity.class}, version = 2, exportSchema = false) -abstract public class AppDatabase extends RoomDatabase { +@Database(entities = {TootEntity.class}, version = 3, exportSchema = false) +public abstract class AppDatabase extends RoomDatabase { + public abstract TootDao tootDao(); } 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 4323c957..760379e8 100644 --- a/app/src/main/java/com/keylesspalace/tusky/db/TootEntity.java +++ b/app/src/main/java/com/keylesspalace/tusky/db/TootEntity.java @@ -16,6 +16,9 @@ public class TootEntity { @ColumnInfo(name = "text") private String text; + @ColumnInfo(name = "urls") + private String urls; + // getter setter public String getText() { return text; @@ -32,4 +35,12 @@ public class TootEntity { public void setUid(int uid) { this.uid = uid; } + + public String getUrls() { + return urls; + } + + public void setUrls(String urls) { + this.urls = urls; + } } diff --git a/app/src/main/res/layout/item_saved_toot.xml b/app/src/main/res/layout/item_saved_toot.xml index 089a993f..cf76d824 100644 --- a/app/src/main/res/layout/item_saved_toot.xml +++ b/app/src/main/res/layout/item_saved_toot.xml @@ -18,7 +18,9 @@ android:layout_width="24dp" android:layout_height="24dp" android:layout_gravity="center_vertical" + android:padding="16dp" android:layout_margin="16dp" + android:contentDescription="@string/action_unmute" app:srcCompat="@drawable/ic_clear_24dp" /> \ No newline at end of file From 76fca004ee93c6cfc9cbf4ef2650c8e265782a63 Mon Sep 17 00:00:00 2001 From: torrentcome Date: Thu, 6 Jul 2017 12:20:54 +0200 Subject: [PATCH 15/19] (correction) fix some forgot code to present the pull request :D --- .../java/com/keylesspalace/tusky/ComposeActivity.java | 10 +--------- .../com/keylesspalace/tusky/SavedTootActivity.java | 5 ----- app/src/main/res/values/strings.xml | 3 ++- 3 files changed, 3 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java index ef75d068..61bf956d 100644 --- a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java @@ -208,7 +208,7 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFragm public boolean onLongClick(View v) { boolean b = saveTheToot(textEditor.getText().toString()); if (b) { - Toast.makeText(ComposeActivity.this, "Toot saved !", Toast.LENGTH_SHORT).show(); + Toast.makeText(ComposeActivity.this, R.string.action_save_one_toot, Toast.LENGTH_SHORT).show(); } return b; } @@ -538,7 +538,6 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFragm List list = new ArrayList<>(); for (QueuedMedia q : mediaQueued) { - Log.d("list", "" + q.uri); list.add(q.uri.toString()); } String json = new Gson().toJson(list); @@ -1267,13 +1266,6 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFragm return super.onOptionsItemSelected(item); } - - @Override - public void onBackPressed() { - super.onBackPressed(); - } - - @Override public void onReceiveHeaderInfo(ParserUtils.HeaderInfo headerInfo) { if (!TextUtils.isEmpty(headerInfo.title)) { diff --git a/app/src/main/java/com/keylesspalace/tusky/SavedTootActivity.java b/app/src/main/java/com/keylesspalace/tusky/SavedTootActivity.java index b7e0f924..88560dab 100644 --- a/app/src/main/java/com/keylesspalace/tusky/SavedTootActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/SavedTootActivity.java @@ -24,7 +24,6 @@ import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; -import android.util.Log; import android.view.MenuItem; import android.view.View; import android.widget.TextView; @@ -98,10 +97,6 @@ public class SavedTootActivity extends BaseActivity implements SavedTootAdapter. @Override protected void onPostExecute(List tootEntities) { super.onPostExecute(tootEntities); - for (TootEntity t : tootEntities) { - Log.e("toot", "id=" + t.getUid() + "text=" + t.getText()); - } - // set ui setNoContent(tootEntities.size()); adapter.addItems(tootEntities); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a88b42a8..da6c5ac8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -102,7 +102,7 @@ Accept Reject Search - Saved Toot + Drafts Share toot URL to… Share toot to… @@ -199,5 +199,6 @@ %1$s %2$s no content + Toot saved ! From 0ce5c49655d3e2a6f6b7389972055a6d4dd48850 Mon Sep 17 00:00:00 2001 From: torrentcome Date: Thu, 6 Jul 2017 12:31:59 +0200 Subject: [PATCH 16/19] (db) version = 1 --- app/src/main/java/com/keylesspalace/tusky/db/AppDatabase.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 0a4936d2..8480d27e 100644 --- a/app/src/main/java/com/keylesspalace/tusky/db/AppDatabase.java +++ b/app/src/main/java/com/keylesspalace/tusky/db/AppDatabase.java @@ -7,7 +7,7 @@ import android.arch.persistence.room.RoomDatabase; * DB version & declare DAO */ -@Database(entities = {TootEntity.class}, version = 3, exportSchema = false) +@Database(entities = {TootEntity.class}, version = 1, exportSchema = false) public abstract class AppDatabase extends RoomDatabase { public abstract TootDao tootDao(); From 42d94633cf63d14f765532e5a89b8995f60b6861 Mon Sep 17 00:00:00 2001 From: torrentcome Date: Fri, 7 Jul 2017 12:32:04 +0200 Subject: [PATCH 17/19] (ui) fix bug that when we comeback from the Composer , there is no update of the view --- .../com/keylesspalace/tusky/SavedTootActivity.java | 12 +++++++++++- .../tusky/adapter/SavedTootAdapter.java | 5 +++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/SavedTootActivity.java b/app/src/main/java/com/keylesspalace/tusky/SavedTootActivity.java index 88560dab..385fa52e 100644 --- a/app/src/main/java/com/keylesspalace/tusky/SavedTootActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/SavedTootActivity.java @@ -72,6 +72,12 @@ public class SavedTootActivity extends BaseActivity implements SavedTootAdapter. adapter = new SavedTootAdapter(this); recyclerView.setAdapter(adapter); + } + + @Override + protected void onResume() { + super.onResume(); + // req getAllToot(); } @@ -99,7 +105,10 @@ public class SavedTootActivity extends BaseActivity implements SavedTootAdapter. super.onPostExecute(tootEntities); // set ui setNoContent(tootEntities.size()); - adapter.addItems(tootEntities); + if (adapter != null) { + adapter.setItems(tootEntities); + adapter.notifyDataSetChanged(); + } } }.execute(); } @@ -126,6 +135,7 @@ public class SavedTootActivity extends BaseActivity implements SavedTootAdapter. @Override public void click(int position, TootEntity item) { Intent intent = new Intent(this, ComposeActivity.class); + intent.putExtra("saved_toot_uid", item.getUid()); intent.putExtra("saved_toot_text", item.getText()); intent.putExtra("saved_json_urls", item.getUrls()); startActivity(intent); diff --git a/app/src/main/java/com/keylesspalace/tusky/adapter/SavedTootAdapter.java b/app/src/main/java/com/keylesspalace/tusky/adapter/SavedTootAdapter.java index 3db08bc0..12a55467 100644 --- a/app/src/main/java/com/keylesspalace/tusky/adapter/SavedTootAdapter.java +++ b/app/src/main/java/com/keylesspalace/tusky/adapter/SavedTootAdapter.java @@ -59,6 +59,11 @@ public class SavedTootAdapter extends RecyclerView.Adapter { return list.size(); } + public void setItems(List newToot) { + list = new ArrayList<>(); + list.addAll(newToot); + } + public void addItems(List newToot) { int end = list.size(); list.addAll(newToot); From 47dccd81e7dfcdeb70974de694c0328b14c3ad23 Mon Sep 17 00:00:00 2001 From: torrentcome Date: Fri, 7 Jul 2017 12:32:47 +0200 Subject: [PATCH 18/19] (db) add the Update feature for each Draft we already have --- .../com/keylesspalace/tusky/ComposeActivity.java | 15 ++++++++++++--- .../java/com/keylesspalace/tusky/db/TootDao.java | 6 ++---- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java index 61bf956d..81c8d69b 100644 --- a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java @@ -158,7 +158,7 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFragm private InputContentInfoCompat currentInputContentInfo; private int currentFlags; private Uri photoUploadUri; - + private int savedTootUid = 0; /** * 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. @@ -311,8 +311,12 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFragm // try to redo a list of media ArrayList playersList = new Gson().fromJson(savedJsonUrls, new TypeToken>() { - }.getType()); + }.getType()); + } + int savedTootUid = intent.getIntExtra("saved_toot_uid", 0); + if (savedTootUid != 0) { + this.savedTootUid = savedTootUid; } } @@ -547,7 +551,12 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFragm new AsyncTask() { @Override protected Void doInBackground(Void... params) { - tootDao.insert(toot); + if (savedTootUid != 0) { + toot.setUid(savedTootUid); + tootDao.updateToot(toot); + } else { + tootDao.insert(toot); + } return null; } }.execute(); diff --git a/app/src/main/java/com/keylesspalace/tusky/db/TootDao.java b/app/src/main/java/com/keylesspalace/tusky/db/TootDao.java index 19461984..b3779af5 100644 --- a/app/src/main/java/com/keylesspalace/tusky/db/TootDao.java +++ b/app/src/main/java/com/keylesspalace/tusky/db/TootDao.java @@ -3,6 +3,7 @@ package com.keylesspalace.tusky.db; import android.arch.persistence.room.Dao; import android.arch.persistence.room.Delete; import android.arch.persistence.room.Insert; +import android.arch.persistence.room.OnConflictStrategy; import android.arch.persistence.room.Query; import android.arch.persistence.room.Update; @@ -23,12 +24,9 @@ public interface TootDao { @Query("SELECT * FROM TootEntity") List loadAll(); - @Query("SELECT * FROM TootEntity WHERE uid IN (:uid)") - List loadAllByTootId(int... uid); - // u @Update - void updateToot(TootEntity... toot); + void updateToot(TootEntity toot); // d @Delete From 65c274d2911d9742b93ef08fc55fbfa77acbfbf4 Mon Sep 17 00:00:00 2001 From: torrentcome Date: Fri, 7 Jul 2017 13:36:39 +0200 Subject: [PATCH 19/19] (gradle) remove useless rx room stuff --- app/build.gradle | 1 - 1 file changed, 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index 46db1be3..ae008c98 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -59,6 +59,5 @@ dependencies { //room compile "android.arch.persistence.room:runtime:1.0.0-alpha3" annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha3" - compile "android.arch.persistence.room:rxjava2:1.0.0-alpha2" }