|
|
@@ -1,11 +1,10 @@
|
|
|
package com.example.david.myapplication;
|
|
|
|
|
|
import android.os.Bundle;
|
|
|
+import android.os.Handler;
|
|
|
+import android.os.Message;
|
|
|
import android.support.annotation.Nullable;
|
|
|
import android.support.v4.app.Fragment;
|
|
|
-import android.text.Layout;
|
|
|
-import android.text.method.ScrollingMovementMethod;
|
|
|
-import android.util.Log;
|
|
|
import android.view.LayoutInflater;
|
|
|
import android.view.View;
|
|
|
import android.view.ViewGroup;
|
|
|
@@ -13,25 +12,29 @@ import android.widget.Button;
|
|
|
import android.widget.EditText;
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
-import com.neovisionaries.ws.client.WebSocket;
|
|
|
-import com.neovisionaries.ws.client.WebSocketAdapter;
|
|
|
-import com.neovisionaries.ws.client.WebSocketException;
|
|
|
-import com.neovisionaries.ws.client.WebSocketFactory;
|
|
|
-import com.neovisionaries.ws.client.WebSocketFrame;
|
|
|
-
|
|
|
-import org.json.JSONObject;
|
|
|
-
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-
|
|
|
-import javax.net.ssl.SSLContext;
|
|
|
|
|
|
/**
|
|
|
* Created by david on 16/04/16.
|
|
|
*/
|
|
|
public class chatFragment extends Fragment implements chatListener {
|
|
|
- private TextView chat;
|
|
|
+ private static TextView chat;
|
|
|
+ private static String lastMessage="";
|
|
|
+ private static Usuario user;
|
|
|
|
|
|
+ Handler h = new Handler(){
|
|
|
+ @Override
|
|
|
+ public void handleMessage(Message msg){
|
|
|
+ if(lastMessage=="")
|
|
|
+ return;
|
|
|
+ if(chat==null)
|
|
|
+ return;
|
|
|
+ chat.append(lastMessage);
|
|
|
+ chat.invalidate();
|
|
|
+ }
|
|
|
+ };
|
|
|
+ public void setUser(Usuario u){
|
|
|
+ this.user=u;
|
|
|
+ }
|
|
|
@Override
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
|
Bundle savedInstanceState) {
|
|
|
@@ -58,22 +61,15 @@ public class chatFragment extends Fragment implements chatListener {
|
|
|
EditText editText = (EditText) getView().findViewById( R.id.send_text );
|
|
|
String text=editText.getText().toString();
|
|
|
if(text.length() < 3) return;
|
|
|
- WebSocketHandler.sendMessage(text);
|
|
|
+ WebSocketHandler.sendMessage(text,user.nombre,user.apellido,user.pais);
|
|
|
editText.setText("");
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void msgReceived(String text) {
|
|
|
- Log.i("Websocket", "msg received: " + text);
|
|
|
- //chat.append(result);
|
|
|
- chat.setText(chat.getText() + "\ntext");
|
|
|
- /*final Layout layout = chat.getLayout();
|
|
|
- if(layout != null){
|
|
|
- int scrollDelta = layout.getLineBottom(chat.getLineCount() - 1)
|
|
|
- - chat.getScrollY() - chat.getHeight();
|
|
|
- if(scrollDelta > 0)
|
|
|
- chat.scrollBy(0, scrollDelta);
|
|
|
- }*/
|
|
|
- chat.invalidate();
|
|
|
+ public void msgReceived(final String text) {
|
|
|
+ if(lastMessage.equals(text))
|
|
|
+ return;
|
|
|
+ lastMessage=text;
|
|
|
+ h.sendEmptyMessage(0);
|
|
|
}
|
|
|
}
|