Skip to content

Commit

Permalink
#6 Custom title, description and buttons for the dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
javiersantos committed Feb 23, 2016
1 parent 1ab7515 commit eaa56c5
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 65 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ repositories {
And add the library to your module **build.gradle**:
```Javascript
dependencies {
compile 'com.github.javiersantos:AppUpdater:1.1'
compile 'com.github.javiersantos:AppUpdater:1.2'
}
```

Expand Down Expand Up @@ -62,12 +62,14 @@ Use the builder and add following:
.setDisplay(Display.SNACKBAR)
.setDisplay(Display.NOTIFICATION)
```

```Java
// (Optional) Provide a duration for the Snackbars.
// Default: Duration.NORMAL
.setDuration(Duration.NORMAL)
.setDuration(Duration.INDEFINITE)
```

```Java
// (Optional) Provide a source for the updates.
// Default: UpdateFrom.GOOGLE_PLAY
Expand All @@ -76,21 +78,34 @@ Use the builder and add following:
.setUpdateFrom(UpdateFrom.AMAZON)
.setUpdateFrom(UpdateFrom.FDROID)
```

```Java
// (Required for GITHUB, optional otherwise) Provide the GitHub user and repo where releases are available.
.setGitHubUserAndRepo("javiersantos", "AppUpdater")
```

```Java
// (Optional) Updates will be displayed only every X times the app ascertains that a new update is available.
// Default: 1 (Always)
.showEvery(5)
```

```Java
// (Optional) Show dialog, snackbar or notification although there aren't updates.
// Default: false
.showAppUpdated(true)
```

```Java
// Customize the dialog title, description and buttons
.setDialogTitleWhenUpdateAvailable("Update available")
.setDialogDescriptionWhenUpdateAvailable("Check out the latest version available of my app!")
.setDialogButtonUpdate("Update now?")
.setDialogButtonDoNotShowAgain("Huh, not interested")
.setDialogTitleWhenUpdateNotAvailable("Update not available")
.setDialogDescriptionWhenUpdateNotAvailable("No update available. Check for updates again later!")
```

## Other features
### Get the latest update and compare with the installed one (asynchronous)
```Java
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.javiersantos.appupdater;

import android.content.Context;
import android.support.annotation.NonNull;

import com.github.javiersantos.appupdater.enums.Display;
import com.github.javiersantos.appupdater.enums.Duration;
Expand All @@ -9,20 +10,30 @@

public class AppUpdater {
private Context context;
private LibraryPreferences libraryPreferences;
private Display display;
private UpdateFrom updateFrom;
private Duration duration;
private GitHub gitHub;
private Integer showEvery;
private Boolean showAppUpdated;
private String titleUpdate, descriptionUpdate, btnUpdate, btnDisable; // Update available
private String titleNoUpdate, descriptionNoUpdate; // Update not available

public AppUpdater(Context context) {
this.context = context;
this.libraryPreferences = new LibraryPreferences(context);
this.display = Display.DIALOG;
this.updateFrom = UpdateFrom.GOOGLE_PLAY;
this.duration = Duration.NORMAL;
this.showEvery = 1;
this.showAppUpdated = false;

// Dialog
this.titleUpdate = context.getResources().getString(R.string.appupdater_update_available);
this.titleNoUpdate = context.getResources().getString(R.string.appupdater_update_not_available);
this.btnUpdate = context.getResources().getString(R.string.appupdater_btn_update);
this.btnDisable = context.getResources().getString(R.string.appupdater_btn_disable);
}

/**
Expand Down Expand Up @@ -68,7 +79,7 @@ public AppUpdater setDuration(Duration duration) {
* @param repo GitHub repository
* @return this
*/
public AppUpdater setGitHubUserAndRepo(String user, String repo) {
public AppUpdater setGitHubUserAndRepo(@NonNull String user, @NonNull String repo) {
this.gitHub = new GitHub(user, repo);
return this;
}
Expand All @@ -95,27 +106,143 @@ public AppUpdater showAppUpdated(Boolean res) {
return this;
}

/**
* Set a custom title for the dialog when an update is available.
*
* @param title for the dialog
* @return this
*/
public AppUpdater setDialogTitleWhenUpdateAvailable(@NonNull String title) {
this.titleUpdate = title;
return this;
}

/**
* Set a custom description for the dialog when an update is available.
*
* @param description for the dialog
* @return this
*/
public AppUpdater setDialogDescriptionWhenUpdateAvailable(@NonNull String description) {
this.descriptionUpdate = description;
return this;
}

/**
* Set a custom title for the dialog when no update is available.
*
* @param title for the dialog
* @return this
*/
public AppUpdater setDialogTitleWhenUpdateNotAvailable(@NonNull String title) {
this.titleNoUpdate = title;
return this;
}

/**
* Set a custom description for the dialog when no update is available.
*
* @param description for the dialog
* @return this
*/
public AppUpdater setDialogDescriptionWhenUpdateNotAvailable(@NonNull String description) {
this.descriptionNoUpdate = description;
return this;
}

/**
* Set a custom "Update" button text when a new update is available.
*
* @param text for the update button
* @return this
*/
public AppUpdater setDialogButtonUpdate(@NonNull String text) {
this.btnUpdate = text;
return this;
}

/**
* Set a custom "Don't show again" button text when a new update is available.
*
* @param text for the disable button
* @return this
*/
public AppUpdater setDialogButtonDoNotShowAgain(@NonNull String text) {
this.btnDisable = text;
return this;
}

/**
* Execute AppUpdater in background.
*
* @return this
* @deprecated use {@link #start()} instead
*/
public AppUpdater init() {
UtilsAsync.LatestAppVersion latestAppVersion = new UtilsAsync.LatestAppVersion(context, showEvery, showAppUpdated, updateFrom, display, duration, gitHub, null);
latestAppVersion.execute();
start();
return this;
}

/**
* Execute AppUpdater in background.
*
* @return this
*/
public AppUpdater start() {
UtilsAsync.LatestAppVersion latestAppVersion = new UtilsAsync.LatestAppVersion(context, showEvery, showAppUpdated, updateFrom, display, duration, gitHub, null);
public void start() {
UtilsAsync.LatestAppVersion latestAppVersion = new UtilsAsync.LatestAppVersion(context, false, updateFrom, gitHub, new LibraryListener() {
@Override
public void onSuccess(String version) {
if (UtilsLibrary.isUpdateAvailable(UtilsLibrary.getAppInstalledVersion(context), version)) {
Integer successfulChecks = libraryPreferences.getSuccessfulChecks();
if (UtilsLibrary.isAbleToShow(successfulChecks, showEvery)) {
switch (display) {
case DIALOG:
UtilsDisplay.showUpdateAvailableDialog(context, titleUpdate, getDescriptionUpdate(context, version), btnUpdate, btnDisable, updateFrom, gitHub);
break;
case SNACKBAR:
UtilsDisplay.showUpdateAvailableSnackbar(context, String.format(context.getResources().getString(R.string.appupdater_update_available_description_snackbar), UtilsLibrary.getAppInstalledVersion(context)), UtilsLibrary.getDurationEnumToBoolean(duration), updateFrom, gitHub);
break;
case NOTIFICATION:
UtilsDisplay.showUpdateAvailableNotification(context, context.getResources().getString(R.string.appupdater_update_available), String.format(context.getResources().getString(R.string.appupdater_update_available_description_notification), version, UtilsLibrary.getAppName(context)), updateFrom, gitHub);
break;
}
}
libraryPreferences.setSuccessfulChecks(successfulChecks + 1);
} else if (showAppUpdated) {
switch (display) {
case DIALOG:
UtilsDisplay.showUpdateNotAvailableDialog(context, titleNoUpdate, getDescriptionNoUpdate(context));
break;
case SNACKBAR:
UtilsDisplay.showUpdateNotAvailableSnackbar(context, context.getResources().getString(R.string.appupdater_update_not_available_description), UtilsLibrary.getDurationEnumToBoolean(duration));
break;
case NOTIFICATION:
UtilsDisplay.showUpdateNotAvailableNotification(context, context.getResources().getString(R.string.appupdater_update_not_available), String.format(context.getResources().getString(R.string.appupdater_update_not_available_description), UtilsLibrary.getAppName(context)));
break;
}
}
}
});

latestAppVersion.execute();
return this;
}

interface LibraryListener {
void onSuccess(String version);
}

private String getDescriptionUpdate(Context context, String version) {
if (descriptionUpdate == null) {
return String.format(context.getResources().getString(R.string.appupdater_update_available_description_dialog), version, UtilsLibrary.getAppName(context));
} else {
return descriptionUpdate;
}
}

private String getDescriptionNoUpdate(Context context) {
if (descriptionNoUpdate == null) {
return String.format(context.getResources().getString(R.string.appupdater_update_not_available_description), UtilsLibrary.getAppName(context));
} else {
return descriptionNoUpdate;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,16 @@ public AppUpdaterUtils withListener(AppUpdaterListener appUpdaterListener) {

/**
* Execute AppUpdaterUtils in background.
*
* @return this
*/
public AppUpdaterUtils start() {
UtilsAsync.LatestAppVersion latestAppVersion = new UtilsAsync.LatestAppVersion(context, null, null, updateFrom, null, null, gitHub, appUpdaterListener);
public void start() {
UtilsAsync.LatestAppVersion latestAppVersion = new UtilsAsync.LatestAppVersion(context, true, updateFrom, gitHub, new AppUpdater.LibraryListener() {
@Override
public void onSuccess(String version) {
appUpdaterListener.onSuccess(version, UtilsLibrary.isUpdateAvailable(UtilsLibrary.getAppInstalledVersion(context), version));
}
});

latestAppVersion.execute();
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import android.content.Context;
import android.os.AsyncTask;

import com.github.javiersantos.appupdater.enums.Display;
import com.github.javiersantos.appupdater.enums.Duration;
import com.github.javiersantos.appupdater.enums.UpdateFrom;
import com.github.javiersantos.appupdater.objects.GitHub;

Expand All @@ -13,32 +11,26 @@ class UtilsAsync {
static class LatestAppVersion extends AsyncTask<Void, Void, String> {
private Context context;
private LibraryPreferences libraryPreferences;
private Integer showEvery;
private Boolean showAppUpdated;
private Boolean fromUtils;
private UpdateFrom updateFrom;
private Display display;
private Duration duration;
private GitHub gitHub;
private AppUpdaterUtils.AppUpdaterListener appUpdaterListener;
private AppUpdater.LibraryListener listener;

public LatestAppVersion(Context context, Integer showEvery, Boolean showAppUpdated, UpdateFrom updateFrom, Display display, Duration duration, GitHub gitHub, AppUpdaterUtils.AppUpdaterListener appUpdaterListener) {
public LatestAppVersion(Context context, Boolean fromUtils, UpdateFrom updateFrom, GitHub gitHub, AppUpdater.LibraryListener libraryListener) {
this.context = context;
this.libraryPreferences = new LibraryPreferences(context);
this.showEvery = showEvery;
this.showAppUpdated = showAppUpdated;
this.fromUtils = fromUtils;
this.updateFrom = updateFrom;
this.display = display;
this.duration = duration;
this.gitHub = gitHub;
this.appUpdaterListener = appUpdaterListener;
this.listener = libraryListener;
}

@Override
protected void onPreExecute() {
super.onPreExecute();

if (UtilsLibrary.isNetworkAvailable(context)) {
if (appUpdaterListener == null && !libraryPreferences.getAppUpdaterShow()) { cancel(true); }
if (!fromUtils && !libraryPreferences.getAppUpdaterShow()) { cancel(true); }
else {
if (updateFrom == UpdateFrom.GITHUB) {
if (!GitHub.isGitHubValid(gitHub)) { cancel(true); }
Expand All @@ -55,40 +47,7 @@ protected String doInBackground(Void... voids) {
@Override
protected void onPostExecute(String version) {
super.onPostExecute(version);

if (appUpdaterListener == null) {
if (UtilsLibrary.isUpdateAvailable(UtilsLibrary.getAppInstalledVersion(context), version)) {
Integer successfulChecks = libraryPreferences.getSuccessfulChecks();
if (UtilsLibrary.isAbleToShow(successfulChecks, showEvery)) {
switch (display) {
case DIALOG:
UtilsDisplay.showUpdateAvailableDialog(context, context.getResources().getString(R.string.appupdater_update_available), String.format(context.getResources().getString(R.string.appupdater_update_available_description_dialog), version, UtilsLibrary.getAppName(context)), updateFrom, gitHub);
break;
case SNACKBAR:
UtilsDisplay.showUpdateAvailableSnackbar(context, String.format(context.getResources().getString(R.string.appupdater_update_available_description_snackbar), UtilsLibrary.getAppInstalledVersion(context)), UtilsLibrary.getDurationEnumToBoolean(duration), updateFrom, gitHub);
break;
case NOTIFICATION:
UtilsDisplay.showUpdateAvailableNotification(context, context.getResources().getString(R.string.appupdater_update_available), String.format(context.getResources().getString(R.string.appupdater_update_available_description_notification), version, UtilsLibrary.getAppName(context)), updateFrom, gitHub);
break;
}
}
libraryPreferences.setSuccessfulChecks(successfulChecks + 1);
} else if (showAppUpdated) {
switch (display) {
case DIALOG:
UtilsDisplay.showUpdateNotAvailableDialog(context, context.getResources().getString(R.string.appupdater_update_not_available), String.format(context.getResources().getString(R.string.appupdater_update_not_available_description), UtilsLibrary.getAppName(context)));
break;
case SNACKBAR:
UtilsDisplay.showUpdateNotAvailableSnackbar(context, context.getResources().getString(R.string.appupdater_update_not_available_description), UtilsLibrary.getDurationEnumToBoolean(duration));
break;
case NOTIFICATION:
UtilsDisplay.showUpdateNotAvailableNotification(context, context.getResources().getString(R.string.appupdater_update_not_available), String.format(context.getResources().getString(R.string.appupdater_update_not_available_description), UtilsLibrary.getAppName(context)));
break;
}
}
} else {
appUpdaterListener.onSuccess(version, UtilsLibrary.isUpdateAvailable(UtilsLibrary.getAppInstalledVersion(context), version));
}
listener.onSuccess(version);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@

class UtilsDisplay {

static void showUpdateAvailableDialog(final Context context, String title, String content, final UpdateFrom updateFrom, final GitHub gitHub) {
static void showUpdateAvailableDialog(final Context context, String title, String content, String btnPositive, String btnNeutral, final UpdateFrom updateFrom, final GitHub gitHub) {
final LibraryPreferences libraryPreferences = new LibraryPreferences(context);

MaterialDialog materialDialog = new MaterialDialog.Builder(context)
.title(title)
.content(content)
.positiveText(context.getResources().getString(R.string.appupdater_btn_update))
.positiveText(btnPositive)
.negativeText(context.getResources().getString(android.R.string.cancel))
.neutralText(context.getResources().getString(R.string.appupdater_btn_disable))
.neutralText(btnNeutral)
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(MaterialDialog dialog, DialogAction which) {
Expand Down

0 comments on commit eaa56c5

Please sign in to comment.