Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: lint example app #112

Merged
merged 3 commits into from
Oct 21, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions example/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id 'com.android.application'
id "com.diffplug.spotless" version "7.0.0.BETA2"
}

def localProperties = new Properties()
Expand Down Expand Up @@ -38,6 +39,21 @@ android {
}
}

spotless {
format 'misc', {
target '*.gradle', '.gitattributes', '.gitignore'

trimTrailingWhitespace()
indentWithSpaces(2)
endWithNewline()
}
java {
target '**/*.java'
googleJavaFormat()
formatAnnotations()
}
}

dependencies {
implementation 'androidx.appcompat:appcompat:1.7.0'
implementation 'com.google.android.material:material:1.12.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cloud.eppo.androidexample;

public class Constants {
public static final String INITIAL_SUBJECT_ID = "";
public static final String INITIAL_FLAG_KEY = "";
public static final String INITIAL_SUBJECT_ID = "";
public static final String INITIAL_FLAG_KEY = "";
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package cloud.eppo.androidexample;

import android.app.Application;
import android.util.Log;

import cloud.eppo.android.EppoClient;

import com.geteppo.androidexample.BuildConfig;

public class EppoApplication extends Application {
private static final String TAG = EppoApplication.class.getSimpleName();
private static final String API_KEY = BuildConfig.API_KEY; // Set in root-level local.properties

@Override
public void onCreate() {
super.onCreate();
private static final String TAG = EppoApplication.class.getSimpleName();
private static final String API_KEY = BuildConfig.API_KEY; // Set in root-level local.properties

}
@Override
public void onCreate() {
super.onCreate();
}
}
63 changes: 31 additions & 32 deletions example/src/main/java/cloud/eppo/androidexample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,48 @@
import android.text.TextUtils;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.geteppo.androidexample.BuildConfig;
import com.geteppo.androidexample.R;

import cloud.eppo.android.ConfigCacheFile;
import cloud.eppo.android.EppoClient;
import com.geteppo.androidexample.BuildConfig;
import com.geteppo.androidexample.R;

public class MainActivity extends AppCompatActivity {
private static final String API_KEY = BuildConfig.API_KEY; // Set in root-level local.properties
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button_start_assigner);
private static final String API_KEY = BuildConfig.API_KEY; // Set in root-level local.properties

Intent launchAssigner = new Intent(MainActivity.this, SecondActivity.class);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button_start_assigner);

button.setOnClickListener(view -> startActivity(launchAssigner));
Intent launchAssigner = new Intent(MainActivity.this, SecondActivity.class);

Button offlineButton = findViewById(R.id.button_start_offline_assigner);
offlineButton.setOnClickListener(view -> startActivity(launchAssigner.putExtra(this.getPackageName()+ ".offlineMode", true)));
button.setOnClickListener(view -> startActivity(launchAssigner));

Button clearCacheButton = findViewById(R.id.button_clear_cache);
clearCacheButton.setOnClickListener(view -> clearCacheFile());
}
Button offlineButton = findViewById(R.id.button_start_offline_assigner);
offlineButton.setOnClickListener(
view ->
startActivity(launchAssigner.putExtra(this.getPackageName() + ".offlineMode", true)));

private void clearCacheFile() {
String cacheFileNameSuffix = safeCacheKey(API_KEY);
ConfigCacheFile cacheFile =
new ConfigCacheFile(getApplication(), cacheFileNameSuffix);
cacheFile.delete();
Toast.makeText(this, "Cache Cleared", Toast.LENGTH_SHORT).show();
}
Button clearCacheButton = findViewById(R.id.button_clear_cache);
clearCacheButton.setOnClickListener(view -> clearCacheFile());
}

private void clearCacheFile() {
String cacheFileNameSuffix = safeCacheKey(API_KEY);
ConfigCacheFile cacheFile = new ConfigCacheFile(getApplication(), cacheFileNameSuffix);
cacheFile.delete();
Toast.makeText(this, "Cache Cleared", Toast.LENGTH_SHORT).show();
}

@Override
protected void onResume() {
super.onResume();
@Override
protected void onResume() {
super.onResume();

// for testing assignments on application/main activity start
if (!TextUtils.isEmpty(INITIAL_FLAG_KEY)) {
EppoClient.getInstance().getStringAssignment(INITIAL_FLAG_KEY, INITIAL_SUBJECT_ID, "");
}
// for testing assignments on application/main activity start
if (!TextUtils.isEmpty(INITIAL_FLAG_KEY)) {
EppoClient.getInstance().getStringAssignment(INITIAL_FLAG_KEY, INITIAL_SUBJECT_ID, "");
}
}
}
142 changes: 75 additions & 67 deletions example/src/main/java/cloud/eppo/androidexample/SecondActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cloud.eppo.androidexample;

import static cloud.eppo.android.util.Utils.safeCacheKey;
import static cloud.eppo.androidexample.Constants.INITIAL_FLAG_KEY;
import static cloud.eppo.androidexample.Constants.INITIAL_SUBJECT_ID;

