From 9ec29e39362914cb5f28c1baf3ea9eb7b04cccf3 Mon Sep 17 00:00:00 2001 From: Boris Timofeev Date: Thu, 31 Aug 2017 16:39:26 +0300 Subject: [PATCH] added donate snackbar --- .../java/org/emunix/unipatcher/Settings.java | 24 +++++++++ .../org/emunix/unipatcher/WorkerService.java | 2 + .../unipatcher/ui/activity/MainActivity.java | 49 +++++++++++++++++-- app/src/main/res/values/strings.xml | 2 + 4 files changed, 73 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/emunix/unipatcher/Settings.java b/app/src/main/java/org/emunix/unipatcher/Settings.java index c71d277..942a255 100644 --- a/app/src/main/java/org/emunix/unipatcher/Settings.java +++ b/app/src/main/java/org/emunix/unipatcher/Settings.java @@ -74,4 +74,28 @@ public class Settings { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); return prefs.getBoolean("ignore_checksum", false); } + + public static void setPatchingSuccessful(Context context, Boolean isSuccessful) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + SharedPreferences.Editor editor = prefs.edit(); + editor.putBoolean("patching_successful", isSuccessful); + editor.apply(); + } + + public static boolean getPatchingSuccessful(Context context) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + return prefs.getBoolean("patching_successful", false); + } + + public static void setDontShowDonateSnackbarCount(Context context, int count) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + SharedPreferences.Editor editor = prefs.edit(); + editor.putInt("dont_show_donate_snackbar", count); + editor.apply(); + } + + public static int getDontShowDonateSnackbarCount(Context context) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + return prefs.getInt("dont_show_donate_snackbar", 0); + } } diff --git a/app/src/main/java/org/emunix/unipatcher/WorkerService.java b/app/src/main/java/org/emunix/unipatcher/WorkerService.java index d77a770..2de0874 100644 --- a/app/src/main/java/org/emunix/unipatcher/WorkerService.java +++ b/app/src/main/java/org/emunix/unipatcher/WorkerService.java @@ -163,6 +163,7 @@ public class WorkerService extends IntentService { if ("ppf".equals(ext)) Utils.copyFile(this, romFile, outputFile); patcher.apply(Settings.getIgnoreChecksum(this)); + Settings.setPatchingSuccessful(this, true); } catch (PatchException | IOException e) { if (Utils.getFreeSpace(outputFile.getParentFile()) == 0) { errorMsg = getString(R.string.notify_error_not_enough_space); @@ -219,6 +220,7 @@ public class WorkerService extends IntentService { try { patcher.create(); + Settings.setPatchingSuccessful(this, true); } catch (PatchException | IOException e) { if (Utils.getFreeSpace(patchFile.getParentFile()) == 0) { errorMsg = getString(R.string.notify_error_not_enough_space); diff --git a/app/src/main/java/org/emunix/unipatcher/ui/activity/MainActivity.java b/app/src/main/java/org/emunix/unipatcher/ui/activity/MainActivity.java index 780df60..f74a1ca 100644 --- a/app/src/main/java/org/emunix/unipatcher/ui/activity/MainActivity.java +++ b/app/src/main/java/org/emunix/unipatcher/ui/activity/MainActivity.java @@ -25,6 +25,7 @@ import android.net.Uri; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.NavigationView; +import android.support.design.widget.Snackbar; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; @@ -40,6 +41,7 @@ import android.view.View; import org.emunix.unipatcher.BuildConfig; import org.emunix.unipatcher.R; +import org.emunix.unipatcher.Settings; import org.emunix.unipatcher.UniPatcher; import org.emunix.unipatcher.ui.fragment.ActionFragment; import org.emunix.unipatcher.ui.fragment.CreatePatchFragment; @@ -47,6 +49,8 @@ import org.emunix.unipatcher.ui.fragment.PatchingFragment; import org.emunix.unipatcher.ui.fragment.SmdFixChecksumFragment; import org.emunix.unipatcher.ui.fragment.SnesSmcHeaderFragment; +import java.util.Random; + import static android.support.v7.app.AppCompatDelegate.MODE_NIGHT_AUTO; import static android.support.v7.app.AppCompatDelegate.MODE_NIGHT_NO; import static android.support.v7.app.AppCompatDelegate.MODE_NIGHT_YES; @@ -78,8 +82,6 @@ public class MainActivity extends AppCompatActivity if (fragment != null) { boolean ret = fragment.runAction(); } - //Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) - // .setAction("Action", null).show(); } }); @@ -91,6 +93,7 @@ public class MainActivity extends AppCompatActivity } parseArgument(); + showDonateSnackbar(); } private void setTheme() { @@ -132,8 +135,7 @@ public class MainActivity extends AppCompatActivity } else if (id == R.id.nav_rate) { rateApp(); } else if (id == R.id.nav_donate) { - Intent donateIntent = new Intent(this, DonateActivity.class); - startActivity(donateIntent); + showDonateActivity(); } else if (id == R.id.nav_share) { shareApp(); } else if (id == R.id.nav_help) { @@ -175,6 +177,45 @@ public class MainActivity extends AppCompatActivity } } + private void showDonateSnackbar() { + // don't show snackbar if the user did not patch the file successfully + if (!Settings.getPatchingSuccessful(this)) + return; + + // don't show snackbar some time if the user swiped off it before + int count = Settings.getDontShowDonateSnackbarCount(this); + if (count != 0) { + Settings.setDontShowDonateSnackbarCount(this, --count); + return; + } + + // don't show snackbar each time you open the application + if (new Random().nextInt(6) != 0) + return; + + Snackbar.make(findViewById(R.id.content_frame), R.string.main_activity_donate_snackbar_text, Snackbar.LENGTH_INDEFINITE) + .setAction(R.string.main_activity_donate_snackbar_button, new View.OnClickListener() { + @Override + public void onClick(View v) { + showDonateActivity(); + } + }) + .addCallback(new Snackbar.Callback() { + @Override + public void onDismissed(Snackbar snackbar, int event) { + if (event == Snackbar.Callback.DISMISS_EVENT_SWIPE) { + Settings.setDontShowDonateSnackbarCount(getApplicationContext(), 30); + } + } + } + ).show(); + } + + private void showDonateActivity() { + Intent donateIntent = new Intent(this, DonateActivity.class); + startActivity(donateIntent); + } + private void shareApp() { Intent shareIntent = new Intent(Intent.ACTION_SEND); shareIntent.setType("text/plain"); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6efbb50..a35444f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -16,6 +16,8 @@ No ROM file selected No patch file selected Archive should be unpacked in an external program + Support the development of UniPatcher by making a donation + Donate Rename