Skip to content

Commit

Permalink
Merge pull request #1206 from JSunde/KotlinUser
Browse files Browse the repository at this point in the history
Convert User to Kotlin
  • Loading branch information
JSunde committed Aug 17, 2022
2 parents b1bbd7f + acdff7c commit f62ae22
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 127 deletions.
Expand Up @@ -31,14 +31,11 @@ object TestModelBuilders {
@JvmStatic
fun newSurvey(): Survey.Builder = Survey.newBuilder().setId("").setTitle("").setDescription("")

@JvmStatic
fun newUser(): User.Builder = User.builder().setId("").setEmail("").setDisplayName("")

@JvmStatic
fun newAuditInfo(): AuditInfo.Builder =
AuditInfo.builder().setClientTimestamp(Date(0)).setUser(newUser().build())

private fun newPoint(): Point.Builder = Point.newBuilder().setLatitude(0.0).setLongitude(0.0)
AuditInfo.builder().setClientTimestamp(Date(0)).setUser(
User("", "", "")
)

private fun newGeoPoint(): GeoPoint = GeoPoint(0.0, 0.0)

Expand Down
56 changes: 0 additions & 56 deletions ground/src/main/java/com/google/android/ground/model/User.java

This file was deleted.

24 changes: 24 additions & 0 deletions ground/src/main/java/com/google/android/ground/model/User.kt
@@ -0,0 +1,24 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.ground.model

/** Represents a single application user. */
data class User @JvmOverloads constructor(
val id: String,
val email: String,
val displayName: String,
val photoUrl: String? = null,
)
Expand Up @@ -52,29 +52,16 @@ public abstract class UserEntity {
public abstract String getPhotoUrl();

public static UserEntity fromUser(User user) {
return UserEntity.builder()
.setId(user.getId())
.setEmail(user.getEmail())
.setDisplayName(user.getDisplayName())
.setPhotoUrl(user.getPhotoUrl())
.build();
return UserEntity.builder().setId(user.getId()).setEmail(user.getEmail())
.setDisplayName(user.getDisplayName()).setPhotoUrl(user.getPhotoUrl()).build();
}

public static User toUser(UserEntity u) {
return User.builder()
.setId(u.getId())
.setEmail(u.getEmail())
.setDisplayName(u.getDisplayName())
.setPhotoUrl(u.getPhotoUrl())
.build();
return new User(u.getId(), u.getEmail(), u.getDisplayName(), u.getPhotoUrl());
}

public static UserEntity create(String id, String email, String displayName, String photoUrl) {
return builder()
.setId(id)
.setEmail(email)
.setDisplayName(displayName)
.setPhotoUrl(photoUrl)
return builder().setId(id).setEmail(email).setDisplayName(displayName).setPhotoUrl(photoUrl)
.build();
}

Expand Down
Expand Up @@ -22,7 +22,9 @@
import com.google.auto.value.AutoValue;
import com.google.auto.value.AutoValue.CopyAnnotations;

/** Component representing cached user details in local db entities. */
/**
* Component representing cached user details in local db entities.
*/
@AutoValue
public abstract class UserDetails {

Expand All @@ -42,19 +44,12 @@ public abstract class UserDetails {
public abstract String getDisplayName();

public static UserDetails fromUser(User user) {
return UserDetails.builder()
.setId(user.getId())
.setEmail(user.getEmail())
.setDisplayName(user.getDisplayName())
.build();
return UserDetails.builder().setId(user.getId()).setEmail(user.getEmail())
.setDisplayName(user.getDisplayName()).build();
}

public static User toUser(UserDetails d) {
return User.builder()
.setId(d.getId())
.setEmail(d.getEmail())
.setDisplayName(d.getDisplayName())
.build();
return new User(d.getId(), d.getEmail(), d.getDisplayName());
}

public static UserDetails create(String id, String email, String displayName) {
Expand Down
Expand Up @@ -24,9 +24,5 @@ internal object UserConverter {
fun toNestedObject(user: User) = UserNestedObject(user.id, user.email, user.displayName)

fun toUser(ud: UserNestedObject?): User =
User.builder()
.setId(ud?.id.orEmpty())
.setEmail(ud?.email.orEmpty())
.setDisplayName(ud?.displayName.orEmpty())
.build()
User(ud?.id.orEmpty(), ud?.email.orEmpty(), ud?.displayName.orEmpty())
}
Expand Up @@ -37,8 +37,7 @@ import javax.inject.Inject
private val SIGN_IN_REQUEST_CODE = AuthenticationManager::class.java.hashCode() and 0xffff

class GoogleAuthenticationManager @Inject constructor(
resources: Resources,
private val activityStreams: ActivityStreams
resources: Resources, private val activityStreams: ActivityStreams
) : AuthenticationManager {

private val activityResultsSubscription: Disposable
Expand All @@ -48,10 +47,8 @@ class GoogleAuthenticationManager @Inject constructor(
// TODO: Update Fragments to access via ProjectRepository rather than directly.
init {
googleSignInOptions = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(resources.getString(R.string.default_web_client_id))
.requestEmail()
.requestProfile()
.build()
.requestIdToken(resources.getString(R.string.default_web_client_id)).requestEmail()
.requestProfile().build()

// TODO: Dispose the subscription when object is destroyed
activityResultsSubscription = activityStreams.getActivityResults(SIGN_IN_REQUEST_CODE)
Expand All @@ -65,9 +62,7 @@ class GoogleAuthenticationManager @Inject constructor(
* guaranteed to be authenticated.
*/
override val currentUser: User
get() = signInState.map(SignInState::user)
.filter { it.isPresent }
.map { it.get() }
get() = signInState.map(SignInState::user).filter { it.isPresent }.map { it.get() }
.blockingFirst() // TODO: Should this be blocking?

override fun init() = signInState.onNext(status)
Expand Down Expand Up @@ -113,8 +108,7 @@ class GoogleAuthenticationManager @Inject constructor(
}

private fun onGoogleSignIn(googleAccount: GoogleSignInAccount) =
firebaseAuth
.signInWithCredential(getFirebaseAuthCredential(googleAccount))
firebaseAuth.signInWithCredential(getFirebaseAuthCredential(googleAccount))
.addOnSuccessListener { authResult: AuthResult -> onFirebaseAuthSuccess(authResult) }
.addOnFailureListener { signInState.onNext(SignInState(it)) }

Expand All @@ -126,11 +120,5 @@ class GoogleAuthenticationManager @Inject constructor(
private fun getFirebaseAuthCredential(googleAccount: GoogleSignInAccount): AuthCredential =
GoogleAuthProvider.getCredential(googleAccount.idToken, null)

private fun FirebaseUser.toUser(): User =
User.builder()
.setId(uid)
.setEmail(email)
.setDisplayName(displayName)
.setPhotoUrl(photoUrl.toString())
.build()
private fun FirebaseUser.toUser(): User = User(uid, email ?: "", displayName ?: "", photoUrl.toString())
}
Expand Up @@ -64,7 +64,7 @@
public class LocalDataStoreTest extends BaseHiltTest {

private static final User TEST_USER =
User.builder().setId("user id").setEmail("user@gmail.com").setDisplayName("user 1").build();
new User("user id", "user@gmail.com", "user 1");

private static final Task TEST_TASK = new Task("task id", 1, Type.TEXT, "task label", false);

Expand Down
Expand Up @@ -19,14 +19,14 @@
import static com.google.android.ground.model.TestModelBuilders.newAuditInfo;
import static com.google.android.ground.model.TestModelBuilders.newSurvey;
import static com.google.android.ground.model.TestModelBuilders.newTask;
import static com.google.android.ground.model.TestModelBuilders.newUser;
import static com.google.common.truth.Truth.assertThat;
import static java8.util.J8Arrays.stream;
import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.when;

import com.google.android.ground.model.AuditInfo;
import com.google.android.ground.model.Survey;
import com.google.android.ground.model.User;
import com.google.android.ground.model.job.Job;
import com.google.android.ground.model.locationofinterest.LocationOfInterest;
import com.google.android.ground.model.submission.MultipleChoiceResponse;
Expand Down Expand Up @@ -60,14 +60,14 @@ public class SubmissionConverterTest {

private static final AuditInfo AUDIT_INFO_1 =
newAuditInfo()
.setUser(newUser().setId("user1").build())
.setUser(new User("user1", "", ""))
.setClientTimestamp(new Date(100))
.setServerTimestamp(Optional.of(new Date(101)))
.build();

private static final AuditInfo AUDIT_INFO_2 =
newAuditInfo()
.setUser(newUser().setId("user2").build())
.setUser(new User("user2", "", ""))
.setClientTimestamp(new Date(200))
.setServerTimestamp(Optional.of(new Date(201)))
.build();
Expand Down
Expand Up @@ -20,6 +20,7 @@

import com.google.android.ground.BaseHiltTest;
import com.google.android.ground.model.AuditInfo;
import com.google.android.ground.model.User;
import com.google.android.ground.model.job.Job;
import com.google.android.ground.model.locationofinterest.LocationOfInterest;
import com.google.android.ground.test.FakeData;
Expand All @@ -45,7 +46,7 @@ public void testGetCreatedBy() {
FakeData.POINT_OF_INTEREST.getJob(),
FakeData.POINT_OF_INTEREST.getCustomId(),
FakeData.POINT_OF_INTEREST.getCaption(),
AuditInfo.now(FakeData.USER.toBuilder().setDisplayName("Test User").build()),
AuditInfo.now(new User("", "", "Test User")),
FakeData.POINT_OF_INTEREST.getLastModified(),
FakeData.POINT_OF_INTEREST.getGeometry());
assertThat(featureHelper.getCreatedBy(Optional.of(feature))).isEqualTo("Added by Test User");
Expand Down
Expand Up @@ -17,12 +17,12 @@ package com.google.android.ground.ui.datacollection

import com.google.android.ground.model.Survey
import com.google.android.ground.model.TestModelBuilders
import com.google.android.ground.model.User
import com.google.android.ground.model.job.Job
import com.google.android.ground.model.locationofinterest.LocationOfInterest
import com.google.android.ground.model.locationofinterest.Point
import com.google.android.ground.model.submission.Submission
import com.google.android.ground.model.task.Task
import com.google.common.collect.ImmutableList
import com.google.common.collect.ImmutableMap
import java8.util.Optional
import java.util.*
Expand All @@ -35,7 +35,7 @@ object DataCollectionTestData {
const val loiName = "loiName"
val args = DataCollectionFragmentArgs.Builder(surveyId, loiId, submissionId).build()
private val auditInfo = TestModelBuilders.newAuditInfo()
.setUser(TestModelBuilders.newUser().setId("user1").build())
.setUser(User("user1", "", ""))
.setClientTimestamp(Date(100))
.setServerTimestamp(Optional.of(Date(101)))
.build()
Expand Down
Expand Up @@ -29,16 +29,16 @@

public abstract class BaseMenuVisibilityTest extends BaseHiltTest {

static final User TEST_USER_OWNER = FakeData.USER.toBuilder().setEmail("user1@gmail.com").build();
static final User TEST_USER_OWNER = new User("user1", "user@gmail.com", "user 1");

static final User TEST_USER_MANAGER =
FakeData.USER.toBuilder().setEmail("user2@gmail.com").build();
new User("user2", "user2@gmail.com", "user 2");

static final User TEST_USER_CONTRIBUTOR =
FakeData.USER.toBuilder().setEmail("user3@gmail.com").build();
new User("user2", "user3@gmail.com", "user 3");

static final User TEST_USER_UNKNOWN =
FakeData.USER.toBuilder().setEmail("user4@gmail.com").build();
new User("user2", "user4@gmail.com", "user 4");

private static final Survey TEST_SURVEY =
FakeData.SURVEY.toBuilder()
Expand Down
Expand Up @@ -43,12 +43,11 @@ object FakeData {

@JvmField
val USER =
User.builder().setId("user_id").setEmail("user@gmail.com").setDisplayName("User").build()
User("user_id", "user@gmail.com", "User")

@JvmField
val USER_2 =
User.builder().setId("user_id_2").setEmail("user2@gmail.com").setDisplayName("User2")
.build()
User("user_id_2", "user2@gmail.com", "User2")

@JvmField
val SURVEY: Survey = newSurvey().build()
Expand Down

0 comments on commit f62ae22

Please sign in to comment.