From 1ddba16c1abd1a510a8c0a13cf2b7cdcc83d8134 Mon Sep 17 00:00:00 2001 From: NudeDude Date: Thu, 28 Mar 2019 11:11:03 +0100 Subject: [PATCH] bug fix --- .../twidda/backend/MessageLoader.java | 60 +++++++++++-------- .../twidda/window/DirectMessage.java | 14 +++-- 2 files changed, 42 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/org/nuclearfog/twidda/backend/MessageLoader.java b/app/src/main/java/org/nuclearfog/twidda/backend/MessageLoader.java index 4032edf9..765d01d3 100644 --- a/app/src/main/java/org/nuclearfog/twidda/backend/MessageLoader.java +++ b/app/src/main/java/org/nuclearfog/twidda/backend/MessageLoader.java @@ -18,11 +18,14 @@ import java.util.List; import twitter4j.TwitterException; -public class MessageLoader extends AsyncTask { +public class MessageLoader extends AsyncTask { - public static final long LOAD = 0; - public static final long DELETE = 1; - private static final long FAIL = -1; + public enum Mode { + LOAD, + DELETE + } + private final Mode mode; + private boolean failure = false; private WeakReference ui; private MessageAdapter mAdapter; @@ -32,13 +35,14 @@ public class MessageLoader extends AsyncTask { private List message; - public MessageLoader(@NonNull DirectMessage context) { + public MessageLoader(@NonNull DirectMessage context, Mode mode) { ui = new WeakReference<>(context); RecyclerView dm_list = context.findViewById(R.id.messagelist); mAdapter = (MessageAdapter) dm_list.getAdapter(); twitter = TwitterEngine.getInstance(context); - message = new ArrayList<>(); mData = new DatabaseAdapter(context); + message = new ArrayList<>(); + this.mode = mode; } @@ -52,45 +56,49 @@ public class MessageLoader extends AsyncTask { @Override - protected Long doInBackground(Long... param) { - final long MODE = param[0]; + protected Void doInBackground(Long... param) { try { - if (MODE == LOAD) { - if (mAdapter.getItemCount() > 0) { - message = twitter.getMessages(); - mData.storeMessage(message); - message = mData.getMessages(); - } else { - message = mData.getMessages(); - if (message.isEmpty()) { + switch(mode) { + case LOAD: + if (mAdapter.getItemCount() > 0) { message = twitter.getMessages(); mData.storeMessage(message); + message = mData.getMessages(); + } else { + message = mData.getMessages(); + if (message.isEmpty()) { + message = twitter.getMessages(); + mData.storeMessage(message); + } } - } - } else if (MODE == DELETE) { - mData.deleteDm(param[1]); - message = mData.getMessages(); - twitter.deleteMessage(param[1]); + break; + + case DELETE: + long messageId = param[0]; + twitter.deleteMessage(messageId); + mData.deleteDm(messageId); + message = mData.getMessages(); + break; } } catch (TwitterException err) { this.err = err; - return FAIL; + failure = true; } catch (Exception err) { Log.e("Direct Message", err.getMessage()); - return FAIL; + failure = true; } - return MODE; + return null; } @Override - protected void onPostExecute(Long mode) { + protected void onPostExecute(Void mode) { if (ui.get() == null) return; SwipeRefreshLayout mRefresh = ui.get().findViewById(R.id.dm_reload); mRefresh.setRefreshing(false); - if (mode != FAIL) { + if (!failure) { mAdapter.setData(message); mAdapter.notifyDataSetChanged(); } else { diff --git a/app/src/main/java/org/nuclearfog/twidda/window/DirectMessage.java b/app/src/main/java/org/nuclearfog/twidda/window/DirectMessage.java index 5ba34679..90b0cd52 100644 --- a/app/src/main/java/org/nuclearfog/twidda/window/DirectMessage.java +++ b/app/src/main/java/org/nuclearfog/twidda/window/DirectMessage.java @@ -22,6 +22,8 @@ import org.nuclearfog.twidda.backend.items.Message; import org.nuclearfog.twidda.database.GlobalSettings; import static android.os.AsyncTask.Status.RUNNING; +import static org.nuclearfog.twidda.backend.MessageLoader.Mode.DELETE; +import static org.nuclearfog.twidda.backend.MessageLoader.Mode.LOAD; /** * Direct Message page @@ -67,8 +69,8 @@ public class DirectMessage extends AppCompatActivity implements OnRefreshListene mAdapter.setColor(settings.getFontColor(), settings.getHighlightColor()); mAdapter.setImageLoad(settings.getImageLoad()); dmList.setAdapter(mAdapter); - mLoader = new MessageLoader(this); - mLoader.execute(MessageLoader.LOAD); + mLoader = new MessageLoader(this, LOAD); + mLoader.execute(); } } @@ -124,8 +126,8 @@ public class DirectMessage extends AppCompatActivity implements OnRefreshListene .setPositiveButton(R.string.yes_confirm, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - mLoader = new MessageLoader(DirectMessage.this); - mLoader.execute(MessageLoader.DELETE, messageId); + mLoader = new MessageLoader(DirectMessage.this, DELETE); + mLoader.execute(messageId); } }).show(); } @@ -156,7 +158,7 @@ public class DirectMessage extends AppCompatActivity implements OnRefreshListene @Override public void onRefresh() { - mLoader = new MessageLoader(this); - mLoader.execute(MessageLoader.LOAD); + mLoader = new MessageLoader(this, LOAD); + mLoader.execute(); } } \ No newline at end of file