Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update client #125

Merged
merged 2 commits into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions app/src/main/java/com/github/gotify/MissedMessageUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
import static com.github.gotify.api.Callback.call;

public class MissedMessageUtil {
static final int NO_MESSAGES = 0;
static final long NO_MESSAGES = 0;

private final MessageApi api;

public MissedMessageUtil(MessageApi api) {
this.api = api;
}

public void lastReceivedMessage(Callback.SuccessCallback<Integer> successCallback) {
api.getMessages(1, 0)
public void lastReceivedMessage(Callback.SuccessCallback<Long> successCallback) {
api.getMessages(1, 0L)
.enqueue(
call(
(messages) -> {
Expand All @@ -37,11 +37,11 @@ public void lastReceivedMessage(Callback.SuccessCallback<Integer> successCallbac
(e) -> {}));
}

public List<Message> missingMessages(int till) {
public List<Message> missingMessages(long till) {
List<Message> result = new ArrayList<>();
try {

Integer since = null;
Long since = null;
while (true) {
PagedMessages pagedMessages = Api.execute(api.getMessages(10, since));
List<Message> messages = pagedMessages.getMessages();
Expand All @@ -61,7 +61,7 @@ public List<Message> missingMessages(int till) {
return result;
}

private List<Message> filter(List<Message> messages, int till) {
private List<Message> filter(List<Message> messages, long till) {
List<Message> result = new ArrayList<>();

for (Message message : messages) {
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/github/gotify/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public static void showSnackBar(Activity activity, String message) {
Snackbar.make(rootView, message, Snackbar.LENGTH_SHORT).show();
}

public static int longToInt(long value) {
return (int) (value % Integer.MAX_VALUE);
}

public static String dateToRelative(OffsetDateTime data) {
long time = data.toInstant().toEpochMilli();
long now = System.currentTimeMillis();
Expand Down
40 changes: 24 additions & 16 deletions app/src/main/java/com/github/gotify/messages/MessagesActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ public void onReceive(Context context, Intent intent) {
private Settings settings;
protected ApplicationHolder appsHolder;

private int appId = MessageState.ALL_MESSAGES;
private long appId = MessageState.ALL_MESSAGES;

private boolean isLoadMore = false;
private Integer selectAppIdOnDrawerClose = null;
private Long selectAppIdOnDrawerClose = null;

private PicassoHandler picassoHandler;

Expand Down Expand Up @@ -223,7 +223,12 @@ protected void onUpdateApps(List<Application> applications) {
targetReferences.clear();
updateMessagesAndStopLoading(messages.get(appId));
for (Application app : applications) {
MenuItem item = menu.add(R.id.apps, app.getId(), APPLICATION_ORDER, app.getName());
MenuItem item =
menu.add(
R.id.apps,
Utils.longToInt(app.getId()),
APPLICATION_ORDER,
app.getName());
item.setCheckable(true);
Target t = Utils.toDrawable(getResources(), item::setIcon);
targetReferences.add(t);
Expand Down Expand Up @@ -284,7 +289,7 @@ public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();

if (item.getGroupId() == R.id.apps) {
selectAppIdOnDrawerClose = id;
selectAppIdOnDrawerClose = (long) id;
startLoading();
toolbar.setSubtitle(item.getTitle());
} else if (id == R.id.nav_all_messages) {
Expand Down Expand Up @@ -340,7 +345,10 @@ protected void onResume() {
new UpdateMissedMessages().execute(messages.getLastReceivedMessage());
navigationView
.getMenu()
.findItem(appId == MessageState.ALL_MESSAGES ? R.id.nav_all_messages : appId)
.findItem(
appId == MessageState.ALL_MESSAGES
? R.id.nav_all_messages
: Utils.longToInt(appId))
.setChecked(true);
super.onResume();
}
Expand Down Expand Up @@ -525,10 +533,10 @@ public void onScrolled(RecyclerView view, int dx, int dy) {
}
}

private class UpdateMissedMessages extends AsyncTask<Integer, Void, Boolean> {
private class UpdateMissedMessages extends AsyncTask<Long, Void, Boolean> {
@Override
protected Boolean doInBackground(Integer... ids) {
Integer id = first(ids);
protected Boolean doInBackground(Long... ids) {
Long id = first(ids);
if (id == -1) {
return false;
}
Expand Down Expand Up @@ -562,10 +570,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onContextItemSelected(item);
}

private class LoadMore extends AsyncTask<Integer, Void, List<MessageWithImage>> {
private class LoadMore extends AsyncTask<Long, Void, List<MessageWithImage>> {

@Override
protected List<MessageWithImage> doInBackground(Integer... appId) {
protected List<MessageWithImage> doInBackground(Long... appId) {
return messages.loadMore(first(appId));
}

Expand All @@ -575,7 +583,7 @@ protected void onPostExecute(List<MessageWithImage> messageWithImages) {
}
}

private class SelectApplicationAndUpdateMessages extends AsyncTask<Integer, Void, Integer> {
private class SelectApplicationAndUpdateMessages extends AsyncTask<Long, Void, Long> {

private SelectApplicationAndUpdateMessages(boolean withLoadingSpinner) {
if (withLoadingSpinner) {
Expand All @@ -584,14 +592,14 @@ private SelectApplicationAndUpdateMessages(boolean withLoadingSpinner) {
}

@Override
protected Integer doInBackground(Integer... appIds) {
Integer appId = first(appIds);
protected Long doInBackground(Long... appIds) {
Long appId = first(appIds);
messages.loadMoreIfNotPresent(appId);
return appId;
}

@Override
protected void onPostExecute(Integer appId) {
protected void onPostExecute(Long appId) {
updateMessagesAndStopLoading(messages.get(appId));
}
}
Expand Down Expand Up @@ -624,14 +632,14 @@ protected void onPostExecute(Void data) {
}
}

private class DeleteMessages extends AsyncTask<Integer, Void, Boolean> {
private class DeleteMessages extends AsyncTask<Long, Void, Boolean> {

DeleteMessages() {
startLoading();
}

@Override
protected Boolean doInBackground(Integer... appId) {
protected Boolean doInBackground(Long... appId) {
return messages.deleteAll(first(appId));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public MessageFacade(MessageApi api, ApplicationHolder applicationHolder) {
this.state = new MessageStateHolder();
}

public synchronized List<MessageWithImage> get(Integer appId) {
public synchronized List<MessageWithImage> get(long appId) {
return combiner.combine(state.state(appId).messages, applicationHolder.get());
}

Expand All @@ -28,7 +28,7 @@ public synchronized void addMessages(List<Message> messages) {
}
}

public synchronized List<MessageWithImage> loadMore(Integer appId) {
public synchronized List<MessageWithImage> loadMore(long appId) {
MessageState state = this.state.state(appId);
if (state.hasNext || !state.loaded) {
PagedMessages pagedMessages = requester.loadMore(state);
Expand All @@ -37,7 +37,7 @@ public synchronized List<MessageWithImage> loadMore(Integer appId) {
return get(appId);
}

public synchronized void loadMoreIfNotPresent(Integer appId) {
public synchronized void loadMoreIfNotPresent(long appId) {
MessageState state = this.state.state(appId);
if (!state.loaded) {
loadMore(appId);
Expand All @@ -48,7 +48,7 @@ public synchronized void clear() {
this.state.clear();
}

public int getLastReceivedMessage() {
public long getLastReceivedMessage() {
return state.getLastReceivedMessage();
}

Expand All @@ -70,13 +70,13 @@ public synchronized MessageDeletion undoDeleteLocal() {
return this.state.undoPendingDeletion();
}

public synchronized boolean deleteAll(Integer appId) {
public synchronized boolean deleteAll(long appId) {
boolean success = this.requester.deleteAll(appId);
this.state.deleteAll(appId);
return success;
}

public synchronized boolean canLoadMore(Integer appId) {
public synchronized boolean canLoadMore(long appId) {
return state.state(appId).hasNext;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class MessageImageCombiner {

List<MessageWithImage> combine(List<Message> messages, List<Application> applications) {
Map<Integer, String> appIdToImage = appIdToImage(applications);
Map<Long, String> appIdToImage = appIdToImage(applications);

List<MessageWithImage> result = new ArrayList<>();

Expand All @@ -26,8 +26,8 @@ List<MessageWithImage> combine(List<Message> messages, List<Application> applica
return result;
}

public static Map<Integer, String> appIdToImage(List<Application> applications) {
Map<Integer, String> map = new ConcurrentHashMap<>();
public static Map<Long, String> appIdToImage(List<Application> applications) {
Map<Long, String> map = new ConcurrentHashMap<>();
for (Application app : applications) {
map.put(app.getId(), app.getImage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void asyncRemoveMessage(Message message) {
messageApi.deleteMessage(message.getId()).enqueue(Callback.call());
}

boolean deleteAll(Integer appId) {
boolean deleteAll(Long appId) {
try {
Log.i("Deleting all messages for " + appId);
if (MessageState.ALL_MESSAGES == appId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import java.util.List;

public class MessageState {
public static final int ALL_MESSAGES = -1;
public static final long ALL_MESSAGES = -1;

int appId;
long appId;
boolean loaded;
boolean hasNext;
int nextSince = 0;
long nextSince = 0;
List<Message> messages = new ArrayList<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
import java.util.Map;

class MessageStateHolder {
private int lastReceivedMessage = -1;
private Map<Integer, MessageState> states = new HashMap<>();
private long lastReceivedMessage = -1;
private Map<Long, MessageState> states = new HashMap<>();

private MessageDeletion pendingDeletion = null;

synchronized void clear() {
states = new HashMap<>();
}

synchronized void newMessages(Integer appId, PagedMessages pagedMessages) {
synchronized void newMessages(Long appId, PagedMessages pagedMessages) {
MessageState state = state(appId);

if (!state.loaded && pagedMessages.getMessages().size() > 0) {
Expand Down Expand Up @@ -49,22 +49,22 @@ synchronized void newMessage(Message message) {
if (deletion != null) deleteMessage(deletion.getMessage());
}

synchronized MessageState state(Integer appId) {
synchronized MessageState state(Long appId) {
MessageState state = states.get(appId);
if (state == null) {
return emptyState(appId);
}
return state;
}

synchronized void deleteAll(Integer appId) {
synchronized void deleteAll(Long appId) {
clear();
MessageState state = state(appId);
state.loaded = true;
states.put(appId, state);
}

private MessageState emptyState(Integer appId) {
private MessageState emptyState(Long appId) {
MessageState emptyState = new MessageState();
emptyState.loaded = false;
emptyState.hasNext = false;
Expand All @@ -73,7 +73,7 @@ private MessageState emptyState(Integer appId) {
return emptyState;
}

synchronized int getLastReceivedMessage() {
synchronized long getLastReceivedMessage() {
return lastReceivedMessage;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class PicassoHandler {
private Cache picassoCache;

private Picasso picasso;
private Map<Integer, String> appIdToAppImage = new ConcurrentHashMap<>();
private Map<Long, String> appIdToAppImage = new ConcurrentHashMap<>();

public PicassoHandler(Context context, Settings settings) {
this.context = context;
Expand All @@ -53,7 +53,7 @@ private Picasso makePicasso() {
return new Picasso.Builder(context).downloader(downloader).build();
}

public Bitmap getIcon(Integer appId) {
public Bitmap getIcon(Long appId) {
if (appId == -1) {
return BitmapFactory.decodeResource(context.getResources(), R.drawable.gotify);
}
Expand Down
Loading