Commit 8e8e9231 authored by Yusuke Iwaki's avatar Yusuke Iwaki

just refactor

parent c50327a8
......@@ -12,7 +12,7 @@ public class RocketChatCache {
public static final String KEY_SELECTED_SERVER_CONFIG_ID = "selectedServerConfigId";
public static final String KEY_SELECTED_ROOM_ID = "selectedRoomId";
private static final String PUSH_ID = "pushId";
private static final String KEY_PUSH_ID = "pushId";
/**
* get SharedPreference instance for RocketChat application cache.
......@@ -21,36 +21,16 @@ public class RocketChatCache {
return context.getSharedPreferences("cache", Context.MODE_PRIVATE);
}
public static String getSelectedServerConfigId(Context context) {
return get(context).getString(KEY_SELECTED_SERVER_CONFIG_ID, "");
}
public static void setSelectedServerConfigId(Context context, String serverConfigId) {
setString(get(context), KEY_SELECTED_SERVER_CONFIG_ID, serverConfigId);
}
public static String getSelectedRoomId(Context context) {
return get(context).getString(KEY_SELECTED_ROOM_ID, "");
}
public static void setSelectedRoomId(Context context, String roomId) {
setString(get(context), KEY_SELECTED_ROOM_ID, roomId);
}
public static String getPushId(Context context) {
public static String getOrCreatePushId(Context context) {
SharedPreferences preferences = get(context);
String pushId = null;
if (!preferences.contains(PUSH_ID)) {
if (!preferences.contains(KEY_PUSH_ID)) {
// generates one and save
pushId = UUID.randomUUID().toString().replace("-", "");
setString(preferences, PUSH_ID, pushId);
String newId = UUID.randomUUID().toString().replace("-", "");
preferences.edit()
.putString(KEY_PUSH_ID, newId)
.apply();
return newId;
}
return preferences.getString(PUSH_ID, pushId);
}
private static void setString(SharedPreferences preferences, String key, String value) {
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, value);
editor.apply();
return preferences.getString(KEY_PUSH_ID, null);
}
}
......@@ -52,8 +52,10 @@ public class GcmRegistrationIntentService extends IntentService {
final User currentUser = realmHelper.executeTransactionForRead(realm ->
User.queryCurrentUser(realm).findFirst());
new RaixPushHelper(getBaseContext(), serverConfig.getServerConfigId()).pushUpdate(
RocketChatCache.getPushId(this), gcmToken, currentUser != null ? currentUser.getId() : null)
final String pushId = RocketChatCache.getOrCreatePushId(this);
final String userId = currentUser != null ? currentUser.getId() : null;
new RaixPushHelper(getBaseContext(), serverConfig.getServerConfigId())
.pushUpdate(pushId, gcmToken, userId)
.onSuccess(task -> {
markRefreshAsDone(serverConfig);
return task;
......
......@@ -63,7 +63,9 @@ public class CurrentUserObserver extends AbstractModelObserver<User> {
final String userId = user.getId();
// update push info
pushHelper.pushSetUser(RocketChatCache.getPushId(context)).continueWith(new LogcatIfError());
pushHelper
.pushSetUser(RocketChatCache.getOrCreatePushId(context))
.continueWith(new LogcatIfError());
// get and observe Room subscriptions.
methodCall.getRoomSubscriptions().onSuccess(task -> {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment