-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewPostActivity.java
More file actions
256 lines (219 loc) · 11 KB
/
Copy pathNewPostActivity.java
File metadata and controls
256 lines (219 loc) · 11 KB
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
package com.hossain.zakaria.firebasephotoapp.activities;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import com.hossain.zakaria.firebasephotoapp.R;
import com.hossain.zakaria.firebasephotoapp.utils.CircularProgressBar;
import com.hossain.zakaria.firebasephotoapp.utils.TranslateAnim;
import com.theartofdev.edmodo.cropper.CropImage;
import com.theartofdev.edmodo.cropper.CropImageView;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import id.zelory.compressor.Compressor;
public class NewPostActivity extends AppCompatActivity {
@BindView(R.id.new_post_toolbar)
Toolbar newPostToolbar;
@BindView(R.id.blog_post_image_view)
ImageView blogPostImageView;
@BindView(R.id.blog_new_post_description)
EditText blogNewPostDescription;
@BindView(R.id.publish_post_button)
Button publishPostButton;
private Uri postImageUri;
private CircularProgressBar progressBar;
private AlertDialog alertDialog;
private Bitmap compressedImageBitmap;
private String currentUserId;
private String randomName;
private StorageReference storageReference;
private FirebaseFirestore firebaseFirestore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_post);
ButterKnife.bind(this);
setSupportActionBar(newPostToolbar);
Objects.requireNonNull(getSupportActionBar()).setTitle("Add New Post");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setAnimatedTranslatedView();
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
storageReference = FirebaseStorage.getInstance().getReference();
firebaseFirestore = FirebaseFirestore.getInstance();
currentUserId = Objects.requireNonNull(firebaseAuth.getCurrentUser()).getUid();
progressBar = new CircularProgressBar(this);
}
private void setAnimatedTranslatedView() {
TranslateAnim.setAnimation(blogPostImageView, this, "top");
TranslateAnim.setAnimation(blogNewPostDescription, this, "left");
TranslateAnim.setAnimation(publishPostButton, this, "right");
}
@OnClick(R.id.blog_post_image_view)
public void onBlogPostImageViewClicked() {
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.setMinCropResultSize(512, 512)
.start(NewPostActivity.this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//after cropping activity we get the cropped image from here
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
if (result != null) {
postImageUri = result.getUri();
Glide.with(this)
.load(postImageUri)
.into(blogPostImageView);
}
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = Objects.requireNonNull(result).getError();
Toast.makeText(this, "Error: " + error.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
@OnClick(R.id.publish_post_button)
public void onPublishPostButtonClicked() {
final String postDescription = blogNewPostDescription.getText().toString();
if (!TextUtils.isEmpty(postDescription) && postImageUri != null) {
alertDialog = progressBar.setCircularProgressBar();
randomName = getRandomName();
final StorageReference postImagePath = storageReference.child("post_images").child(randomName + ".jpg");
postImagePath.putFile(postImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
postImagePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(final Uri imageUri) {
String downloadPostImageUri = imageUri.toString();
File newImageFile = new File(Objects.requireNonNull(postImageUri.getPath()));
try {
compressedImageBitmap = new Compressor(NewPostActivity.this)
.setMaxWidth(100)
.setMaxHeight(100)
.setQuality(2)
.compressToBitmap(newImageFile);
} catch (IOException e) {
Toast.makeText(NewPostActivity.this, "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
compressedImageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] thumbnailDataByte = baos.toByteArray();
storePostDataAndThumbnailToFireStore(thumbnailDataByte, downloadPostImageUri, postDescription);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
alertDialog.dismiss();
Toast.makeText(NewPostActivity.this, "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
alertDialog.dismiss();
Toast.makeText(NewPostActivity.this, "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
});
} else {
Toast.makeText(this, "Image and description fields can not be empty", Toast.LENGTH_SHORT).show();
}
}
private void storePostDataAndThumbnailToFireStore(byte[] thumbnailData, final String downloadPostImageUri, final String postDescription) {
final StorageReference thumbnailImagePath = storageReference.child("post_images/thumbnail").child(randomName + ".jpg");
UploadTask uploadTask = thumbnailImagePath.putBytes(thumbnailData);
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
thumbnailImagePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri thumbnailUri) {
String downloadThumbnailUri = thumbnailUri.toString();
//map tag name should be the same as model class (BlogPost) name
Map<String, Object> postMap = new HashMap<>();
postMap.put("userId", currentUserId);
postMap.put("postImageUrl", downloadPostImageUri);
postMap.put("postThumbnailUrl", downloadThumbnailUri);
postMap.put("postDescription", postDescription);
postMap.put("postDateAndTime", getCurrentDateAndTime());
firebaseFirestore.collection("Posts").add(postMap).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
@Override
public void onComplete(@NonNull Task<DocumentReference> task) {
if (task.isSuccessful()) {
alertDialog.dismiss();
Toast.makeText(NewPostActivity.this, "Post added successfully", Toast.LENGTH_SHORT).show();
startActivity(new Intent(NewPostActivity.this, MainActivity.class));
finish();
overridePendingTransition(0, 0);
} else {
alertDialog.dismiss();
Toast.makeText(NewPostActivity.this, "Error " + Objects.requireNonNull(task.getException()).getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
alertDialog.dismiss();
Toast.makeText(NewPostActivity.this, "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
});
}
private String getRandomName() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH);
return simpleDateFormat.format(new Date());
}
private String getCurrentDateAndTime() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mma", Locale.ENGLISH);
return simpleDateFormat.format(new Date()).toLowerCase();
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(0, 0);
}
@Override
public boolean onSupportNavigateUp() {
overridePendingTransition(0, 0);
return super.onSupportNavigateUp();
}
}