Skip to content

Commit b553506

Browse files
Refactor code #21 (#28)
1 parent 0acf35d commit b553506

10 files changed

Lines changed: 283 additions & 220 deletions

File tree

DemoApp/DemoAppJava/app/src/main/java/co/optable/demoappjava/MainActivity.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.google.android.material.bottomnavigation.BottomNavigationView;
1111

1212
public class MainActivity extends AppCompatActivity {
13+
1314
public static OptableSDK OPTABLE;
1415

1516
@Override
@@ -19,13 +20,13 @@ protected void onCreate(Bundle savedInstanceState) {
1920

2021
MainActivity.OPTABLE = new OptableSDK(this.getApplicationContext(), "sandbox.optable.co", "ios-sdk-demo");
2122

23+
initUi();
24+
}
25+
26+
private void initUi() {
2227
BottomNavigationView navView = findViewById(R.id.nav_view);
2328
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
24-
// Passing each menu ID as a set of Ids because each
25-
// menu should be considered as top level destinations.
26-
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
27-
R.id.navigation_identify, R.id.navigation_gambanner)
28-
.build();
29+
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(R.id.navigation_identify, R.id.navigation_gambanner).build();
2930
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
3031
NavigationUI.setupWithNavController(navView, navController);
3132
}

DemoApp/DemoAppJava/app/src/main/java/co/optable/demoappjava/ui/GAMBanner/GAMBannerFragment.java

Lines changed: 101 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import android.view.LayoutInflater;
55
import android.view.View;
66
import android.view.ViewGroup;
7-
import android.widget.Button;
87
import android.widget.TextView;
98
import androidx.annotation.NonNull;
9+
import androidx.annotation.Nullable;
1010
import androidx.fragment.app.Fragment;
1111
import co.optable.android_sdk.OptableSDK;
1212
import co.optable.demoappjava.MainActivity;
@@ -15,123 +15,147 @@
1515
import com.google.android.gms.ads.admanager.AdManagerAdRequest;
1616
import com.google.android.gms.ads.admanager.AdManagerAdView;
1717

18+
import java.util.Collection;
1819
import java.util.HashMap;
1920
import java.util.List;
21+
import java.util.Map;
2022

