From 82384b48a596f3c3f671db9e2f30dba39d5172fd Mon Sep 17 00:00:00 2001 From: stom79 Date: Wed, 2 Jan 2019 13:31:11 +0100 Subject: Fix some errors --- .../mastodon/activities/ShowAccountActivity.java | 113 +++++++++++---------- .../asynctasks/RetrieveFeedsAsyncTask.java | 6 ++ .../etalab/mastodon/client/Entities/Status.java | 6 +- .../etalab/mastodon/drawers/StatusListAdapter.java | 12 +-- .../mastodon/fragments/DisplayStatusFragment.java | 2 +- .../etalab/mastodon/sqlite/StatusCacheDAO.java | 40 ++++++++ 6 files changed, 115 insertions(+), 64 deletions(-) (limited to 'app') diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/ShowAccountActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/ShowAccountActivity.java index 8221cac32..2a749cdb8 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/ShowAccountActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/ShowAccountActivity.java @@ -514,68 +514,69 @@ public class ShowAccountActivity extends BaseActivity implements OnPostActionInt while (it.hasNext()) { Map.Entry pair = (Map.Entry)it.next(); String label = (String)pair.getKey(); - String value = (String)pair.getValue(); - boolean verified = fieldsVerified.get(label); - - LinearLayout field; - TextView labelView; - TextView valueView; - LinearLayout verifiedView; - switch(i){ - case 1: - field = findViewById(R.id.field1); - labelView = findViewById(R.id.label1); - valueView = findViewById(R.id.value1); - verifiedView = findViewById(R.id.value1BG); - break; - case 2: - field = findViewById(R.id.field2); - labelView = findViewById(R.id.label2); - valueView = findViewById(R.id.value2); - verifiedView = findViewById(R.id.value2BG); - break; - case 3: - field = findViewById(R.id.field3); - labelView = findViewById(R.id.label3); - valueView = findViewById(R.id.value3); - verifiedView = findViewById(R.id.value3BG); - break; - case 4: - field = findViewById(R.id.field4); - labelView = findViewById(R.id.label4); - valueView = findViewById(R.id.value4); - verifiedView = findViewById(R.id.value4BG); - break; - default: - field = findViewById(R.id.field1); - labelView = findViewById(R.id.label1); - valueView = findViewById(R.id.value1); - verifiedView = findViewById(R.id.value1BG); - break; - } - if( field != null && labelView != null && valueView != null) { - switch (theme){ - case Helper.THEME_LIGHT: - labelView.setBackgroundColor(ContextCompat.getColor(ShowAccountActivity.this, R.color.notif_light_2)); - valueView.setBackgroundColor(ContextCompat.getColor(ShowAccountActivity.this, R.color.notif_light_4)); + if( label != null && fieldsVerified.containsKey(label)) { + boolean verified = fieldsVerified.get(label); + + LinearLayout field; + TextView labelView; + TextView valueView; + LinearLayout verifiedView; + switch (i) { + case 1: + field = findViewById(R.id.field1); + labelView = findViewById(R.id.label1); + valueView = findViewById(R.id.value1); + verifiedView = findViewById(R.id.value1BG); + break; + case 2: + field = findViewById(R.id.field2); + labelView = findViewById(R.id.label2); + valueView = findViewById(R.id.value2); + verifiedView = findViewById(R.id.value2BG); break; - case Helper.THEME_DARK: - labelView.setBackgroundColor(ContextCompat.getColor(ShowAccountActivity.this, R.color.notif_dark_2)); - valueView.setBackgroundColor(ContextCompat.getColor(ShowAccountActivity.this, R.color.notif_dark_4)); + case 3: + field = findViewById(R.id.field3); + labelView = findViewById(R.id.label3); + valueView = findViewById(R.id.value3); + verifiedView = findViewById(R.id.value3BG); break; - case Helper.THEME_BLACK: - labelView.setBackgroundColor(ContextCompat.getColor(ShowAccountActivity.this, R.color.notif_black_2)); - valueView.setBackgroundColor(ContextCompat.getColor(ShowAccountActivity.this, R.color.notif_black_4)); + case 4: + field = findViewById(R.id.field4); + labelView = findViewById(R.id.label4); + valueView = findViewById(R.id.value4); + verifiedView = findViewById(R.id.value4BG); break; default: - labelView.setBackgroundColor(ContextCompat.getColor(ShowAccountActivity.this, R.color.notif_dark_2)); - valueView.setBackgroundColor(ContextCompat.getColor(ShowAccountActivity.this, R.color.notif_dark_4)); - } - field.setVisibility(View.VISIBLE); - if( verified) { - verifiedView.setBackgroundResource(R.drawable.verified); + field = findViewById(R.id.field1); + labelView = findViewById(R.id.label1); + valueView = findViewById(R.id.value1); + verifiedView = findViewById(R.id.value1BG); + break; } + if (field != null && labelView != null && valueView != null) { + switch (theme) { + case Helper.THEME_LIGHT: + labelView.setBackgroundColor(ContextCompat.getColor(ShowAccountActivity.this, R.color.notif_light_2)); + valueView.setBackgroundColor(ContextCompat.getColor(ShowAccountActivity.this, R.color.notif_light_4)); + break; + case Helper.THEME_DARK: + labelView.setBackgroundColor(ContextCompat.getColor(ShowAccountActivity.this, R.color.notif_dark_2)); + valueView.setBackgroundColor(ContextCompat.getColor(ShowAccountActivity.this, R.color.notif_dark_4)); + break; + case Helper.THEME_BLACK: + labelView.setBackgroundColor(ContextCompat.getColor(ShowAccountActivity.this, R.color.notif_black_2)); + valueView.setBackgroundColor(ContextCompat.getColor(ShowAccountActivity.this, R.color.notif_black_4)); + break; + default: + labelView.setBackgroundColor(ContextCompat.getColor(ShowAccountActivity.this, R.color.notif_dark_2)); + valueView.setBackgroundColor(ContextCompat.getColor(ShowAccountActivity.this, R.color.notif_dark_4)); + } + field.setVisibility(View.VISIBLE); + if (verified) { + verifiedView.setBackgroundResource(R.drawable.verified); + } + } } i++; } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveFeedsAsyncTask.java b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveFeedsAsyncTask.java index 0689281fb..ddb7bd935 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveFeedsAsyncTask.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveFeedsAsyncTask.java @@ -249,6 +249,12 @@ public class RetrieveFeedsAsyncTask extends AsyncTask { case HASHTAG: break; } + List bookmarks = new StatusCacheDAO(contextReference.get(), db).getAllStatusId(StatusCacheDAO.BOOKMARK_CACHE); + if( apiResponse != null && apiResponse.getStatuses() != null && apiResponse.getStatuses().size() > 0){ + List statuses = apiResponse.getStatuses(); + for(fr.gouv.etalab.mastodon.client.Entities.Status status: statuses) + status.setBookmarked(bookmarks.contains(status.getId())); + } return null; } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Status.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Status.java index 819859236..313734aca 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Status.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Status.java @@ -139,7 +139,11 @@ public class Status implements Parcelable{ reblog = in.readParcelable(Status.class.getClassLoader()); mentions = in.readArrayList(Mention.class.getClassLoader()); account = in.readParcelable(Account.class.getClassLoader()); - application = in.readParcelable(Application.class.getClassLoader()); + try { + application = in.readParcelable(Application.class.getClassLoader()); + }catch (Exception e){ + application = new Application(); + } tags = in.readArrayList(Tag.class.getClassLoader()); contentTranslated = in.readString(); reblogs_count = in.readInt(); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java index cbb803355..67df9eda6 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java @@ -626,12 +626,6 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct //Display a preview for accounts that have replied *if enabled and only for home timeline* - final SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); - Status statusBookmarked = new StatusCacheDAO(context, db).getStatus(StatusCacheDAO.BOOKMARK_CACHE, status.getId()); - if( statusBookmarked != null) - status.setBookmarked(true); - else - status.setBookmarked(false); if( status.isNew()) holder.new_element.setVisibility(View.VISIBLE); @@ -979,6 +973,7 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct if( type != RetrieveFeedsAsyncTask.Type.CACHE_BOOKMARKS) { status.setBookmarked(!status.isBookmarked()); try { + final SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); if (status.isBookmarked()) { new StatusCacheDAO(context, db).insertStatus(StatusCacheDAO.BOOKMARK_CACHE, status); Toasty.success(context, context.getString(R.string.status_bookmarked), Toast.LENGTH_LONG).show(); @@ -997,6 +992,7 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct if (statustmp.getId().equals(status.getId())) { statuses.remove(status); statusListAdapter.notifyItemRemoved(position); + final SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); new StatusCacheDAO(context, db).remove(StatusCacheDAO.BOOKMARK_CACHE, statustmp); Toasty.success(context, context.getString(R.string.status_unbookmarked), Toast.LENGTH_LONG).show(); break; @@ -1017,6 +1013,7 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct //-------- END -> Manages translations if( status.getAccount() == null) { + final SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); Account account = new AccountDAO(context, db).getAccountByID(userId); status.setAccount(account); } @@ -1857,6 +1854,7 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct case R.id.action_bookmark: if( type != RetrieveFeedsAsyncTask.Type.CACHE_BOOKMARKS) { status.setBookmarked(!status.isBookmarked()); + final SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); try { if (status.isBookmarked()) { new StatusCacheDAO(context, db).insertStatus(StatusCacheDAO.BOOKMARK_CACHE, status); @@ -1876,6 +1874,7 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct if (statustmp.getId().equals(status.getId())) { statuses.remove(status); statusListAdapter.notifyItemRemoved(position); + final SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); new StatusCacheDAO(context, db).remove(StatusCacheDAO.BOOKMARK_CACHE, statustmp); Toasty.success(context, context.getString(R.string.status_unbookmarked), Toast.LENGTH_LONG).show(); break; @@ -2115,6 +2114,7 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct toot.setSpoiler_text(status.getSpoiler_text().trim()); toot.setVisibility(status.getVisibility()); toot.setContent(status.getContent()); + final SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); long id = new StatusStoredDAO(context, db).insertStatus(toot, null); Intent intentToot = new Intent(context, TootActivity.class); Bundle b = new Bundle(); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java index 24012c347..f5cdc8cfe 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java @@ -804,7 +804,7 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn if( this.statuses != null) { if( type != RetrieveFeedsAsyncTask.Type.HOME){ if( tagTimeline == null || !tagTimeline.isART() || (tagTimeline.isART() && tagTimeline.isNSFW())) { - if (Long.parseLong(statuses.get(i).getId()) > Long.parseLong(this.statuses.get(0).getId())) { + if (this.statuses.size() == 0 || Long.parseLong(statuses.get(i).getId()) > Long.parseLong(this.statuses.get(0).getId())) { inserted++; this.statuses.add(0, statuses.get(i)); } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/StatusCacheDAO.java b/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/StatusCacheDAO.java index 3f6837d0d..706ee1957 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/StatusCacheDAO.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/StatusCacheDAO.java @@ -224,6 +224,24 @@ public class StatusCacheDAO { } + /** + * Returns all cached Statuses in db depending of their cache type + * @return stored status List + */ + public List getAllStatusId(int cacheType){ + SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); + String instance = Helper.getLiveInstance(context); + try { + Cursor c = db.query(Sqlite.TABLE_STATUSES_CACHE, new String[]{Sqlite.COL_STATUS_ID}, Sqlite.COL_CACHED_ACTION + " = '" + cacheType+ "' AND " + Sqlite.COL_INSTANCE + " = '" + instance+ "' AND " + Sqlite.COL_USER_ID + " = '" + userId+ "'", null, null, null, Sqlite.COL_CREATED_AT + " DESC", null); + return cursorToListStatusesId(c); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** * Returns all cached Statuses in db depending of their cache type * @return stored status List @@ -512,4 +530,26 @@ public class StatusCacheDAO { //Statuses list is returned return statuses; } + + + /*** + * Method to get cached statuses ID from database + * @param c Cursor + * @return List + */ + private List cursorToListStatusesId(Cursor c){ + //No element found + if (c.getCount() == 0) + return null; + List statusesId = new ArrayList<>(); + while (c.moveToNext() ) { + //Restore cached status + + statusesId.add(c.getString(c.getColumnIndex(Sqlite.COL_STATUS_ID))); + } + //Close the cursor + c.close(); + //Statuses list is returned + return statusesId; + } } -- cgit v1.2.3