This commit is contained in:
NudeDude 2019-03-28 11:11:03 +01:00
parent 43109bfcac
commit 1ddba16c1a
2 changed files with 42 additions and 32 deletions

View File

@ -18,11 +18,14 @@ import java.util.List;
import twitter4j.TwitterException;
public class MessageLoader extends AsyncTask<Long, Void, Long> {
public class MessageLoader extends AsyncTask<Long, Void, Void> {
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<DirectMessage> ui;
private MessageAdapter mAdapter;
@ -32,13 +35,14 @@ public class MessageLoader extends AsyncTask<Long, Void, Long> {
private List<Message> 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<Long, Void, Long> {
@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 {

View File

@ -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();
}
}