roles
This commit is contained in:
parent
8c8cbfd7dd
commit
41d48bf60a
28 changed files with 434 additions and 7 deletions
58
presentation/role.go
Normal file
58
presentation/role.go
Normal file
|
@ -0,0 +1,58 @@
|
|||
package presentation
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.sapphic.engineer/roleypoly/v4/types"
|
||||
"git.sapphic.engineer/roleypoly/v4/utils"
|
||||
)
|
||||
|
||||
type InputType string
|
||||
|
||||
const (
|
||||
InputCheckbox InputType = "checkbox"
|
||||
InputRadio InputType = "radio"
|
||||
)
|
||||
|
||||
type PresentableRole struct {
|
||||
ID string
|
||||
CategoryID string
|
||||
Name string
|
||||
Selected bool
|
||||
InputType InputType
|
||||
Colors PresentableRoleColors
|
||||
}
|
||||
|
||||
func Role(category *types.Category, role *types.Role, selected bool) PresentableRole {
|
||||
inputType := InputCheckbox
|
||||
if category.Type == types.CategorySingle {
|
||||
inputType = InputRadio
|
||||
}
|
||||
|
||||
colors := GetColors(role.Color)
|
||||
|
||||
return PresentableRole{
|
||||
ID: role.ID,
|
||||
CategoryID: category.ID,
|
||||
Name: role.Name,
|
||||
Selected: selected,
|
||||
InputType: inputType,
|
||||
Colors: colors,
|
||||
}
|
||||
}
|
||||
|
||||
type PresentableRoleColors struct {
|
||||
Main string
|
||||
IsDark bool
|
||||
}
|
||||
|
||||
func GetColors(roleColor uint32) PresentableRoleColors {
|
||||
// TODO: no color
|
||||
|
||||
r, g, b := utils.IntToRgb(roleColor)
|
||||
|
||||
return PresentableRoleColors{
|
||||
Main: fmt.Sprintf("#%x", roleColor),
|
||||
IsDark: utils.IsDarkColor(r, g, b),
|
||||
}
|
||||
}
|
33
presentation/role_test.go
Normal file
33
presentation/role_test.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
package presentation_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.sapphic.engineer/roleypoly/v4/presentation"
|
||||
"git.sapphic.engineer/roleypoly/v4/types/fixtures"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRole(t *testing.T) {
|
||||
r := presentation.Role(&fixtures.CategoryMulti, &fixtures.RoleWithDarkColor, true)
|
||||
assert.Equal(t, fixtures.RoleWithDarkColor.ID, r.ID)
|
||||
assert.Equal(t, fixtures.RoleWithDarkColor.Name, r.Name)
|
||||
assert.Equal(t, presentation.InputCheckbox, r.InputType)
|
||||
assert.Equal(t, "#a20000", r.Colors.Main)
|
||||
assert.True(t, r.Colors.IsDark)
|
||||
assert.True(t, r.Selected)
|
||||
|
||||
r = presentation.Role(&fixtures.CategorySingle, &fixtures.RoleWithDarkColor, false)
|
||||
assert.Equal(t, presentation.InputRadio, r.InputType)
|
||||
assert.False(t, r.Selected)
|
||||
|
||||
r = presentation.Role(&fixtures.CategorySingle, &fixtures.RoleWithLightColor, true)
|
||||
assert.False(t, r.Colors.IsDark)
|
||||
assert.True(t, r.Selected)
|
||||
}
|
||||
|
||||
func TestGetColors(t *testing.T) {
|
||||
c := presentation.GetColors(0xa20000)
|
||||
assert.Equal(t, "#a20000", c.Main)
|
||||
assert.True(t, c.IsDark)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue