1
1
package com .example .nobulijava .activity ;
2
2
3
- import static android .view .View .GONE ;
4
-
3
+ import androidx .annotation .NonNull ;
5
4
import androidx .appcompat .app .AppCompatActivity ;
6
5
import androidx .recyclerview .widget .RecyclerView ;
7
6
18
17
import com .example .nobulijava .interfaces .BotReply ;
19
18
import com .example .nobulijava .model .MessageObj ;
20
19
import com .example .nobulijava .utils .SendMessageInBg ;
20
+ import com .google .android .gms .tasks .OnCompleteListener ;
21
+ import com .google .android .gms .tasks .Task ;
21
22
import com .google .api .gax .core .FixedCredentialsProvider ;
22
23
import com .google .auth .oauth2 .GoogleCredentials ;
23
24
import com .google .auth .oauth2 .ServiceAccountCredentials ;
28
29
import com .google .cloud .dialogflow .v2 .SessionsSettings ;
29
30
import com .google .cloud .dialogflow .v2 .TextInput ;
30
31
import com .google .common .collect .Lists ;
32
+ import com .google .firebase .database .DataSnapshot ;
33
+ import com .google .firebase .database .DatabaseReference ;
34
+ import com .google .firebase .database .FirebaseDatabase ;
31
35
32
36
import java .io .InputStream ;
33
37
import java .util .ArrayList ;
@@ -43,7 +47,9 @@ public class UserChatBotActivity extends AppCompatActivity implements BotReply {
43
47
TextView textViewDisclaimer ;
44
48
45
49
MessageAdapter messageAdapter ;
46
- List <MessageObj > messageList = new ArrayList <>();
50
+ List <MessageObj > messageObjArrayList = new ArrayList <>();
51
+
52
+ private DatabaseReference mDatabase ;
47
53
48
54
//dialogFlow
49
55
private SessionsClient sessionsClient ;
@@ -58,27 +64,41 @@ protected void onCreate(Bundle savedInstanceState) {
58
64
59
65
getSupportActionBar ().setTitle ("Nobuli Chatbot" );
60
66
67
+ mDatabase = FirebaseDatabase .getInstance ().getReference ();
68
+
61
69
recyclerViewChat = findViewById (R .id .chatRecyclerView );
62
70
editTextMessageBox = findViewById (R .id .messageBoxEditText );
63
71
btnSend = findViewById (R .id .sendButton );
64
72
textViewDisclaimer = findViewById (R .id .textView_userChatBot_textDisclaimer );
65
73
66
- messageAdapter = new MessageAdapter (messageList , this );
74
+ messageAdapter = new MessageAdapter (messageObjArrayList , this );
67
75
recyclerViewChat .setAdapter (messageAdapter );
68
76
77
+ mDatabase .child ("Message" ).get ().addOnCompleteListener (new OnCompleteListener <DataSnapshot >() {
78
+ @ Override
79
+ public void onComplete (@ NonNull Task <DataSnapshot > task ) {
80
+ for (DataSnapshot msgSnapshot : task .getResult ().getChildren ()){
81
+ MessageObj msgObj = msgSnapshot .getValue (MessageObj .class );
82
+ messageObjArrayList .add (msgObj );
83
+ }
84
+ Objects .requireNonNull (recyclerViewChat .getAdapter ()).notifyDataSetChanged ();
85
+ }
86
+ });
87
+
69
88
btnSend .setOnClickListener (new View .OnClickListener () {
70
89
@ Override
71
90
public void onClick (View view ) {
72
- textViewDisclaimer .setVisibility (GONE );
73
- String message = editTextMessageBox .getText ().toString ();
91
+ textViewDisclaimer .setVisibility (View . GONE );
92
+ String message = editTextMessageBox .getText ().toString (). trim () ;
74
93
if (!message .isEmpty ()) {
75
-
76
- messageList .add (new MessageObj (message , false ));
94
+ MessageObj newMessage = new MessageObj (message , false );
95
+ messageObjArrayList .add (newMessage );
96
+ mDatabase .child ("Message" ).push ().setValue (newMessage );
77
97
editTextMessageBox .setText ("" );
78
98
sendMessageToBot (message );
79
99
Objects .requireNonNull (recyclerViewChat .getAdapter ()).notifyDataSetChanged ();
80
100
Objects .requireNonNull (recyclerViewChat .getLayoutManager ())
81
- .scrollToPosition (messageList .size () - 1 );
101
+ .scrollToPosition (messageObjArrayList .size () - 1 );
82
102
} else {
83
103
Toast .makeText (UserChatBotActivity .this , "Please enter text!" , Toast .LENGTH_SHORT ).show ();
84
104
}
@@ -118,13 +138,15 @@ public void callback(DetectIntentResponse returnResponse) {
118
138
if (returnResponse != null ) {
119
139
String botReply = returnResponse .getQueryResult ().getFulfillmentText ();
120
140
if (!botReply .isEmpty ()) {
121
- messageList .add (new MessageObj (botReply , true , returnResponse .getQueryResult ().getIntent ().getDisplayName ()));
141
+ MessageObj newBotMsg = new MessageObj (botReply , true , returnResponse .getQueryResult ().getIntent ().getDisplayName ());
142
+ mDatabase .child ("Message" ).push ().setValue (newBotMsg );
143
+ messageObjArrayList .add (newBotMsg );
122
144
messageAdapter .notifyDataSetChanged ();
123
145
if (returnResponse .getQueryResult ().getIntent ().getDisplayName ().equals ("Identify Cyberbully - report" )){
124
- messageList .add (new MessageObj ("Is there anything I can help you with?" , true , "null" ));
146
+ messageObjArrayList .add (new MessageObj ("Is there anything I can help you with?" , true , "null" ));
125
147
}
126
148
messageAdapter .notifyDataSetChanged ();
127
- Objects .requireNonNull (recyclerViewChat .getLayoutManager ()).scrollToPosition (messageList .size () - 1 );
149
+ Objects .requireNonNull (recyclerViewChat .getLayoutManager ()).scrollToPosition (messageObjArrayList .size () - 1 );
128
150
} else {
129
151
Toast .makeText (this , "Something went wrong" , Toast .LENGTH_SHORT ).show ();
130
152
}
0 commit comments