Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor refactorings and test cases #131

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,5 @@ android {
dependencies {
implementation("androidx.appcompat:appcompat:1.4.0")
implementation("com.google.android.material:material:1.4.0")
testImplementation("junit:junit:4.+")
}
11 changes: 5 additions & 6 deletions app/src/main/java/org/grapheneos/pdfviewer/GestureHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,22 @@ public boolean onSingleTapUp(MotionEvent motionEvent) {

final ScaleGestureDetector scaleDetector = new ScaleGestureDetector(context,
new ScaleGestureDetector.SimpleOnScaleGestureListener() {
float SPAN_RATIO = 600;
final float SPAN_RATIO = 600;
float initialSpan;
float prevNbStep;
float prevNbStep = 0;

@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
initialSpan = detector.getCurrentSpan();
prevNbStep = 0;
return true;
}

@Override
public boolean onScale(ScaleGestureDetector detector) {
float spanDiff = initialSpan - detector.getCurrentSpan();
float curNbStep = spanDiff / SPAN_RATIO;
final float spanDiff = initialSpan - detector.getCurrentSpan();
final float curNbStep = spanDiff / SPAN_RATIO;

float stepDiff = curNbStep - prevNbStep;
final float stepDiff = curNbStep - prevNbStep;
if (stepDiff > 0) {
listener.onZoomOut(stepDiff);
} else {
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/org/grapheneos/pdfviewer/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

public class Utils {
public static String parseFileSize(long fileSize) {
final double kb = fileSize / 1000;
final double kb = fileSize / 1000d;

if (kb == 0) {
if (kb == 0d) {
return fileSize + " Bytes";
}

Expand All @@ -39,14 +39,14 @@ public static String parseDate(String date) throws ParseException {

final Calendar calendar = Calendar.getInstance();
final int currentYear = calendar.get(Calendar.YEAR);
int year;

// Year is required
String field = date.substring(position += 2, 6);
if (!TextUtils.isDigitsOnly(field)) {
throw new ParseException("Invalid year", position);
}
year = Integer.valueOf(field);

int year = Integer.valueOf(field);
if (year > currentYear) {
year = currentYear;
}
Expand Down
30 changes: 30 additions & 0 deletions app/src/test/kotlin/org/grapheneos/pdfviewer/UtilsTests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.grapheneos.pdfviewer

import org.junit.Assert
import org.junit.Test

class UtilsTests {
@Test
fun `Should parse File Size (kB)`() {
val fileSize: Long = 1_000
Assert.assertEquals("1 kB (${fileSize} Bytes)", Utils.parseFileSize(fileSize))
}

@Test
fun `Should parse File Size (decimal kB)`() {
val fileSize: Long = 1_024
Assert.assertEquals("1,03 kB (${fileSize} Bytes)", Utils.parseFileSize(fileSize))
}

@Test
fun `Should parse File Size (MB)`() {
val fileSize: Long = 1_000_000
Assert.assertEquals("1 MB (${fileSize} Bytes)", Utils.parseFileSize(fileSize))
}

@Test
fun `Should parse File Size (decimal MB)`() {
val fileSize: Long = 1_240_000
Assert.assertEquals("1,24 MB (${fileSize} Bytes)", Utils.parseFileSize(fileSize))
}
}