Expand All @@ -11,79 +10,88 @@
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.geteppo.androidexample.BuildConfig;
import com.geteppo.androidexample.R;

import cloud.eppo.android.EppoClient;
import cloud.eppo.api.Attributes;
import com.geteppo.androidexample.BuildConfig;
import com.geteppo.androidexample.R;

public class SecondActivity extends AppCompatActivity {
private static final String TAG = SecondActivity.class.getSimpleName();
private static final String API_KEY = BuildConfig.API_KEY; // Set in root-level local.properties
private EditText experiment;
private EditText subject;
private TextView assignmentLog;
private ScrollView assignmentLogScrollView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Bundle extras = getIntent().getExtras();
boolean offlineMode = extras != null && extras.getBoolean(this.getPackageName() + ".offlineMode", false);

new EppoClient.Builder(API_KEY, getApplication())
.isGracefulMode(true)
.offlineMode(offlineMode)
.forceReinitialize(true)
.assignmentLogger(assignment -> {
Log.d(TAG, assignment.getExperiment() + "-> subject: " + assignment.getSubject() + " assigned to " + assignment.getExperiment());
})
.buildAndInitAsync().thenAccept(
client -> {
Log.d(TAG, "Eppo SDK initialized");

}).exceptionally(error ->
{
throw new RuntimeException("Unable to initialize. Ensure you API key is set correctly in EppoApplication.java", error);
});

setContentView(R.layout.activity_assigner);

experiment = findViewById(R.id.experiment);
subject = findViewById(R.id.subject);
assignmentLog = findViewById(R.id.assignment_log);
assignmentLogScrollView = findViewById(R.id.assignment_log_scrollview);

experiment.setText(INITIAL_FLAG_KEY);
subject.setText(INITIAL_SUBJECT_ID);

findViewById(R.id.btn_assign).setOnClickListener(view -> handleAssignment());
private static final String TAG = SecondActivity.class.getSimpleName();
private static final String API_KEY = BuildConfig.API_KEY; // Set in root-level local.properties
private EditText experiment;
private EditText subject;
private TextView assignmentLog;
private ScrollView assignmentLogScrollView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Bundle extras = getIntent().getExtras();
boolean offlineMode =
extras != null && extras.getBoolean(this.getPackageName() + ".offlineMode", false);

new EppoClient.Builder(API_KEY, getApplication())
.isGracefulMode(true)
.offlineMode(offlineMode)
.forceReinitialize(true)
.assignmentLogger(
assignment -> {
Log.d(
TAG,
assignment.getExperiment()
+ "-> subject: "
+ assignment.getSubject()
+ " assigned to "
+ assignment.getExperiment());
})
.buildAndInitAsync()
.thenAccept(
client -> {
Log.d(TAG, "Eppo SDK initialized");
})
.exceptionally(
error -> {
throw new RuntimeException(
"Unable to initialize. Ensure you API key is set correctly in EppoApplication.java",
error);
});

setContentView(R.layout.activity_assigner);

experiment = findViewById(R.id.experiment);
subject = findViewById(R.id.subject);
assignmentLog = findViewById(R.id.assignment_log);
assignmentLogScrollView = findViewById(R.id.assignment_log_scrollview);

experiment.setText(INITIAL_FLAG_KEY);
subject.setText(INITIAL_SUBJECT_ID);

findViewById(R.id.btn_assign).setOnClickListener(view -> handleAssignment());
}

private void handleAssignment() {
String subjectId = subject.getText().toString();
if (TextUtils.isEmpty(subjectId)) {
appendToAssignmentLogView("Subject must not be empty");
return;
}

private void handleAssignment() {
String subjectId = subject.getText().toString();
if (TextUtils.isEmpty(subjectId)) {
appendToAssignmentLogView("Subject must not be empty");
return;
}

String experimentKey = experiment.getText().toString();
if (TextUtils.isEmpty(experimentKey)) {
appendToAssignmentLogView("Experiment key must not be empty");
return;
}

String assignedVariation = EppoClient.getInstance().getStringAssignment(
experimentKey, subjectId, new Attributes(), "");
appendToAssignmentLogView("Assigned variation: " + assignedVariation);
String experimentKey = experiment.getText().toString();
if (TextUtils.isEmpty(experimentKey)) {
appendToAssignmentLogView("Experiment key must not be empty");
return;
}

private void appendToAssignmentLogView(String message) {
assignmentLog.append(message + "\n\n");
assignmentLogScrollView.post(() -> assignmentLogScrollView.fullScroll(View.FOCUS_DOWN));
}
String assignedVariation =
EppoClient.getInstance()
.getStringAssignment(experimentKey, subjectId, new Attributes(), "");
appendToAssignmentLogView("Assigned variation: " + assignedVariation);
}

private void appendToAssignmentLogView(String message) {
assignmentLog.append(message + "\n\n");
assignmentLogScrollView.post(() -> assignmentLogScrollView.fullScroll(View.FOCUS_DOWN));
}
}
Loading