1

I am planning to implement an easter egg to change my API through an alertdialog, but currently after I change my url endpoint and submit it it saves it in shared preferences, but next time i save it, it does save it in shared preferences ,but doesn't reflect in the app. How do I make it such that every single time i change it ,it applies to my apiendpoint as expected:

Alertdialog which triggers on 5 taps:

    myimage.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      long time= System.currentTimeMillis();


                      //if it is the first time, or if it has been more than 3 seconds since the first tap ( so it is like a new try), we reset everything
                      if (startMillis==0 || (time-startMillis> 3000) ) {
                          startMillis=time;
                          count=1;
                      }
                      //it is not the first, and it has been  less than 3 seconds since the first
                      else{ //  time-startMillis< 3000
                          count++;
                      }

                      if (count==5) {
                          final AlertDialog dialogBuilder = new AlertDialog.Builder(LoginActivity.this).create();
                          LayoutInflater inflater = LoginActivity.this.getLayoutInflater();
                          View dialogView = inflater.inflate(R.layout.custom_dialog, null);

                          final EditText editText = (EditText) dialogView.findViewById(R.id.edt_comment);
                          Button submitButton = (Button) dialogView.findViewById(R.id.buttonSubmit);
                          Button cancelButton = (Button) dialogView.findViewById(R.id.buttonCancel);
                          Button resetButton = (Button) dialogView.findViewById(R.id.buttonReset);
                          editText.setTextColor(getResources().getColor(R.color.black,null));
                          if(SharedPreferencesHelper.getSomeStringValue(getApplicationContext()) != null) {
                              editText.setText( SharedPreferencesHelper.getSomeStringValue(getApplicationContext()));
                          }

                          cancelButton.setOnClickListener(new View.OnClickListener() {
                              @Override
                              public void onClick(View view) {

                                  dialogBuilder.dismiss();
                              }
                          });
                          submitButton.setOnClickListener(new View.OnClickListener() {
                              @Override
                              public void onClick(View view) {
                                  //save the endpoint value in shared preferences
SharedPreferencesHelper.setSomeStringValue(getApplicationContext(),editText.getText().toString());


                                  dialogBuilder.dismiss();
                              }
                          });
                          resetButton.setOnClickListener(new View.OnClickListener() {
                              @Override
                              public void onClick(View v) {
                                  SharedPreferencesHelper.setSomeStringValue(getApplicationContext(),BuildConfig.BASE_URL);

                                  dialogBuilder.dismiss();

                              }
                          });

                          dialogBuilder.setView(dialogView);
                          dialogBuilder.setCanceledOnTouchOutside(false);
                          dialogBuilder.show();
                      }
                  }



        });

Here's my sharedpreferenceshelper:

public class SharedPreferencesHelper {

    private static final String APP_SETTINGS = "APP_SETTINGS";


    // properties
    private static final String SOME_STRING_VALUE = "SOME_STRING_VALUE";
    // other properties...


    private SharedPreferencesHelper() {}

    private static SharedPreferences getSharedPreferences(Context context) {
        return context.getSharedPreferences(APP_SETTINGS, Context.MODE_PRIVATE);
    }

    public static String getSomeStringValue(Context context) {
        return getSharedPreferences(context).getString(SOME_STRING_VALUE , null);
    }

    public static void setSomeStringValue(Context context, String newValue) {
        final SharedPreferences.Editor editor = getSharedPreferences(context).edit();
        editor.putString(SOME_STRING_VALUE , newValue);
        editor.apply();
    }

}

Finally, here's my Apiendpoint class:

final class ApiEndPoint {
 private static String NEW_URL = SharedPreferencesHelper.getSomeStringValue(MyApp.getAppContext());
    static final String ENDPOINT_SERVER_LOGIN = NEW_URL
            + "/service-myverification-link/v1/link/verify";

    private ApiEndPoint() {
        // This class is not publicly instantiable
    }
}

For the code above, I was wondering how do i make the NEW_URL link more dynamic, seems like its sticking to the value saved first time and does not change after. I need to keep this link static due to :

 @Override
    public Single<LoginResponse> doServerLoginApiCall(LoginRequest request) {
            return Rx2AndroidNetworking.post(ApiEndPoint.ENDPOINT_SERVER_LOGIN)
                    .doNotCacheResponse()
                    .addBodyParameter(request)
                    .build()
                    .getObjectSingle(LoginResponse.class);

    }

Any idea, how I can fix this so I can update the link in NEW_URL every single time that it sticks to the new value instead of the value stored the first time?

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • try editor.commit(), maybe you are saving and retrieving values very fast, hence it is not properly being saved inside the preference file. – Abdul Aziz Apr 26 '19 at 11:48

2 Answers2

0

You will need to use the onSharedPreferenceChangeListener

 SharedPreferencesHelper.getSharedPreferences(context).registerOnSharedPreferenceChangeListener(
  new SharedPreferences.OnSharedPreferenceChangeListener() {
    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
       if(key.equals("SOME_STRING_VALUE")){
          NEW_URL = prefs.getString("SOME_STRING_VALUE" , null);
       }
  }
});
Tiago Oliveira
  • 1,582
  • 1
  • 16
  • 31
  • where do i place this, in my activity onresume? i have new_url only defined in apiendpoint, how would it work? –  Apr 21 '19 at 00:37
0

Why don't you send the Url as a parameter every time:

@Override
    public Single<LoginResponse> doServerLoginApiCall(LoginRequest request, String url) {
            return Rx2AndroidNetworking.post(url)
                    .doNotCacheResponse()
                    .addBodyParameter(request)
                    .build()
                    .getObjectSingle(LoginResponse.class);
    }

Method call:

doServerLoginApiCall(request, SharedPreferencesHelper.getSomeStringValue(getApplicationContext()) + ENDPOINT);
Rishabh Jain
  • 297
  • 2
  • 13