Skip to content

Commit

Permalink
Convert Java -> Kotlin (#5)
Browse files Browse the repository at this point in the history
* [Roles] We want the user to be able to click on each row and change the role of that user, and see that change reflected in the UI

Bugs from most obvious to least:
1. We're showing the role UID, not the role String
3. The 2nd clause of the switch case in the UserAdapter::openRolePickerDialog is super broken (doesn't have break and is putting the wrong role_uid)
4. We're copy/pasting expensive getters
5. We're doing a lot of this role changing on the main thread
6. This dialog logic should probably live outside of the UserAdapter

And probably more

* Convert Java to Kotlin

* Java -> Kotlin

Co-authored-by: Amy Tang <[email protected]>
Co-authored-by: Brian Amesbury <[email protected]>
  • Loading branch information
3 people authored Nov 22, 2022
1 parent 1a0f778 commit 962a8e5
Show file tree
Hide file tree
Showing 29 changed files with 311 additions and 373 deletions.
59 changes: 26 additions & 33 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'
apply plugin: "com.android.application"
apply plugin: "kotlin-android"
apply plugin: "kotlin-kapt"

android {
compileSdkVersion 31
compileSdkVersion 32
defaultConfig {
applicationId "buggy.plandroid.com.plandroidbugs"
minSdkVersion 21
targetSdkVersion 31
targetSdkVersion 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
}
}
compileOptions {
Expand All @@ -27,39 +25,34 @@ android {
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:27.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

implementation 'com.android.support:recyclerview-v7:27.1.0'
implementation 'androidx.appcompat:appcompat:1.4.2'

implementation "androidx.constraintlayout:constraintlayout:2.1.4"

implementation 'android.arch.core:runtime:1.1.0'
implementation "android.arch.persistence.room:runtime:$room_version"
annotationProcessor "android.arch.persistence.room:compiler:$room_version" // use kapt for Kotlin
implementation "androidx.recyclerview:recyclerview:1.2.1"

// optional - RxJava support for Room
implementation "android.arch.persistence.room:rxjava2:$room_version"
kapt "androidx.room:room-compiler:2.4.3"
implementation "androidx.room:room-runtime:2.4.3"
implementation "androidx.room:room-rxjava2:2.4.3"

// Some popular third party libraries
// Use as many or as few as you want
// Feel free to add more
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.7.0'
implementation 'com.fasterxml.jackson.core:jackson-core:2.7.0'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.7.0'
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin:2.7.9.1'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.squareup.retrofit2:converter-jackson:2.4.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'io.reactivex:rxjava:1.3.5'
implementation "com.fasterxml.jackson.core:jackson-annotations:2.11.1"
implementation "com.fasterxml.jackson.core:jackson-core:2.11.1"
implementation "com.fasterxml.jackson.core:jackson-databind:2.11.1"
implementation "com.fasterxml.jackson.module:jackson-module-kotlin:2.11.1"
implementation "com.squareup.retrofit2:retrofit:2.9.0"
implementation "com.squareup.retrofit2:converter-gson:2.9.0"
implementation "com.squareup.retrofit2:converter-jackson:2.6.1"
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.6.1'
implementation "com.squareup.okhttp3:okhttp:5.0.0-alpha.6"
implementation 'io.reactivex:rxjava:1.3.8'
implementation 'io.reactivex:rxandroid:1.2.1'
implementation 'io.reactivex.rxjava2:rxjava:2.1.9'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.2.50'
implementation 'io.reactivex.rxjava2:rxjava:2.2.11'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package buggy.plandroid.com.plandroidbugs

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import buggy.plandroid.com.plandroidbugs.app.PlanDroidApp.Companion.getApp
import io.reactivex.android.schedulers.AndroidSchedulers

class MainActivity : AppCompatActivity() {
@SuppressLint("CheckResult")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val userAdapter = UserAdapter()
findViewById<RecyclerView>(R.id.recycler).apply {
adapter = userAdapter
layoutManager = LinearLayoutManager(this@MainActivity)
}
val userDao = getApp(applicationContext).database.userDao()

// Listens to changes to the User database table.
userDao.all().observeOn(AndroidSchedulers.mainThread())
.subscribe { userAdapter.setUsers(it) }

}
}
75 changes: 0 additions & 75 deletions app/src/main/java/buggy/plandroid/com/plandroidbugs/PGApi.java

This file was deleted.

69 changes: 69 additions & 0 deletions app/src/main/java/buggy/plandroid/com/plandroidbugs/PGApi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package buggy.plandroid.com.plandroidbugs

import android.util.Base64
import android.util.Log
import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.KotlinModule
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.jackson.JacksonConverterFactory

class PGApi {
private val objectMapper = getObjectMapper()
private val okHttpClient: OkHttpClient = OkHttpClient.Builder()
.addInterceptor(provideUrlAndHeaderInterceptor())
.build()

private fun provideUrlAndHeaderInterceptor(): Interceptor {
return Interceptor { chain: Interceptor.Chain ->
chain.proceed(
chain.request().newBuilder()
.addHeader("Accept", "application/vnd.plangrid+json; version=1")
.addHeader("Authorization", basicAuth(AUTH_KEY + ":\"\""))
.build()
)
}
}

private val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(JacksonConverterFactory.create(ObjectMapper()))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
private val apiService = retrofit.create(PGApiService::class.java)

val projectUsersObservable: Observable<UserList> =
apiService.getAllUsers().subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())

companion object {
const val BASE_URL = "https://plangrid-c-api-dispatcher-test.planfront.net"
private const val AUTH_KEY = "521dd37f15b497e01fd7aeacab0892ec"

fun basicAuth(toEncode: String): String {
return String.format(
"Basic %s",
String(Base64.encode(toEncode.toByteArray(), Base64.DEFAULT))
).trim { it <= ' ' }
}

fun getObjectMapper(): ObjectMapper {
val mapper = ObjectMapper()
mapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false)
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
mapper.configure(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY, false)
mapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false)
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
mapper.registerModule(KotlinModule())
return mapper
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package buggy.plandroid.com.plandroidbugs

import retrofit2.http.GET
import io.reactivex.Observable
import retrofit2.Call

interface PGApiService {
@GET("projects/fc6facf8-c69c-4780-bac1-1774bd91cc8c/users")
fun getAllUsers(): Observable<UserList>

@GET("projects/fc6facf8-c69c-4780-bac1-1774bd91cc8c/users/540e0bdcde37b40013533497")
fun getUser(): Call<UserWire>
}

This file was deleted.

Loading

0 comments on commit 962a8e5

Please sign in to comment.