diff --git a/lib/src/main/java/com/kirkbushman/zammad/ZammadApi.kt b/lib/src/main/java/com/kirkbushman/zammad/ZammadApi.kt index 4fdf716..d516fe6 100644 --- a/lib/src/main/java/com/kirkbushman/zammad/ZammadApi.kt +++ b/lib/src/main/java/com/kirkbushman/zammad/ZammadApi.kt @@ -85,6 +85,19 @@ interface ZammadApi { @HeaderMap header: HashMap ): Call + @GET("/api/v1/roles") + fun roles( + @Query("expand") expanded: Boolean, + @HeaderMap header: HashMap + ): Call> + + @GET("/api/v1/roles/{id}") + fun role( + @Path("id") id: Int, + @Query("expand") expanded: Boolean, + @HeaderMap header: HashMap + ): Call + @DELETE("/api/v1/groups/{id}") fun deleteGroup( @Path("id") id: Int, diff --git a/lib/src/main/java/com/kirkbushman/zammad/ZammadClient.kt b/lib/src/main/java/com/kirkbushman/zammad/ZammadClient.kt index 70a1fa9..00774d8 100644 --- a/lib/src/main/java/com/kirkbushman/zammad/ZammadClient.kt +++ b/lib/src/main/java/com/kirkbushman/zammad/ZammadClient.kt @@ -287,6 +287,42 @@ class ZammadClient( return res.body() } + fun roles(expanded: Boolean = false): List? { + + val authMap = getHeaderMap() + val req = api.roles(expanded, authMap) + val res = req.execute() + + if (!res.isSuccessful) { + + if (logging) { + Log.i("Retrofit Error [roles]", res.errorBody().toString()) + } + + return null + } + + return res.body() + } + + fun role(id: Int, expanded: Boolean = false): Role? { + + val authMap = getHeaderMap() + val req = api.role(id, expanded, authMap) + val res = req.execute() + + if (!res.isSuccessful) { + + if (logging) { + Log.i("Retrofit Error [role]", res.errorBody().toString()) + } + + return null + } + + return res.body() + } + fun deleteGroup(id: Int): Boolean { val authMap = getHeaderMap() diff --git a/lib/src/main/java/com/kirkbushman/zammad/models/Organization.kt b/lib/src/main/java/com/kirkbushman/zammad/models/Organization.kt index fa1936d..eb9d1e0 100644 --- a/lib/src/main/java/com/kirkbushman/zammad/models/Organization.kt +++ b/lib/src/main/java/com/kirkbushman/zammad/models/Organization.kt @@ -52,6 +52,7 @@ data class Organization( val members: List? ) : Parcelable, Identifiable, Creatable, Updatable { + override fun equals(other: Any?): Boolean { if (this === other) return true if (javaClass != other?.javaClass) return false diff --git a/lib/src/main/java/com/kirkbushman/zammad/models/Role.kt b/lib/src/main/java/com/kirkbushman/zammad/models/Role.kt index 44746d9..d1482d1 100644 --- a/lib/src/main/java/com/kirkbushman/zammad/models/Role.kt +++ b/lib/src/main/java/com/kirkbushman/zammad/models/Role.kt @@ -45,4 +45,17 @@ data class Role( @Json(name = "permission_ids") val permissionIds: List -) : Parcelable, Identifiable, Creatable, Updatable +) : Parcelable, Identifiable, Creatable, Updatable { + + override fun hashCode(): Int = id + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as Role + + if (id != other.id) return false + + return true + } +} diff --git a/sampleapp/src/main/AndroidManifest.xml b/sampleapp/src/main/AndroidManifest.xml index 77e544e..b3f8875 100644 --- a/sampleapp/src/main/AndroidManifest.xml +++ b/sampleapp/src/main/AndroidManifest.xml @@ -35,6 +35,8 @@ + + diff --git a/sampleapp/src/main/java/com/kirkbushman/sampleapp/MainActivity.kt b/sampleapp/src/main/java/com/kirkbushman/sampleapp/MainActivity.kt index fae37cf..038f245 100644 --- a/sampleapp/src/main/java/com/kirkbushman/sampleapp/MainActivity.kt +++ b/sampleapp/src/main/java/com/kirkbushman/sampleapp/MainActivity.kt @@ -42,6 +42,12 @@ class MainActivity : AppCompatActivity() { startActivity(intent) } + bttn_roles.setOnClickListener { + + val intent = Intent(this, RolesActivity::class.java) + startActivity(intent) + } + bttn_overviews.setOnClickListener { val intent = Intent(this, OverviewsActivity::class.java) diff --git a/sampleapp/src/main/java/com/kirkbushman/sampleapp/activities/RoleActivity.kt b/sampleapp/src/main/java/com/kirkbushman/sampleapp/activities/RoleActivity.kt new file mode 100644 index 0000000..08f2cf0 --- /dev/null +++ b/sampleapp/src/main/java/com/kirkbushman/sampleapp/activities/RoleActivity.kt @@ -0,0 +1,44 @@ +package com.kirkbushman.sampleapp.activities + +import android.content.Context +import android.content.Intent +import android.os.Bundle +import androidx.appcompat.app.AppCompatActivity +import com.kirkbushman.sampleapp.R +import com.kirkbushman.sampleapp.SampleApplication +import com.kirkbushman.sampleapp.doAsync +import com.kirkbushman.zammad.models.Role +import kotlinx.android.synthetic.main.activity_role.* + +class RoleActivity : AppCompatActivity() { + + companion object { + + private const val PARAM_ROLE = "intent_param_role" + + fun start(context: Context, role: Role) { + + val intent = Intent(context, RoleActivity::class.java) + intent.putExtra(PARAM_ROLE, role) + + context.startActivity(intent) + } + } + + private val client by lazy { SampleApplication.instance.getClient() } + private val role by lazy { intent.getParcelableExtra(PARAM_ROLE) as Role } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_role) + + var newRole: Role? = null + doAsync(doWork = { + + newRole = client?.role(role.id, true) + }, onPost = { + + role_text.text = newRole.toString() + }) + } +} diff --git a/sampleapp/src/main/java/com/kirkbushman/sampleapp/activities/RolesActivity.kt b/sampleapp/src/main/java/com/kirkbushman/sampleapp/activities/RolesActivity.kt new file mode 100644 index 0000000..313fb1d --- /dev/null +++ b/sampleapp/src/main/java/com/kirkbushman/sampleapp/activities/RolesActivity.kt @@ -0,0 +1,50 @@ +package com.kirkbushman.sampleapp.activities + +import android.os.Bundle +import androidx.appcompat.app.AppCompatActivity +import com.kirkbushman.sampleapp.R +import com.kirkbushman.sampleapp.SampleApplication +import com.kirkbushman.sampleapp.controllers.OnClickCallback +import com.kirkbushman.sampleapp.controllers.RolesController +import com.kirkbushman.sampleapp.doAsync +import com.kirkbushman.zammad.models.Role +import kotlinx.android.synthetic.main.activity_groups.* + +class RolesActivity : AppCompatActivity() { + + private val client by lazy { SampleApplication.instance.getClient() } + + private val roles = ArrayList() + private val controller by lazy { + RolesController(object : OnClickCallback { + + override fun onClick(position: Int) { + + val role = roles[position] + RoleActivity.start(this@RolesActivity, role) + } + }) + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_roles) + + setSupportActionBar(toolbar) + supportActionBar?.let { + it.setDisplayHomeAsUpEnabled(true) + it.setDisplayShowHomeEnabled(true) + } + + list.setHasFixedSize(true) + list.setController(controller) + + doAsync(doWork = { + + roles.addAll(client?.roles() ?: listOf()) + }, onPost = { + + controller.setRoles(roles) + }) + } +} diff --git a/sampleapp/src/main/java/com/kirkbushman/sampleapp/controllers/RolesController.kt b/sampleapp/src/main/java/com/kirkbushman/sampleapp/controllers/RolesController.kt new file mode 100644 index 0000000..7fbc53e --- /dev/null +++ b/sampleapp/src/main/java/com/kirkbushman/sampleapp/controllers/RolesController.kt @@ -0,0 +1,35 @@ +package com.kirkbushman.sampleapp.controllers + +import com.airbnb.epoxy.EpoxyController +import com.kirkbushman.sampleapp.models.empty +import com.kirkbushman.sampleapp.models.role +import com.kirkbushman.zammad.models.Role + +class RolesController(private val callback: OnClickCallback) : EpoxyController() { + + private val roles = ArrayList() + + fun setRoles(roles: Collection) { + this.roles.clear() + this.roles.addAll(roles) + requestModelBuild() + } + + override fun buildModels() { + + if (roles.isEmpty()) { + empty { + id("empty_items") + } + } + + roles.forEach { + + role { + id(it.id) + role(it) + clickListener { _, _, _, position -> callback.onClick(position) } + } + } + } +} diff --git a/sampleapp/src/main/java/com/kirkbushman/sampleapp/models/RoleModel.kt b/sampleapp/src/main/java/com/kirkbushman/sampleapp/models/RoleModel.kt new file mode 100644 index 0000000..3756cb0 --- /dev/null +++ b/sampleapp/src/main/java/com/kirkbushman/sampleapp/models/RoleModel.kt @@ -0,0 +1,41 @@ +package com.kirkbushman.sampleapp.models + +import android.view.View +import android.widget.LinearLayout +import android.widget.TextView +import com.airbnb.epoxy.EpoxyAttribute +import com.airbnb.epoxy.EpoxyModelClass +import com.airbnb.epoxy.EpoxyModelWithHolder +import com.kirkbushman.sampleapp.R +import com.kirkbushman.zammad.models.Role + +@EpoxyModelClass(layout = R.layout.item_role) +abstract class RoleModel : EpoxyModelWithHolder() { + + @EpoxyAttribute + lateinit var role: Role + + @EpoxyAttribute(EpoxyAttribute.Option.DoNotHash) + lateinit var clickListener: View.OnClickListener + + override fun bind(holder: RoleHolder) { + + holder.roleName.text = role.name + holder.roleActive.text = role.active.toString() + holder.roleCreated.text = role.createdAt + + holder.container.setOnClickListener(clickListener) + } + + override fun unbind(holder: RoleHolder) { + holder.container.setOnClickListener(null) + } +} + +class RoleHolder : KotlinHolder() { + + val container by bind(R.id.container) + val roleName by bind(R.id.role_name) + val roleActive by bind(R.id.role_active) + val roleCreated by bind(R.id.role_created) +} diff --git a/sampleapp/src/main/res/layout/activity_main.xml b/sampleapp/src/main/res/layout/activity_main.xml index aa33b77..ef70524 100644 --- a/sampleapp/src/main/res/layout/activity_main.xml +++ b/sampleapp/src/main/res/layout/activity_main.xml @@ -69,6 +69,13 @@ android:layout_margin="8dp" android:text="@string/bttn_groups" /> +