Skip to content

Commit

Permalink
Move Button to separate file, add properties and functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mileusna committed Jul 21, 2017
1 parent 101a700 commit 3b640d2
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 150 deletions.
178 changes: 178 additions & 0 deletions button.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package viber

// Button for carousel and keyboards
type Button struct {
Columns int `json:"Columns"`
Rows int `json:"Rows"`
ActionType ActionType `json:"ActionType"`
ActionBody string `json:"ActionBody"`
Image string `json:"Image,omitempty"`
Text string `json:"Text,omitempty"`
TextSize TextSize `json:"TextSize,omitempty"`
TextVAlign TextVAlign `json:"TextVAlign,omitempty"`
TextHAlign TextHAlign `json:"TextHAlign,omitempty"`
Silent bool `json:"Silent,omitempty"`
BgColor string `json:"BgColor,omitempty"`
BgMediaType string `json:"BgMediaType,omitempty"`
BgMedia string `json:"BgMedia,omitempty"`
BgLoop bool `json:"BgLoop,omitempty"`
TextOpacity int8 `json:"TextOpacity,omitempty"`
}

// NewButton helper function for creating button with text and image
func (v *Viber) NewButton(cols, rows int, typ ActionType, actionBody string, text, image string) *Button {
return &Button{
Columns: cols,
Rows: rows,
ActionType: typ,
ActionBody: actionBody,
Text: text,
Image: image,
}
}

// NewImageButton helper function for creating image button struct with common params
func (v *Viber) NewImageButton(cols, rows int, typ ActionType, actionBody string, image string) *Button {
return &Button{
Columns: cols,
Rows: rows,
ActionType: typ,
ActionBody: actionBody,
Image: image,
}
}

// NewTextButton helper function for creating image button struct with common params
func (v *Viber) NewTextButton(cols, rows int, t ActionType, actionBody, text string) *Button {
return &Button{
Columns: cols,
Rows: rows,
ActionType: t,
ActionBody: actionBody,
Text: text,
}
}

// TextSizeSmall for button text
func (b *Button) TextSizeSmall() *Button {
b.TextSize = Small
return b
}

// TextSizeMedium for button text
func (b *Button) TextSizeMedium() *Button {
b.TextSize = Medium
return b
}

// TextSizeLarge for button text
func (b *Button) TextSizeLarge() *Button {
b.TextSize = Large
return b
}

// TextSize for carousel buttons
// viber.Small
// viber.Medium
// viber.Large
type TextSize string

// TextSize values
const (
Small = TextSize("small")
Medium = TextSize("medium")
Large = TextSize("large")
)

// ActionType for carousel buttons
// viber.Reply
// viber.OpenURL
type ActionType string

// ActionType values
const (
Reply = ActionType("reply")
OpenURL = ActionType("open-url")
)

// TextVAlign for carousel buttons
// viber.Top
// viber.Middle
// viber.Bottom
type TextVAlign string

// TextVAlign values
const (
Top = TextVAlign("top")
Middle = TextVAlign("middle")
Bottom = TextVAlign("bottom")
)

// TextVAlignTop vertically align text to the top
func (b *Button) TextVAlignTop() *Button {
b.TextVAlign = Top
return b
}

// TextVAlignMiddle vertically align text to the middle
func (b *Button) TextVAlignMiddle() *Button {
b.TextVAlign = Middle
return b
}

// TextVAlignBottom vertically align text to the bottom
func (b *Button) TextVAlignBottom() *Button {
b.TextVAlign = Bottom
return b
}

// TextHAlign for carousel buttons
// viber.Left
// viber.Center
// viber.Middle
type TextHAlign string

// TextHAlign values
const (
Left = TextHAlign("left")
Center = TextHAlign("middle")
Right = TextHAlign("right")
)

// TextHAlignLeft horizontaly center text left
func (b *Button) TextHAlignLeft() *Button {
b.TextHAlign = Left
return b
}

// TextHAlignMiddle horizontaly center text
func (b *Button) TextHAlignMiddle() *Button {
b.TextHAlign = Center
return b
}

// TextHAlignRight horizontaly align text right
func (b *Button) TextHAlignRight() *Button {
b.TextHAlign = Right
return b
}

// SetSilent response from button
func (b *Button) SetSilent() *Button {
b.Silent = true
return b
}

// SetBgColor for button
func (b *Button) SetBgColor(hex string) *Button {
b.BgColor = hex
return b
}

// SetTextOpacity 0-100
func (b *Button) SetTextOpacity(o int8) *Button {
if o >= 0 && o <= 100 {
b.TextOpacity = o
}
return b
}
155 changes: 5 additions & 150 deletions carousel.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,6 @@ type RichMedia struct {
Buttons []Button `json:"Buttons"`
}

