Skip to content

Commit

Permalink
Added UberAuthButton in compose
Browse files Browse the repository at this point in the history
  • Loading branch information
lalwani committed Jun 10, 2024
1 parent a4b5f57 commit 85ebf15
Show file tree
Hide file tree
Showing 10 changed files with 369 additions and 0 deletions.
8 changes: 8 additions & 0 deletions authentication/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,17 @@ android {
}

buildTypes { release { isMinifyEnabled = false } }

buildFeatures { compose = true }
composeOptions { kotlinCompilerExtensionVersion = "1.5.11" }
}

dependencies {
implementation(libs.androidx.ui.tooling.preview.android)
val composeBom = platform(libs.compose.bom)
implementation(composeBom)
implementation(libs.androidx.compose.foundation)
implementation(libs.material3)
implementation(libs.appCompat)
implementation(libs.chrometabs)
implementation(libs.material)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright (C) 2016. Uber Technologies
*
* 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.uber.sdk2.auth.ui

import android.content.Context
import android.util.AttributeSet
import androidx.annotation.StringRes
import androidx.annotation.StyleRes
import androidx.annotation.VisibleForTesting
import com.uber.sdk2.auth.R
import com.uber.sdk2.auth.UberAuthClientImpl
import com.uber.sdk2.auth.request.AuthContext
import com.uber.sdk2.core.ui.UberButton
import com.uber.sdk2.core.ui.UberStyle

/** The [LoginButton] is used to initiate the Uber SDK Login flow. */
class LoginButton : UberButton {
private var authContext: AuthContext? = null

constructor(context: Context) : super(context)

constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)

constructor(
context: Context,
attrs: AttributeSet?,
defStyleAttr: Int,
) : super(context, attrs, defStyleAttr)

constructor(
context: Context,
attrs: AttributeSet?,
defStyleAttr: Int,
defStyleRes: Int,
) : super(context, attrs, defStyleAttr, defStyleRes)

override fun init(
context: Context,
@StringRes defaultText: Int,
attrs: AttributeSet?,
defStyleAttr: Int,
uberStyle: UberStyle,
) {
isAllCaps = true

val defStyleRes = STYLES[uberStyle.value]

applyStyle(context, R.string.ub__sign_in, attrs, defStyleAttr, defStyleRes)

setOnClickListener { login() }
}

@VisibleForTesting
fun login() {
val activity = activity
UberAuthClientImpl().authenticate(activity, authContext!!)
}

/**
* A [AuthContext] is required to identify the app being authenticated.
*
* @param authContext to be identified.
* @return this instance of [LoginButton]
*/
fun authContext(authContext: AuthContext): LoginButton {
this.authContext = authContext
return this
}

companion object {
@StyleRes
private val STYLES = intArrayOf(R.style.UberButton_Login, R.style.UberButton_Login_White)
}
}
122 changes: 122 additions & 0 deletions authentication/src/main/kotlin/com/uber/sdk2/auth/ui/UberAuthButton.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package com.uber.sdk2.auth.ui

import android.media.Image
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsPressedAsState
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.uber.sdk2.core.R

/** A button that looks like the Uber button. */
@Composable
fun UberCustomButton(
text: String,
isWhite: Boolean = false,
onClick: () -> Unit
) {
val interactionSource = remember { MutableInteractionSource() }
val isPressed = interactionSource.collectIsPressedAsState().value
val backgroundColor = if (isPressed) {
if (isWhite) colorResource(id = R.color.uber_white_40) else colorResource(id = R.color.uber_black_90)
} else {
if (isWhite) colorResource(id = R.color.uber_white) else colorResource(id = R.color.uber_black)
}

val textColor = if (isWhite) {
colorResource(id = R.color.uber_black)
} else {
colorResource(id = R.color.uber_white)
}

Text(
text = text,
color = textColor,
style = TextStyle(
fontSize = dimensionResource(id = R.dimen.ub__text_size).value.sp
),
modifier = Modifier
.clip(RoundedCornerShape(4.dp))
.background(backgroundColor)
.clickable(interactionSource = interactionSource, indication = null, onClick = onClick)
.padding(16.dp)
)
}

