Skip to content

Commit aaea591

Browse files
authored
Sample App improvements (#2)
* more space on test app (remove actionBar) * update CONTRIBUTING.md * add property support to all events * add placeholder as default value for events * rename ISSUE TEMPLATE folder
1 parent 1039373 commit aaea591

File tree

15 files changed

+191
-186
lines changed

15 files changed

+191
-186
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.

CONTRIBUTING.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,25 @@ We want this community to be friendly and respectful to each other. Please follo
44

55
## Development workflow
66

7+
We recommend using the Android Studio IDE to jumpstart your development workflow
8+
79
To get started with the project
8-
TODO
10+
- clone this repository
11+
- import the project into Android Studio
912

1013
While developing, you can run the [example app](/example/) to test your changes.
11-
TODO
12-
13-
To fix formatting errors, run the following:
14-
15-
TODO
16-
17-
Remember to add tests for your change if possible. Run the unit tests by:
18-
19-
TODO
14+
- You can do this via the IDE itself using the `Run app` button in the toolbar
15+
- You can install the debug version of the app using the gradle command:
16+
```
17+
./gradlew installDebug
18+
```
19+
20+
Remember to add tests for your change if possible. Run the unit tests:
21+
- Running the tests via the IDE
22+
- via the command line
23+
```
24+
./gradlew test
25+
```
2026

2127
### Commit message convention
2228

@@ -29,11 +35,9 @@ We follow the [conventional commits specification](https://www.conventionalcommi
2935
- `test`: adding or updating tests, eg add integration tests using detox.
3036
- `chore`: tooling changes, e.g. change CI config.
3137

32-
Our pre-commit hooks verify that your commit message matches this format when committing.
33-
3438
### Linting and tests
3539

36-
TODO
40+
./gradlew test
3741

3842
Our pre-commit hooks verify that the linter and tests pass when committing.
3943

samples/kotlin-android-app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
android:label="@string/app_name"
2222
android:roundIcon="@mipmap/ic_launcher_round"
2323
android:supportsRtl="true"
24-
android:theme="@style/Theme.AnalyticsAndroidNext">
24+
android:theme="@style/Theme.MaterialComponents.DayNight.NoActionBar">
2525
<meta-data
2626
android:name="com.google.android.gms.ads.APPLICATION_ID"
2727
android:value="ca-app-pub-3940256099942544~3347511713" />

samples/kotlin-android-app/src/main/java/com/segment/analytics/next/ConsentActivity.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import androidx.appcompat.app.AppCompatActivity
66
import androidx.fragment.app.Fragment
77
import androidx.fragment.app.FragmentManager
88
import androidx.fragment.app.FragmentTransaction
9+
import com.segment.analytics.EventType
910
import com.segment.analytics.next.plugins.ConsentTracking
1011

1112
/**
@@ -15,10 +16,10 @@ import com.segment.analytics.next.plugins.ConsentTracking
1516
class ConsentActivity : AppCompatActivity() {
1617

1718
val analytics = MainApplication.analytics
18-
val trackFragment = EventFragment("Track", analytics)
19-
val identifyFragment = EventFragment("Identify", analytics)
20-
val screenFragment = EventFragment("Screen", analytics)
21-
val groupFragment = EventFragment("Group", analytics)
19+
val trackFragment = EventFragment(EventType.Track, analytics)
20+
val identifyFragment = EventFragment(EventType.Identify, analytics)
21+
val screenFragment = EventFragment(EventType.Screen, analytics)
22+
val groupFragment = EventFragment(EventType.Group, analytics)
2223

2324
override fun onCreate(savedInstanceState: Bundle?) {
2425
super.onCreate(savedInstanceState)

samples/kotlin-android-app/src/main/java/com/segment/analytics/next/EventFragment.kt

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,58 @@ import android.view.View
88
import android.view.ViewGroup
99
import android.widget.Button
1010
import android.widget.EditText
11+
import android.widget.LinearLayout
1112
import android.widget.TextView
1213
import androidx.fragment.app.Fragment
1314
import com.segment.analytics.*
1415
import com.segment.analytics.platform.Plugin
15-
import com.segment.analytics.platform.plugins.log
1616
import kotlinx.serialization.encodeToString
1717
import kotlinx.serialization.json.Json
1818

1919
/**
2020
* A UI fragment allowing users to send events through the analytics timeline
2121
* It leverages the Plugin concept to display in-flight events
2222
*/
23-
class EventFragment(val type: String, val analytics: Analytics) : Fragment() {
24-
val properties = mutableMapOf<String, String>()
23+
class EventFragment(val type: EventType, val analytics: Analytics) : Fragment() {
24+
2525
override fun onCreateView(
26-
inflater: LayoutInflater, container: ViewGroup?,
26+
inflater: LayoutInflater,
27+
container: ViewGroup?,
2728
savedInstanceState: Bundle?
2829
): View {
2930
val props = fetch(type)
3031
// Inflate the layout for this fragment
3132
val view = inflater.inflate(props.second, container, false)
33+
34+
val traitRoot = view.findViewById<LinearLayout>(R.id.props)
35+
// add the first one
36+
addPropertyLayout(traitRoot)
37+
view.findViewById<Button>(R.id.add).setOnClickListener {
38+
addPropertyLayout(traitRoot)
39+
}
40+
3241
view.findViewById<Button>(R.id.sendEvent).setOnClickListener {
33-
val input = view.findViewById<EditText>(R.id.input).text.toString()
42+
val input = view.findViewById<EditText>(R.id.input).text.toString().let {
43+
if (it.isNotEmpty()) {
44+
it
45+
} else {
46+
"Placeholder"
47+
}
48+
}
49+
50+
51+
val properties = getUserProps(traitRoot)
3452
when (type) {
35-
"Track" -> sendTrack(eventName = input, props = properties)
36-
"Identify" -> sendIdentify(userId = input, traits = properties)
37-
"Screen" -> sendScreen(screenName = input, props = properties)
38-
"Group" -> sendGroup(groupId = input, traits = properties)
39-
else -> "" to 0
53+
EventType.Track -> sendTrack(eventName = input, props = properties)
54+
EventType.Identify -> sendIdentify(userId = input, traits = properties)
55+
EventType.Screen -> sendScreen(screenName = input, props = properties)
56+
EventType.Group -> sendGroup(groupId = input, traits = properties)
4057
}
4158
}
59+
4260
val codeView = view.findViewById<TextView>(R.id.code_view)
4361
codeView.movementMethod = ScrollingMovementMethod()
62+
4463
analytics.add(object : Plugin {
4564
override val type: Plugin.Type = Plugin.Type.After
4665
override val name: String = "TempResult-$type"
@@ -60,18 +79,44 @@ class EventFragment(val type: String, val analytics: Analytics) : Fragment() {
6079
return super.execute(event)
6180
}
6281
})
63-
// TODO implement default properties logic for events
64-
// TODO implement add property button logic
82+
6583
return view
6684
}
6785

86+
private fun getUserProps(root: LinearLayout): MutableMap<String, String> {
87+
val map = mutableMapOf<String, String>()
88+
for (i in 0 until root.childCount) {
89+
val ll = root.getChildAt(i)
90+
val key = ll.findViewWithTag<EditText>("key").text.toString()
91+
val value = ll.findViewWithTag<EditText>("value").text.toString()
92+
if (key.isNotEmpty()) {
93+
map[key] = value
94+
}
95+
}
96+
return map
97+
}
98+
99+
private fun addPropertyLayout(container: LinearLayout) {
100+
val inflater = LayoutInflater.from(context);
101+
//to get the MainLayout
102+
val layoutXml: Int = when (type) {
103+
EventType.Track -> R.layout.property
104+
EventType.Identify -> R.layout.trait
105+
EventType.Screen -> R.layout.property
106+
EventType.Group -> R.layout.trait
107+
else -> 0
108+
}
109+
val view = inflater.inflate(layoutXml, container, false)
110+
container.addView(view)
111+
}
112+
68113
private inline fun <reified T : BaseEvent> eventStr(event: T) = Json {
69114
prettyPrint = true
70115
encodeDefaults = true
71116
}.encodeToString(event)
72117

73118
private fun colorFormat(text: String): String {
74-
val spacer = fun (match: MatchResult): CharSequence {
119+
val spacer = fun(match: MatchResult): CharSequence {
75120
return "<br>" + "&nbsp;".repeat(match.value.length - 1)
76121
}
77122

@@ -81,28 +126,28 @@ class EventFragment(val type: String, val analytics: Analytics) : Fragment() {
81126
return newString
82127
}
83128

84-
private fun sendGroup(groupId: String, traits: MutableMap<String, String>) {
129+
private fun sendGroup(groupId: String, traits: Map<String, String>) {
85130
analytics.group(groupId, traits)
86131
}
87132

88-
private fun sendScreen(screenName: String, props: MutableMap<String, String>) {
133+
private fun sendScreen(screenName: String, props: Map<String, String>) {
89134
analytics.screen(screenName, props)
90135
}
91136

92-
private fun sendIdentify(userId: String, traits: MutableMap<String, String>) {
137+
private fun sendIdentify(userId: String, traits: Map<String, String>) {
93138
analytics.identify(userId, traits)
94139
}
95140

96-
private fun sendTrack(eventName: String, props: MutableMap<String, String>) {
141+
private fun sendTrack(eventName: String, props: Map<String, String>) {
97142
analytics.track(eventName, props)
98143
}
99144

100-
fun fetch(type: String): Pair<String, Int> {
145+
private fun fetch(type: EventType): Pair<String, Int> {
101146
return when (type) {
102-
"Track" -> "props" to R.layout.fragment_track
103-
"Identify" -> "traits" to R.layout.fragment_identify
104-
"Screen" -> "props" to R.layout.fragment_screen
105-
"Group" -> "traits" to R.layout.fragment_group
147+
EventType.Track -> "props" to R.layout.fragment_track
148+
EventType.Identify -> "traits" to R.layout.fragment_identify
149+
EventType.Screen -> "props" to R.layout.fragment_screen
150+
EventType.Group -> "traits" to R.layout.fragment_group
106151
else -> "" to 0
107152
}
108153
}

samples/kotlin-android-app/src/main/java/com/segment/analytics/next/MainActivity.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ import androidx.appcompat.app.AppCompatActivity
66
import androidx.fragment.app.Fragment
77
import androidx.fragment.app.FragmentManager
88
import androidx.fragment.app.FragmentTransaction
9+
import com.segment.analytics.EventType
910

1011

1112
class MainActivity : AppCompatActivity() {
1213

1314
val analytics = MainApplication.analytics
14-
val trackFragment = EventFragment("Track", analytics)
15-
val identifyFragment = EventFragment("Identify", analytics)
16-
val screenFragment = EventFragment("Screen", analytics)
17-
val groupFragment = EventFragment("Group", analytics)
15+
val trackFragment = EventFragment(EventType.Track, analytics)
16+
val identifyFragment = EventFragment(EventType.Identify, analytics)
17+
val screenFragment = EventFragment(EventType.Screen, analytics)
18+
val groupFragment = EventFragment(EventType.Group, analytics)
1819

1920
override fun onCreate(savedInstanceState: Bundle?) {
2021
super.onCreate(savedInstanceState)

samples/kotlin-android-app/src/main/res/layout/fragment_group.xml

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,52 +16,27 @@
1616
android:id="@+id/input"
1717
android:layout_width="match_parent"
1818
android:layout_height="wrap_content"
19-
android:hint="Group Id"
2019
android:backgroundTint="@color/white"
20+
android:hint="Group Id"
2121
android:textColor="@color/segment_green"
2222
android:textColorHint="@color/segment_green" />
2323

2424
<LinearLayout
25-
android:id="@+id/traits"
26-
android:orientation="vertical"
25+
android:id="@+id/props"
2726
android:layout_width="match_parent"
28-
android:layout_height="wrap_content">
29-
30-
<LinearLayout
31-
android:layout_width="match_parent"
32-
android:layout_height="wrap_content"
33-
android:layout_gravity="center_horizontal"
34-
android:orientation="horizontal"
35-
android:weightSum="5">
36-
37-
<EditText
38-
android:layout_width="wrap_content"
39-
android:layout_height="wrap_content"
40-
android:hint="Trait"
41-
android:layout_weight="2"
42-
android:backgroundTint="@color/white"
43-
android:textColor="@color/segment_green"
44-
android:textColorHint="@color/segment_green" />
45-
46-
<EditText
47-
android:layout_width="wrap_content"
48-
android:layout_height="wrap_content"
49-
android:hint="Value"
50-
android:layout_weight="2"
51-
android:backgroundTint="@color/white"
52-
android:textColor="@color/segment_green"
53-
android:textColorHint="@color/segment_green" />
27+
android:layout_height="wrap_content"
28+
android:orientation="vertical">
5429

55-
<Button
56-
android:layout_width="wrap_content"
57-
android:layout_height="wrap_content"
58-
android:backgroundTint="@color/segment_green"
59-
android:text="+"
60-
android:layout_weight="1"
61-
android:textColor="@color/white" />
62-
</LinearLayout>
6330
</LinearLayout>
6431

32+
<Button
33+
android:id="@+id/add"
34+
android:layout_width="match_parent"
35+
android:layout_height="wrap_content"
36+
android:backgroundTint="@color/segment_green"
37+
android:text="Add Trait"
38+
android:textColor="@color/white" />
39+
6540
<Button
6641
android:id="@+id/sendEvent"
6742
android:layout_width="match_parent"

samples/kotlin-android-app/src/main/res/layout/fragment_identify.xml

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,46 +22,21 @@
2222
android:textColorHint="@color/segment_green" />
2323

2424
<LinearLayout
25-
android:id="@+id/traits"
25+
android:id="@+id/props"
2626
android:orientation="vertical"
2727
android:layout_width="match_parent"
2828
android:layout_height="wrap_content">
2929

30-
<LinearLayout
31-
android:layout_width="match_parent"
32-
android:layout_height="wrap_content"
33-
android:layout_gravity="center_horizontal"
34-
android:orientation="horizontal"
35-
android:weightSum="5">
36-
37-
<EditText
38-
android:layout_width="wrap_content"
39-
android:layout_height="wrap_content"
40-
android:hint="Trait"
41-
android:layout_weight="2"
42-
android:backgroundTint="@color/white"
43-
android:textColor="@color/segment_green"
44-
android:textColorHint="@color/segment_green" />
45-
46-
<EditText
47-
android:layout_width="wrap_content"
48-
android:layout_height="wrap_content"
49-
android:hint="Value"
50-
android:layout_weight="2"
51-
android:backgroundTint="@color/white"
52-
android:textColor="@color/segment_green"
53-
android:textColorHint="@color/segment_green" />
54-
55-
<Button
56-
android:layout_width="wrap_content"
57-
android:layout_height="wrap_content"
58-
android:backgroundTint="@color/segment_green"
59-
android:text="+"
60-
android:layout_weight="1"
61-
android:textColor="@color/white" />
62-
</LinearLayout>
6330
</LinearLayout>
6431

32+
<Button
33+
android:id="@+id/add"
34+
android:layout_width="match_parent"
35+
android:layout_height="wrap_content"
36+
android:backgroundTint="@color/segment_green"
37+
android:text="Add Trait"
38+
android:textColor="@color/white" />
39+
6540
<Button
6641
android:id="@+id/sendEvent"
6742
android:layout_width="match_parent"
@@ -70,8 +45,6 @@
7045
android:text="Send Event"
7146
android:textColor="@color/white" />
7247

73-
74-
7548
<TextView
7649
android:id="@+id/code_view"
7750
android:layout_width="match_parent"

0 commit comments

Comments
 (0)