Skip to content

Commit 4f012b6

Browse files
authored
automatic read flag once the message was shown in editor (#1) (#2)
1 parent 1a71559 commit 4f012b6

File tree

6 files changed

+82
-4
lines changed

6 files changed

+82
-4
lines changed

modules/web/src/de/diedavids/cuba/userinbox/web-screens.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
<screen-config xmlns="http://schemas.haulmont.com/cuba/screens.xsd">
33
<include file="de/diedavids/cuba/userinbox/screens.xml"/>
44

5-
<!--
65

6+
<!--
77
<screen id="mainWindow"
88
template="de/diedavids/cuba/userinbox/web/screens/mainwindow-with-messages.xml"/>
99
10+
-->
1011
<screen id="mainWindow"
1112
template="de/diedavids/cuba/userinbox/web/screens/side-mainwindow-with-messages.xml"/>
1213

13-
-->
1414
<screen id="user-inbox"
1515
template="de/diedavids/cuba/userinbox/web/message/user-inbox.xml"/>
1616
<screen id="send-message"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
package de.diedavids.cuba.userinbox.web.message
22

33
import com.haulmont.cuba.gui.components.AbstractEditor
4+
import com.haulmont.cuba.gui.data.Datasource
45
import de.diedavids.cuba.userinbox.entity.Message
56

7+
import javax.inject.Inject
8+
69
class MessageEdit extends AbstractEditor<Message> {
10+
11+
12+
@Inject
13+
Datasource<Message> messageDs
14+
15+
@Override
16+
protected void postInit() {
17+
item.read = true
18+
messageDs.commit()
19+
}
720
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package de.diedavids.cuba.userinbox.web.message
2+
3+
import com.haulmont.cuba.core.global.Messages
4+
import com.haulmont.cuba.gui.components.Action
5+
import com.haulmont.cuba.gui.data.Datasource
6+
import de.diedavids.cuba.userinbox.entity.Message
7+
8+
9+
class ToggleReadActionListener implements Datasource.ItemChangeListener<Message> {
10+
11+
Action toggleReadAction
12+
Messages messages
13+
14+
15+
@Override
16+
void itemChanged(Datasource.ItemChangeEvent<Message> e) {
17+
if (e.item) {
18+
updateToggleReadAction(e.item)
19+
}
20+
}
21+
22+
private updateToggleReadAction(Message message) {
23+
if(message.read) {
24+
toggleReadAction.caption = formatMessage('markAsUnread')
25+
toggleReadAction.icon = 'font-icon:SQUARE_O'
26+
}
27+
else {
28+
toggleReadAction.caption = formatMessage('markAsRead')
29+
toggleReadAction.icon = 'font-icon:CHECK_SQUARE_O'
30+
}
31+
}
32+
33+
private String formatMessage(String messageKey) {
34+
messages.formatMessage(getClass(), messageKey)
35+
}
36+
}

modules/web/src/de/diedavids/cuba/userinbox/web/message/UserInbox.groovy

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.haulmont.cuba.core.global.LoadContext
66
import com.haulmont.cuba.core.global.Metadata
77
import com.haulmont.cuba.gui.WindowManager
88
import com.haulmont.cuba.gui.components.AbstractLookup
9+
import com.haulmont.cuba.gui.components.Action
910
import com.haulmont.cuba.gui.components.Frame
1011
import com.haulmont.cuba.gui.components.Table
1112
import com.haulmont.cuba.gui.components.actions.CreateAction
@@ -25,6 +26,9 @@ class UserInbox extends AbstractLookup {
2526
@Named('messagesTable.edit')
2627
EditAction editAction
2728

29+
@Named('messagesTable.toggleRead')
30+
Action toggleReadAction
31+
2832
@Inject
2933
Table<Message> messagesTable
3034

@@ -48,9 +52,14 @@ class UserInbox extends AbstractLookup {
4852
showMessageSendNotification()
4953
}
5054
}
51-
}
5255

5356

57+
messagesDs.addItemChangeListener(new ToggleReadActionListener(
58+
messages: messages,
59+
toggleReadAction: toggleReadAction
60+
))
61+
}
62+
5463
private showMessageSendNotification() {
5564
showNotification(messages.formatMessage(getClass(), 'messageSend'), Frame.NotificationType.TRAY)
5665
}
@@ -68,7 +77,7 @@ class UserInbox extends AbstractLookup {
6877
LoadContext loadContext = LoadContext.create(entityClass)
6978
.setId(getRecordId(item))
7079

71-
dataManager.load(loadContext)
80+
dataManager.load(loadContext)
7281
}
7382

7483
protected UUID getRecordId(Message item) {
@@ -78,4 +87,14 @@ class UserInbox extends AbstractLookup {
7887
protected Class getClassFromMessageRecord(Message item) {
7988
metadata.getClass(item.entityReferenceClass)?.javaClass
8089
}
90+
91+
void toggleRead() {
92+
93+
94+
def message = messagesTable.singleSelected
95+
message.read = !message.read
96+
97+
messagesDs.commit()
98+
99+
}
81100
}

modules/web/src/de/diedavids/cuba/userinbox/web/message/messages.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ newMessage= Send Message
33
editorCaption = Message Details
44

55
messageSend = Message successful send
6+
markAsRead=Mark as read
7+
markAsUnread=Mark as unread

modules/web/src/de/diedavids/cuba/userinbox/web/message/user-inbox.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
caption="msg://newMessage"/>
3232
<action id="edit" caption="mainMsg://actions.View" icon="font-icon:SEARCH"/>
3333
<action id="remove"/>
34+
<action id="toggleRead"
35+
invoke="toggleRead"
36+
caption="msg://markAsRead"
37+
icon="font-icon:CHECK_SQUARE_O"
38+
trackSelection="true"
39+
/>
3440
</actions>
3541
<columns>
3642
<column id="sender"/>
@@ -49,6 +55,8 @@
4955
action="messagesTable.edit"/>
5056
<button id="removeBtn"
5157
action="messagesTable.remove"/>
58+
<button id="toggleReadBtn"
59+
action="messagesTable.toggleRead"/>
5260
</buttonsPanel>
5361
</groupTable>
5462
</layout>

0 commit comments

Comments
 (0)