Capture images and videos from camera when click capture fab in PostImageActivity and PostVideoActivity. Go to user's profile after sending image post.

This commit is contained in:
Alex Ning 2019-07-24 18:51:57 +08:00
parent fcff7784c9
commit 469542fb68
8 changed files with 67 additions and 22 deletions

View File

@ -101,6 +101,17 @@
android:name=".ViewUserDetailActivity" android:name=".ViewUserDetailActivity"
android:parentActivityName=".MainActivity" android:parentActivityName=".MainActivity"
android:theme="@style/AppTheme.NoActionBarWithTranslucentWindow" /> android:theme="@style/AppTheme.NoActionBarWithTranslucentWindow" />
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths">
</meta-data>
</provider>
</application> </application>
</manifest> </manifest>

View File

@ -68,9 +68,11 @@ public class CommentDataSource extends PageKeyedDataSource<String, CommentData>
initialParams = params; initialParams = params;
initialCallback = callback; initialCallback = callback;
initialLoadStateLiveData.postValue(NetworkState.LOADING);
RedditAPI api = retrofit.create(RedditAPI.class); RedditAPI api = retrofit.create(RedditAPI.class);
Call<String> bestPost = api.getUserComments(username, null); Call<String> commentsCall = api.getUserComments(username, null);
bestPost.enqueue(new Callback<String>() { commentsCall.enqueue(new Callback<String>() {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if(response.isSuccessful()) { if(response.isSuccessful()) {
@ -117,6 +119,8 @@ public class CommentDataSource extends PageKeyedDataSource<String, CommentData>
this.params = params; this.params = params;
this.callback = callback; this.callback = callback;
paginationNetworkStateLiveData.postValue(NetworkState.LOADING);
RedditAPI api = retrofit.create(RedditAPI.class); RedditAPI api = retrofit.create(RedditAPI.class);
Call<String> bestPost = api.getUserComments(username, params.key); Call<String> bestPost = api.getUserComments(username, params.key);
bestPost.enqueue(new Callback<String>() { bestPost.enqueue(new Callback<String>() {

View File

@ -113,7 +113,7 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni
mProgressBar.setVisibility(View.GONE); mProgressBar.setVisibility(View.GONE);
} else if(networkState.getStatus().equals(NetworkState.Status.FAILED)) { } else if(networkState.getStatus().equals(NetworkState.Status.FAILED)) {
mFetchCommentInfoLinearLayout.setOnClickListener(view -> mCommentViewModel.retry()); mFetchCommentInfoLinearLayout.setOnClickListener(view -> mCommentViewModel.retry());
showErrorView(R.string.load_posts_error); showErrorView(R.string.load_comments_failed);
} else { } else {
mFetchCommentInfoLinearLayout.setVisibility(View.GONE); mFetchCommentInfoLinearLayout.setVisibility(View.GONE);
mProgressBar.setVisibility(View.VISIBLE); mProgressBar.setVisibility(View.VISIBLE);

View File

@ -6,6 +6,8 @@ import android.graphics.Bitmap;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
@ -13,6 +15,7 @@ import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -20,6 +23,7 @@ import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout; import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.content.FileProvider;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager; import com.bumptech.glide.RequestManager;
@ -30,7 +34,8 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import com.libRG.CustomTextView; import com.libRG.CustomTextView;
import java.util.ArrayList; import java.io.File;
import java.io.IOException;
import java.util.Locale; import java.util.Locale;
import javax.inject.Inject; import javax.inject.Inject;
@ -61,6 +66,7 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0; private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0;
private static final int PICK_IMAGE_REQUEST_CODE = 1; private static final int PICK_IMAGE_REQUEST_CODE = 1;
private static final int CAPTURE_IMAGE_REQUEST_CODE = 2;
@BindView(R.id.coordinator_layout_post_image_activity) CoordinatorLayout coordinatorLayout; @BindView(R.id.coordinator_layout_post_image_activity) CoordinatorLayout coordinatorLayout;
@BindView(R.id.subreddit_icon_gif_image_view_post_image_activity) GifImageView iconGifImageView; @BindView(R.id.subreddit_icon_gif_image_view_post_image_activity) GifImageView iconGifImageView;
@ -228,7 +234,19 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
}); });
captureFab.setOnClickListener(view -> { captureFab.setOnClickListener(view -> {
Intent pictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(pictureIntent.resolveActivity(getPackageManager()) != null) {
try {
imageUri = FileProvider.getUriForFile(this, "ml.docilealligator.infinityforreddit.provider",
File.createTempFile("temp_img", ".jpg", getExternalFilesDir(Environment.DIRECTORY_PICTURES)));
pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(pictureIntent, CAPTURE_IMAGE_REQUEST_CODE);
} catch (IOException ex) {
Snackbar.make(coordinatorLayout, R.string.error_creating_temp_file, Snackbar.LENGTH_SHORT).show();
}
} else {
Snackbar.make(coordinatorLayout, R.string.no_camera_available, Snackbar.LENGTH_SHORT).show();
}
}); });
selectFromLibraryFab.setOnClickListener(view -> { selectFromLibraryFab.setOnClickListener(view -> {
@ -330,21 +348,12 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if(response.isSuccessful()) { if(response.isSuccessful()) {
ParsePost.parsePosts(response.body(), mLocale, 1, Toast.makeText(PostImageActivity.this, R.string.image_is_processing, Toast.LENGTH_SHORT).show();
new ParsePost.ParsePostsListingListener() { Intent intent = new Intent(PostImageActivity.this, ViewUserDetailActivity.class);
@Override intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY,
public void onParsePostsListingSuccess(ArrayList<Post> newPostData, String lastItem) { mUserInfoSharedPreferences.getString(SharedPreferencesUtils.USER_KEY, ""));
Intent intent = new Intent(PostImageActivity.this, ViewPostDetailActivity.class);
intent.putExtra(ViewPostDetailActivity.EXTRA_POST_DATA, newPostData.get(0));
startActivity(intent); startActivity(intent);
finish(); finish();
}
@Override
public void onParsePostsListingFail() {
startViewUserDetailActivity();
}
});
} else { } else {
startViewUserDetailActivity(); startViewUserDetailActivity();
} }
@ -435,6 +444,10 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
imageUri = data.getData(); imageUri = data.getData();
loadImage(); loadImage();
} }
} else if(requestCode == CAPTURE_IMAGE_REQUEST_CODE) {
if(resultCode == RESULT_OK) {
loadImage();
}
} }
} }

View File

@ -7,6 +7,7 @@ import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
@ -62,6 +63,7 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0; private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0;
private static final int PICK_VIDEO_REQUEST_CODE = 1; private static final int PICK_VIDEO_REQUEST_CODE = 1;
private static final int CAPTURE_VIDEO_REQUEST_CODE = 2;
@BindView(R.id.coordinator_layout_post_video_activity) CoordinatorLayout coordinatorLayout; @BindView(R.id.coordinator_layout_post_video_activity) CoordinatorLayout coordinatorLayout;
@BindView(R.id.subreddit_icon_gif_image_view_post_video_activity) GifImageView iconGifImageView; @BindView(R.id.subreddit_icon_gif_image_view_post_video_activity) GifImageView iconGifImageView;
@ -233,7 +235,10 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
}); });
captureFab.setOnClickListener(view -> { captureFab.setOnClickListener(view -> {
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takeVideoIntent, CAPTURE_VIDEO_REQUEST_CODE);
}
}); });
selectFromLibraryFab.setOnClickListener(view -> { selectFromLibraryFab.setOnClickListener(view -> {
@ -416,6 +421,9 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
videoUri = data.getData(); videoUri = data.getData();
loadImage(); loadImage();
} }
} else if (requestCode == CAPTURE_VIDEO_REQUEST_CODE) {
videoUri = data.getData();
loadImage();
} }
} }

View File

@ -25,7 +25,8 @@
android:id="@+id/progress_bar_view_post_detail_activity" android:id="@+id/progress_bar_view_post_detail_activity"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" /> android:layout_gravity="center"
android:visibility="gone" />
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_view_post_detail" android:id="@+id/recycler_view_view_post_detail"

View File

@ -117,8 +117,11 @@
<string name="select_from_gallery">Select a picture</string> <string name="select_from_gallery">Select a picture</string>
<string name="select_again">Select again</string> <string name="select_again">Select again</string>
<string name="error_getting_image">Error getting the image</string> <string name="error_getting_image">Error getting the image</string>
<string name="no_camera_available">No camera app available</string>
<string name="error_creating_temp_file">Error creating temp file</string>
<string name="video_is_processing">Video is processing. Please wait.</string> <string name="video_is_processing">Video is processing. Please wait.</string>
<string name="image_is_processing">Image is processing. Please wait.</string>
<string name="flair">Flair</string> <string name="flair">Flair</string>
<string name="spoiler">Spoiler</string> <string name="spoiler">Spoiler</string>

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="image_temp"
path="Android/data/ml.docilealligator.infinityforreddit/files/Pictures" />
</paths>