Skip to content

Commit 180a493

Browse files
author
AR Abdul Azeez
committed
better ui
1 parent 3578d69 commit 180a493

5 files changed

Lines changed: 104 additions & 84 deletions

File tree

Examples/OneSignalDemoV2/app/src/main/java/com/onesignal/sdktest/data/repository/OneSignalRepository.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,6 @@ class OneSignalRepository {
8181
OneSignal.User.removeTag(key)
8282
}
8383

84-
fun removeAllTags(keys: Collection<String>) {
85-
Log.d(TAG, "Removing all tags: $keys")
86-
if (keys.isNotEmpty()) {
87-
OneSignal.User.removeTags(keys)
88-
}
89-
}
90-
9184
fun getTags(): Map<String, String> {
9285
return OneSignal.User.getTags()
9386
}
@@ -103,6 +96,13 @@ class OneSignalRepository {
10396
OneSignal.InAppMessages.removeTrigger(key)
10497
}
10598

99+
fun clearTriggers(keys: Collection<String>) {
100+
Log.d(TAG, "Clearing all triggers: $keys")
101+
if (keys.isNotEmpty()) {
102+
OneSignal.InAppMessages.removeTriggers(keys)
103+
}
104+
}
105+
106106
// Outcome operations
107107
fun sendOutcome(name: String) {
108108
Log.d(TAG, "Sending outcome: $name")

Examples/OneSignalDemoV2/app/src/main/java/com/onesignal/sdktest/ui/main/MainActivity.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,16 @@ class MainActivity : AppCompatActivity() {
206206
}
207207
}
208208

209-
binding.btnRemoveAllTags.setOnClickListener {
210-
viewModel.removeAllTags()
211-
}
212-
213209
binding.btnAddTrigger.setOnClickListener {
214210
showAddPairDialog(getString(R.string.add_trigger_title)) { key, value ->
215211
viewModel.addTrigger(key, value)
216212
}
217213
}
218214

215+
binding.btnClearTriggers.setOnClickListener {
216+
viewModel.clearTriggers()
217+
}
218+
219219
binding.btnSendOutcome.setOnClickListener {
220220
showOutcomeDialog()
221221
}
@@ -322,14 +322,15 @@ class MainActivity : AppCompatActivity() {
322322
val hasItems = tags.isNotEmpty()
323323
binding.rvTags.visibility = if (hasItems) View.VISIBLE else View.GONE
324324
binding.tvNoTags.visibility = if (hasItems) View.GONE else View.VISIBLE
325-
binding.layoutRemoveAllTags.visibility = if (hasItems) View.VISIBLE else View.GONE
326325
}
327326

328327
// Triggers
329328
viewModel.triggers.observe(this) { triggers ->
330329
triggersAdapter.submitList(triggers)
331330
binding.rvTriggers.visibility = if (triggers.isNotEmpty()) View.VISIBLE else View.GONE
332331
binding.tvNoTriggers.visibility = if (triggers.isEmpty()) View.VISIBLE else View.GONE
332+
// Show Clear Triggers when at least 1 trigger exists
333+
binding.layoutClearTriggers.visibility = if (triggers.isNotEmpty()) View.VISIBLE else View.GONE
333334
}
334335

335336
// In-App Messages Paused

Examples/OneSignalDemoV2/app/src/main/java/com/onesignal/sdktest/ui/main/MainViewModel.kt

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ class MainViewModel(application: Application) : AndroidViewModel(application), I
193193

