From 469542fb683a983824cf7f4b75f29a69b9d6307f Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 24 Jul 2019 18:51:57 +0800 Subject: Capture images and videos from camera when click capture fab in PostImageActivity and PostVideoActivity. Go to user's profile after sending image post. --- app/src/main/AndroidManifest.xml | 11 +++++ .../infinityforreddit/CommentDataSource.java | 8 +++- .../infinityforreddit/CommentsListingFragment.java | 2 +- .../infinityforreddit/PostImageActivity.java | 47 ++++++++++++++-------- .../infinityforreddit/PostVideoActivity.java | 10 ++++- .../main/res/layout/activity_view_post_detail.xml | 3 +- app/src/main/res/values/strings.xml | 3 ++ app/src/main/res/xml/file_paths.xml | 5 +++ 8 files changed, 67 insertions(+), 22 deletions(-) create mode 100644 app/src/main/res/xml/file_paths.xml (limited to 'app/src') diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d77b0afe..423600da 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -101,6 +101,17 @@ android:name=".ViewUserDetailActivity" android:parentActivityName=".MainActivity" android:theme="@style/AppTheme.NoActionBarWithTranslucentWindow" /> + + + + + \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentDataSource.java index acda274a..cef80dc5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentDataSource.java @@ -68,9 +68,11 @@ public class CommentDataSource extends PageKeyedDataSource initialParams = params; initialCallback = callback; + initialLoadStateLiveData.postValue(NetworkState.LOADING); + RedditAPI api = retrofit.create(RedditAPI.class); - Call bestPost = api.getUserComments(username, null); - bestPost.enqueue(new Callback() { + Call commentsCall = api.getUserComments(username, null); + commentsCall.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { if(response.isSuccessful()) { @@ -117,6 +119,8 @@ public class CommentDataSource extends PageKeyedDataSource this.params = params; this.callback = callback; + paginationNetworkStateLiveData.postValue(NetworkState.LOADING); + RedditAPI api = retrofit.create(RedditAPI.class); Call bestPost = api.getUserComments(username, params.key); bestPost.enqueue(new Callback() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentsListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentsListingFragment.java index 5371d220..fbe4a7a6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentsListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentsListingFragment.java @@ -113,7 +113,7 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni mProgressBar.setVisibility(View.GONE); } else if(networkState.getStatus().equals(NetworkState.Status.FAILED)) { mFetchCommentInfoLinearLayout.setOnClickListener(view -> mCommentViewModel.retry()); - showErrorView(R.string.load_posts_error); + showErrorView(R.string.load_comments_failed); } else { mFetchCommentInfoLinearLayout.setVisibility(View.GONE); mProgressBar.setVisibility(View.VISIBLE); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java index 6e71c523..4aec1ddc 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java @@ -6,6 +6,8 @@ import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; +import android.os.Environment; +import android.provider.MediaStore; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -13,6 +15,7 @@ import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -20,6 +23,7 @@ import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; import androidx.constraintlayout.widget.ConstraintLayout; import androidx.coordinatorlayout.widget.CoordinatorLayout; +import androidx.core.content.FileProvider; import com.bumptech.glide.Glide; 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.libRG.CustomTextView; -import java.util.ArrayList; +import java.io.File; +import java.io.IOException; import java.util.Locale; 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 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.subreddit_icon_gif_image_view_post_image_activity) GifImageView iconGifImageView; @@ -228,7 +234,19 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS }); 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 -> { @@ -330,21 +348,12 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { if(response.isSuccessful()) { - ParsePost.parsePosts(response.body(), mLocale, 1, - new ParsePost.ParsePostsListingListener() { - @Override - public void onParsePostsListingSuccess(ArrayList newPostData, String lastItem) { - Intent intent = new Intent(PostImageActivity.this, ViewPostDetailActivity.class); - intent.putExtra(ViewPostDetailActivity.EXTRA_POST_DATA, newPostData.get(0)); - startActivity(intent); - finish(); - } - - @Override - public void onParsePostsListingFail() { - startViewUserDetailActivity(); - } - }); + Toast.makeText(PostImageActivity.this, R.string.image_is_processing, Toast.LENGTH_SHORT).show(); + Intent intent = new Intent(PostImageActivity.this, ViewUserDetailActivity.class); + intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, + mUserInfoSharedPreferences.getString(SharedPreferencesUtils.USER_KEY, "")); + startActivity(intent); + finish(); } else { startViewUserDetailActivity(); } @@ -435,6 +444,10 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS imageUri = data.getData(); loadImage(); } + } else if(requestCode == CAPTURE_IMAGE_REQUEST_CODE) { + if(resultCode == RESULT_OK) { + loadImage(); + } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java index e4ccbfe1..b4c266eb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java @@ -7,6 +7,7 @@ import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.os.ParcelFileDescriptor; +import android.provider.MediaStore; import android.view.Menu; import android.view.MenuItem; 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 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.subreddit_icon_gif_image_view_post_video_activity) GifImageView iconGifImageView; @@ -233,7 +235,10 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS }); 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 -> { @@ -416,6 +421,9 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS videoUri = data.getData(); loadImage(); } + } else if (requestCode == CAPTURE_VIDEO_REQUEST_CODE) { + videoUri = data.getData(); + loadImage(); } } diff --git a/app/src/main/res/layout/activity_view_post_detail.xml b/app/src/main/res/layout/activity_view_post_detail.xml index 11f5ce1e..1e26a65a 100644 --- a/app/src/main/res/layout/activity_view_post_detail.xml +++ b/app/src/main/res/layout/activity_view_post_detail.xml @@ -25,7 +25,8 @@ android:id="@+id/progress_bar_view_post_detail_activity" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_gravity="center" /> + android:layout_gravity="center" + android:visibility="gone" /> Select a picture Select again Error getting the image + No camera app available + Error creating temp file Video is processing. Please wait. + Image is processing. Please wait. Flair Spoiler diff --git a/app/src/main/res/xml/file_paths.xml b/app/src/main/res/xml/file_paths.xml new file mode 100644 index 00000000..79f4cf3f --- /dev/null +++ b/app/src/main/res/xml/file_paths.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file -- cgit v1.2.3