mirror of
https://github.com/sudoxnym/habitica-android.git
synced 2026-05-19 04:09:03 +00:00
Add UI for push settings. Added push manager. Added toggle for push enabled/disabled.
This commit is contained in:
parent
e9d03004c7
commit
335453e926
9 changed files with 195 additions and 12 deletions
|
|
@ -22,6 +22,17 @@
|
|||
<string name="pref_reminder_checkbox">Activate Reminder</string>
|
||||
<string name="pref_reminder_picker">Set Reminder Time</string>
|
||||
|
||||
<string name="pref_push_notifications_checkbox">User Push Notifications</string>
|
||||
<string name="push_notifications">Push Notifications</string>
|
||||
<string name="preference_push_you_won_challenge">You won a Challenge!</string>
|
||||
<string name="preference_push_received_a_private_message">Received a Private Message</string>
|
||||
<string name="preference_push_gifted_gems">Gifted Gems</string>
|
||||
<string name="preference_push_gifted_subscription">Gifted Subscription</string>
|
||||
<string name="preference_push_invited_to_party">Invited to Party</string>
|
||||
<string name="preference_push_invited_to_guild">Invited to Guiild</string>
|
||||
<string name="preference_push_your_quest_has_begun">Your Quest has Begun</string>
|
||||
<string name="preference_push_invited_to_quest">Invited to Quest</string>
|
||||
|
||||
<!-- Adding tasks -->
|
||||
<string name="task_value">Value</string>
|
||||
<string name="new_todo">New todo</string>
|
||||
|
|
|
|||
|
|
@ -75,4 +75,67 @@
|
|||
|
||||
</PreferenceCategory>
|
||||
|
||||
<PreferenceCategory
|
||||
android:title="@string/push_notifications">
|
||||
|
||||
<CheckBoxPreference
|
||||
android:key="usePushNotifications"
|
||||
android:defaultValue="true"
|
||||
android:title="@string/pref_push_notifications_checkbox"/>
|
||||
|
||||
<PreferenceScreen
|
||||
android:key="pushNotifications"
|
||||
android:title="@string/push_notifications"
|
||||
android:summary="Set your push notifications settings"
|
||||
android:order="1">
|
||||
|
||||
<PreferenceCategory android:title="Push Notifications">
|
||||
|
||||
<CheckBoxPreference
|
||||
android:key="preference_push_you_won_challenge"
|
||||
android:defaultValue="true"
|
||||
android:title="@string/preference_push_you_won_challenge"/>
|
||||
|
||||
<CheckBoxPreference
|
||||
android:key="preference_push_received_a_private_message"
|
||||
android:defaultValue="true"
|
||||
android:title="@string/preference_push_received_a_private_message"/>
|
||||
|
||||
<CheckBoxPreference
|
||||
android:key="preference_push_gifted_gems"
|
||||
android:defaultValue="true"
|
||||
android:title="@string/preference_push_gifted_gems"/>
|
||||
|
||||
<CheckBoxPreference
|
||||
android:key="preference_push_gifted_subscription"
|
||||
android:defaultValue="true"
|
||||
android:title="@string/preference_push_gifted_subscription"/>
|
||||
|
||||
<CheckBoxPreference
|
||||
android:key="preference_push_invited_to_party"
|
||||
android:defaultValue="true"
|
||||
android:title="@string/preference_push_invited_to_party"/>
|
||||
|
||||
<CheckBoxPreference
|
||||
android:key="preference_push_invited_to_guild"
|
||||
android:defaultValue="true"
|
||||
android:title="@string/preference_push_invited_to_guild"/>
|
||||
|
||||
<CheckBoxPreference
|
||||
android:key="preference_push_your_quest_has_begun"
|
||||
android:defaultValue="true"
|
||||
android:title="@string/preference_push_your_quest_has_begun"/>
|
||||
|
||||
<CheckBoxPreference
|
||||
android:key="preference_push_invited_to_quest"
|
||||
android:defaultValue="true"
|
||||
android:title="@string/preference_push_invited_to_quest"/>
|
||||
|
||||
|
||||
</PreferenceCategory>
|
||||
|
||||
</PreferenceScreen>
|
||||
|
||||
</PreferenceCategory>
|
||||
|
||||
</PreferenceScreen>
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
package com.habitrpg.android.habitica.components;
|
||||
|
||||
import com.habitrpg.android.habitica.HabiticaApplication;
|
||||
import com.habitrpg.android.habitica.helpers.notifications.PushNotificationManager;
|
||||
import com.habitrpg.android.habitica.modules.ApiModule;
|
||||
import com.habitrpg.android.habitica.modules.AppModule;
|
||||
import com.habitrpg.android.habitica.ui.activities.AboutActivity;
|
||||
|
|
@ -156,4 +157,7 @@ public interface AppComponent {
|
|||
void inject(ShopsFragment shopsFragment);
|
||||
|
||||
void inject(ShopFragment shopFragment);
|
||||
|
||||
void inject(PushNotificationManager pushNotificationManager);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,19 +4,26 @@ import android.util.Log;
|
|||
|
||||
import com.google.firebase.iid.FirebaseInstanceId;
|
||||
import com.google.firebase.iid.FirebaseInstanceIdService;
|
||||
import com.habitrpg.android.habitica.APIHelper;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Created by keithholliday on 6/24/16.
|
||||
*/
|
||||
public class HabiticaFirebaseInstanceIDService extends FirebaseInstanceIdService {
|
||||
|
||||
public PushNotificationManager pushNotificationManager;
|
||||
|
||||
@Override
|
||||
public void onTokenRefresh() {
|
||||
// Get updated InstanceID token.
|
||||
pushNotificationManager = PushNotificationManager.getInstance(this);
|
||||
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
|
||||
pushNotificationManager.setRefreshedToken(refreshedToken);
|
||||
Log.d("test", "Refreshed token: " + refreshedToken);
|
||||
|
||||
// TODO: Implement this method to send any registration to your app's servers.
|
||||
// sendRegistrationToServer(refreshedToken);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,19 +1,59 @@
|
|||
package com.habitrpg.android.habitica.helpers.notifications;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.preference.PreferenceManager;
|
||||
|
||||
import com.habitrpg.android.habitica.APIHelper;
|
||||
import com.habitrpg.android.habitica.HabiticaApplication;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Created by keithholliday on 6/27/16.
|
||||
*/
|
||||
public class PushNotificationManager {
|
||||
private static PushNotificationManager instance = null;
|
||||
|
||||
protected PushNotificationManager() {
|
||||
// Exists only to defeat instantiation.
|
||||
private static PushNotificationManager instance = null;
|
||||
private static String DEVICE_TOKEN_PREFERENCE_STRING = "device-token-preference-string";
|
||||
|
||||
@Inject
|
||||
public APIHelper apiHelper;
|
||||
|
||||
private String refreshedToken;
|
||||
private SharedPreferences sharedPreferences;
|
||||
|
||||
protected PushNotificationManager(Context context) {
|
||||
HabiticaApplication.getInstance(context).getComponent().inject(this);
|
||||
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
}
|
||||
|
||||
public static PushNotificationManager getInstance() {
|
||||
public static PushNotificationManager getInstance(Context context) {
|
||||
if(instance == null) {
|
||||
instance = new PushNotificationManager();
|
||||
instance = new PushNotificationManager(context);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void setRefreshedToken (String refreshedToken) {
|
||||
if (this.refreshedToken == null) {
|
||||
this.refreshedToken = refreshedToken;
|
||||
// sharedPreferences.put
|
||||
}
|
||||
}
|
||||
|
||||
//@TODO: Use preferences
|
||||
public void addPushDeviceUsingStoredToken () {
|
||||
Map<String, String> pushDeviceData = new HashMap<String, String>();
|
||||
pushDeviceData.put("regIdRequired", this.refreshedToken);
|
||||
pushDeviceData.put("typeRequired", "android");
|
||||
apiHelper.apiService.addPushDevice(pushDeviceData);
|
||||
}
|
||||
|
||||
public void removePushDeviceUsingStoredToken () {
|
||||
apiHelper.apiService.deletePushDevice(this.refreshedToken);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import com.habitrpg.android.habitica.R;
|
|||
import com.habitrpg.android.habitica.components.AppComponent;
|
||||
import com.habitrpg.android.habitica.ui.fragments.preferences.AccountDetailsFragment;
|
||||
import com.habitrpg.android.habitica.ui.fragments.preferences.PreferencesFragment;
|
||||
import com.habitrpg.android.habitica.ui.fragments.preferences.PushNotificationsPreferencesFragment;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
|
|
@ -88,6 +89,10 @@ public class PrefsActivity extends BaseActivity implements
|
|||
if (preferenceScreen.getKey().equals("accountDetails")) {
|
||||
fragment = new AccountDetailsFragment();
|
||||
}
|
||||
|
||||
if (preferenceScreen.getKey().equals("pushNotifications")) {
|
||||
fragment = new PushNotificationsPreferencesFragment();
|
||||
}
|
||||
return fragment;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import com.habitrpg.android.habitica.APIHelper;
|
|||
import com.habitrpg.android.habitica.HabiticaApplication;
|
||||
import com.habitrpg.android.habitica.NotificationPublisher;
|
||||
import com.habitrpg.android.habitica.R;
|
||||
import com.habitrpg.android.habitica.helpers.notifications.PushNotificationManager;
|
||||
import com.habitrpg.android.habitica.prefs.TimePreference;
|
||||
import com.habitrpg.android.habitica.ui.activities.ClassSelectionActivity;
|
||||
import com.habitrpg.android.habitica.ui.activities.MainActivity;
|
||||
|
|
@ -21,6 +22,8 @@ import android.content.SharedPreferences;
|
|||
import android.os.Bundle;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.Calendar;
|
||||
|
||||
|
|
@ -33,8 +36,11 @@ public class PreferencesFragment extends BasePreferencesFragment implements
|
|||
public APIHelper apiHelper;
|
||||
private Context context;
|
||||
private TimePreference timePreference;
|
||||
private PreferenceScreen pushNotificationsPreference;
|
||||
private Preference classSelectionPreference;
|
||||
private HabitRPGUser user;
|
||||
private PushNotificationManager pushNotificationManager;
|
||||
|
||||
private TransactionListener<HabitRPGUser> userTransactionListener = new TransactionListener<HabitRPGUser>() {
|
||||
@Override
|
||||
public void onResultReceived(HabitRPGUser habitRPGUser) {
|
||||
|
|
@ -64,6 +70,7 @@ public class PreferencesFragment extends BasePreferencesFragment implements
|
|||
new Select().from(HabitRPGUser.class).where(Condition.column("id").eq(userID)).async().querySingle(userTransactionListener);
|
||||
}
|
||||
|
||||
pushNotificationManager = PushNotificationManager.getInstance(this.getActivity());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -72,6 +79,12 @@ public class PreferencesFragment extends BasePreferencesFragment implements
|
|||
boolean useReminder = getPreferenceManager().getSharedPreferences().getBoolean("use_reminder", false);
|
||||
timePreference.setEnabled(useReminder);
|
||||
|
||||
|
||||
pushNotificationsPreference = (PreferenceScreen) findPreference("pushNotifications");
|
||||
boolean userPushNotifications = getPreferenceManager().getSharedPreferences().getBoolean("usePushNotifications", true);
|
||||
pushNotificationsPreference.setEnabled(userPushNotifications);
|
||||
|
||||
|
||||
classSelectionPreference = findPreference("choose_class");
|
||||
classSelectionPreference.setVisible(false);
|
||||
}
|
||||
|
|
@ -176,6 +189,15 @@ public class PreferencesFragment extends BasePreferencesFragment implements
|
|||
} else if (key.equals("reminder_time")) {
|
||||
removeNotifications();
|
||||
scheduleNotifications();
|
||||
} else if (key.equals("usePushNotifications")) {
|
||||
boolean userPushNotifications = sharedPreferences.getBoolean(key, false);
|
||||
pushNotificationsPreference.setEnabled(userPushNotifications);
|
||||
Log.v("test", "test");
|
||||
if (userPushNotifications) {
|
||||
pushNotificationManager.addPushDeviceUsingStoredToken();
|
||||
} else {
|
||||
pushNotificationManager.removePushDeviceUsingStoredToken();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,31 @@
|
|||
package com.habitrpg.android.habitica.ui.fragments.preferences;
|
||||
|
||||
import android.content.ClipData;
|
||||
import android.content.ClipboardManager;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.habitrpg.android.habitica.R;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by keithholliday on 6/27/16.
|
||||
*/
|
||||
public class PushNotificationsPreferencesFragment {
|
||||
}
|
||||
public class PushNotificationsPreferencesFragment extends BasePreferencesFragment implements
|
||||
SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
|
||||
@Override
|
||||
protected void setupPreferences() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -232,10 +232,17 @@ public interface ApiService {
|
|||
//Members URL
|
||||
@POST("members/send-private-message")
|
||||
Observable<PostChatMessageResult> postPrivateMessage(@Body HashMap<String, String> messageDetails);
|
||||
|
||||
|
||||
@GET("shops/{identifier}")
|
||||
Observable<Shop> fetchShopInventory(@Path("identifier") String identifier);
|
||||
|
||||
//Push notifications
|
||||
@POST("/user/push-devices")
|
||||
Observable<Void> addPushDevice(@Body Map<String, String> pushDeviceData);
|
||||
|
||||
@DELETE("/user/push-devices/{regId}")
|
||||
Observable<Void> deletePushDevice(@Path("regId") String regId);
|
||||
|
||||
//DEBUG: These calls only work on a local development server
|
||||
|
||||
@POST("debug/add-ten-gems")
|
||||
|
|
|
|||
Loading…
Reference in a new issue