194194
android.util.Log.d("MainViewModel", "User data loaded from API: aliases=${aliasesList.size}, tags=${tagsList.size}, emails=${emailsList.size}, sms=${smsNumbersList.size}")
195195
android.util.Log.d("MainViewModel", "LiveData values - aliases=${_aliases.value?.size}, tags=${_tags.value?.size}, emails=${_emails.value?.size}, sms=${_smsNumbers.value?.size}")
196+
197+
// Small delay to let UI render before dismissing loading indicator
198+
kotlinx.coroutines.delay(100)
196199
} else {
197200
android.util.Log.w("MainViewModel", "Failed to fetch user data from API")
198201
}
@@ -324,7 +327,10 @@ class MainViewModel(application: Application) : AndroidViewModel(application), I
324327
viewModelScope.launch(Dispatchers.IO) {
325328
repository.addAlias(label, id)
326329
withContext(Dispatchers.Main) {
327-
loadExistingAliases()
330+
// Add to local list directly (SDK syncs to server asynchronously)
331+
aliasesList.removeAll { it.first == label }
332+
aliasesList.add(Pair(label, id))
333+
refreshAliases()
328334
showToast("Alias added: $label")
329335
}
330336
}
@@ -334,7 +340,8 @@ class MainViewModel(application: Application) : AndroidViewModel(application), I
334340
viewModelScope.launch(Dispatchers.IO) {
335341
repository.removeAlias(label)
336342
withContext(Dispatchers.Main) {
337-
loadExistingAliases()
343+
aliasesList.removeAll { it.first == label }
344+
refreshAliases()
338345
showToast("Alias removed: $label")
339346
}
340347
}
@@ -345,7 +352,8 @@ class MainViewModel(application: Application) : AndroidViewModel(application), I
345352
val labels = aliasesList.map { it.first }
346353
repository.removeAllAliases(labels)
347354
withContext(Dispatchers.Main) {
348-
loadExistingAliases()
355+
aliasesList.clear()
356+
refreshAliases()
349357
showToast("All aliases removed")
350358
}
351359
}
@@ -422,17 +430,6 @@ class MainViewModel(application: Application) : AndroidViewModel(application), I
422430
}
423431
}
424432

425-
fun removeAllTags() {
426-
viewModelScope.launch(Dispatchers.IO) {
427-
val keys = tagsList.map { it.first }
428-
repository.removeAllTags(keys)
429-
withContext(Dispatchers.Main) {
430-
loadExistingTags()
431-
showToast("All tags removed")
432-
}
433-
}
434-
}
435-
436433
// Trigger operations
437434
fun addTrigger(key: String, value: String) {
438435
viewModelScope.launch(Dispatchers.IO) {
@@ -457,6 +454,18 @@ class MainViewModel(application: Application) : AndroidViewModel(application), I
457454
}
458455
}
459456

