use of com.google.firebase.firestore.DocumentSnapshot in project PhotoBlog-Android-Blog-App by akshayejh.
the class MainActivity method onStart.
@Override
protected void onStart() {
super.onStart();
FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
if (currentUser == null) {
sendToLogin();
} else {
current_user_id = mAuth.getCurrentUser().getUid();
firebaseFirestore.collection("Users").document(current_user_id).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
if (!task.getResult().exists()) {
Intent setupIntent = new Intent(MainActivity.this, SetupActivity.class);
startActivity(setupIntent);
finish();
}
} else {
String errorMessage = task.getException().getMessage();
Toast.makeText(MainActivity.this, "Error : " + errorMessage, Toast.LENGTH_LONG).show();
}
}
});
}
}
use of com.google.firebase.firestore.DocumentSnapshot in project FirebaseUI-Android by firebase.
the class FirestorePagingAdapter method onBindViewHolder.
@Override
public void onBindViewHolder(@NonNull VH holder, int position) {
DocumentSnapshot snapshot = getItem(position);
onBindViewHolder(holder, position, mParser.parseSnapshot(snapshot));
}
use of com.google.firebase.firestore.DocumentSnapshot in project FirebaseUI-Android by firebase.
the class FirestorePagingSource method getNextPageKey.
@NonNull
private PageKey getNextPageKey(@NonNull QuerySnapshot snapshot) {
List<DocumentSnapshot> data = snapshot.getDocuments();
DocumentSnapshot last = getLast(data);
return new PageKey(last, null);
}
use of com.google.firebase.firestore.DocumentSnapshot in project PhotoBlog-Android-Blog-App by akshayejh.
the class BlogRecyclerAdapter method onBindViewHolder.
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.setIsRecyclable(false);
final String blogPostId = blog_list.get(position).BlogPostId;
final String currentUserId = firebaseAuth.getCurrentUser().getUid();
String desc_data = blog_list.get(position).getDesc();
holder.setDescText(desc_data);
String image_url = blog_list.get(position).getImage_url();
String thumbUri = blog_list.get(position).getImage_thumb();
holder.setBlogImage(image_url, thumbUri);
String user_id = blog_list.get(position).getUser_id();
// User Data will be retrieved here...
firebaseFirestore.collection("Users").document(user_id).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
String userName = task.getResult().getString("name");
String userImage = task.getResult().getString("image");
holder.setUserData(userName, userImage);
} else {
// Firebase Exception
}
}
});
try {
long millisecond = blog_list.get(position).getTimestamp().getTime();
String dateString = DateFormat.format("MM/dd/yyyy", new Date(millisecond)).toString();
holder.setTime(dateString);
} catch (Exception e) {
Toast.makeText(context, "Exception : " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
// Get Likes Count
firebaseFirestore.collection("Posts/" + blogPostId + "/Likes").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
if (!documentSnapshots.isEmpty()) {
int count = documentSnapshots.size();
holder.updateLikesCount(count);
} else {
holder.updateLikesCount(0);
}
}
});
// Get Likes
firebaseFirestore.collection("Posts/" + blogPostId + "/Likes").document(currentUserId).addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(DocumentSnapshot documentSnapshot, FirebaseFirestoreException e) {
if (documentSnapshot.exists()) {
holder.blogLikeBtn.setImageDrawable(context.getDrawable(R.mipmap.action_like_accent));
} else {
holder.blogLikeBtn.setImageDrawable(context.getDrawable(R.mipmap.action_like_gray));
}
}
});
// Likes Feature
holder.blogLikeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
firebaseFirestore.collection("Posts/" + blogPostId + "/Likes").document(currentUserId).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (!task.getResult().exists()) {
Map<String, Object> likesMap = new HashMap<>();
likesMap.put("timestamp", FieldValue.serverTimestamp());
firebaseFirestore.collection("Posts/" + blogPostId + "/Likes").document(currentUserId).set(likesMap);
} else {
firebaseFirestore.collection("Posts/" + blogPostId + "/Likes").document(currentUserId).delete();
}
}
});
}
});
holder.blogCommentBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent commentIntent = new Intent(context, CommentsActivity.class);
commentIntent.putExtra("blog_post_id", blogPostId);
context.startActivity(commentIntent);
}
});
}
use of com.google.firebase.firestore.DocumentSnapshot in project PhotoBlog-Android-Blog-App by akshayejh.
the class SetupActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setup);
Toolbar setupToolbar = findViewById(R.id.setupToolbar);
setSupportActionBar(setupToolbar);
getSupportActionBar().setTitle("Account Setup");
firebaseAuth = FirebaseAuth.getInstance();
user_id = firebaseAuth.getCurrentUser().getUid();
firebaseFirestore = FirebaseFirestore.getInstance();
storageReference = FirebaseStorage.getInstance().getReference();
setupImage = findViewById(R.id.setup_image);
setupName = findViewById(R.id.setup_name);
setupBtn = findViewById(R.id.setup_btn);
setupProgress = findViewById(R.id.setup_progress);
setupProgress.setVisibility(View.VISIBLE);
setupBtn.setEnabled(false);
firebaseFirestore.collection("Users").document(user_id).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
if (task.getResult().exists()) {
String name = task.getResult().getString("name");
String image = task.getResult().getString("image");
mainImageURI = Uri.parse(image);
setupName.setText(name);
RequestOptions placeholderRequest = new RequestOptions();
placeholderRequest.placeholder(R.drawable.default_image);
Glide.with(SetupActivity.this).setDefaultRequestOptions(placeholderRequest).load(image).into(setupImage);
}
} else {
String error = task.getException().getMessage();
Toast.makeText(SetupActivity.this, "(FIRESTORE Retrieve Error) : " + error, Toast.LENGTH_LONG).show();
}
setupProgress.setVisibility(View.INVISIBLE);
setupBtn.setEnabled(true);
}
});
setupBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String user_name = setupName.getText().toString();
if (!TextUtils.isEmpty(user_name) && mainImageURI != null) {
setupProgress.setVisibility(View.VISIBLE);
if (isChanged) {
user_id = firebaseAuth.getCurrentUser().getUid();
File newImageFile = new File(mainImageURI.getPath());
try {
compressedImageFile = new Compressor(SetupActivity.this).setMaxHeight(125).setMaxWidth(125).setQuality(50).compressToBitmap(newImageFile);
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
compressedImageFile.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] thumbData = baos.toByteArray();
UploadTask image_path = storageReference.child("profile_images").child(user_id + ".jpg").putBytes(thumbData);
image_path.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
storeFirestore(task, user_name);
} else {
String error = task.getException().getMessage();
Toast.makeText(SetupActivity.this, "(IMAGE Error) : " + error, Toast.LENGTH_LONG).show();
setupProgress.setVisibility(View.INVISIBLE);
}
}
});
} else {
storeFirestore(null, user_name);
}
}
}
});
setupImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(SetupActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(SetupActivity.this, "Permission Denied", Toast.LENGTH_LONG).show();
ActivityCompat.requestPermissions(SetupActivity.this, new String[] { Manifest.permission.READ_EXTERNAL_STORAGE }, 1);
} else {
BringImagePicker();
}
} else {
BringImagePicker();
}
}
});
}
Aggregations