Skip to content

Commit

Permalink
feat: Port code to dart
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee31416 committed Jun 12, 2024
1 parent 838cde0 commit 86a0d8e
Show file tree
Hide file tree
Showing 30 changed files with 831 additions and 151 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@ app.*.symbols
app.*.map.json

# Test related
coverage
coverage

# Mockito generated files.
*.mocks.dart
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
## 0.3.0

- Migrated the pipeline from the .NET version of the package.
- Ported the code from the .NET version of the package to dart.
321 changes: 300 additions & 21 deletions README.md

Large diffs are not rendered by default.

79 changes: 0 additions & 79 deletions build/azure-pipelines.yml

This file was deleted.

27 changes: 0 additions & 27 deletions build/gitversion-config.yml

This file was deleted.

5 changes: 5 additions & 0 deletions build/native_assets/windows/native_assets.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
format-version:
- 1
- 0
- 0
native-assets: {}
Binary file added build/test_cache/build/cache.dill.track.dill
Binary file not shown.
Binary file added build/unit_test_assets/AssetManifest.bin
Binary file not shown.
1 change: 1 addition & 0 deletions build/unit_test_assets/AssetManifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions build/unit_test_assets/FontManifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Binary file added build/unit_test_assets/NOTICES.Z
Binary file not shown.
Binary file added build/unit_test_assets/shaders/ink_sparkle.frag
Binary file not shown.
7 changes: 0 additions & 7 deletions build/variables.yml

This file was deleted.

4 changes: 0 additions & 4 deletions lib/review_service.dart

This file was deleted.

7 changes: 0 additions & 7 deletions lib/src/review_service.dart

This file was deleted.

15 changes: 15 additions & 0 deletions lib/src/review_service/asynchronous_review_condition.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:review_service/src/review_service/review_condition_interface.dart';
import 'package:review_service/src/review_service/review_settings.dart';

/// Asynchronous implementation of [ReviewCondition].
class AsynchronousReviewCondition<TReviewSettings extends ReviewSettings>
implements ReviewCondition<TReviewSettings> {
final Future<bool> Function(TReviewSettings, DateTime) _condition;

AsynchronousReviewCondition(this._condition);

@override
Future<bool> validate(TReviewSettings currentSettings, DateTime currentDateTime) {
return _condition(currentSettings, currentDateTime);
}
}
17 changes: 17 additions & 0 deletions lib/src/review_service/logging_review_prompter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:logger/logger.dart';
import 'package:review_service/src/review_service/review_prompter.dart';

/// Implementation of [ReviewPrompter] that logs information.
final class LoggingReviewPrompter implements ReviewPrompter {
final Logger logger;

/// <summary>
/// Initializes a new instance of the <see cref="LoggingReviewPrompter"/> class.
/// </summary>
LoggingReviewPrompter({Logger? logger}) : logger = logger ?? Logger();

@override
Future<void> tryPrompt() async {
logger.i('TryPrompt was invoked.');
}
}
19 changes: 19 additions & 0 deletions lib/src/review_service/memory_review_settings_source.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:review_service/src/review_service/review_settings.dart';
import 'package:review_service/src/review_service/review_settings_source.dart';

/// In-memory implementation of [ReviewSettingsSource].
final class MemoryReviewSettingsSource<TReviewSettings extends ReviewSettings> implements ReviewSettingsSource<TReviewSettings> {
TReviewSettings _reviewSettings = const ReviewSettings() as TReviewSettings;

@override
Future<TReviewSettings> read() {
return Future.value(_reviewSettings);
}

@override
Future<void> write(TReviewSettings reviewSettings) {
_reviewSettings = reviewSettings;

return Future.value();
}
}
36 changes: 36 additions & 0 deletions lib/src/review_service/review_condition_builder.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'package:review_service/src/review_service/review_condition_interface.dart';
import 'package:review_service/src/review_service/review_conditions_builder.extensions.dart';
import 'package:review_service/src/review_service/review_settings.dart';

/// Provide a way to gather the review conditions.
abstract interface class ReviewConditionsBuilder<TReviewSettings extends ReviewSettings> {
/// Gets the review conditions.
List<ReviewCondition<TReviewSettings>> get conditions;
}

/// Implementation of [ReviewConditionsBuilder].
/// Provides methods to get an empty or default [ReviewConditionsBuilder].
final class ReviewConditionsBuilderImplementation<TReviewSettings extends ReviewSettings>
implements ReviewConditionsBuilder<TReviewSettings> {

ReviewConditionsBuilderImplementation();

@override
List<ReviewCondition<TReviewSettings>> conditions = [];

static ReviewConditionsBuilder<TReviewSettings>
empty<TReviewSettings extends ReviewSettings>() {
return ReviewConditionsBuilderImplementation<TReviewSettings>();
}

static ReviewConditionsBuilder<TReviewSettings>
defaultBuilder<TReviewSettings extends ReviewSettings>() {
return ReviewConditionsBuilderImplementation<TReviewSettings>()
..minimumApplicationLaunchCount(3)
..minimumElapsedTimeSinceApplicationFirstLaunch(const Duration(days: 5))
..minimumPrimaryActionsCompleted(2)
..minimumElapsedTimeSinceLastReviewRequest(const Duration(days: 15));
}


}
10 changes: 10 additions & 0 deletions lib/src/review_service/review_condition_interface.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:review_service/src/review_service/review_settings.dart';

