From 19784f0e82871fb8a7b136a0271f41db0132e012 Mon Sep 17 00:00:00 2001 From: Neslihan Date: Sun, 23 Jul 2017 18:10:42 +0300 Subject: [PATCH] Add shared preferences --- .../free/nrw/commons/CommonsApplication.java | 3 + .../contributions/ContributionsActivity.java | 103 ++++++++++++------ 2 files changed, 71 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/CommonsApplication.java b/app/src/main/java/fr/free/nrw/commons/CommonsApplication.java index b78bfdc6f..3de167b82 100644 --- a/app/src/main/java/fr/free/nrw/commons/CommonsApplication.java +++ b/app/src/main/java/fr/free/nrw/commons/CommonsApplication.java @@ -11,6 +11,7 @@ import android.content.pm.PackageManager; import android.database.sqlite.SQLiteDatabase; import android.preference.PreferenceManager; import android.support.v4.util.LruCache; +import android.util.Log; import com.facebook.drawee.backends.pipeline.Fresco; import com.facebook.stetho.Stetho; @@ -139,6 +140,8 @@ public class CommonsApplication extends Application { System.setProperty("in.yuvi.http.fluent.PROGRESS_TRIGGER_THRESHOLD", "3.0"); Fresco.initialize(this); + PreferenceManager.getDefaultSharedPreferences(CommonsApplication.getInstance()).edit() + .putBoolean("is_app_started", true).commit(); //For caching area -> categories cacheData = new CacheController(); diff --git a/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsActivity.java b/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsActivity.java index 74cff702b..0793db8be 100644 --- a/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsActivity.java +++ b/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsActivity.java @@ -30,7 +30,10 @@ import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import butterknife.ButterKnife; import fr.free.nrw.commons.CommonsApplication; @@ -116,41 +119,7 @@ public class ContributionsActivity @Override protected void onStart() { super.onStart(); - if (true) { - AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setTitle(getResources().getString(R.string.feedback_popup_title)); - builder.setMessage(getResources().getString(R.string.feedback_popup_description)); - builder.setPositiveButton(getResources().getString(R.string.feedback_popup_accept) - , new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int which) { - // Go to the page - Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getResources() - .getString(R.string.feedback_page_url))); - startActivity(browserIntent); - dialog.dismiss(); - } - }); - builder.setNeutralButton(getResources().getString(R.string.feedback_popup_remind) - ,new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - // Dismiss the dialog to show it later - dialog.dismiss(); - } - }); - builder.setNegativeButton(getResources().getString(R.string.feedback_popup_decline) - , new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - // Dismiss the dialog and not to show it later - dialog.dismiss(); - } - }); - AlertDialog alert = builder.create(); - alert.show(); - } else { - - } + displayFeedbackPopup(); } @Override @@ -385,4 +354,68 @@ public class ContributionsActivity Intent contributionsIntent = new Intent(context, ContributionsActivity.class); context.startActivity(contributionsIntent); } + + private void displayFeedbackPopup(){ + + Date strDate = null; + try { + String valid_until = "23/08/2017"; + SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); + strDate = sdf.parse(valid_until); + } catch (ParseException e) { + e.printStackTrace(); + } + + final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences + (CommonsApplication.getInstance()); + + // boolean to save users request about displaying popup + boolean displayFeedbackPopup = prefs.getBoolean("display_feedbak_popup", true); + + // boolean to recognize is application re-started. Will be used for "remind me later" option + boolean isApplicationStarted = prefs.getBoolean("is_app_started" , true); + + // if time is valid and shared pref says display + if (new Date().before(strDate) && displayFeedbackPopup && isApplicationStarted) { + // The window will be displayed once per application start + prefs.edit().putBoolean("is_app_started" , false).commit(); + + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(getResources().getString(R.string.feedback_popup_title)); + builder.setMessage(getResources().getString(R.string.feedback_popup_description)); + builder.setPositiveButton(getResources().getString(R.string.feedback_popup_accept) + , new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + // Go to the page + Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri + .parse(getResources() + .getString(R.string.feedback_page_url))); + startActivity(browserIntent); + // No need to dislay this window to the user again. + prefs.edit().putBoolean("display_feedbak_popup" , false).commit(); + dialog.dismiss(); + } + }); + builder.setNeutralButton(getResources().getString(R.string.feedback_popup_remind) + ,new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + // Dismiss the dialog to show it later, + // don't set "display_feedbak_popup" to false, user wants to see it latr + dialog.dismiss(); + } + }); + builder.setNegativeButton(getResources().getString(R.string.feedback_popup_decline) + , new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + // Dismiss the dialog and not to show it later + prefs.edit().putBoolean("display_feedbak_popup", false).commit(); + dialog.dismiss(); + } + }); + AlertDialog alert = builder.create(); + alert.show(); + } + } }