2123
public class GAMBannerFragment extends Fragment {
2224

2325
private AdManagerAdView mAdView;
24-
private TextView targetingDataView;
26+
private TextView statusTextView;
2527

2628
@Override
27-
public View onCreateView(@NonNull LayoutInflater inflater,
28-
ViewGroup container, Bundle savedInstanceState) {
29+
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
2930
View root = inflater.inflate(R.layout.fragment_gambanner, container, false);
31+
initUi(root);
32+
return root;
33+
}
34+
35+
private void initUi(View root) {
3036
mAdView = root.findViewById(R.id.publisherAdView);
31-
targetingDataView = root.findViewById(R.id.targetingDataView);
32-
33-
// loadAdButton loads targeting data and then the GAM banner:
34-
Button btn = root.findViewById(R.id.loadAdButton);
35-
btn.setOnClickListener(view -> {
36-
targetingDataView.setText("");
37-
38-
MainActivity.OPTABLE.targeting().observe(getViewLifecycleOwner(), result -> {
39-
AdManagerAdRequest.Builder adRequest = new AdManagerAdRequest.Builder();
40-
final StringBuilder msg = new StringBuilder();
41-
msg.append(targetingDataView.getText().toString());
42-
43-
if (result.getStatus() == OptableSDK.Status.SUCCESS) {
44-
msg.append("Loading GAM ad with targeting data:\n\n");
45-
result.getData().forEach((key, values) -> {
46-
adRequest.addCustomTargeting(key, values);
47-
msg.append(key.toString() + " = " + values.toString());
48-
});
49-
} else {
50-
msg.append("OptableSDK Error: " + result.getMessage());
51-
}
52-
53-
targetingDataView.setText(msg.toString());
54-
mAdView.loadAd(adRequest.build());
55-
profile();
56-
witness();
57-
});
58-
});
59-
60-
// loadAdButton2 loads targeting data from cache, and then the GAM banner:
61-
btn = root.findViewById(R.id.loadAdButton2);
62-
btn.setOnClickListener(view -> {
63-
targetingDataView.setText("");
64-
AdRequest.Builder adRequest = new AdRequest.Builder();
65-
final StringBuilder msg = new StringBuilder();
66-
HashMap<String, List<String>> data = MainActivity.OPTABLE.targetingFromCache();
67-
68-
if (data != null) {
69-
msg.append("Loading GAM ad with cached targeting data:\n\n");
70-
data.forEach((key, values) -> {
71-
adRequest.addCustomTargeting(key, values);
72-
msg.append(key.toString() + " = " + values.toString());
37+
statusTextView = root.findViewById(R.id.targetingDataView);
38+
39+
root.findViewById(R.id.btnLoadBanner).setOnClickListener(view -> onClickLoadAd());
40+
root.findViewById(R.id.btnCachedBanner).setOnClickListener(view -> onClickCachedBanner());
41+
root.findViewById(R.id.btnClearCache).setOnClickListener(view -> onClickClearCache());
42+
}
43+
44+
/**
45+
* Loads targeting data and then the GAM banner
46+
*/
47+
private void onClickLoadAd() {
48+
statusTextView.setText("");
49+
50+
MainActivity.OPTABLE
51+
.targeting()
52+
.observe(getViewLifecycleOwner(), result -> {
53+
AdManagerAdRequest.Builder adRequest = new AdManagerAdRequest.Builder();
54+
55+
if (result.getStatus() == OptableSDK.Status.SUCCESS) {
56+
HashMap<String, List<String>> data = result.getData();
57+
changeStatusText("Loading GAM ad with targeting data", data);
58+
59+
if (data != null) {
60+
for (String key : data.keySet()) {
61+
List<String> values = data.get(key);
62+
if (values == null) continue;
63+
adRequest.addCustomTargeting(key, values);
64+
}
65+
}
66+
} else {
67+
changeStatusText("Error getting targeting data: " + result.getMessage(), null);
68+
}
69+
70+
mAdView.loadAd(adRequest.build());
71+
profile();
72+
witness();
7373
});
74-
} else {
75-
msg.append("Targeting data cache empty.");
76-
}
74+
}
7775

78-
targetingDataView.setText(msg.toString());
79-
mAdView.loadAd(adRequest.build());
80-
profile();
81-
witness();
82-
});
76+
/**
77+
* Loads targeting data from cache and then the GAM banner
78+
*/
79+
private void onClickCachedBanner() {
80+
statusTextView.setText("");
81+
82+
AdRequest.Builder adRequest = new AdRequest.Builder();
83+
HashMap<String, List<String>> data = MainActivity.OPTABLE.targetingFromCache();
84+
85+
if (data != null) {
86+
changeStatusText("Loading GAM ad with cached targeting data", data);
87+
for (String key : data.keySet()) {
88+
List<String> values = data.get(key);
89+
if (values == null) continue;
90+
adRequest.addCustomTargeting(key, values);
91+
}
92+
} else {
93+
changeStatusText("Targeting data cache empty.", null);
94+
}
8395

84-
// loadAdButton3 clears targeting data cache:
85-
btn = root.findViewById(R.id.loadAdButton3);
86-
btn.setOnClickListener(view -> {
87-
targetingDataView.setText("Clearing targeting data cache.\n\n");
88-
MainActivity.OPTABLE.targetingClearCache();
89-
});
96+
mAdView.loadAd(adRequest.build());
97+
profile();
98+
witness();
99+
}
90100

91-
return root;
101+
/**
102+
* Clears targeting data cache.
103+
*/
104+
private void onClickClearCache() {
105+
statusTextView.setText("Clearing targeting data cache.\n\n");
106+
MainActivity.OPTABLE.targetingClearCache();
92107
}
93108

94109
private void profile() {
95-
HashMap<String,Object> traits = new HashMap<String,Object>();
110+
HashMap<String, Object> traits = new HashMap<>();
96111
traits.put("gender", "F");
97112
traits.put("age", 38);
98113
traits.put("hasAccount", true);
99114

100115
MainActivity.OPTABLE
101116
.profile(traits)
102117
.observe(getViewLifecycleOwner(), result -> {
103-
final StringBuilder msg = new StringBuilder();
104-
msg.append(targetingDataView.getText().toString());
105-
106118
if (result.getStatus() == OptableSDK.Status.SUCCESS) {
107-
msg.append("\n\nSuccess calling profile API to set user traits.\n\n");
119+
appendStatusText("Success calling profile API to set traits on user.");
108120
} else {
109-
msg.append("\n\nOptableSDK Error: " + result.getMessage() + "\n\n");
121+
appendStatusText("Error during sending profile: " + result.getMessage());
110122
}
111-
112-
targetingDataView.setText(msg.toString());
113123
});
114124
}
115125

116126
private void witness() {
117-
HashMap<String,Object> eventProperties = new HashMap<String,Object>();
127+
HashMap<String, Object> eventProperties = new HashMap<>();
118128
eventProperties.put("exampleKey", "exampleValue");
119129
eventProperties.put("exampleKey2", 123);
120130
eventProperties.put("exampleKey3", false);
121131

122132
MainActivity.OPTABLE
123133
.witness("GAMBannerFragment.loadAdButtonClicked", eventProperties)
124134
.observe(getViewLifecycleOwner(), result -> {
125-
final StringBuilder msg = new StringBuilder();
126-
msg.append(targetingDataView.getText().toString());
127-
128135
if (result.getStatus() == OptableSDK.Status.SUCCESS) {
129-
msg.append("\n\nSuccess calling witness API to log loadAdButtonClicked event.\n\n");
136+
appendStatusText("Success calling witness API to log loadAdButtonClicked event.");
130137
} else {
131-
msg.append("\n\nOptableSDK Error: " + result.getMessage() + "\n\n");
138+
appendStatusText("Error during sending witness: " + result.getMessage());
132139
}
133-
134-
targetingDataView.setText(msg.toString());
135140
});
136141
}
142+
143+
private void changeStatusText(@NonNull String message, @Nullable HashMap<String, List<String>> optableResponse) {
144+
StringBuilder formattedMessage = new StringBuilder(message);
145+
if (optableResponse != null) {
146+
formattedMessage.append("\n\nTargeting data: ");
147+
for (Map.Entry<String, ? extends Collection<?>> entry : optableResponse.entrySet()) {
148+
formattedMessage.append(entry.getKey())
149+
.append(" = ")
150+
.append(entry.getValue())
151+
.append("\n");
152+
}
153+
}
154+
statusTextView.setText(formattedMessage.toString());
155+
}
156+
157+
private void appendStatusText(@NonNull String message) {
158+
statusTextView.append("\n\n" + message);
159+
}
160+
137161
}

DemoApp/DemoAppJava/app/src/main/java/co/optable/demoappjava/ui/Identify/IdentifyFragment.java

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import android.view.LayoutInflater;
55
import android.view.View;
66
import android.view.ViewGroup;
7-
import android.widget.Button;
87
import android.widget.EditText;
98
import android.widget.Switch;
109
import android.widget.TextView;
@@ -15,37 +14,42 @@
1514
import co.optable.demoappjava.R;
1615

1716
public class IdentifyFragment extends Fragment {
17+
1818
private TextView identifyView;
1919
private EditText emailText;
2020
private Switch gaidSwitch;
2121

2222
@Override
23-
public View onCreateView(@NonNull LayoutInflater inflater,
24-
ViewGroup container, Bundle savedInstanceState) {
23+
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
2524
View root = inflater.inflate(R.layout.fragment_identify, container, false);
25+
initUi(root);
26+
return root;
27+
}
28+
29+
private void initUi(View root) {
2630
identifyView = root.findViewById(R.id.identifyView);
2731
emailText = root.findViewById(R.id.editTextTextEmailAddress);
2832
gaidSwitch = root.findViewById(R.id.gaidSwitch);
2933

30-
Button btn = root.findViewById(R.id.identifyButton);
31-
btn.setOnClickListener(view -> {
32-
identifyView.setText("");
33-
MainActivity.OPTABLE
34-
.identify(emailText.getText().toString(), gaidSwitch.isChecked())
35-
.observe(getViewLifecycleOwner(), result -> {
36-
String msg = "Calling identify API... ";
37-
38-
if (result.getStatus() == OptableSDK.Status.SUCCESS) {
39-
msg += "Success";
40-
} else {
41-
msg += "\n\nOptableSDK Error: " + result.getMessage();
42-
}
43-
44-
identifyView.setText(msg);
45-
});
46-
}
47-
);
34+
root.findViewById(R.id.identifyButton).setOnClickListener(view -> onClickIdentify());
35+
}
36+
37+
private void onClickIdentify() {
38+
identifyView.setText("");
4839

49-
return root;
40+
MainActivity.OPTABLE
41+
.identify(emailText.getText().toString(), gaidSwitch.isChecked())
42+
.observe(getViewLifecycleOwner(), result -> {
43+
String msg = "Calling identify API... ";
44+
45+
if (result.getStatus() == OptableSDK.Status.SUCCESS) {
46+
msg += "Success";
47+
} else {
48+
msg += "\n\nOptableSDK Error: " + result.getMessage();
49+
}
50+
51+
identifyView.setText(msg);
52+
});
5053
}
54+
5155
}

DemoApp/DemoAppJava/app/src/main/res/layout/fragment_gambanner.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
app:layout_constraintStart_toStartOf="parent" />
2222

2323
<Button
24-
android:id="@+id/loadAdButton"
24+
android:id="@+id/btnLoadBanner"
2525
android:layout_width="120dp"
2626
android:layout_height="50dp"
2727
android:layout_marginTop="8dp"
@@ -33,7 +33,7 @@
3333
app:layout_constraintTop_toTopOf="parent" />
3434

3535
<Button
36-
android:id="@+id/loadAdButton2"
36+
android:id="@+id/btnCachedBanner"
3737
android:layout_width="120dp"
3838
android:layout_height="50dp"
3939
android:layout_marginTop="8dp"
@@ -45,7 +45,7 @@
4545
app:layout_constraintTop_toTopOf="parent" />
4646

4747
<Button
48-
android:id="@+id/loadAdButton3"
48+
android:id="@+id/btnClearCache"
4949
android:layout_width="120dp"
5050
android:layout_height="50dp"
5151
android:layout_marginTop="8dp"
@@ -63,6 +63,6 @@
6363
android:layout_marginTop="100dp"
6464
app:layout_constraintEnd_toEndOf="parent"
6565
app:layout_constraintStart_toStartOf="parent"
66-
app:layout_constraintTop_toBottomOf="@+id/loadAdButton" />
66+
app:layout_constraintTop_toBottomOf="@+id/btnLoadBanner" />
6767

6868
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)