Skip to content

Commit

Permalink
Merge pull request #172 from Countly/rc-api-turtledreams
Browse files Browse the repository at this point in the history
[A/B API] Adding testFetchAllVariants
  • Loading branch information
ArtursKadikis authored May 31, 2023
2 parents b7dd864 + bbe7a4d commit 7d95450
Show file tree
Hide file tree
Showing 13 changed files with 801 additions and 12 deletions.
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@
android:label="@string/activity_name_feedback"
android:configChanges="orientation|screenSize"></activity>

<activity
android:name=".ActivityExampleTests"
android:exported="false" />

<activity
android:name=".ActivityExampleDeepLinkA"
android:label="Deep link A"
Expand Down
105 changes: 105 additions & 0 deletions app/src/main/java/ly/count/android/demo/ActivityExampleTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package ly.count.android.demo;

import android.content.res.Configuration;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import java.util.Arrays;
import java.util.Map;
import ly.count.android.sdk.Countly;
import ly.count.android.sdk.RequestResponse;
import ly.count.android.sdk.RCVariantCallback;

public class ActivityExampleTests extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example_tests);
}

// For fetching all variants with a button click
public void onClickFetchAllVariants(View v) {
Countly.sharedInstance().remoteConfig().testingFetchVariantInformation(new RCVariantCallback() {
@Override
public void callback(RequestResponse result, String error) {
if (result == RequestResponse.SUCCESS) {
Toast.makeText(getApplicationContext(), "Fetch finished", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Error: " + result, Toast.LENGTH_SHORT).show();
}
}
});
}

// To get all variants from the storage and show them with a toast
public void onClickVariantsPrintValues(View v) {
Map<String, String[]> values = Countly.sharedInstance().remoteConfig().testingGetAllVariants();
if (values == null) {
Countly.sharedInstance().L.w("No variants present");
return;
}
Countly.sharedInstance().L.d("Get all variants: " + values);

StringBuilder sb = new StringBuilder();
sb.append("Stored Variant Values:\n");
for (Map.Entry<String, String[]> entry : values.entrySet()) {
String key = entry.getKey();
String[] variants = entry.getValue();
sb.append(key).append(": ").append(Arrays.toString(variants)).append("\n");
}

Toast t = Toast.makeText(getApplicationContext(), sb.toString(), Toast.LENGTH_LONG);
t.setGravity(Gravity.BOTTOM, 0, 0);
t.show();
}

public void onClickEnrollVariant(View v) {
Map<String, String[]> values = Countly.sharedInstance().remoteConfig().testingGetAllVariants();
if (values == null) {
Countly.sharedInstance().L.w("No variants present");
return;
}
Countly.sharedInstance().L.d("Get all variants: [" + values.toString() + "]");

// Get the first key and variant
String key = null;
String variant = null;
for (Map.Entry<String, String[]> entry : values.entrySet()) {
key = entry.getKey();
variant = entry.getValue()[0]; // first variant
break; // Get only the first key-value pair
}

Countly.sharedInstance().remoteConfig().testingEnrollIntoVariant(key, variant, new RCVariantCallback() {
@Override
public void callback(RequestResponse result, String error) {
if (result == RequestResponse.SUCCESS) {
Toast.makeText(getApplicationContext(), "Fetch finished", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Error: " + result, Toast.LENGTH_SHORT).show();
}
}
});
}

@Override
public void onStart() {
super.onStart();
Countly.sharedInstance().onStart(this);
}

@Override
public void onStop() {
Countly.sharedInstance().onStop();
super.onStop();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Countly.sharedInstance().onConfigurationChanged(newConfig);
}
}
4 changes: 4 additions & 0 deletions app/src/main/java/ly/count/android/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ public void onClickButtonRemoteConfig(View v) {
startActivity(new Intent(this, ActivityExampleRemoteConfig.class));
}

public void onClickButtonTests(View v) {
startActivity(new Intent(this, ActivityExampleTests.class));
}

public void onClickButtonDeviceId(View v) {
startActivity(new Intent(this, ActivityExampleDeviceId.class));
}
Expand Down
43 changes: 43 additions & 0 deletions app/src/main/res/layout/activity_example_tests.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ActivityExampleTests"
>

<LinearLayout
android:layout_width="409dp"
android:layout_height="729dp"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="@dimen/activity_horizontal_margin"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
>
<Button
android:id="@+id/enroll_variant"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClickEnrollVariant"
android:text="Enroll first variant"
/>
<Button
android:id="@+id/get_all_variants"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClickVariantsPrintValues"
android:text="get all variants"
/>
<Button
android:id="@+id/fetch_ab_variants"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClickFetchAllVariants"
android:text="Fetch All vARIANTS"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
8 changes: 8 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,13 @@
android:onClick="onClickButtonOthers"
/>

<Button
android:id="@+id/tests_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClickButtonTests"
android:text="Tests"
/>

</LinearLayout>
</RelativeLayout>
Loading

0 comments on commit 7d95450

Please sign in to comment.