Skip to content
This repository has been archived by the owner on Oct 17, 2021. It is now read-only.

Feature #65

Merged
merged 6 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ If you have any question or feature idea for app, you can open issue on [this pa

## How to contribute

Read [Commit Convention](https://github.com/fartem/repository-rules/blob/master/commit-convention/COMMIT_CONVENTION.md). Make sure your build is green before you contribute your pull request. Then:
Start with [How to build project](https://github.com/hash-checker/hash-checker-lite/wiki/How-to-build-project).

Before commit, read [Commit Convention](https://github.com/fartem/repository-rules/blob/master/commit-convention/COMMIT_CONVENTION.md). Make sure your build is green before you contribute your pull request. Then:

```shell
$ ./gradlew clean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package com.smlnskgmail.jaman.hashcheckerlite.rateapp;

import android.content.Context;
import android.content.SharedPreferences;

import androidx.test.InstrumentationRegistry;

import com.smlnskgmail.jaman.hashcheckerlite.R;
import com.smlnskgmail.jaman.hashcheckerlite.components.BaseUITest;
import com.smlnskgmail.jaman.hashcheckerlite.logic.hashcalculator.HashType;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.clearText;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.typeText;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static org.junit.Assert.assertTrue;

public class RateAppDialogTest extends BaseUITest {

private static final String TEST_TEXT = "TEST";

private static final HashType TEST_HASH_TYPE = HashType.MD5;

private static final int TEXT_BUTTON_POSITION = 0;
private static final int GENERATE_BUTTON_POSITION = 0;

@Override
public void runTest() throws InterruptedException {
clearRateAppCounter();
for (int i = 0; i < 6; i++) {
showInputDialog();
enterText();
selectHashType();
generateHashFromText();
}
secondDelay();
checkRateAppDialog();
}

private void clearRateAppCounter() {
Context context = InstrumentationRegistry.getTargetContext();
SharedPreferences prefs = context.getSharedPreferences(
context.getPackageName() + "_preferences",
Context.MODE_PRIVATE
);
prefs.edit().putInt(
context.getString(R.string.key_hash_generation_count),
0
).apply();
}

private void showInputDialog() {
clickById(R.id.btn_generate_from);
secondDelay();

inRecyclerViewClickOnPosition(
R.id.rv_bottom_sheet_list_items,
TEXT_BUTTON_POSITION
);
secondDelay();
}

private void enterText() {
onView(withId(R.id.et_dialog_input_text)).perform(clearText());
onView(withId(R.id.et_dialog_input_text)).perform(typeText(TEST_TEXT));
secondDelay();

clickById(R.id.btn_dialog_input_text_add);
secondDelay();
}

private void selectHashType() {
clickById(R.id.tv_selected_hash_type);
secondDelay();

List<HashType> hashTypes = new ArrayList<>(
Arrays.asList(HashType.values())
);
int hashTypePosition = hashTypes.indexOf(TEST_HASH_TYPE);
assertTrue(hashTypePosition >= 0);
inRecyclerViewClickOnPosition(
R.id.rv_bottom_sheet_list_items,
hashTypePosition
);
textEquals(
TEST_HASH_TYPE.getTypeAsString(),
R.id.tv_selected_hash_type
);
}

private void generateHashFromText() throws InterruptedException {
clickById(R.id.btn_hash_actions);
secondDelay();

CountDownLatch countDownLatch = new CountDownLatch(1);
inRecyclerViewClickOnPosition(
R.id.rv_bottom_sheet_list_items,
GENERATE_BUTTON_POSITION
);
countDownLatch.await(
SECOND_IN_MILLIS,
TimeUnit.MILLISECONDS
);
secondDelay();
}

private void checkRateAppDialog() {
onView(withText(R.string.rate_app_message)).check(matches(isDisplayed()));
onView(withText(R.string.common_cancel)).perform(click());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.smlnskgmail.jaman.hashcheckerlite.MainActivity;
import com.smlnskgmail.jaman.hashcheckerlite.R;
import com.smlnskgmail.jaman.hashcheckerlite.components.BaseFragment;
import com.smlnskgmail.jaman.hashcheckerlite.components.dialogs.system.AppAlertDialog;
import com.smlnskgmail.jaman.hashcheckerlite.components.dialogs.system.AppProgressDialog;
import com.smlnskgmail.jaman.hashcheckerlite.components.dialogs.system.AppSnackbar;
import com.smlnskgmail.jaman.hashcheckerlite.components.watchers.AppTextWatcher;
Expand All @@ -45,6 +46,7 @@
import com.smlnskgmail.jaman.hashcheckerlite.logic.support.Clipboard;
import com.smlnskgmail.jaman.hashcheckerlite.logic.themes.api.ThemeHelper;
import com.smlnskgmail.jaman.hashcheckerlite.utils.LogUtils;
import com.smlnskgmail.jaman.hashcheckerlite.utils.WebUtils;

import java.io.File;

Expand Down Expand Up @@ -97,6 +99,23 @@ public class HashCalculatorFragment extends BaseFragment
);
} else {
etGeneratedHash.setText(hashValue);
if (settingsHelper.canShowRateAppDialog()) {
settingsHelper.increaseHashGenerationCount();
new AppAlertDialog(
context,
R.string.settings_title_rate_app,
R.string.rate_app_message,
R.string.rate_app_action,
(dialog, which) -> WebUtils.openGooglePlay(
context,
getView(),
themeHelper
),
themeHelper
).show();
} else {
settingsHelper.increaseHashGenerationCount();
}
}
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,7 @@ public interface SettingsHelper {

void setRefreshSelectedFileStatus(boolean status);

boolean canShowRateAppDialog();

void increaseHashGenerationCount();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
public class SharedPreferencesSettingsHelper implements SettingsHelper {

private final Context context;
private final int HASH_GENERATION_COUNT_BEFORE_RATE_APP_DIALOG_CALL = 5;

public SharedPreferencesSettingsHelper(
@NonNull Context context
Expand Down Expand Up @@ -199,6 +200,30 @@ public void setRefreshSelectedFileStatus(
);
}

public boolean canShowRateAppDialog() {
int hashGenerationCount = getIntPreference(
context,
context.getString(R.string.key_hash_generation_count),
0
);
return hashGenerationCount == HASH_GENERATION_COUNT_BEFORE_RATE_APP_DIALOG_CALL;
}

public void increaseHashGenerationCount() {
int count = getIntPreference(
context,
context.getString(R.string.key_hash_generation_count),
0
);
if (count <= HASH_GENERATION_COUNT_BEFORE_RATE_APP_DIALOG_CALL) {
saveIntPreference(
context,
context.getString(R.string.key_hash_generation_count),
++count
);
}
}

private void saveStringPreference(
@NonNull String key,
@Nullable String value
Expand Down Expand Up @@ -236,4 +261,24 @@ private boolean getBooleanPreference(
.getBoolean(key, defaultValue);
}

private void saveIntPreference(
@NonNull Context context,
@NonNull String key,
int value
) {
PreferenceManager.getDefaultSharedPreferences(context)
.edit()
.putInt(key, value)
.apply();
}

private int getIntPreference(
@NonNull Context context,
@NonNull String key,
int defaultValue
) {
return PreferenceManager.getDefaultSharedPreferences(context)
.getInt(key, defaultValue);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,62 @@
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;

import androidx.annotation.NonNull;

import com.smlnskgmail.jaman.hashcheckerlite.R;
import com.smlnskgmail.jaman.hashcheckerlite.components.dialogs.system.AppSnackbar;
import com.smlnskgmail.jaman.hashcheckerlite.logic.themes.api.ThemeHelper;

public class WebUtils {

private WebUtils() {

}

public static void openGooglePlay(
@NonNull Context context,
@NonNull View view,
ThemeHelper themeHelper
) {
final String appPackageName = context.getPackageName();
Uri link;
try {
link = Uri.parse("market://details?id=" + appPackageName);
context.startActivity(
new Intent(
Intent.ACTION_VIEW,
link
)
);
} catch (ActivityNotFoundException e) {
try {
link = Uri.parse(
"https://play.google.com/store/apps/details?id=" + appPackageName
);
context.startActivity(
new Intent(
Intent.ACTION_VIEW,
link
)
);
} catch (ActivityNotFoundException e2) {

new AppSnackbar(
context,
view,
context.getString(
R.string.message_error_start_google_play
),
themeHelper

).show();

}
}
}

public static void openWebLink(
@NonNull Context context,
@NonNull String link
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings-non-translatable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<string name="key_selected_theme" translatable="false">selected_theme</string>
<string name="key_generate_from_share_intent" translatable="false">generate_from_share_intent</string>
<string name="key_refresh_selected_file" translatable="false">refresh_selected_file</string>
<string name="key_hash_generation_count" translatable="false">hash_generation_count</string>
<string name="key_rate_app" translatable="false">rate_app</string>
<string name="key_multiline" translatable="false">multiline</string>
<string name="key_help_with_translation" translatable="false">help_with_translation</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
<!-- history -->
<string name="history_item_click_text">Hash value copied to clipboard</string>
<!-- rate app -->
<string name="rate_app_message" translatable="false">If you like the app, you can rate it in Google Play</string>
<string name="rate_app_action" translatable="false">Rate</string>
<!-- in-app updates -->
<string name="update_downloaded_message">An update has just been downloaded</string>
<string name="update_restart_action">Restart</string>
Expand Down