Skip to content

Commit

Permalink
chore: Add ktlint, fix ktlint errors, fix deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
pklawikowski-schibsted committed Sep 19, 2024
1 parent 793d08b commit ce99f2c
Show file tree
Hide file tree
Showing 63 changed files with 1,368 additions and 1,042 deletions.
10 changes: 5 additions & 5 deletions app/src/main/java/com/schibsted/account/example/ClientConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import com.schibsted.account.webflows.client.Environment
object ClientConfig {
@JvmStatic
val environment = Environment.PRE
const val clientId = "602525f2b41fa31789a95aa8"
const val loginRedirectUri = "com.sdk-example.pre.602525f2b41fa31789a95aa8:///login"
const val manualLoginRedirectUri = "com.sdk-example.pre.602525f2b41fa31789a95aa8:///manual-login"
const val webClientId = "599fd705ed21dc0d55011d2a"
const val webClientRedirectUri = "https://pre.sdk-example.com/safepage"
const val CLIENT_ID = "602525f2b41fa31789a95aa8"
const val LOGIN_REDIRECT_URI = "com.sdk-example.pre.602525f2b41fa31789a95aa8:///login"
const val MANUAL_LOGIN_REDIRECT_URI = "com.sdk-example.pre.602525f2b41fa31789a95aa8:///manual-login"
const val WEB_CLIENT_ID = "599fd705ed21dc0d55011d2a"
const val WEB_CLIENT_REDIRECT_URI = "https://pre.sdk-example.com/safepage"
}
85 changes: 46 additions & 39 deletions app/src/main/java/com/schibsted/account/example/ExampleApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import com.schibsted.account.webflows.tracking.SchibstedAccountTrackingListener
import timber.log.Timber

