Skip to content

Commit

Permalink
switch to using Fragments and ViewModel
Browse files Browse the repository at this point in the history
- Host the WebView and all of its related code inside of a Fragment
instead of an Activity. Fragments can give us more flexibility in the
future on how we can display the WebView, how we could manage some
bottom menu via a BottomNavigationView or similar, etc.

- Use a ViewModel and LiveData architecture in order to move away from
Loaders, which are now deprecated. Almost all state information for the
viewer (like current page, zoom ratio) is now stored in the ViewModel,
which survives configuration changes.
  - Rewrite the document property parsing in DocumentPropertiesLoader in
    Kotlin, taking advantage of Kotlin coroutines to do asynchronous
    parsing of the document properties.

- Dynamically update the properties dialog after PDF loads.
If viewing the PDF properties while the PDF loads, before, the dialog
would show an error message. Now, the error message will be swapped out
with the parsed info as soon the document properties have been parsed.
This is done using an Observer on LiveData.

- Use an alpha version of AndroidX Fragment so that we can use a simpler
way to pass data between two Fragments:
https://developer.android.com/training/basics/fragments/pass-data-between

- Alpha version of Fragments also has ActivityResultLauncher, which
simplifies the SAF launch
https://developer.android.com/training/basics/intents/result
"While the underlying startActivityForResult() and onActivityResult()
APIs are available on the Activity class on all API levels, it is
strongly recommended to use the Activity Result APIs introduced in
AndroidX Activity 1.2.0-alpha02 and Fragment 1.3.0-alpha02."

Closes GrapheneOS#69, GrapheneOS#70
  • Loading branch information
inthewaves committed Aug 28, 2020
1 parent 6f8a37c commit a96d97c
Show file tree
Hide file tree
Showing 13 changed files with 416 additions and 382 deletions.
10 changes: 10 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 29
Expand Down Expand Up @@ -36,6 +37,12 @@ dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.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"
}

def props = new Properties()
Expand All @@ -61,3 +68,6 @@ if (propFile.canRead()) {
println 'signing.properties not found'
android.buildTypes.release.signingConfig = null
}
repositories {
mavenCentral()
}
4 changes: 4 additions & 0 deletions app/src/debug/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">PDF Viewer Debug</string>
</resources>
37 changes: 23 additions & 14 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.grapheneos.pdfviewer"
android:targetSandboxVersion="2">
<application android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:allowBackup="true">
<activity android:name=".PdfViewer"
android:label="@string/app_name">
package="org.grapheneos.pdfviewer"
android:targetSandboxVersion="2">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:theme="@style/AppTheme">
<activity
android:name=".PdfViewerActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data android:mimeType="application/pdf" />
</intent-filter>
</activity>

<meta-data android:name="android.webkit.WebView.MetricsOptOut"
android:value="true" />
<meta-data android:name="android.webkit.WebView.EnableSafeBrowsing"
android:value="false" />
<meta-data
android:name="android.webkit.WebView.MetricsOptOut"
android:value="true" />
<meta-data
android:name="android.webkit.WebView.EnableSafeBrowsing"
android:value="false" />
</application>
</manifest>

</manifest>
24 changes: 24 additions & 0 deletions app/src/main/java/org/grapheneos/pdfviewer/PdfViewerActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.grapheneos.pdfviewer;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

public class PdfViewerActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdf_viewer);

if (savedInstanceState == null) {
PdfViewerFragment fragment = PdfViewerFragment.newInstance();
getSupportFragmentManager()
.beginTransaction()
.setPrimaryNavigationFragment(fragment)
.add(R.id.pdf_fragment_container, fragment)
.commit();
}

}
}
Loading

0 comments on commit a96d97c

Please sign in to comment.