Search in sources :

Example 1 with DocumentSnapshot

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();
                }
            }
        });
    }
}
Also used : DocumentSnapshot(com.google.firebase.firestore.DocumentSnapshot) Intent(android.content.Intent) FirebaseUser(com.google.firebase.auth.FirebaseUser)

Example 2 with DocumentSnapshot

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));
}
Also used : DocumentSnapshot(com.google.firebase.firestore.DocumentSnapshot)

Example 3 with DocumentSnapshot

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);
}
Also used : DocumentSnapshot(com.google.firebase.firestore.DocumentSnapshot) NonNull(androidx.annotation.NonNull)

Example 4 with DocumentSnapshot

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);
        }
    });
}
Also used : Task(com.google.android.gms.tasks.Task) HashMap(java.util.HashMap) FirebaseFirestoreException(com.google.firebase.firestore.FirebaseFirestoreException) Intent(android.content.Intent) ImageView(android.widget.ImageView) CircleImageView(de.hdodenhof.circleimageview.CircleImageView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) Date(java.util.Date) FirebaseFirestoreException(com.google.firebase.firestore.FirebaseFirestoreException) QuerySnapshot(com.google.firebase.firestore.QuerySnapshot) DocumentSnapshot(com.google.firebase.firestore.DocumentSnapshot) OnCompleteListener(com.google.android.gms.tasks.OnCompleteListener) NonNull(android.support.annotation.NonNull)

Example 5 with DocumentSnapshot

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();
            }
        }
    });
}
Also used : Task(com.google.android.gms.tasks.Task) UploadTask(com.google.firebase.storage.UploadTask) RequestOptions(com.bumptech.glide.request.RequestOptions) Compressor(id.zelory.compressor.Compressor) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CircleImageView(de.hdodenhof.circleimageview.CircleImageView) CropImageView(com.theartofdev.edmodo.cropper.CropImageView) View(android.view.View) DocumentSnapshot(com.google.firebase.firestore.DocumentSnapshot) UploadTask(com.google.firebase.storage.UploadTask) OnCompleteListener(com.google.android.gms.tasks.OnCompleteListener) NonNull(android.support.annotation.NonNull) File(java.io.File) Toolbar(android.support.v7.widget.Toolbar)

Aggregations

DocumentSnapshot (com.google.firebase.firestore.DocumentSnapshot)5 Intent (android.content.Intent)2 NonNull (android.support.annotation.NonNull)2 View (android.view.View)2 OnCompleteListener (com.google.android.gms.tasks.OnCompleteListener)2 Task (com.google.android.gms.tasks.Task)2 CircleImageView (de.hdodenhof.circleimageview.CircleImageView)2 RecyclerView (android.support.v7.widget.RecyclerView)1 Toolbar (android.support.v7.widget.Toolbar)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 NonNull (androidx.annotation.NonNull)1 RequestOptions (com.bumptech.glide.request.RequestOptions)1 FirebaseUser (com.google.firebase.auth.FirebaseUser)1 FirebaseFirestoreException (com.google.firebase.firestore.FirebaseFirestoreException)1 QuerySnapshot (com.google.firebase.firestore.QuerySnapshot)1 UploadTask (com.google.firebase.storage.UploadTask)1 CropImageView (com.theartofdev.edmodo.cropper.CropImageView)1 Compressor (id.zelory.compressor.Compressor)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1