class ExampleApp : Application() {

override fun onCreate() {
super.onCreate()

Expand All @@ -29,11 +28,12 @@ class ExampleApp : Application() {
}

private fun initTracking() {
val listener = object : SchibstedAccountTrackingListener {
override fun onEvent(event: SchibstedAccountTrackingEvent) {
Timber.d("Tracked event ${event::class.simpleName.toString()}")
val listener =
object : SchibstedAccountTrackingListener {
override fun onEvent(event: SchibstedAccountTrackingEvent) {
Timber.d("Tracked event ${event::class.simpleName}")
}
}
}

SchibstedAccountTrackerStore.addTrackingListener(listener)
}
Expand All @@ -45,31 +45,36 @@ class ExampleApp : Application() {
}

private fun initClient() {
val clientConfig = ClientConfiguration(
env = environment,
clientId = ClientConfig.clientId,
redirectUri = ClientConfig.loginRedirectUri
)
client = Client(
context = applicationContext,
configuration = clientConfig,
httpClient = instance
)
val clientConfig =
ClientConfiguration(
env = environment,
clientId = ClientConfig.CLIENT_ID,
redirectUri = ClientConfig.LOGIN_REDIRECT_URI,
)
client =
Client(
context = applicationContext,
configuration = clientConfig,
httpClient = instance,
)
}

private fun initManualClient() {
val clientConfig = ClientConfiguration(
env = environment,
clientId = ClientConfig.clientId,
redirectUri = ClientConfig.manualLoginRedirectUri
)
manualClient = Client(
context = applicationContext,
configuration = clientConfig,
httpClient = instance,
logoutCallback = {
Timber.i("Received a logout event from client")
})
val clientConfig =
ClientConfiguration(
env = environment,
clientId = ClientConfig.CLIENT_ID,
redirectUri = ClientConfig.MANUAL_LOGIN_REDIRECT_URI,
)
manualClient =
Client(
context = applicationContext,
configuration = clientConfig,
httpClient = instance,
logoutCallback = {
Timber.i("Received a logout event from client")
},
)
}

private fun initAuthorizationManagement() {
Expand All @@ -79,18 +84,20 @@ class ExampleApp : Application() {
cancelIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
AuthorizationManagementActivity.setup(
client = client,
completionIntent = PendingIntent.getActivity(
this,
0,
completionIntent,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE else 0
),
cancelIntent = PendingIntent.getActivity(
this,
1,
cancelIntent,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE else 0
)
completionIntent =
PendingIntent.getActivity(
this,
0,
completionIntent,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE else 0,
),
cancelIntent =
PendingIntent.getActivity(
this,
1,
cancelIntent,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE else 0,
),
)
}

Expand Down
11 changes: 6 additions & 5 deletions app/src/main/java/com/schibsted/account/example/HttpClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import okhttp3.logging.HttpLoggingInterceptor

object HttpClient {
@JvmStatic
val instance: OkHttpClient = run {
val logging = HttpLoggingInterceptor()
logging.setLevel(HttpLoggingInterceptor.Level.BODY)
OkHttpClient.Builder().addNetworkInterceptor(logging).build()
}
val instance: OkHttpClient =
run {
val logging = HttpLoggingInterceptor()
logging.setLevel(HttpLoggingInterceptor.Level.BODY)
OkHttpClient.Builder().addNetworkInterceptor(logging).build()
}
}
44 changes: 25 additions & 19 deletions app/src/main/java/com/schibsted/account/example/LoggedInActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,9 @@ class LoggedInActivity : AppCompatActivity() {
binding.sessionExchangeButton.setOnClickListener {
if (isUserLoggedIn) {
user?.webSessionUrl(
clientId = ClientConfig.webClientId,
redirectUri = ClientConfig.webClientRedirectUri,
)
{ result: Either<HttpError?, URL> ->
clientId = ClientConfig.WEB_CLIENT_ID,
redirectUri = ClientConfig.WEB_CLIENT_REDIRECT_URI,
) { result: Either<HttpError?, URL> ->
result
.onSuccess { value: URL ->
Timber.i("Session exchange URL: $value")
Expand All @@ -121,11 +120,12 @@ class LoggedInActivity : AppCompatActivity() {
}

private fun evaluateAndUpdateUserSession() {
client = when (intent.getSerializableExtra(FLOW_EXTRA)) {
Flow.AUTOMATIC -> ExampleApp.client
Flow.MANUAL -> ExampleApp.manualClient
else -> throw RuntimeException("Must provide a flow enum")
}
client =
when (intent.getSerializableExtra(FLOW_EXTRA)) {
Flow.AUTOMATIC -> ExampleApp.client
Flow.MANUAL -> ExampleApp.manualClient
else -> throw RuntimeException("Must provide a flow enum")
}

val userSession: UserSession? = intent.getParcelableExtra(USER_SESSION_EXTRA)
client?.let { client ->
Expand All @@ -136,14 +136,15 @@ class LoggedInActivity : AppCompatActivity() {

private fun initMakeAuthenticatedRequestButton() {
binding.testRetrofitAuthenticatedRequest.setOnClickListener {
val myService: SimpleService = HttpClient.instance.newBuilder().let {
user?.bind(it)
Retrofit.Builder()
.baseUrl(ClientConfig.environment.url)
.client(it.build())
.build()
.create(SimpleService::class.java)
}
val myService: SimpleService =
HttpClient.instance.newBuilder().let {
user?.bind(it)
Retrofit.Builder()
.baseUrl(ClientConfig.environment.url)
.client(it.build())
.build()
.create(SimpleService::class.java)
}

CoroutineScope(Dispatchers.IO).launch {
val profileData = myService.userProfile(user?.userId.toString()).await()
Expand All @@ -164,10 +165,15 @@ class LoggedInActivity : AppCompatActivity() {
private const val FLOW_EXTRA = "com.schibsted.account.FLOW"

enum class Flow {
AUTOMATIC, MANUAL
AUTOMATIC,
MANUAL,
}

fun intentWithUser(context: Context?, user: User, flow: Flow): Intent {
fun intentWithUser(
context: Context?,
user: User,
flow: Flow,
): Intent {
val intent = Intent(context, LoggedInActivity::class.java)
intent.putExtra(USER_SESSION_EXTRA, user.session)
intent.putExtra(FLOW_EXTRA, flow)
Expand Down
24 changes: 13 additions & 11 deletions app/src/main/java/com/schibsted/account/example/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class MainActivity : AppCompatActivity() {
}
}


private fun initializeButtons() {
binding.loginButton.setOnClickListener {
startActivity(ExampleApp.client.getAuthenticationIntent(this, "customState"))
Expand All @@ -51,13 +50,16 @@ class MainActivity : AppCompatActivity() {
}

private fun observeAuthResultLiveData() {
AuthResultLiveData.get(ExampleApp.client).observe(this, Observer { result: Either<NotAuthed, User> ->
result
.onSuccess { user: User -> startLoggedInActivity(user) }
.onFailure { state: NotAuthed ->
handleNotAuthedState(state)
}
} as Observer<Either<NotAuthed, User>>)
AuthResultLiveData.get(ExampleApp.client).observe(
this,
Observer { result: Either<NotAuthed, User> ->
result
.onSuccess { user: User -> startLoggedInActivity(user) }
.onFailure { state: NotAuthed ->
handleNotAuthedState(state)
}
} as Observer<Either<NotAuthed, User>>,
)
}

private fun handleNotAuthedState(state: NotAuthed) {
Expand Down Expand Up @@ -86,12 +88,12 @@ class MainActivity : AppCompatActivity() {
LoggedInActivity.intentWithUser(
this,
user,
LoggedInActivity.Companion.Flow.AUTOMATIC
)
LoggedInActivity.Companion.Flow.AUTOMATIC,
),
)
}

companion object {
var LOGIN_FAILED_EXTRA = "com.schibsted.account.LOGIN_FAILED"
const val LOGIN_FAILED_EXTRA = "com.schibsted.account.LOGIN_FAILED"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class ManualLoginActivity : AppCompatActivity() {
Toast.makeText(
this,
"User could not be resumed, error: ${it.cause.message} ",
Toast.LENGTH_SHORT
Toast.LENGTH_SHORT,
)
.show()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ import retrofit2.http.Path

interface SimpleService {
@GET("/api/2/user/{userId}")
fun userProfile(@Path("userId") userId: String): Call<ResponseBody>
fun userProfile(
@Path("userId") userId: String,
): Call<ResponseBody>
}
5 changes: 2 additions & 3 deletions app/src/test/java/com/schibsted/account/ExampleUnitTest.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.schibsted.account

import org.junit.Assert.assertEquals
import org.junit.Test

import org.junit.Assert.*

/**
* Example local unit test, which will execute on the development machine (host).
*
Expand All @@ -14,4 +13,4 @@ class ExampleUnitTest {
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
}
}
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ buildscript {
classpath 'com.android.tools.build:gradle:8.5.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

classpath "org.jlleitschuh.gradle:ktlint-gradle:12.1.1"

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand All @@ -24,6 +26,8 @@ plugins {
def gitVersion = ['sh', '-c', 'git describe --tag 2> /dev/null || git rev-parse --short HEAD'].execute().text.trim()

allprojects {
apply plugin: "org.jlleitschuh.gradle.ktlint" // Version should be inherited from parent

repositories {
google()
mavenCentral()
Expand Down
Loading

0 comments on commit ce99f2c

Please sign in to comment.