/// A condition used to determine if a review should be requested based on [ReviewSettings].
abstract interface class ReviewCondition<TReviewSettings extends ReviewSettings> {
/// Validates that the condition is satisfied.
Future<bool> validate(
TReviewSettings currentSettings,
DateTime currentDateTime,
);
}
64 changes: 64 additions & 0 deletions lib/src/review_service/review_conditions_builder.extensions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import 'package:review_service/src/review_service/asynchronous_review_condition.dart';
import 'package:review_service/src/review_service/review_condition_builder.dart';
import 'package:review_service/src/review_service/review_settings.dart';
import 'package:review_service/src/review_service/synchronous_review_condition.dart';

/// Extensions for [ReviewConditionsBuilder].
extension ReviewConditionBuilderExtensions<TReviewSettings extends ReviewSettings>
on ReviewConditionsBuilder<TReviewSettings> {

/// The number of completed primary actions must be greater than the review settings.
ReviewConditionsBuilder<TReviewSettings> minimumPrimaryActionsCompleted(int minimumActionCompleted) {
conditions.add(SynchronousReviewCondition<TReviewSettings>(
(reviewSettings, currentDateTime) => reviewSettings.primaryActionCompletedCount >= minimumActionCompleted,
));
return this;
}

/// The number of completed secondary actions must be greater than the review settings.
ReviewConditionsBuilder<TReviewSettings> minimumSecondaryActionsCompleted(int minimumActionCompleted) {
conditions.add(SynchronousReviewCondition<TReviewSettings>(
(reviewSettings, currentDateTime) => reviewSettings.secondaryActionCompletedCount >= minimumActionCompleted,
));
return this;
}

/// The number of times the application has been launched must be greater than the review settings.
ReviewConditionsBuilder<TReviewSettings> minimumApplicationLaunchCount(int minimumCount) {
conditions.add(SynchronousReviewCondition<TReviewSettings>(
(reviewSettings, currentDateTime) => reviewSettings.applicationLaunchCount >= minimumCount,
));
return this;
}

/// The elapsed time since the first application launch must be greater than the review settings.
ReviewConditionsBuilder<TReviewSettings> minimumElapsedTimeSinceApplicationFirstLaunch(Duration minimumTimeElapsed) {
conditions.add(SynchronousReviewCondition<TReviewSettings>(
(reviewSettings, currentDateTime) => reviewSettings.firstApplicationLaunch != null &&
reviewSettings.firstApplicationLaunch!.add(minimumTimeElapsed).isBefore(currentDateTime),
));
return this;
}

/// The time elapsed since the last review requested must be greater than the review settings.
ReviewConditionsBuilder<TReviewSettings> minimumElapsedTimeSinceLastReviewRequest(Duration minimumTimeElapsed) {
conditions.add(SynchronousReviewCondition<TReviewSettings>(
(reviewSettings, currentDateTime) => reviewSettings.lastRequest == null ||
reviewSettings.lastRequest!.add(minimumTimeElapsed).isBefore(currentDateTime),
));
return this;
}

/// Adds a custom synchronous condition.
ReviewConditionsBuilder<TReviewSettings> custom(bool Function(TReviewSettings, DateTime) condition) {
conditions.add(SynchronousReviewCondition<TReviewSettings>(condition));
return this;
}

/// Adds a custom asynchronous condition.
ReviewConditionsBuilder<TReviewSettings> customAsync(
Future<bool> Function(TReviewSettings, DateTime) condition) {
conditions.add(AsynchronousReviewCondition<TReviewSettings>(condition));
return this;
}
}
35 changes: 35 additions & 0 deletions lib/src/review_service/review_prompter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'package:in_app_review/in_app_review.dart';
import 'package:logger/logger.dart';

/// Provides ways to prompt user to review the current application.
abstract interface class ReviewPrompter {
factory ReviewPrompter({
required Logger logger,
}) = _ReviewPrompter;

/// Prompts the user to rate the current application using the platform's default application store.
Future<void> tryPrompt();
}

/// Implementation of [ReviewPrompter].
final class _ReviewPrompter implements ReviewPrompter {
final Logger _logger;

_ReviewPrompter({
required Logger logger,
}) : _logger = logger;

@override
Future<void> tryPrompt() async {
_logger.d('Trying to prompt user to review the current application.');

if (await InAppReview.instance.isAvailable()) {
InAppReview.instance.requestReview();

_logger.i('Prompted user to review the current application.');
} else {
_logger.i(
'Failed to prompt user to review the current application because the platform does not support it.');
}
}
}
Loading

0 comments on commit 86a0d8e

Please sign in to comment.