Skip to content

Commit

Permalink
add review manager implementation
Browse files Browse the repository at this point in the history
to try get more reviews
  • Loading branch information
norkator committed Jul 21, 2024
1 parent b6246fe commit e42b1fc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ dependencies {
testImplementation 'org.json:json:20230618'
// Other
implementation(platform('org.jetbrains.kotlin:kotlin-bom:2.0.0'))
implementation('com.google.android.play:review:2.0.1')
}
4 changes: 4 additions & 0 deletions app/src/main/java/com/nitramite/apcupsdmonitor/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ public class Constants {
public final static String SP_UPDATE_INTERVAL = "SP_UPDATE_INTERVAL";
public final static String SP_LAST_PUSH_UPDATE = "SP_LAST_PUSH_UPDATE";

// Others
public static final String SP_APP_LAUNCH_COUNT = "APP_LAUNCH_COUNT";
public static final String SP_HAS_REVIEWED = "HAS_REVIEWED";

}
42 changes: 42 additions & 0 deletions app/src/main/java/com/nitramite/apcupsdmonitor/MainMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,21 @@
import com.android.billingclient.api.Purchase;
import com.android.billingclient.api.PurchasesUpdatedListener;
import com.android.billingclient.api.QueryProductDetailsParams;
import com.google.android.gms.tasks.Task;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.play.core.review.ReviewException;
import com.google.android.play.core.review.ReviewInfo;
import com.google.android.play.core.review.ReviewManager;
import com.google.android.play.core.review.ReviewManagerFactory;
import com.google.android.play.core.review.model.ReviewErrorCode;
import com.nitramite.apcupsdmonitor.notifier.PushUtils;
import com.wdullaer.swipeactionadapter.SwipeActionAdapter;
import com.wdullaer.swipeactionadapter.SwipeDirection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@SuppressWarnings("FieldCanBeLocal")
public class MainMenu extends AppCompatActivity implements ConnectorInterface, PurchasesUpdatedListener,
Expand All @@ -60,6 +67,7 @@ public class MainMenu extends AppCompatActivity implements ConnectorInterface, P
private ArrayList<UPS> upsArrayList = new ArrayList<>();
private ListView upsListView;
private SwipeRefreshLayout swipeRefreshLayout;
private ReviewManager reviewManager;

// Activity request codes
public static final int ACTIVITY_RESULT_NEW_UPS_ADDED = 1;
Expand Down Expand Up @@ -105,6 +113,8 @@ protected void onCreate(Bundle savedInstanceState) {

setAppActivityRunning(true);

reviewManager = ReviewManagerFactory.create(this);

// Floating action buttons
FloatingActionButton floatingAddUpsBtn = findViewById(R.id.floatingAddNewUpsBtn);
floatingAddUpsBtn.setOnClickListener(view -> {
Expand Down Expand Up @@ -137,6 +147,7 @@ protected void onCreate(Bundle savedInstanceState) {

// Get status data
startConnectorTask();
reviewFlow();
} // End of onCreate()


Expand Down Expand Up @@ -410,6 +421,37 @@ public void onRefresh() {
startConnectorTask();
}


private void reviewFlow() {
int launchCount = sharedPreferences.getInt(Constants.SP_APP_LAUNCH_COUNT, 0);
launchCount++;
sharedPreferences.edit().putInt(Constants.SP_APP_LAUNCH_COUNT, launchCount).apply();

if (launchCount % 5 == 0) {
Task<ReviewInfo> request = reviewManager.requestReviewFlow();
request.addOnCompleteListener(task -> {
if (task.isSuccessful()) {
ReviewInfo reviewInfo = task.getResult();
boolean hasReviewed = sharedPreferences.getBoolean(Constants.SP_HAS_REVIEWED, false);
if (!hasReviewed) {
Task<Void> flow = reviewManager.launchReviewFlow(MainMenu.this, reviewInfo);
flow.addOnCompleteListener(t -> {
sharedPreferences.edit().putBoolean(Constants.SP_HAS_REVIEWED, true).apply();
});
} else {
Log.d(TAG, "User has already reviewed the app.");
}
} else {
@ReviewErrorCode int reviewErrorCode = ((ReviewException) Objects.requireNonNull(task.getException())).getErrorCode();
Log.e(TAG, "Review flow error with code " + reviewErrorCode);
}
});
} else {
Log.d(TAG, "App launch count: " + launchCount + ". Not running review flow.");
}
}


// ---------------------------------------------------------------------------------------------

@Override
Expand Down

0 comments on commit e42b1fc

Please sign in to comment.