Skip to content

Commit

Permalink
Merge pull request #418 from fobo66/auth
Browse files Browse the repository at this point in the history
Obtain Steam Id from the phone
  • Loading branch information
fobo66 authored Jan 19, 2024
2 parents ee7cb54 + 34d320e commit d64ec1a
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,42 @@ package io.github.fobo66.wearmmr.ui
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.widget.TextView.BufferType.EDITABLE
import androidx.activity.ComponentActivity
import androidx.core.content.getSystemService
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.wear.ambient.AmbientLifecycleObserver
import androidx.wear.phone.interactions.authentication.CodeChallenge
import androidx.wear.phone.interactions.authentication.CodeVerifier
import androidx.wear.phone.interactions.authentication.OAuthRequest
import androidx.wear.phone.interactions.authentication.OAuthResponse
import androidx.wear.phone.interactions.authentication.RemoteAuthClient
import androidx.wear.watchface.complications.datasource.ComplicationDataSourceUpdateRequester
import com.google.android.material.color.DynamicColors
import io.github.fobo66.wearmmr.databinding.ActivitySettingsBinding
import io.github.fobo66.wearmmr.domain.RatingComplicationDataSource
import io.github.fobo66.wearmmr.model.SettingsViewModel
import kotlinx.coroutines.launch
import org.koin.androidx.viewmodel.ext.android.viewModel
import timber.log.Timber

class SettingsActivity : ComponentActivity() {

private lateinit var binding: ActivitySettingsBinding

private val ambientObserver = AmbientLifecycleObserver(
this,
object: AmbientLifecycleObserver.AmbientLifecycleCallback {

}
)

private val updateRequester: ComplicationDataSourceUpdateRequester by lazy {
ComplicationDataSourceUpdateRequester.create(
this,
Expand All @@ -49,14 +67,17 @@ class SettingsActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
DynamicColors.applyToActivityIfAvailable(this)
lifecycle.addObserver(ambientObserver)
binding = ActivitySettingsBinding.inflate(layoutInflater)
setContentView(binding.root)

lifecycleScope.launchWhenResumed {
binding.playerIdInput.editText?.setText(
settingsViewModel.loadPlayerId(),
EDITABLE
)
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.RESUMED) {
binding.playerIdInput.editText?.setText(
settingsViewModel.loadPlayerId(),
EDITABLE
)
}
}

binding.playerIdInput.editText?.setOnEditorActionListener { _, actionId, _ ->
Expand All @@ -72,6 +93,33 @@ class SettingsActivity : ComponentActivity() {
false
}
}

binding.signInButton.setOnClickListener {
val codeVerifier = CodeVerifier()
val request = OAuthRequest.Builder(this.applicationContext)
.setAuthProviderUrl(Uri.parse("https://steamcommunity.com/oauth/login"))
.setCodeChallenge(CodeChallenge(codeVerifier))
.setClientId("wearmmr")
.build()
val client = RemoteAuthClient.create(this)
client.sendAuthorizationRequest(request,
{ command -> command?.run() },
object : RemoteAuthClient.Callback() {
override fun onAuthorizationError(request: OAuthRequest, errorCode: Int) {
Timber.e("Auth failed with code %d", errorCode)
}

override fun onAuthorizationResponse(
request: OAuthRequest,
response: OAuthResponse
) {
Timber.d("Auth successful")
}
}
)


}
}

private fun updateComplication() {
Expand All @@ -87,7 +135,10 @@ class SettingsActivity : ComponentActivity() {

companion object {
fun start(context: Context) {
context.startActivity(Intent(context, SettingsActivity::class.java))
context.startActivity(Intent(context, SettingsActivity::class.java).setClassName(
context.packageName,
SettingsActivity::class.java.name
))
}
}
}
12 changes: 11 additions & 1 deletion app/src/main/res/layout/activity_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textview.MaterialTextView
style="@style/TextAppearance.Material3.BodySmall"
android:id="@+id/settings_player_id_hint"
style="@style/TextAppearance.Material3.BodySmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
Expand All @@ -69,6 +69,16 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/player_id_input" />

<Button
android:id="@+id/sign_in_button"
style="@style/Widget.Material3.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/settings_action_on_the_phone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/settings_player_id_hint" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.wear.widget.BoxInsetLayout>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@
<string name="settings_player_id_hint">Please specify your Steam ID in the input above so we can fetch your MMR</string>
<string name="invalid_player_id_error">Your player ID seems to be incorrect. Please specify your Steam ID in the settings so we can fetch your MMR</string>
<string name="no_rating_error">Unable to fetch your MMR. Please try again later</string>
<string name="settings_action_on_the_phone">Sign in on the phone</string>
</resources>
4 changes: 4 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ plugins {
`kotlin-dsl`
}

kotlin {
jvmToolchain(17)
}

repositories {
mavenCentral()
}
Expand Down
5 changes: 3 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
# limitations under the License.
#

org.gradle.jvmargs=-Xms1g -Xmx8g -XX:+UseParallelGC
org.gradle.jvmargs=-Xms512m -Xmx16g -XX:+UseSerialGC -XX:CompileCommand=exclude,com/android/tools/r8/internal/fb,a
org.gradle.parallel=true
org.gradle.configureondemand=false
org.gradle.caching=true
android.useAndroidX=true
android.enableJetifier=false
android.disableResourceValidation=true
android.nonFinalResIds=true
kotlin.code.style=official
org.gradle.vfs.watch=true
org.gradle.unsafe.configuration-cache=true
org.gradle.configuration-cache=true
kotlin.incremental.useClasspathSnapshot=true
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-rc-2-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
6 changes: 3 additions & 3 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ dependencyResolutionManagement {
}

register("libs") {
version("kotlin", "1.9.21")
version("kotlin", "1.9.22")
version("coroutines", "1.7.3")
plugin("ksp", "com.google.devtools.ksp").version("1.9.21-1.0.16")
plugin("ksp", "com.google.devtools.ksp").version("1.9.22-1.0.16")
plugin("detekt", "io.gitlab.arturbosch.detekt").version("1.23.4")
library("coil", "io.coil-kt:coil-compose:2.5.0")
library("material", "com.google.android.material:material:1.11.0")
Expand All @@ -56,7 +56,7 @@ dependencyResolutionManagement {
}

register("compose") {
version("compiler", "1.5.7")
version("compiler", "1.5.8")
version("regular", "1.5.4")
version("wear", "1.2.1")
library("foundation", "androidx.compose.foundation", "foundation").versionRef("regular")
Expand Down

0 comments on commit d64ec1a

Please sign in to comment.