Skip to content

Commit

Permalink
created sample activity for roles.
Browse files Browse the repository at this point in the history
  • Loading branch information
luca committed Aug 11, 2019
1 parent 3f44708 commit f351da3
Show file tree
Hide file tree
Showing 15 changed files with 332 additions and 1 deletion.
13 changes: 13 additions & 0 deletions lib/src/main/java/com/kirkbushman/zammad/ZammadApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ interface ZammadApi {
@HeaderMap header: HashMap<String, String>
): Call<Group>

@GET("/api/v1/roles")
fun roles(
@Query("expand") expanded: Boolean,
@HeaderMap header: HashMap<String, String>
): Call<List<Role>>

@GET("/api/v1/roles/{id}")
fun role(
@Path("id") id: Int,
@Query("expand") expanded: Boolean,
@HeaderMap header: HashMap<String, String>
): Call<Role>

@DELETE("/api/v1/groups/{id}")
fun deleteGroup(
@Path("id") id: Int,
Expand Down
36 changes: 36 additions & 0 deletions lib/src/main/java/com/kirkbushman/zammad/ZammadClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,42 @@ class ZammadClient(
return res.body()
}

fun roles(expanded: Boolean = false): List<Role>? {

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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ data class Organization(
val members: List<String>?

) : Parcelable, Identifiable, Creatable, Updatable {

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
Expand Down
15 changes: 14 additions & 1 deletion lib/src/main/java/com/kirkbushman/zammad/models/Role.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,17 @@ data class Role(
@Json(name = "permission_ids")
val permissionIds: List<Int>

) : 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
}
}
2 changes: 2 additions & 0 deletions sampleapp/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
<activity android:name=".activities.UsersActivity"></activity>
<activity android:name=".activities.GroupActivity"></activity>
<activity android:name=".activities.GroupsActivity"></activity>
<activity android:name=".activities.RoleActivity"></activity>
<activity android:name=".activities.RolesActivity"></activity>
<activity android:name=".activities.OverviewActivity"></activity>
<activity android:name=".activities.OverviewsActivity"></activity>
<activity android:name=".activities.OrganizationActivity"></activity>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
})
}
}
Original file line number Diff line number Diff line change
@@ -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<Role>()
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)
})
}
}
Original file line number Diff line number Diff line change
@@ -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<Role>()

fun setRoles(roles: Collection<Role>) {
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) }
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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<RoleHolder>() {

@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<LinearLayout>(R.id.container)
val roleName by bind<TextView>(R.id.role_name)
val roleActive by bind<TextView>(R.id.role_active)
val roleCreated by bind<TextView>(R.id.role_created)
}
7 changes: 7 additions & 0 deletions sampleapp/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@
android:layout_margin="8dp"
android:text="@string/bttn_groups" />

<Button
android:id="@+id/bttn_roles"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="@string/bttn_roles" />

<Button
android:id="@+id/bttn_overviews"
android:layout_width="match_parent"
Expand Down
14 changes: 14 additions & 0 deletions sampleapp/src/main/res/layout/activity_role.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/role_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
/>

</LinearLayout>
33 changes: 33 additions & 0 deletions sampleapp/src/main/res/layout/activity_roles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways">

</androidx.appcompat.widget.Toolbar>

</com.google.android.material.appbar.AppBarLayout>

<com.airbnb.epoxy.EpoxyRecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="true"
android:scrollbars="vertical"
android:fadeScrollbars="true"
app:itemSpacing="16dp"
app:layoutManager="@string/linear_layout"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
35 changes: 35 additions & 0 deletions sampleapp/src/main/res/layout/item_role.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">

<LinearLayout
android:id="@+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/role_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:layout_margin="8dp" />

<TextView
android:id="@+id/role_active"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />

<TextView
android:id="@+id/role_created"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />

</LinearLayout>
</com.google.android.material.card.MaterialCardView>
1 change: 1 addition & 0 deletions sampleapp/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<string name="bttn_tickets_search">Tickets Search</string>
<string name="bttn_users">Users</string>
<string name="bttn_groups">Groups</string>
<string name="bttn_roles">Roles</string>
<string name="bttn_overviews">Overviews</string>
<string name="bttn_organizations">Organizations</string>
<string name="bttn_priorities">Priorities</string>
Expand Down

0 comments on commit f351da3

Please sign in to comment.