Skip to content

Commit 85710ad

Browse files
committed
Add auto-parcel example
1 parent 0abc8b5 commit 85710ad

File tree

27 files changed

+579
-0
lines changed

27 files changed

+579
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.gradle
2+
/local.properties
3+
/.idea
4+
.DS_Store
5+
/build
6+
/captures
7+
*.iml
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
apply plugin: 'com.android.application'
2+
apply plugin: 'kotlin-android'
3+
apply plugin: 'kotlin-android-extensions'
4+
apply plugin: 'kotlin-kapt'
5+
6+
dependencies {
7+
provided "frankiesardo:auto-parcel:$autoparcel_version"
8+
kapt "frankiesardo:auto-parcel:$autoparcel_version"
9+
10+
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
11+
}
12+
13+
android {
14+
compileSdkVersion 22
15+
buildToolsVersion "22.0.1"
16+
17+
defaultConfig {
18+
minSdkVersion 14
19+
targetSdkVersion 25
20+
}
21+
22+
compileSdkVersion 25
23+
buildToolsVersion "25.0.2"
24+
}
25+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="auto.parcel.sample" >
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@drawable/ic_launcher"
8+
android:label="@string/app_name"
9+
android:theme="@style/AppTheme" >
10+
<activity
11+
android:name=".MainActivity"
12+
android:label="@string/app_name" >
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
16+
<category android:name="android.intent.category.LAUNCHER" />
17+
</intent-filter>
18+
</activity>
19+
<activity android:name=".DetailActivity"
20+
android:label="@string/app_name" />
21+
</application>
22+
23+
</manifest>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package auto.parcel.sample;
2+
3+
import android.app.Activity;
4+
import android.os.Bundle;
5+
import android.widget.TextView;
6+
import model3.Person;
7+
8+
public class DetailActivity extends Activity {
9+
10+
@Override protected void onCreate(Bundle savedInstanceState) {
11+
super.onCreate(savedInstanceState);
12+
setContentView(R.layout.activity_detail);
13+
Person person = getIntent().getParcelableExtra("Person");
14+
textView(R.id.name).setText("Name:" + person.name());
15+
textView(R.id.id).setText("Id:" + person.id());
16+
textView(R.id.height).setText("Height:" + person.heightType());
17+
textView(R.id.addresses).setText("Addresses:" + person.addresses());
18+
textView(R.id.friends).setText("Friends:" + person.friends());
19+
}
20+
21+
private TextView textView(int id) {
22+
return (TextView) findViewById(id);
23+
}
24+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package auto.parcel.sample
2+
3+
import android.app.Activity
4+
import android.content.Intent
5+
import android.os.Bundle
6+
import android.view.View
7+
import kotlinx.android.synthetic.main.activity_main.*
8+
9+
class MainActivity : Activity() {
10+
override fun onCreate(savedInstanceState: Bundle?) {
11+
super.onCreate(savedInstanceState)
12+
setContentView(R.layout.activity_main)
13+
14+
click_me.setOnClickListener {
15+
val detailIntent = Intent(this, DetailActivity::class.java)
16+
detailIntent.putExtra("Person", SampleData.BOB)
17+
startActivity(detailIntent)
18+
}
19+
}
20+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package auto.parcel.sample
2+
3+
import java.util.Arrays
4+
import java.util.Collections
5+
import java.util.HashMap
6+
7+
import model1.HeightBucket
8+
import model2.Address
9+
import model3.Person
10+
11+
interface SampleData {
12+
companion object {
13+
val ALICE = Person.create("Alice", 1L, HeightBucket.AVERAGE,
14+
addresses = hashMapOf("home" to Address.create(doubleArrayOf(0.3, 0.7), "Rome")),
15+
friends = emptyList<Person>())
16+
17+
val BOB = Person.builder()
18+
.name("Bob")
19+
.id(2L)
20+
.heightType(HeightBucket.TALL)
21+
.addresses(hashMapOf(
22+
"home" to Address.create(doubleArrayOf(3.2, 143.2), "Turin"),
23+
"work" to Address.create(doubleArrayOf(5.9, 156.1), "Genoa")))
24+
.friends(Arrays.asList(ALICE)).build()
25+
}
26+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package model1
2+
3+
enum class HeightBucket {
4+
SHORT, AVERAGE, TALL
5+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package model2
2+
3+
import com.google.auto.value.AutoValue
4+
5+
import android.os.Parcelable
6+
7+
@AutoValue
8+
abstract class Address : Parcelable {
9+
abstract fun coordinates(): DoubleArray
10+
abstract fun cityName(): String
11+
12+
@AutoValue.Builder
13+
interface Builder {
14+
fun coordinates(x: DoubleArray): Builder
15+
fun cityName(x: String): Builder
16+
fun build(): Address
17+
}
18+
19+
companion object {
20+
fun create(coordinates: DoubleArray, cityName: String): Address {
21+
return builder().coordinates(coordinates).cityName(cityName).build()
22+
}
23+
24+
fun builder(): Builder = `$AutoValue_Address`.Builder()
25+
}
26+
27+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package model3
2+
3+
import com.google.auto.value.AutoValue
4+
5+
import android.os.Parcelable
6+
7+
import model1.HeightBucket
8+
import model2.Address
9+
10+
@AutoValue
11+
abstract class Person : Parcelable {
12+
abstract fun name(): String
13+
abstract fun id(): Long
14+
abstract fun heightType(): HeightBucket
15+
abstract fun addresses(): MutableMap<String, Address>
16+
abstract fun friends(): MutableList<Person>
17+
18+
@AutoValue.Builder
19+
interface Builder {
20+
fun name(s: String): Builder
21+
fun id(n: Long): Builder
22+
fun heightType(x: HeightBucket): Builder
23+
fun addresses(x: MutableMap<String, Address>): Builder
24+
fun friends(x: MutableList<Person>): Builder
25+
fun build(): Person
26+
}
27+
28+
companion object {
29+
fun create(name: String,
30+
id: Long,
31+
heightType: HeightBucket,
32+
addresses: Map<String, Address>,
33+
friends: List<Person>
34+
) = builder()
35+
.name(name)
36+
.id(id)
37+
.heightType(heightType)
38+
.addresses(addresses as? MutableMap<String, Address> ?: addresses.toMutableMap())
39+
.friends(friends as? MutableList<Person> ?: friends.toMutableList())
40+
.build()
41+
42+
fun builder(): Builder = `$AutoValue_Person`.Builder()
43+
}
44+
}

0 commit comments

Comments
 (0)