Skip to content

Commit

Permalink
Added a custom condition example.
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyost committed Jan 2, 2020
1 parent 83962b8 commit 3d4ba19
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ You can easily create your custom conditions ! All you have to do is to extend t
* `reset` You should reset your condition values here.
* `isMet` Whether this condition is met.
* `onEventOccurred` When an event occurs in the plugin lifecycle. This is usually here that you can update your condition values.
Please note that you're not obligated to override this one (although this is recommended).

You can have an easy example of it by checking the source code of [`DoNotOpenAgainCondition`](https://github.com/Skyost/rate_my_app/tree/master/lib/src/conditions.dart#L163).

Expand Down
72 changes: 72 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:rate_my_app/rate_my_app.dart';
import 'package:shared_preferences/shared_preferences.dart';

/// Main Rate my app instance.
RateMyApp _rateMyApp = RateMyApp();
Expand All @@ -8,6 +9,7 @@ RateMyApp _rateMyApp = RateMyApp();
void main() {
WidgetsFlutterBinding.ensureInitialized(); // This allows to use async methods in the main method without any problem.

// _rateMyApp.conditions.add(MaxDialogOpeningCondition(_rateMyApp)); // This one is a little example of a custom condition. See below for more info.
_rateMyApp.init().then((_) {
// We initialize our Rate my app instance.
runApp(_RateMyAppTestApp());
Expand Down Expand Up @@ -139,3 +141,73 @@ class _RateMyAppTestAppBodyState extends State<_RateMyAppTestAppBody> {
textAlign: TextAlign.center,
);
}

/// Here's an example of a custom condition.
/// Will not be met if the dialog has been opened too many times.
class MaxDialogOpeningCondition extends DebuggableCondition {
/// Maximum default dialog opening count (inclusive).
final int maxDialogOpeningCount;
/// Maximum star dialog opening count (inclusive).
final int maxStarDialogOpeningCount;
/// Current dialog opening count.
int dialogOpeningCount;
/// Current star dialog opening count.
int starDialogOpeningCount;

/// Creates a new max dialog opening condition instance.
MaxDialogOpeningCondition(
RateMyApp rateMyApp, {
this.maxDialogOpeningCount = 3,
this.maxStarDialogOpeningCount = 3,
}) : assert(maxDialogOpeningCount != null),
assert(maxStarDialogOpeningCount != null),
super(rateMyApp);

@override
void readFromPreferences(SharedPreferences preferences) {
// Here we can read the values (or we set their default values).
dialogOpeningCount = preferences.getInt(rateMyApp.preferencesPrefix + 'dialogOpeningCount') ?? 0;
starDialogOpeningCount = preferences.getInt(rateMyApp.preferencesPrefix + 'starDialogOpeningCount') ?? 0;
}

@override
Future<void> saveToPreferences(SharedPreferences preferences) async {
// Here we save our current values.
await preferences.setInt(rateMyApp.preferencesPrefix + 'dialogOpeningCount', dialogOpeningCount);
return preferences.setInt(rateMyApp.preferencesPrefix + 'starDialogOpeningCount', starDialogOpeningCount);
}

@override
void reset() {
// Allows to reset this condition values back to their default values.
dialogOpeningCount = 0;
starDialogOpeningCount = 0;
}

@override
bool onEventOccurred(RateMyAppEventType eventType) {
if(eventType == RateMyAppEventType.dialogOpen) { // If the default dialog has been opened, we update our default dialog counter.
dialogOpeningCount++;
return true; // Returning true allows to trigger a shared preferences save.
}

if(eventType == RateMyAppEventType.starDialogOpen) { // If the star dialog has been opened, we update our star dialog counter.
starDialogOpeningCount++;
return true;
}

return false; // Otherwise, no need to save anything.
}

@override
String valuesAsString() {
// Allows to easily debug this condition.
return 'Dialog opening count : ' + dialogOpeningCount.toString() + '\nMax dialog opening count : ' + maxDialogOpeningCount.toString() + 'Star dialog opening count : ' + starDialogOpeningCount.toString() + '\nMax star dialog opening count : ' + maxStarDialogOpeningCount.toString();
}

@override
bool get isMet {
// This allows to check whether this condition is met in its current state.
return dialogOpeningCount <= maxDialogOpeningCount && starDialogOpeningCount <= maxStarDialogOpeningCount;
}
}

0 comments on commit 3d4ba19

Please sign in to comment.