457+
fun clearTriggers() {
458+
viewModelScope.launch(Dispatchers.IO) {
459+
val keys = triggersList.map { it.first }
460+
repository.clearTriggers(keys)
461+
withContext(Dispatchers.Main) {
462+
triggersList.clear()
463+
refreshTriggers()
464+
showToast("All triggers cleared")
465+
}
466+
}
467+
}
468+
460469
// Outcome operations
461470
fun sendOutcome(name: String) {
462471
viewModelScope.launch(Dispatchers.IO) {

Examples/OneSignalDemoV2/app/src/main/res/layout/activity_main.xml

Lines changed: 67 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,50 @@
205205

206206
</androidx.cardview.widget.CardView>
207207

208-
<!-- Login User Button -->
208+
<!-- External User ID Display (shown when logged in) - ABOVE buttons -->
209+
<androidx.cardview.widget.CardView
210+
android:id="@+id/layout_external_id"
211+
android:layout_width="match_parent"
212+
android:layout_height="wrap_content"
213+
android:layout_marginStart="12dp"
214+
android:layout_marginEnd="12dp"
215+
android:layout_marginBottom="12dp"
216+
android:visibility="gone"
217+
app:cardCornerRadius="6dp"
218+
app:cardElevation="2dp"
219+
app:cardBackgroundColor="#E8F5E9">
220+
221+
<LinearLayout
222+
android:layout_width="match_parent"
223+
android:layout_height="wrap_content"
224+
android:orientation="vertical"
225+
android:padding="16dp"
226+
android:gravity="center">
227+
228+
<TextView
229+
android:layout_width="wrap_content"
230+
android:layout_height="wrap_content"
231+
android:text="@string/logged_in_as"
232+
android:textColor="@color/colorDarkText"
233+
android:textSize="16sp" />
234+
235+
<TextView
236+
android:id="@+id/tv_external_id"
237+
android:layout_width="match_parent"
238+
android:layout_height="wrap_content"
239+
android:layout_marginTop="4dp"
240+
android:textColor="#2E7D32"
241+
android:textSize="22sp"
242+
android:textStyle="bold"
243+
android:gravity="center"
244+
android:ellipsize="middle"
245+
android:maxLines="1" />
246+
247+
</LinearLayout>
248+
249+
</androidx.cardview.widget.CardView>
250+
251+
<!-- Login/Switch User Button -->
209252
<Button
210253
android:id="@+id/btn_login_user"
211254
android:layout_width="match_parent"
@@ -218,39 +261,6 @@
218261
android:textColor="@android:color/white"
219262
android:background="@color/colorPrimary"/>
220263

221-
<!-- External User ID Display (shown when logged in) -->
222-
<LinearLayout
223-
android:id="@+id/layout_external_id"
224-
android:layout_width="match_parent"
225-
android:layout_height="wrap_content"
226-
android:orientation="horizontal"
227-
android:layout_marginStart="12dp"
228-
android:layout_marginEnd="12dp"
229-
android:layout_marginBottom="12dp"
230-
android:gravity="center_vertical"
231-
android:visibility="gone">
232-
233-
<TextView
234-
android:layout_width="wrap_content"
235-
android:layout_height="wrap_content"
236-
android:text="@string/logged_in_as"
237-
android:textColor="@color/colorDarkText"
238-
android:textSize="14sp" />
239-
240-
<TextView
241-
android:id="@+id/tv_external_id"
242-
android:layout_width="0dp"
243-
android:layout_height="wrap_content"
244-
android:layout_weight="1"
245-
android:layout_marginStart="8dp"
246-
android:textColor="@color/colorPrimary"
247-
android:textSize="14sp"
248-
android:textStyle="bold"
249-
android:ellipsize="end"
250-
android:maxLines="1" />
251-
252-
</LinearLayout>
253-
254264
<!-- Logout User Button -->
255265
<Button
256266
android:id="@+id/btn_logout_user"
@@ -977,29 +987,6 @@
977987
android:textColor="@android:color/white"
978988
android:background="@color/colorPrimary"/>
979989

980-
<LinearLayout
981-
android:id="@+id/layout_remove_all_tags"
982-
android:layout_width="match_parent"
983-
android:layout_height="wrap_content"
984-
android:orientation="vertical"
985-
android:visibility="gone">
986-
987-
<View
988-
android:layout_width="match_parent"
989-
android:layout_height="1dp"
990-
android:background="@color/colorDivider"/>
991-
992-
<Button
993-
android:id="@+id/btn_remove_all_tags"
994-
android:layout_width="match_parent"
995-
android:layout_height="56dp"
996-
android:text="@string/remove_all_tags"
997-
android:textSize="19sp"
998-
android:textColor="@android:color/white"
999-
android:background="@color/colorRed"/>
1000-
1001-
</LinearLayout>
1002-
1003990
</LinearLayout>
1004991

1005992
</androidx.cardview.widget.CardView>
@@ -1135,6 +1122,29 @@
11351122
android:textColor="@android:color/white"
11361123
android:background="@color/colorPrimary"/>
11371124

1125+
<LinearLayout
1126+
android:id="@+id/layout_clear_triggers"
1127+
android:layout_width="match_parent"
1128+
android:layout_height="wrap_content"
1129+
android:orientation="vertical"
1130+
android:visibility="gone">
1131+
1132+
<View
1133+
android:layout_width="match_parent"
1134+
android:layout_height="1dp"
1135+
android:background="@color/colorDivider"/>
1136+
1137+
<Button
1138+
android:id="@+id/btn_clear_triggers"
1139+
android:layout_width="match_parent"
1140+
android:layout_height="56dp"
1141+
android:text="@string/clear_triggers"
1142+
android:textSize="19sp"
1143+
android:textColor="@android:color/white"
1144+
android:background="@color/colorRed"/>
1145+
1146+
</LinearLayout>
1147+
11381148
</LinearLayout>
11391149

11401150
</androidx.cardview.widget.CardView>

Examples/OneSignalDemoV2/app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
<string name="tags">Tags</string>
5353
<string name="no_tags_added">No Tags Added</string>
5454
<string name="add_tag">ADD TAG</string>
55-
<string name="remove_all_tags">REMOVE ALL TAGS</string>
5655

5756
<!-- Outcome Events -->
5857
<string name="outcome_events">Outcome Events</string>
@@ -75,6 +74,7 @@
7574
<string name="triggers">Triggers</string>
7675
<string name="no_triggers_added">No Triggers Added</string>
7776
<string name="add_trigger">ADD TRIGGER</string>
77+
<string name="clear_triggers">CLEAR TRIGGERS</string>
7878

7979
<!-- Track Event -->
8080
<string name="track_event">Track Event</string>

0 commit comments

Comments
 (0)