2222import android .view .LayoutInflater ;
2323import android .view .View ;
2424import android .view .ViewGroup ;
25+ import android .widget .ImageView ;
2526import android .widget .LinearLayout ;
2627import android .widget .TextView ;
2728
3233
3334import java .util .ArrayList ;
3435
35- import nie .translator .rtranslator .Global ;
3636import nie .translator .rtranslator .R ;
3737
3838/** Is used to connect to the RecycleView, which functions as a ListView, a list of strings, which will be inserted in the ViewHolder layout and this will be inserted in the list**/
@@ -42,11 +42,13 @@ public class MessagesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
4242 //private static final int PREVIEW = 2;
4343 private ArrayList <GuiMessage > mResults = new ArrayList <>();
4444 private Callback callback ;
45+ private long playingMessageID = -1 ;
4546
4647 private static boolean showOriginalTranscriptionMsg ;
4748
48- public MessagesAdapter (ArrayList <GuiMessage > messages , Application application , @ NonNull Callback callback ) {
49+ public MessagesAdapter (ArrayList <GuiMessage > messages , Application application , long playingMessageID , @ NonNull Callback callback ) {
4950 this .callback = callback ;
51+ this .playingMessageID = playingMessageID ;
5052 if (messages != null ) {
5153 if (messages .size () > 0 ) {
5254 callback .onFirstItemAdded ();
@@ -74,12 +76,17 @@ public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int positi
7476 if (holder instanceof MessageHolder ) {
7577 final GuiMessage message = mResults .get (position );
7678 if (holder instanceof ReceivedHolder && message .getMessage ().getSender () != null ) {
77- ((ReceivedHolder ) holder ).text .setVisibility (View .GONE );
79+ ((ReceivedHolder ) holder ).containerNoSender .setVisibility (View .GONE );
7880 ((ReceivedHolder ) holder ).containerSender .setVisibility (View .VISIBLE );
7981 ((ReceivedHolder ) holder ).sender .setText (message .getMessage ().getSender ().getName ());
8082 Log .d ("recyclerview" , "RecyclerView bind sender" );
8183 }
8284 ((MessageHolder ) holder ).setText (message .getMessage ().getTextToTranslate (), message .getMessage ().getText ());
85+ if (message .getMessageID () == playingMessageID ){
86+ ((MessageHolder ) holder ).setIsPlayingTTS (true );
87+ }else {
88+ ((MessageHolder ) holder ).setIsPlayingTTS (false );
89+ }
8390 Log .d ("recyclerview" , "RecyclerView bind text" );
8491 //holder.itemView.requestLayout();
8592
@@ -88,7 +95,9 @@ public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int positi
8895 @ Override
8996 public void onClick (View v ) {
9097 if (callback != null && v instanceof android .widget .ImageView ) {
91- callback .onPlayAudio (message , (android .widget .ImageView ) v );
98+ boolean play = false ;
99+ if (playingMessageID != message .getMessageID ()) play = true ;
100+ callback .onTTSButtonClick (message , play );
92101 }
93102 }
94103 };
@@ -178,25 +187,51 @@ public ArrayList<GuiMessage> getMessages() {
178187 return mResults ;
179188 }
180189
190+ public long getPlayingMessageID () {
191+ return playingMessageID ;
192+ }
193+
194+ public void setPlayingMessageID (long playingMessageID ) {
195+ long oldPlayingMessageId = this .playingMessageID ;
196+ this .playingMessageID = playingMessageID ;
197+ if (oldPlayingMessageId != playingMessageID ){
198+ if (oldPlayingMessageId != -1 ) {
199+ int index = getMessageIndex (oldPlayingMessageId );
200+ notifyItemChanged (index );
201+ }
202+ if (playingMessageID != -1 ){
203+ int index = getMessageIndex (playingMessageID );
204+ notifyItemChanged (index );
205+ //todo: I can insert a scroll to the playing message here
206+ }
207+ }
208+ }
209+
181210 /** The layout for each item in the RecicleView list*/
182211 private static class ReceivedHolder extends RecyclerView .ViewHolder implements MessageHolder {
183212 TextView originalTextToBeTranslated ;
184213 TextView text ;
214+ LinearLayout containerNoSender ;
185215 LinearLayout containerSender ;
186216 TextView textSender ;
187217 TextView sender ;
218+ ImageView ttsButton ;
219+ ImageView ttsButton2 ;
188220
189221 ReceivedHolder (LayoutInflater inflater , ViewGroup parent ) {
190222 super (inflater .inflate (R .layout .component_message_received , parent , false ));
191223 originalTextToBeTranslated = itemView .findViewById (R .id .original_text_to_be_translated );
192224 if (!showOriginalTranscriptionMsg ) {
193225 originalTextToBeTranslated .setVisibility (View .GONE );
194226 }
227+ containerNoSender = itemView .findViewById (R .id .no_sender_container );
195228 text = itemView .findViewById (R .id .text_content );
229+ ttsButton = itemView .findViewById (R .id .tts_button_content );
196230
197231 containerSender = itemView .findViewById (R .id .sender_container );
198232 textSender = itemView .findViewById (R .id .text_content2 );
199233 sender = itemView .findViewById (R .id .text_sender );
234+ ttsButton2 = itemView .findViewById (R .id .tts_button_content2 );
200235 }
201236
202237 @ Override
@@ -205,13 +240,34 @@ public void setText(String originalTextToBeTranslated, String text) {
205240 this .originalTextToBeTranslated .setText (originalTextToBeTranslated );
206241 this .text .setText (text );
207242 }
243+
244+ @ Override
245+ public boolean isPlayingTTS () {
246+ return ((int ) ttsButton .getTag () == R .drawable .stop_icon );
247+ }
248+
249+ @ Override
250+ public void setIsPlayingTTS (boolean playingTTS ) {
251+ if (playingTTS ) {
252+ ttsButton .setImageResource (R .drawable .stop_icon );
253+ ttsButton .setTag (R .drawable .stop_icon );
254+ ttsButton2 .setImageResource (R .drawable .stop_icon );
255+ ttsButton2 .setTag (R .drawable .stop_icon );
256+ }else {
257+ ttsButton .setImageResource (R .drawable .sound_icon );
258+ ttsButton .setTag (R .drawable .sound_icon );
259+ ttsButton2 .setImageResource (R .drawable .stop_icon );
260+ ttsButton2 .setTag (R .drawable .stop_icon );
261+ }
262+ }
208263 }
209264
210265 /** The layout for each item in the RecicleView list*/
211266 private static class SendHolder extends RecyclerView .ViewHolder implements MessageHolder {
212267 TextView originalTextToBeTranslated ;
213268 TextView text ;
214269 CardView card ;
270+ ImageView ttsButton ;
215271
216272 SendHolder (LayoutInflater inflater , ViewGroup parent ) {
217273 super (inflater .inflate (R .layout .component_message_send , parent , false ));
@@ -221,21 +277,40 @@ private static class SendHolder extends RecyclerView.ViewHolder implements Messa
221277 }
222278 text = itemView .findViewById (R .id .text );
223279 card = itemView .findViewById (R .id .card );
280+ ttsButton = itemView .findViewById (R .id .tts_button );
224281 }
225282
226283 @ Override
227284 public void setText (String originalTextToBeTranslated , String text ) {
228285 this .originalTextToBeTranslated .setText (originalTextToBeTranslated );
229286 this .text .setText (text );
230287 }
288+
289+ @ Override
290+ public boolean isPlayingTTS () {
291+ return ((int ) ttsButton .getTag () == R .drawable .stop_icon );
292+ }
293+
294+ @ Override
295+ public void setIsPlayingTTS (boolean playingTTS ) {
296+ if (playingTTS ) {
297+ ttsButton .setImageResource (R .drawable .stop_icon );
298+ ttsButton .setTag (R .drawable .stop_icon );
299+ }else {
300+ ttsButton .setImageResource (R .drawable .sound_icon );
301+ ttsButton .setTag (R .drawable .sound_icon );
302+ }
303+ }
231304 }
232305
233306 interface MessageHolder {
234307 void setText (String originalTextToBeTranslated , String text );
308+ boolean isPlayingTTS ();
309+ void setIsPlayingTTS (boolean playingTTS );
235310 }
236311
237312 public interface Callback {
238313 void onFirstItemAdded ();
239- void onPlayAudio (GuiMessage message , android . widget . ImageView icon );
314+ void onTTSButtonClick (GuiMessage message , boolean play );
240315 }
241316}
0 commit comments