Skip to content

Commit

Permalink
Add option to hide loading dialog (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
DSergiu authored Jul 22, 2021
1 parent 952b4a7 commit af109c5
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ jobs:
with:
java-version: '1.8'
- name: Assemble & Test
run: gradle build --stacktrace
run: ./gradlew build --stacktrace
- name: JitPack Test
run: gradle publishReleasePublicationToMavenLocal
run: ./gradlew publishReleasePublicationToMavenLocal
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ final HCaptchaConfig config = HCaptchaConfig.builder()
.apiEndpoint("https://hcaptcha.com/1/api.js")
.locale("ro")
.size(HCaptchaSize.INVISIBLE)
.loading(false)
.theme(HCaptchaTheme.DARK)
.build();
HCaptcha.getClient(this).verifyWithHCaptcha(config)...;
Expand All @@ -89,6 +90,7 @@ HCaptcha.getClient(this).verifyWithHCaptcha(config)...;
|`reportapi`|String|No|-|See Enterprise docs.|
|`assethost`|String|No|-|See Enterprise docs.|
|`imghost`|String|No|-|See Enterprise docs.|
|`loading`|Boolean|No|True|Show or hide the loading dialog.|

#### Error handling

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.2"
classpath 'com.android.tools.build:gradle:4.1.1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public void onClickHCaptcha(final HCaptchaSize hCaptchaSize) {
.siteKey(YOUR_API_SITE_KEY)
.apiEndpoint("https://hcaptcha.com/1/api.js")
.size(hCaptchaSize)
.loading(true)
.build();
HCaptcha.getClient(this).verifyWithHCaptcha(config)
.addOnSuccessListener(new OnSuccessListener<HCaptchaTokenResponse>() {
Expand Down
Empty file modified gradlew
100644 → 100755
Empty file.
4 changes: 2 additions & 2 deletions sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ android {
// See https://developer.android.com/studio/publish/versioning
// versionCode must be integer and be incremented by one for every new update
// android system uses this to prevent downgrades
versionCode 5
versionCode 6

// version number visible to the user
// should follow semantic versioning (See https://semver.org)
versionName "1.0.3"
versionName "1.1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
Expand Down
6 changes: 6 additions & 0 deletions sdk/src/main/java/com/hcaptcha/sdk/HCaptchaConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public class HCaptchaConfig implements Serializable {
@Builder.Default
private Boolean sentry = true;

/**
* Show / Hide loading dialog.
*/
@Builder.Default
private Boolean loading = true;

/**
* Custom supplied challenge data.
*/
Expand Down
25 changes: 20 additions & 5 deletions sdk/src/main/java/com/hcaptcha/sdk/HCaptchaDialogFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.*;
import android.webkit.*;
import android.widget.LinearLayout;
import androidx.annotation.NonNull;
Expand Down Expand Up @@ -49,6 +47,8 @@ public class HCaptchaDialogFragment extends DialogFragment implements

private HCaptchaJSInterface hCaptchaJsInterface;

private boolean showLoader;

private HCaptchaDialogListener hCaptchaDialogListener;

private View rootView;
Expand Down Expand Up @@ -90,13 +90,15 @@ public void onCreate(Bundle savedInstanceState) {
}
final HCaptchaConfig hCaptchaConfig = (HCaptchaConfig) getArguments().getSerializable(KEY_CONFIG);
this.hCaptchaJsInterface = new HCaptchaJSInterface(hCaptchaConfig, this, this, this);
this.showLoader = hCaptchaConfig.getLoading();
setStyle(STYLE_NO_FRAME, R.style.HCaptchaDialogTheme);
}

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.hcaptcha_fragment, container, false);
loadingContainer = rootView.findViewById(R.id.loadingContainer);
loadingContainer.setVisibility(showLoader ? View.VISIBLE : View.GONE);
webView = rootView.findViewById(R.id.webView);
setupWebView(webView);
return rootView;
Expand All @@ -121,7 +123,12 @@ public void onStart() {
super.onStart();
final Dialog dialog = getDialog();
if (dialog != null) {
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
final Window window = dialog.getWindow();
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
if (!showLoader) {
// Remove dialog shadow to appear completely invisible
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
}
}
}

Expand Down Expand Up @@ -155,7 +162,15 @@ public void onLoaded() {
handler.post(new Runnable() {
@Override
public void run() {
loadingContainer.animate().alpha(0.0f).setDuration(200);
if (showLoader) {
loadingContainer.animate().alpha(0.0f).setDuration(200);
} else {
// Add back dialog shadow in case the checkbox or challenge is shown
final Dialog dialog = getDialog();
if (dialog != null) {
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
}
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void full_config_serialization() throws JsonProcessingException {
.reportapi("https://example.com/reportapi")
.build();
final HCaptchaJSInterface HCaptchaJsInterface = new HCaptchaJSInterface(config, null, null, null);
assertEquals("{\"siteKey\":\"0000-1111-2222-3333\",\"sentry\":true,\"rqdata\":\"custom rqdata\",\"apiEndpoint\":\"127.0.0.1/api.js\",\"endpoint\":\"https://example.com/endpoint\",\"reportapi\":\"https://example.com/reportapi\",\"assethost\":\"https://example.com/assethost\",\"imghost\":\"https://example.com/imghost\",\"locale\":\"ro\",\"size\":\"normal\",\"theme\":\"dark\"}",
assertEquals("{\"siteKey\":\"0000-1111-2222-3333\",\"sentry\":true,\"loading\":true,\"rqdata\":\"custom rqdata\",\"apiEndpoint\":\"127.0.0.1/api.js\",\"endpoint\":\"https://example.com/endpoint\",\"reportapi\":\"https://example.com/reportapi\",\"assethost\":\"https://example.com/assethost\",\"imghost\":\"https://example.com/imghost\",\"locale\":\"ro\",\"size\":\"normal\",\"theme\":\"dark\"}",
HCaptchaJsInterface.getConfig());
}

Expand All @@ -65,7 +65,7 @@ public void subset_config_serialization() throws JsonProcessingException {
.rqdata("custom rqdata")
.build();
final HCaptchaJSInterface HCaptchaJsInterface = new HCaptchaJSInterface(config, null, null, null);
assertEquals("{\"siteKey\":\"0000-1111-2222-3333\",\"sentry\":true,\"rqdata\":\"custom rqdata\",\"apiEndpoint\":\"https://hcaptcha.com/1/api.js\",\"endpoint\":null,\"reportapi\":null,\"assethost\":null,\"imghost\":null,\"locale\":\"ro\",\"size\":\"normal\",\"theme\":\"dark\"}",
assertEquals("{\"siteKey\":\"0000-1111-2222-3333\",\"sentry\":true,\"loading\":true,\"rqdata\":\"custom rqdata\",\"apiEndpoint\":\"https://hcaptcha.com/1/api.js\",\"endpoint\":null,\"reportapi\":null,\"assethost\":null,\"imghost\":null,\"locale\":\"ro\",\"size\":\"normal\",\"theme\":\"dark\"}",
HCaptchaJsInterface.getConfig());
}

Expand Down

0 comments on commit af109c5

Please sign in to comment.