@Composable
fun UberAuthButton(
text: String,
isWhite: Boolean = false,
onClick: () -> Unit
) {
val interactionSource = remember { MutableInteractionSource() }
val isPressed = interactionSource.collectIsPressedAsState().value
val backgroundColor = if (isPressed) {
if (isWhite) colorResource(id = R.color.uber_white_40) else colorResource(id = R.color.uber_black_90)
} else {
if (isWhite) colorResource(id = R.color.uber_white) else colorResource(id = R.color.uber_black)
}

val textColor = if (isWhite) {
colorResource(id = R.color.uber_black)
} else {
colorResource(id = R.color.uber_white)
}

val logo = if (isWhite) {
painterResource(id = R.drawable.uber_logotype_black)
} else {
painterResource(id = R.drawable.uber_logotype_white)
}

Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.clip(RoundedCornerShape(4.dp))
.background(backgroundColor)
.clickable(interactionSource = interactionSource, indication = null, onClick = onClick)
.padding(dimensionResource(id = R.dimen.ub__standard_padding))
) {
Image(
painter = logo,
contentDescription = null,
modifier = Modifier.padding(end = dimensionResource(id = com.uber.sdk2.auth.R.dimen.ub__signin_margin))
)
Text(
text = text,
color = textColor,
style = TextStyle(
fontSize = dimensionResource(id = R.dimen.ub__secondary_text_size).value.sp
),
modifier = Modifier.weight(1f)
)
}
}

@Preview(showBackground = true)
@Composable
fun UberButtonPreview() {
Column {
UberCustomButton(text = "Default Uber Button", onClick = { /* Do something */ })
UberCustomButton(text = "White Uber Button", isWhite = true, onClick = { /* Do something */ })
UberAuthButton(text = "Sign in with Uber", onClick = { /* Do something */ })
UberAuthButton(text = "Sign in with Uber (White)", isWhite = true, onClick = { /* Do something */ })
}
}
20 changes: 20 additions & 0 deletions authentication/src/main/res/values-hi-rIN/strings_localized.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (c) 2024. Uber Technologies
~
~ 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.
-->

<resources>
<string name="ub__sign_in">साइन इन करें</string>
</resources>
20 changes: 20 additions & 0 deletions authentication/src/main/res/values-zh-rCN/strings_localized.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (c) 2024. Uber Technologies
~
~ 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.
-->

<resources>
<string name="ub__sign_in">登录</string>
</resources>
20 changes: 20 additions & 0 deletions authentication/src/main/res/values-zh-rHK/strings_localized.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (c) 2024. Uber Technologies
~
~ 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.
-->

<resources>
<string name="ub__sign_in">Sign in</string>
</resources>
20 changes: 20 additions & 0 deletions authentication/src/main/res/values-zh-rTW/strings_localized.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (c) 2024. Uber Technologies
~
~ 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.
-->

<resources>
<string name="ub__sign_in">登入</string>
</resources>
20 changes: 20 additions & 0 deletions authentication/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (c) 2024. Uber Technologies
~
~ 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.
-->

<resources>
<dimen name="ub__signin_margin">48dp</dimen>
</resources>
20 changes: 20 additions & 0 deletions authentication/src/main/res/values/strings_localized.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (c) 2024. Uber Technologies
~
~ 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.
-->

<resources>
<string name="ub__sign_in">Sign in</string>
</resources>
32 changes: 32 additions & 0 deletions authentication/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (c) 2024. Uber Technologies
~
~ 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.
-->

<resources>
<style name="UberButton.Login">
<item name="android:padding">@dimen/ub__standard_padding</item>
<item name="android:textSize">@dimen/ub__secondary_text_size</item>
<item name="android:gravity">center_vertical|end</item>
<item name="android:drawablePadding">@dimen/ub__signin_margin</item>
<item name="android:drawableLeft">@drawable/uber_logotype_white</item>
</style>

<style name="UberButton.Login.White">
<item name="android:textColor">@color/uber_black</item>
<item name="android:background">@drawable/uber_button_background_selector_white</item>
<item name="android:drawableLeft">@drawable/uber_logotype_black</item>
</style>
</resources>

0 comments on commit 85ebf15

Please sign in to comment.