1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
| package com.kirinno.maligaclassique;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import com.squareup.picasso.Picasso;
import de.hdodenhof.circleimageview.CircleImageView;
import java.util.ArrayList;
public class DiscutionFragment extends Fragment {
private static final String TAG = "DiscutionFragment";
ChatRoomRepository chatRoomRepository;
private RecyclerView chatRooms;
private View discutionFragmentView;
private RecyclerView myDiscutionList;
//private ListView listView;
private ArrayAdapter<String> adapter;
ArrayList<String> boutiques;
String idBoutique;
// private ChatRoomsAdapter adapter;
private static final String CURRENT_USER_KEY = "CURRENT_USER_KEY";
public static final String CURRENT_USER_PHONE= "CURRENT_USER_PHONE";
AuthenticationRepository authentication;
public static String phone = "";
private Object ChatBoutiquesAdapter;
public DiscutionFragment () {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
discutionFragmentView = inflater.inflate(R.layout.activity_discution, container, false);
myDiscutionList = (RecyclerView) discutionFragmentView.findViewById(R.id.discution_list);
myDiscutionList.setLayoutManager(new LinearLayoutManager(getContext()));
return discutionFragmentView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
/*
* If the device is having android oreo we will create a notification channel
* */
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager mNotificationManager =
(NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(ChannelInfo.CHANNEL_ID, ChannelInfo.CHANNEL_NAME, importance);
mChannel.setDescription(ChannelInfo.CHANNEL_DESCRIPTION);
mChannel.enableLights(true);
mChannel.setLightColor(Color.BLUE);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mNotificationManager.createNotificationChannel(mChannel);
}
Bundle extras = getActivity().getIntent().getExtras();
if (extras != null) {
phone = extras.getString(CURRENT_USER_PHONE, "");
}
Toast.makeText(getActivity(), phone, Toast.LENGTH_SHORT).show();
chatRoomRepository = new ChatRoomRepository(FirebaseFirestore.getInstance());
authentication = new AuthenticationRepository(FirebaseFirestore.getInstance());
// listView = view.findViewById(R.id.list_item);
//chatRooms = view.findViewById(R.id.list_of_pseudo);
//initUI();
//getChatRooms();
//getChatBoutiques();
authenticate();
/*listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String text = (String) listView.getItemAtPosition(arg2);
getBoutiqueId(text);
Intent intent = new Intent(getActivity(), ChatRoomActivity.class);
intent.putExtra(ChatRoomActivity.CHAT_ROOM_ID, idBoutique);
intent.putExtra(ChatRoomActivity.CHAT_ROOM_NAME, text);
startActivity(intent);
//Toast.makeText(getActivity(), idBoutique, Toast.LENGTH_SHORT).show();
}
});*/
}
private void getChatBoutiques(){
chatRoomRepository.getBoutiques(phone, new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot snapshots, FirebaseFirestoreException e) {
if (e != null) {
Log.e("DiscutionFragment", "Listen failed.", e);
return;
}
boutiques = new ArrayList<>();
for (QueryDocumentSnapshot doc : snapshots) {
boutiques.add(doc.getString("nom_boutique"));
}
/* adapter = new ChatBoutiquesAdapter(getActivity(), R.layout.item_boutique_listview, boutiques);
listView.setAdapter(adapter);*/
}
});
}
public void getBoutiqueId(String text){
chatRoomRepository.getBoutiquesId(text, new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot snapshots, FirebaseFirestoreException e) {
if (e != null) {
Log.e("DiscutionFragment", "Listen failed.", e);
return;
}
for (QueryDocumentSnapshot doc : snapshots) {
idBoutique = doc.getId();
}
}
});
}
/*
private void initUI() {
chatRooms.setLayoutManager(new LinearLayoutManager(getActivity()));
}
private void getChatRooms() {
chatRoomRepository.getRooms(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot snapshots, FirebaseFirestoreException e) {
if (e != null) {
Log.e("DiscutionFragment", "Listen failed.", e);
return;
}
List<ChatRoom> rooms = new ArrayList<>();
for (QueryDocumentSnapshot doc : snapshots) {
rooms.add(new ChatRoom(doc.getId(), doc.getString("name")));
}
adapter = new ChatRoomsAdapter(rooms, listener);
chatRooms.setAdapter(adapter);
}
});
}
ChatRoomsAdapter.OnChatRoomClickListener listener = new ChatRoomsAdapter.OnChatRoomClickListener() {
@Override
public void onClick(ChatRoom chatRoom) {
Intent intent = new Intent(getActivity(), ChatRoomActivity.class);
intent.putExtra(ChatRoomActivity.CHAT_ROOM_ID, chatRoom.getId());
intent.putExtra(ChatRoomActivity.CHAT_ROOM_NAME, chatRoom.getName());
startActivity(intent);
}
};
*/
private void authenticate() {
String currentUserKey = getCurrentUserKey();
if (currentUserKey.isEmpty())
{
authentication.createNewUser(
new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
saveCurrentUserKey(documentReference.getId());
Toast.makeText(getActivity(), "New user created", Toast.LENGTH_SHORT).show();
}
},
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(
getActivity(),
"Error creating user. Check your internet connection",
Toast.LENGTH_SHORT)
.show();
}
}
);
} else {
authentication.login(
currentUserKey,
new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Toast.makeText(getActivity(), "Logged in", Toast.LENGTH_SHORT).show();
}
},
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(
getActivity(),
"Error signing in. Check your internet connection",
Toast.LENGTH_SHORT)
.show();
}
}
);
}
}
private String getCurrentUserKey() {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
return preferences.getString(CURRENT_USER_KEY, "");
}
private void saveCurrentUserKey(String key) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putString(CURRENT_USER_KEY, key);
editor.apply();
}
} |
Partager