Skip to content

Commit 594579b

Browse files
authored
Convert database quickstart to Kotlin (#654)
1 parent ab0315a commit 594579b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1114
-123
lines changed

database/app/build.gradle

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
apply plugin: 'com.android.application'
2+
apply plugin: 'kotlin-android'
3+
apply plugin: 'kotlin-android-extensions'
4+
apply plugin: 'org.jetbrains.kotlin.android.extensions'
5+
26
check.dependsOn 'assembleDebugAndroidTest'
37

48
android {
@@ -26,7 +30,15 @@ configurations.all {
2630
resolutionStrategy.force 'com.android.support:support-annotations:27.1.1'
2731
}
2832

33+
androidExtensions {
34+
experimental = true
35+
}
36+
2937
dependencies {
38+
implementation project(":internal:lintchecks")
39+
implementation project(":internal:chooser")
40+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.61"
41+
3042
implementation 'com.android.support:appcompat-v7:27.1.1'
3143
implementation 'com.android.support:recyclerview-v7:27.1.1'
3244
implementation 'com.android.support:cardview-v7:27.1.1'

database/app/proguard-rules.pro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
-keepattributes EnclosingMethod
2222
-keepattributes InnerClasses
2323

24-
-keep class com.google.firebase.quickstart.database.viewholder.** {
24+
-keep class com.google.firebase.quickstart.database.java.viewholder.** {
2525
*;
2626
}
2727

28-
-keepclassmembers class com.google.firebase.quickstart.database.models.** {
28+
-keepclassmembers class com.google.firebase.quickstart.database.java.models.** {
2929
*;
3030
}

database/app/src/androidTest/java/com/google/firebase/quickstart/database/NewPostTest.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import android.support.test.runner.AndroidJUnit4;
99
import android.test.suitebuilder.annotation.LargeTest;
1010

11+
import com.google.firebase.quickstart.database.java.SignInActivity;
12+
1113
import org.junit.Rule;
1214
import org.junit.Test;
1315
import org.junit.runner.RunWith;
@@ -46,50 +48,50 @@ public void newPostTest() {
4648

4749
// Select email field
4850
ViewInteraction appCompatEditText = onView(
49-
allOf(withId(R.id.field_email),
50-
withParent(withId(R.id.layout_email_password)),
51+
allOf(withId(R.id.fieldEmail),
52+
withParent(withId(R.id.layoutEmailPassword)),
5153
isDisplayed()));
5254
appCompatEditText.perform(click());
5355

5456
// Enter email address
5557
ViewInteraction appCompatEditText2 = onView(
56-
allOf(withId(R.id.field_email),
57-
withParent(withId(R.id.layout_email_password)),
58+
allOf(withId(R.id.fieldEmail),
59+
withParent(withId(R.id.layoutEmailPassword)),
5860
isDisplayed()));
5961
appCompatEditText2.perform(replaceText(email));
6062

6163
// Enter password
6264
ViewInteraction appCompatEditText3 = onView(
63-
allOf(withId(R.id.field_password),
64-
withParent(withId(R.id.layout_email_password)),
65+
allOf(withId(R.id.fieldPassword),
66+
withParent(withId(R.id.layoutEmailPassword)),
6567
isDisplayed()));
6668
appCompatEditText3.perform(replaceText(password));
6769

6870
// Click sign up
6971
ViewInteraction appCompatButton = onView(
70-
allOf(withId(R.id.button_sign_up), withText(R.string.sign_up),
71-
withParent(withId(R.id.layout_buttons)),
72+
allOf(withId(R.id.buttonSignUp), withText(R.string.sign_up),
73+
withParent(withId(R.id.layoutButtons)),
7274
isDisplayed()));
7375
appCompatButton.perform(click());
7476

7577
// Click new post button
7678
ViewInteraction floatingActionButton = onView(
77-
allOf(withId(R.id.fab_new_post), isDisplayed()));
79+
allOf(withId(R.id.fabNewPost), isDisplayed()));
7880
floatingActionButton.perform(click());
7981

8082
// Enter post title
8183
ViewInteraction appCompatEditText4 = onView(
82-
allOf(withId(R.id.field_title), isDisplayed()));
84+
allOf(withId(R.id.fieldTitle), isDisplayed()));
8385
appCompatEditText4.perform(replaceText(postTitle));
8486

8587
// Enter post content
8688
ViewInteraction appCompatEditText5 = onView(
87-
allOf(withId(R.id.field_body), isDisplayed()));
89+
allOf(withId(R.id.fieldBody), isDisplayed()));
8890
appCompatEditText5.perform(replaceText(postContent));
8991

9092
// Click submit button
9193
ViewInteraction floatingActionButton2 = onView(
92-
allOf(withId(R.id.fab_submit_post), isDisplayed()));
94+
allOf(withId(R.id.fabSubmitPost), isDisplayed()));
9395
floatingActionButton2.perform(click());
9496

9597
// Navigate to "My Posts"
@@ -99,18 +101,18 @@ public void newPostTest() {
99101

100102
// Check that the title is correct
101103
ViewInteraction textView = onView(
102-
allOf(withId(R.id.post_title), withText(postTitle), isDisplayed()));
104+
allOf(withId(R.id.postTitle), withText(postTitle), isDisplayed()));
103105
textView.check(matches(withText(postTitle)));
104106

105107
// Check that the content is correct
106108
ViewInteraction textView2 = onView(
107-
allOf(withId(R.id.post_body), withText(postContent), isDisplayed()));
109+
allOf(withId(R.id.postBody), withText(postContent), isDisplayed()));
108110
textView2.check(matches(withText(postContent)));
109111

110112
// Check that it has zero stars
111113
ViewInteraction textView3 = onView(
112-
allOf(withId(R.id.post_num_stars), withText("0"),
113-
withParent(withId(R.id.star_layout)),
114+
allOf(withId(R.id.postNumStars), withText("0"),
115+
withParent(withId(R.id.starLayout)),
114116
isDisplayed()));
115117
textView3.check(matches(withText("0")));
116118

database/app/src/main/AndroidManifest.xml

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,33 @@
88
android:label="@string/app_name"
99
android:supportsRtl="true"
1010
android:theme="@style/AppTheme">
11-
<activity
12-
android:name=".MainActivity"
13-
android:label="@string/app_name"
14-
android:theme="@style/AppTheme" />
15-
<activity android:name=".NewPostActivity" />
16-
<activity android:name=".SignInActivity">
11+
12+
<activity android:name=".EntryChoiceActivity">
1713
<intent-filter>
1814
<action android:name="android.intent.action.MAIN" />
1915

2016
<category android:name="android.intent.category.LAUNCHER" />
2117
</intent-filter>
2218
</activity>
23-
<activity android:name=".PostDetailActivity"></activity>
19+
20+
<activity
21+
android:name=".java.MainActivity"
22+
android:label="@string/app_name"
23+
android:theme="@style/AppTheme" />
24+
25+
<activity
26+
android:name=".kotlin.MainActivity"
27+
android:label="@string/app_name"
28+
android:theme="@style/AppTheme" />
29+
30+
<activity android:name=".java.SignInActivity" />
31+
<activity android:name=".kotlin.SignInActivity" />
32+
33+
<activity android:name=".java.NewPostActivity" />
34+
<activity android:name=".kotlin.NewPostActivity" />
35+
36+
<activity android:name=".java.PostDetailActivity" />
37+
<activity android:name=".kotlin.PostDetailActivity" />
2438
</application>
2539

2640
</manifest>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.google.firebase.quickstart.database
2+
3+
import android.content.Intent
4+
import com.firebase.example.internal.BaseEntryChoiceActivity
5+
import com.firebase.example.internal.Choice
6+
7+
class EntryChoiceActivity : BaseEntryChoiceActivity() {
8+
9+
override fun getChoices(): List<Choice> {
10+
return listOf(
11+
Choice(
12+
"Java",
13+
"Run the Firebase Realtime Database quickstart written in Java.",
14+
Intent(this, com.google.firebase.quickstart.database.java.MainActivity::class.java)),
15+
Choice(
16+
"Kotlin",
17+
"Run the Firebase Realtime Database quickstart written in Kotlin.",
18+
Intent(this, com.google.firebase.quickstart.database.kotlin.MainActivity::class.java))
19+
)
20+
}
21+
22+
}

database/app/src/main/java/com/google/firebase/quickstart/database/BaseActivity.java renamed to database/app/src/main/java/com/google/firebase/quickstart/database/java/BaseActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.google.firebase.quickstart.database;
1+
package com.google.firebase.quickstart.database.java;
22

33
import android.app.ProgressDialog;
44
import android.support.v7.app.AppCompatActivity;

database/app/src/main/java/com/google/firebase/quickstart/database/MainActivity.java renamed to database/app/src/main/java/com/google/firebase/quickstart/database/java/MainActivity.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.google.firebase.quickstart.database;
17+
package com.google.firebase.quickstart.database.java;
1818

1919
import android.content.Intent;
2020
import android.os.Bundle;
@@ -27,9 +27,10 @@
2727
import android.view.View;
2828

2929
import com.google.firebase.auth.FirebaseAuth;
30-
import com.google.firebase.quickstart.database.fragment.MyPostsFragment;
31-
import com.google.firebase.quickstart.database.fragment.MyTopPostsFragment;
32-
import com.google.firebase.quickstart.database.fragment.RecentPostsFragment;
30+
import com.google.firebase.quickstart.database.R;
31+
import com.google.firebase.quickstart.database.java.fragment.MyPostsFragment;
32+
import com.google.firebase.quickstart.database.java.fragment.MyTopPostsFragment;
33+
import com.google.firebase.quickstart.database.java.fragment.RecentPostsFragment;
3334

3435
public class MainActivity extends BaseActivity {
3536

@@ -75,7 +76,7 @@ public CharSequence getPageTitle(int position) {
7576
tabLayout.setupWithViewPager(mViewPager);
7677

7778
// Button launches NewPostActivity
78-
findViewById(R.id.fab_new_post).setOnClickListener(new View.OnClickListener() {
79+
findViewById(R.id.fabNewPost).setOnClickListener(new View.OnClickListener() {
7980
@Override
8081
public void onClick(View v) {
8182
startActivity(new Intent(MainActivity.this, NewPostActivity.class));

database/app/src/main/java/com/google/firebase/quickstart/database/NewPostActivity.java renamed to database/app/src/main/java/com/google/firebase/quickstart/database/java/NewPostActivity.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.google.firebase.quickstart.database;
1+
package com.google.firebase.quickstart.database.java;
22

33
import android.os.Bundle;
44
import android.support.design.widget.FloatingActionButton;
@@ -13,8 +13,9 @@
1313
import com.google.firebase.database.DatabaseReference;
1414
import com.google.firebase.database.FirebaseDatabase;
1515
import com.google.firebase.database.ValueEventListener;
16-
import com.google.firebase.quickstart.database.models.Post;
17-
import com.google.firebase.quickstart.database.models.User;
16+
import com.google.firebase.quickstart.database.R;
17+
import com.google.firebase.quickstart.database.java.models.Post;
18+
import com.google.firebase.quickstart.database.java.models.User;
1819

1920
import java.util.HashMap;
2021
import java.util.Map;
@@ -41,9 +42,9 @@ protected void onCreate(Bundle savedInstanceState) {
4142
mDatabase = FirebaseDatabase.getInstance().getReference();
4243
// [END initialize_database_ref]
4344

44-
mTitleField = findViewById(R.id.field_title);
45-
mBodyField = findViewById(R.id.field_body);
46-
mSubmitButton = findViewById(R.id.fab_submit_post);
45+
mTitleField = findViewById(R.id.fieldTitle);
46+
mBodyField = findViewById(R.id.fieldBody);
47+
mSubmitButton = findViewById(R.id.fabSubmitPost);
4748

4849
mSubmitButton.setOnClickListener(new View.OnClickListener() {
4950
@Override

database/app/src/main/java/com/google/firebase/quickstart/database/PostDetailActivity.java renamed to database/app/src/main/java/com/google/firebase/quickstart/database/java/PostDetailActivity.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.google.firebase.quickstart.database;
1+
package com.google.firebase.quickstart.database.java;
22

33
import android.content.Context;
44
import android.os.Bundle;
@@ -19,9 +19,10 @@
1919
import com.google.firebase.database.DatabaseReference;
2020
import com.google.firebase.database.FirebaseDatabase;
2121
import com.google.firebase.database.ValueEventListener;
22-
import com.google.firebase.quickstart.database.models.User;
23-
import com.google.firebase.quickstart.database.models.Comment;
24-
import com.google.firebase.quickstart.database.models.Post;
22+
import com.google.firebase.quickstart.database.R;
23+
import com.google.firebase.quickstart.database.java.models.User;
24+
import com.google.firebase.quickstart.database.java.models.Comment;
25+
import com.google.firebase.quickstart.database.java.models.Post;
2526

2627
import java.util.ArrayList;
2728
import java.util.List;
@@ -63,12 +64,12 @@ protected void onCreate(Bundle savedInstanceState) {
6364
.child("post-comments").child(mPostKey);
6465

6566
// Initialize Views
66-
mAuthorView = findViewById(R.id.post_author);
67-
mTitleView = findViewById(R.id.post_title);
68-
mBodyView = findViewById(R.id.post_body);
69-
mCommentField = findViewById(R.id.field_comment_text);
70-
mCommentButton = findViewById(R.id.button_post_comment);
71-
mCommentsRecycler = findViewById(R.id.recycler_comments);
67+
mAuthorView = findViewById(R.id.postAuthor);
68+
mTitleView = findViewById(R.id.postTitle);
69+
mBodyView = findViewById(R.id.postBody);
70+
mCommentField = findViewById(R.id.fieldCommentText);
71+
mCommentButton = findViewById(R.id.buttonPostComment);
72+
mCommentsRecycler = findViewById(R.id.recyclerPostComments);
7273

7374
mCommentButton.setOnClickListener(this);
7475
mCommentsRecycler.setLayoutManager(new LinearLayoutManager(this));
@@ -130,7 +131,7 @@ public void onStop() {
130131
@Override
131132
public void onClick(View v) {
132133
int i = v.getId();
133-
if (i == R.id.button_post_comment) {
134+
if (i == R.id.buttonPostComment) {
134135
postComment();
135136
}
136137
}
@@ -171,8 +172,8 @@ private static class CommentViewHolder extends RecyclerView.ViewHolder {
171172
public CommentViewHolder(View itemView) {
172173
super(itemView);
173174

174-
authorView = itemView.findViewById(R.id.comment_author);
175-
bodyView = itemView.findViewById(R.id.comment_body);
175+
authorView = itemView.findViewById(R.id.commentAuthor);
176+
bodyView = itemView.findViewById(R.id.commentBody);
176177
}
177178
}
178179

database/app/src/main/java/com/google/firebase/quickstart/database/SignInActivity.java renamed to database/app/src/main/java/com/google/firebase/quickstart/database/java/SignInActivity.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.google.firebase.quickstart.database;
1+
package com.google.firebase.quickstart.database.java;
22

33
import android.content.Intent;
44
import android.os.Bundle;
@@ -17,7 +17,8 @@
1717
import com.google.firebase.auth.FirebaseUser;
1818
import com.google.firebase.database.DatabaseReference;
1919
import com.google.firebase.database.FirebaseDatabase;
20-
import com.google.firebase.quickstart.database.models.User;
20+
import com.google.firebase.quickstart.database.R;
21+
import com.google.firebase.quickstart.database.java.models.User;
2122

2223
public class SignInActivity extends BaseActivity implements View.OnClickListener {
2324

@@ -40,10 +41,10 @@ protected void onCreate(Bundle savedInstanceState) {
4041
mAuth = FirebaseAuth.getInstance();
4142

4243
// Views
43-
mEmailField = findViewById(R.id.field_email);
44-
mPasswordField = findViewById(R.id.field_password);
45-
mSignInButton = findViewById(R.id.button_sign_in);
46-
mSignUpButton = findViewById(R.id.button_sign_up);
44+
mEmailField = findViewById(R.id.fieldEmail);
45+
mPasswordField = findViewById(R.id.fieldPassword);
46+
mSignInButton = findViewById(R.id.buttonSignIn);
47+
mSignUpButton = findViewById(R.id.buttonSignUp);
4748

4849
// Click listeners
4950
mSignInButton.setOnClickListener(this);
@@ -163,9 +164,9 @@ private void writeNewUser(String userId, String name, String email) {
163164
@Override
164165
public void onClick(View v) {
165166
int i = v.getId();
166-
if (i == R.id.button_sign_in) {
167+
if (i == R.id.buttonSignIn) {
167168
signIn();
168-
} else if (i == R.id.button_sign_up) {
169+
} else if (i == R.id.buttonSignUp) {
169170
signUp();
170171
}
171172
}

0 commit comments

Comments
 (0)