Skip to content

Commit

Permalink
Merge pull request #248 from uber/button
Browse files Browse the repository at this point in the history
Added UberAuth composable button
  • Loading branch information
lalwani authored Jun 27, 2024
2 parents 82994e3 + 60525e4 commit 03629ad
Show file tree
Hide file tree
Showing 17 changed files with 433 additions and 3 deletions.
8 changes: 8 additions & 0 deletions authentication/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,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.UberStyle
import com.uber.sdk2.core.ui.legacy.UberButton

/** 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)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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.
*/
package com.uber.sdk2.auth.ui

import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsPressedAsState
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import com.uber.sdk2.auth.ui.theme.UberDimens
import com.uber.sdk2.auth.ui.theme.UberTypography
import com.uber.sdk2.core.R

@Composable
fun UberAuthButton(
isWhite: Boolean = false,
shape: Shape = MaterialTheme.shapes.large,
onClick: () -> Unit,
) {
val text = stringResource(id = com.uber.sdk2.auth.R.string.ub__sign_in)
val interactionSource = remember { MutableInteractionSource() }
val isPressed = interactionSource.collectIsPressedAsState().value
val backgroundColor =
if (isPressed) {
MaterialTheme.colorScheme.onSecondary
} else {
MaterialTheme.colorScheme.onPrimary
}

val textColor = MaterialTheme.colorScheme.primary

val iconResId = if (isWhite) R.drawable.uber_logotype_black else R.drawable.uber_logotype_white

Button(
onClick = onClick,
modifier = Modifier.wrapContentSize(),
colors =
ButtonDefaults.buttonColors(containerColor = backgroundColor, contentColor = textColor),
shape = shape,
interactionSource = interactionSource,
) {
Icon(
painter = painterResource(id = iconResId),
contentDescription = null,
modifier = Modifier.padding(end = UberDimens.signInMargin),
)
Text(
text = text.uppercase(),
color = textColor,
style = UberTypography.bodyMedium,
modifier = Modifier.padding(UberDimens.standardPadding).wrapContentWidth(),
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.
*/
package com.uber.sdk2.auth.ui.theme

import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Shapes
import androidx.compose.ui.unit.dp

val UberButtonShapes =
Shapes(
small = RoundedCornerShape(4.dp),
medium = RoundedCornerShape(8.dp),
large = RoundedCornerShape(16.dp),
)

object UberDimens {
val smallPadding = 8.dp
val standardPadding = 16.dp
val signInMargin = 48.dp
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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.
*/
package com.uber.sdk2.auth.ui.theme

import androidx.compose.ui.graphics.Color

val UberBlack = Color(0xFF000000)
val UberBlack90 = Color(0xFF282727)
val UberWhite = Color(0xFFFFFFFF)
val UberWhite40 = Color(0xFFE5E5E4)
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.
*/
package com.uber.sdk2.auth.ui.theme

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable

@Composable
fun UberTheme(darkTheme: Boolean = isSystemInDarkTheme(), content: @Composable () -> Unit) {
val colors =
if (darkTheme) {
darkColorScheme(primary = UberBlack, onPrimary = UberWhite, onSecondary = UberWhite40)
} else {
lightColorScheme(primary = UberWhite, onPrimary = UberBlack, onSecondary = UberBlack90)
}

MaterialTheme(
colorScheme = colors,
typography = UberTypography,
shapes = UberButtonShapes,
content = content,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.
*/
package com.uber.sdk2.auth.ui.theme

import androidx.compose.material3.Typography
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.sp

val UberTypography =
Typography(
bodySmall = TextStyle(fontSize = 12.sp),
bodyMedium = TextStyle(fontSize = 14.sp),
bodyLarge = TextStyle(fontSize = 20.sp),
)
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>
Loading

0 comments on commit 03629ad

Please sign in to comment.