Skip to content

Commit

Permalink
Revert "don't use androidx.fragment 1.3.0-alpha08 for now"
Browse files Browse the repository at this point in the history
This reverts commit 3986cb1c
  • Loading branch information
inthewaves committed Aug 30, 2020
1 parent 95ed5a9 commit 4de0b96
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 42 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies {
implementation 'com.google.android.material:material:1.2.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation "androidx.core:core-ktx:1.3.1"
implementation "androidx.fragment:fragment:1.3.0-alpha08"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9"
Expand Down
53 changes: 23 additions & 30 deletions app/src/main/java/org/grapheneos/pdfviewer/PdfViewerFragment.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.grapheneos.pdfviewer;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
Expand All @@ -26,6 +25,9 @@
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
Expand Down Expand Up @@ -74,9 +76,6 @@ public class PdfViewerFragment extends Fragment {
"usb 'none'; " +
"vr 'none'";

private static final int REQUEST_CODE_JUMP_PAGE = 1000;
private static final int REQUEST_CODE_ACTION_OPEN_DOCUMENT = 1001;

private static final float MIN_ZOOM_RATIO = 0.5f;
private static final float MAX_ZOOM_RATIO = 1.5f;
private static final int ALPHA_LOW = 130;
Expand Down Expand Up @@ -184,25 +183,11 @@ public static PdfViewerFragment newInstance() {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}

// Can be replaced when support for passing results between two Fragments
// via new APIs on FragmentManager arrive (Androidx Fragment 1.3.0)
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (requestCode == REQUEST_CODE_JUMP_PAGE) {
if (resultCode == Activity.RESULT_OK) {
final int newPage = data.getIntExtra(JumpToPageFragment.INTENT_KEY, -1);
onJumpToPageInDocument(newPage);
}
} else if (requestCode == REQUEST_CODE_ACTION_OPEN_DOCUMENT) {
if (resultCode == Activity.RESULT_OK && data != null) {
mViewModel.setUri(data.getData());
mViewModel.setPage(1);
loadPdf(true);
requireActivity().invalidateOptionsMenu();
}
}
getParentFragmentManager().setFragmentResultListener(JumpToPageFragment.REQUEST_KEY,
this, (requestKey, result) -> {
final int newPage = result.getInt(JumpToPageFragment.BUNDLE_KEY);
onJumpToPageInDocument(newPage);
});
}

@Override
Expand Down Expand Up @@ -431,11 +416,20 @@ private void documentOrientationChanged(final int orientationDegreesOffset) {
renderPage(0);
}

private ActivityResultLauncher<String> mGetDocumentUriLauncher = registerForActivityResult(
new ActivityResultContracts.GetContent(), new ActivityResultCallback<Uri>() {
@Override
public void onActivityResult(Uri uri) {
if (uri != null) {
mViewModel.setUri(uri);
mViewModel.setPage(1);
loadPdf(true);
}
}
});

private void openDocument() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/pdf");
startActivityForResult(intent, REQUEST_CODE_ACTION_OPEN_DOCUMENT);
mGetDocumentUriLauncher.launch("application/pdf");
}

private void zoomIn(float value, boolean end) {
Expand Down Expand Up @@ -595,9 +589,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
return true;

case R.id.action_jump_to_page:
final JumpToPageFragment fragment = new JumpToPageFragment();
fragment.setTargetFragment(this, REQUEST_CODE_JUMP_PAGE);
fragment.show(getParentFragmentManager(), JumpToPageFragment.TAG);
new JumpToPageFragment()
.show(getParentFragmentManager(), JumpToPageFragment.TAG);
return true;

default:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package org.grapheneos.pdfviewer.fragment;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.FrameLayout;
Expand All @@ -18,8 +16,8 @@
public class JumpToPageFragment extends DialogFragment {
public static final String TAG = "JumpToPageFragment";

public static final int REQUEST_CODE = 1000;
public static final String INTENT_KEY = "jumpToPageBundle";
public static final String REQUEST_KEY = "jumpToPage";
public static final String BUNDLE_KEY = "jumpToPageBundle";

private final static String STATE_PICKER_CUR = "picker_cur";
private final static String STATE_PICKER_MIN = "picker_min";
Expand Down Expand Up @@ -59,15 +57,10 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mPicker.clearFocus();
if (getTargetFragment() == null) {
return;
}

Intent data = new Intent();
data.putExtra(INTENT_KEY, mPicker.getValue());

getTargetFragment().onActivityResult(REQUEST_CODE, Activity.RESULT_OK,
data);
Bundle result = new Bundle();
result.putInt(BUNDLE_KEY, mPicker.getValue());
getParentFragmentManager().setFragmentResult(REQUEST_KEY, result);
}
})
.setNegativeButton(android.R.string.cancel, null)
Expand Down

0 comments on commit 4de0b96

Please sign in to comment.