Skip to content

Commit

Permalink
[Roles] Click on user and change their role
Browse files Browse the repository at this point in the history
  • Loading branch information
C2H6O committed Nov 22, 2022
1 parent 962a8e5 commit 41aff42
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 2 deletions.
11 changes: 11 additions & 0 deletions app/src/main/java/buggy/plandroid/com/plandroidbugs/PGApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ class PGApi {
apiService.getAllUsers().subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())

fun changeUserRole(userUid: String, roleUid: String) {
val map = mapOf("role_uid" to roleUid)
try {
apiService.changeUserRole(userUid, map).subscribeOn(Schedulers.computation())
.doOnError { e: Throwable? -> Log.d("PGApi", "Failed to change roles", e) }
.subscribe()
} catch (e: Exception) {
Log.e("PGApi", e.message, e)
}
}

companion object {
const val BASE_URL = "https://plangrid-c-api-dispatcher-test.planfront.net"
private const val AUTH_KEY = "521dd37f15b497e01fd7aeacab0892ec"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
package buggy.plandroid.com.plandroidbugs

import retrofit2.http.GET
import buggy.plandroid.com.plandroidbugs.UserList
import buggy.plandroid.com.plandroidbugs.UserWire
import io.reactivex.Observable
import retrofit2.http.PATCH
import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.http.Body
import retrofit2.http.Path

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>

@PATCH("projects/fc6facf8-c69c-4780-bac1-1774bd91cc8c/users/{userId}")
fun changeUserRole(
@Path("userId") userId: String,
@Body parameters: Map<String, String>
): Observable<ResponseBody?>
}
10 changes: 10 additions & 0 deletions app/src/main/java/buggy/plandroid/com/plandroidbugs/Role.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package buggy.plandroid.com.plandroidbugs

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty

@JsonIgnoreProperties(ignoreUnknown = true)
data class Role(
@JsonProperty(value = "uid")
val uid: String
)
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package buggy.plandroid.com.plandroidbugs

import android.app.AlertDialog
import android.content.Context
import android.content.DialogInterface
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import buggy.plandroid.com.plandroidbugs.app.PlanDroidApp.Companion.getApp
import buggy.plandroid.com.plandroidbugs.room.UserEntity

class UserAdapter : RecyclerView.Adapter<ViewHolder>() {

private lateinit var levelDialog: AlertDialog
private var userEntities: List<UserEntity> = ArrayList()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
Expand All @@ -17,7 +22,36 @@ class UserAdapter : RecyclerView.Adapter<ViewHolder>() {
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.itemView.findViewById<TextView>(R.id.email_text).text = userEntities[position].email
holder.itemView.findViewById<TextView>(R.id.email_text).text =
getDisplayText(userEntities[position])
holder.itemView.setOnClickListener {
openRolePickerDialog(
userEntities[position], holder.itemView.context
)
}
}

private fun getDisplayText(userEntity: UserEntity): String {
return "${userEntity.firstName}: ${roleUidsToNames[userEntity.roleUid]}"
}

private fun openRolePickerDialog(user: UserEntity, context: Context) {
val builder = AlertDialog.Builder(context)
builder.setTitle("Select Role")
val items = arrayOf(COLLABORATOR, POWER_COLLABORATOR, ADMIN)
builder.setSingleChoiceItems(items, -1) { _: DialogInterface?, item: Int ->
var role = COLLABORATOR_ROLE
when (item) {
0 -> role = COLLABORATOR_ROLE
1 -> role = POWER_COLLABORATOR_ROLE
2 -> role = ADMIN_ROLE
}
pgApi.changeUserRole(user.userId, role)
getApp(context).database.userDao().update(user)
levelDialog.dismiss()
}
levelDialog = builder.create()
levelDialog.show()
}

override fun getItemCount(): Int {
Expand All @@ -29,4 +63,18 @@ class UserAdapter : RecyclerView.Adapter<ViewHolder>() {
notifyDataSetChanged()
}

companion object {
private const val COLLABORATOR = "Collaborator"
private const val COLLABORATOR_ROLE = "17dce2c5-4931-47e7-8c98-84b35f00ba03"
private const val POWER_COLLABORATOR = "Power Collaborator"
private const val POWER_COLLABORATOR_ROLE = "e7295fe5-5312-4559-b784-a74498464fb7"
private const val ADMIN = "Admin"
const val ADMIN_ROLE = "9d139e64-cac9-4f23-b4d5-9fd3688b498e"
private val pgApi = PGApi()
private val roleUidsToNames = mapOf(
COLLABORATOR_ROLE to COLLABORATOR,
POWER_COLLABORATOR_ROLE to POWER_COLLABORATOR,
ADMIN_ROLE to ADMIN
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ object UserMapper {
company = it.company,
title = it.title,
language = it.language,
roleUid = it.role?.uid ?: UserAdapter.ADMIN_ROLE
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ data class UserWire(

@JsonProperty(value = "language")
val language: String,

@JsonProperty(value = "role")
val role: Role?,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package buggy.plandroid.com.plandroidbugs.app

import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase


class Migration1To2 : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE users ADD role_uid TEXT")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class PlanDroidApp : Application() {
val database: PGDatabase by lazy {
Room.databaseBuilder(this,
PGDatabase::class.java, "database-name")
.allowMainThreadQueries()
.addMigrations(Migration1To2())
.build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import androidx.room.Database
import androidx.room.RoomDatabase

@Database(
version = 1,
version = 2,
entities = [
UserEntity::class
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ interface UserDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll(userEntityies: Collection<UserEntity>)

@Update
fun update(vararg userEntity: UserEntity)

@Delete
fun delete(userEntity: UserEntity)
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ data class UserEntity(

@ColumnInfo(name = "language")
val language: String,

@ColumnInfo(name = "role_uid")
val roleUid: String,
)

0 comments on commit 41aff42

Please sign in to comment.