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

just refactor

parent c50327a8
...@@ -12,7 +12,7 @@ public class RocketChatCache { ...@@ -12,7 +12,7 @@ public class RocketChatCache {
public static final String KEY_SELECTED_SERVER_CONFIG_ID = "selectedServerConfigId"; public static final String KEY_SELECTED_SERVER_CONFIG_ID = "selectedServerConfigId";
public static final String KEY_SELECTED_ROOM_ID = "selectedRoomId"; 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. * get SharedPreference instance for RocketChat application cache.
...@@ -21,36 +21,16 @@ public class RocketChatCache { ...@@ -21,36 +21,16 @@ public class RocketChatCache {
return context.getSharedPreferences("cache", Context.MODE_PRIVATE); return context.getSharedPreferences("cache", Context.MODE_PRIVATE);
} }
public static String getSelectedServerConfigId(Context context) { public static String getOrCreatePushId(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) {
SharedPreferences preferences = get(context); SharedPreferences preferences = get(context);
String pushId = null; if (!preferences.contains(KEY_PUSH_ID)) {
if (!preferences.contains(PUSH_ID)) {
// generates one and save // generates one and save
pushId = UUID.randomUUID().toString().replace("-", ""); String newId = UUID.randomUUID().toString().replace("-", "");
setString(preferences, PUSH_ID, pushId); preferences.edit()
.putString(KEY_PUSH_ID, newId)
.apply();
return newId;
} }
return preferences.getString(PUSH_ID, pushId); return preferences.getString(KEY_PUSH_ID, null);
}
private static void setString(SharedPreferences preferences, String key, String value) {
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, value);
editor.apply();
} }
} }
...@@ -52,8 +52,10 @@ public class GcmRegistrationIntentService extends IntentService { ...@@ -52,8 +52,10 @@ public class GcmRegistrationIntentService extends IntentService {
final User currentUser = realmHelper.executeTransactionForRead(realm -> final User currentUser = realmHelper.executeTransactionForRead(realm ->
User.queryCurrentUser(realm).findFirst()); User.queryCurrentUser(realm).findFirst());
new RaixPushHelper(getBaseContext(), serverConfig.getServerConfigId()).pushUpdate( final String pushId = RocketChatCache.getOrCreatePushId(this);
RocketChatCache.getPushId(this), gcmToken, currentUser != null ? currentUser.getId() : null) final String userId = currentUser != null ? currentUser.getId() : null;
new RaixPushHelper(getBaseContext(), serverConfig.getServerConfigId())
.pushUpdate(pushId, gcmToken, userId)
.onSuccess(task -> { .onSuccess(task -> {
markRefreshAsDone(serverConfig); markRefreshAsDone(serverConfig);
return task; return task;
......
...@@ -63,7 +63,9 @@ public class CurrentUserObserver extends AbstractModelObserver<User> { ...@@ -63,7 +63,9 @@ public class CurrentUserObserver extends AbstractModelObserver<User> {
final String userId = user.getId(); final String userId = user.getId();
// update push info // update push info
pushHelper.pushSetUser(RocketChatCache.getPushId(context)).continueWith(new LogcatIfError()); pushHelper
.pushSetUser(RocketChatCache.getOrCreatePushId(context))
.continueWith(new LogcatIfError());
// get and observe Room subscriptions. // get and observe Room subscriptions.
methodCall.getRoomSubscriptions().onSuccess(task -> { 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