diff --git a/app/src/main/java/buggy/plandroid/com/plandroidbugs/PGApi.kt b/app/src/main/java/buggy/plandroid/com/plandroidbugs/PGApi.kt index fe162dd..e52e115 100644 --- a/app/src/main/java/buggy/plandroid/com/plandroidbugs/PGApi.kt +++ b/app/src/main/java/buggy/plandroid/com/plandroidbugs/PGApi.kt @@ -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" diff --git a/app/src/main/java/buggy/plandroid/com/plandroidbugs/PGApiService.kt b/app/src/main/java/buggy/plandroid/com/plandroidbugs/PGApiService.kt index 5bd164b..998da7a 100644 --- a/app/src/main/java/buggy/plandroid/com/plandroidbugs/PGApiService.kt +++ b/app/src/main/java/buggy/plandroid/com/plandroidbugs/PGApiService.kt @@ -1,8 +1,14 @@ 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") @@ -10,4 +16,10 @@ interface PGApiService { @GET("projects/fc6facf8-c69c-4780-bac1-1774bd91cc8c/users/540e0bdcde37b40013533497") fun getUser(): Call + + @PATCH("projects/fc6facf8-c69c-4780-bac1-1774bd91cc8c/users/{userId}") + fun changeUserRole( + @Path("userId") userId: String, + @Body parameters: Map + ): Observable } \ No newline at end of file diff --git a/app/src/main/java/buggy/plandroid/com/plandroidbugs/Role.kt b/app/src/main/java/buggy/plandroid/com/plandroidbugs/Role.kt new file mode 100644 index 0000000..59635ff --- /dev/null +++ b/app/src/main/java/buggy/plandroid/com/plandroidbugs/Role.kt @@ -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 +) \ No newline at end of file diff --git a/app/src/main/java/buggy/plandroid/com/plandroidbugs/UserAdapter.kt b/app/src/main/java/buggy/plandroid/com/plandroidbugs/UserAdapter.kt index 5b7f99e..3d748d7 100644 --- a/app/src/main/java/buggy/plandroid/com/plandroidbugs/UserAdapter.kt +++ b/app/src/main/java/buggy/plandroid/com/plandroidbugs/UserAdapter.kt @@ -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() { + private lateinit var levelDialog: AlertDialog private var userEntities: List = ArrayList() override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { @@ -17,7 +22,36 @@ class UserAdapter : RecyclerView.Adapter() { } override fun onBindViewHolder(holder: ViewHolder, position: Int) { - holder.itemView.findViewById(R.id.email_text).text = userEntities[position].email + holder.itemView.findViewById(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 { @@ -29,4 +63,18 @@ class UserAdapter : RecyclerView.Adapter() { 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 + ) + } } \ No newline at end of file diff --git a/app/src/main/java/buggy/plandroid/com/plandroidbugs/UserMapper.kt b/app/src/main/java/buggy/plandroid/com/plandroidbugs/UserMapper.kt index 9768803..4db639a 100644 --- a/app/src/main/java/buggy/plandroid/com/plandroidbugs/UserMapper.kt +++ b/app/src/main/java/buggy/plandroid/com/plandroidbugs/UserMapper.kt @@ -13,6 +13,7 @@ object UserMapper { company = it.company, title = it.title, language = it.language, + roleUid = it.role?.uid ?: UserAdapter.ADMIN_ROLE ) } } diff --git a/app/src/main/java/buggy/plandroid/com/plandroidbugs/UserWire.kt b/app/src/main/java/buggy/plandroid/com/plandroidbugs/UserWire.kt index 8ca1025..af9ef42 100644 --- a/app/src/main/java/buggy/plandroid/com/plandroidbugs/UserWire.kt +++ b/app/src/main/java/buggy/plandroid/com/plandroidbugs/UserWire.kt @@ -25,4 +25,7 @@ data class UserWire( @JsonProperty(value = "language") val language: String, + + @JsonProperty(value = "role") + val role: Role?, ) \ No newline at end of file diff --git a/app/src/main/java/buggy/plandroid/com/plandroidbugs/app/Migration1To2.kt b/app/src/main/java/buggy/plandroid/com/plandroidbugs/app/Migration1To2.kt new file mode 100644 index 0000000..82a1284 --- /dev/null +++ b/app/src/main/java/buggy/plandroid/com/plandroidbugs/app/Migration1To2.kt @@ -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") + } + +} \ No newline at end of file diff --git a/app/src/main/java/buggy/plandroid/com/plandroidbugs/app/PlanDroidApp.kt b/app/src/main/java/buggy/plandroid/com/plandroidbugs/app/PlanDroidApp.kt index e20f335..80268bd 100644 --- a/app/src/main/java/buggy/plandroid/com/plandroidbugs/app/PlanDroidApp.kt +++ b/app/src/main/java/buggy/plandroid/com/plandroidbugs/app/PlanDroidApp.kt @@ -22,6 +22,8 @@ class PlanDroidApp : Application() { val database: PGDatabase by lazy { Room.databaseBuilder(this, PGDatabase::class.java, "database-name") + .allowMainThreadQueries() + .addMigrations(Migration1To2()) .build() } diff --git a/app/src/main/java/buggy/plandroid/com/plandroidbugs/room/PGDatabase.kt b/app/src/main/java/buggy/plandroid/com/plandroidbugs/room/PGDatabase.kt index 715c292..45fd60c 100644 --- a/app/src/main/java/buggy/plandroid/com/plandroidbugs/room/PGDatabase.kt +++ b/app/src/main/java/buggy/plandroid/com/plandroidbugs/room/PGDatabase.kt @@ -4,7 +4,7 @@ import androidx.room.Database import androidx.room.RoomDatabase @Database( - version = 1, + version = 2, entities = [ UserEntity::class ] diff --git a/app/src/main/java/buggy/plandroid/com/plandroidbugs/room/UserDao.kt b/app/src/main/java/buggy/plandroid/com/plandroidbugs/room/UserDao.kt index 70f1bcc..8fbda86 100644 --- a/app/src/main/java/buggy/plandroid/com/plandroidbugs/room/UserDao.kt +++ b/app/src/main/java/buggy/plandroid/com/plandroidbugs/room/UserDao.kt @@ -19,6 +19,9 @@ interface UserDao { @Insert(onConflict = OnConflictStrategy.REPLACE) fun insertAll(userEntityies: Collection) + @Update + fun update(vararg userEntity: UserEntity) + @Delete fun delete(userEntity: UserEntity) } \ No newline at end of file diff --git a/app/src/main/java/buggy/plandroid/com/plandroidbugs/room/UserEntity.kt b/app/src/main/java/buggy/plandroid/com/plandroidbugs/room/UserEntity.kt index a6dd0bd..18b9dcb 100644 --- a/app/src/main/java/buggy/plandroid/com/plandroidbugs/room/UserEntity.kt +++ b/app/src/main/java/buggy/plandroid/com/plandroidbugs/room/UserEntity.kt @@ -27,4 +27,7 @@ data class UserEntity( @ColumnInfo(name = "language") val language: String, + + @ColumnInfo(name = "role_uid") + val roleUid: String, )