// Button for carousel
type Button struct {
Columns int `json:"Columns"`
Rows int `json:"Rows"`
ActionType ActionType `json:"ActionType"`
ActionBody string `json:"ActionBody"`
Image string `json:"Image,omitempty"`
Text string `json:"Text,omitempty"`
TextSize TextSize `json:"TextSize,omitempty"`
TextVAlign TextVAlign `json:"TextVAlign,omitempty"`
TextHAlign TextHAlign `json:"TextHAlign,omitempty"`
}

// AddButton to rich media message
func (rm *RichMediaMessage) AddButton(b *Button) {
rm.RichMedia.Buttons = append(rm.RichMedia.Buttons, *b)
Expand All @@ -53,148 +40,16 @@ func (v *Viber) NewRichMediaMessage(cols, rows int, bgColor string) *RichMediaMe
}
}

// NewButton helper function for creating button with text and image
func (v *Viber) NewButton(cols, rows int, typ ActionType, actionBody string, text, image string) *Button {
return &Button{
Columns: cols,
Rows: rows,
ActionType: typ,
ActionBody: actionBody,
Text: text,
Image: image,
}
}

// NewImageButton helper function for creating image button struct with common params
func (v *Viber) NewImageButton(cols, rows int, typ ActionType, actionBody string, image string) *Button {
return &Button{
Columns: cols,
Rows: rows,
ActionType: typ,
ActionBody: actionBody,
Image: image,
}
}

// NewTextButton helper function for creating image button struct with common params
func (v *Viber) NewTextButton(cols, rows int, t ActionType, actionBody, text string) *Button {
return &Button{
Columns: cols,
Rows: rows,
ActionType: t,
ActionBody: actionBody,
Text: text,
}
// SetKeyboard for text message
func (rm *RichMediaMessage) SetKeyboard(k *Keyboard) {
// TODO
// rm.Keyboard = k
}

// SetReceiver for text message
// SetReceiver for RichMedia message
func (rm *RichMediaMessage) SetReceiver(r string) {
rm.Receiver = r
}

// SetFrom to satisfy interface although RichMedia messages can't be sent to publich chat and don't have From
func (rm *RichMediaMessage) SetFrom(from string) {}

// TextSizeSmall for button text
func (b *Button) TextSizeSmall() *Button {
b.TextSize = Small
return b
}

// TextSizeMedium for button text
func (b *Button) TextSizeMedium() *Button {
b.TextSize = Medium
return b
}

// TextSizeLarge for button text
func (b *Button) TextSizeLarge() *Button {
b.TextSize = Large
return b
}

// TextSize for carousel buttons
// viber.Small
// viber.Medium
// viber.Large
type TextSize string

// TextSize values
const (
Small = TextSize("small")
Medium = TextSize("medium")
Large = TextSize("large")
)

// ActionType for carousel buttons
// viber.Reply
// viber.OpenURL
type ActionType string

// ActionType values
const (
Reply = ActionType("reply")
OpenURL = ActionType("open-url")
)

// TextVAlign for carousel buttons
// viber.Top
// viber.Middle
// viber.Bottom
type TextVAlign string

// TextVAlign values
const (
Top = TextVAlign("top")
Middle = TextVAlign("middle")
Bottom = TextVAlign("bottom")
)

// TextVAlignTop vertically align text to the top
func (b *Button) TextVAlignTop() *Button {
b.TextVAlign = Top
return b
}

// TextVAlignMiddle vertically align text to the middle
func (b *Button) TextVAlignMiddle() *Button {
b.TextVAlign = Middle
return b
}

// TextVAlignBottom vertically align text to the bottom
func (b *Button) TextVAlignBottom() *Button {
b.TextVAlign = Bottom
return b
}

// TextHAlign for carousel buttons
// viber.Left
// viber.Center
// viber.Middle
type TextHAlign string

// TextHAlign values
const (
Left = TextHAlign("left")
Center = TextHAlign("middle")
Right = TextHAlign("right")
)

// TextHAlignLeft horizontaly center text left
func (b *Button) TextHAlignLeft() *Button {
b.TextHAlign = Left
return b
}

// TextHAlignMiddle horizontaly center text
func (b *Button) TextHAlignMiddle() *Button {
b.TextHAlign = Center
return b
}

// TextHAlignRight horizontaly align text right
func (b *Button) TextHAlignRight() *Button {
b.TextHAlign = Right
return b
}

0 comments on commit 3b640d2

Please sign in to comment.