Skip to content

Commit d77e98a

Browse files
committed
move document parsing away from ViewModel
1 parent 4d734fc commit d77e98a

File tree

2 files changed

+88
-84
lines changed

2 files changed

+88
-84
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package org.grapheneos.pdfviewer.viewmodel
2+
3+
import android.content.Context
4+
import android.database.Cursor
5+
import android.graphics.Typeface
6+
import android.net.Uri
7+
import android.provider.OpenableColumns
8+
import android.text.SpannableStringBuilder
9+
import android.text.Spanned
10+
import android.text.style.StyleSpan
11+
import android.util.Log
12+
import org.grapheneos.pdfviewer.R
13+
import org.grapheneos.pdfviewer.Utils
14+
import org.json.JSONException
15+
import org.json.JSONObject
16+
import java.text.ParseException
17+
18+
private const val TAG = "DocumentPropertiesParser"
19+
private const val MISSING_STRING = "-"
20+
21+
internal fun parsePropertiesString(
22+
propertiesString: String,
23+
context: Context,
24+
numPages: Int,
25+
uri: Uri?
26+
): ArrayList<CharSequence>? {
27+
val properties = ArrayList<CharSequence>()
28+
val names = context.resources.getStringArray(R.array.property_names);
29+
val cursor: Cursor? = context.contentResolver.query(uri!!, null, null, null, null);
30+
cursor?.let {
31+
it.moveToFirst();
32+
33+
val indexOfName = it.getColumnIndex(OpenableColumns.DISPLAY_NAME)
34+
if (indexOfName >= 0) {
35+
properties.add(
36+
getProperty(null, names[0], it.getString(indexOfName), context)
37+
)
38+
}
39+
40+
val indexOfSize = it.getColumnIndex(OpenableColumns.SIZE)
41+
if (indexOfSize >= 0) {
42+
val fileSize = it.getString(indexOfSize).toLong()
43+
properties.add(
44+
getProperty(null, names[1], Utils.parseFileSize(fileSize), context)
45+
)
46+
}
47+
}
48+
cursor?.close()
49+
50+
val specNames = arrayOf("Title", "Author", "Subject", "Keywords", "CreationDate",
51+
"ModDate", "Producer", "Creator", "PDFFormatVersion", numPages.toString())
52+
try {
53+
val json = JSONObject(propertiesString)
54+
for (i in 2 until names.size) {
55+
properties.add(getProperty(json, names[i], specNames[i - 2], context))
56+
}
57+
58+
Log.d(TAG, "Successfully parsed properties")
59+
return properties
60+
} catch (e: JSONException) {
61+
Log.e(TAG, "Failed to parse properties: ${e.message}", e)
62+
}
63+
return null
64+
}
65+
66+
private fun getProperty(
67+
json: JSONObject?,
68+
name: String,
69+
specName: String,
70+
context: Context
71+
): CharSequence {
72+
val property = SpannableStringBuilder(name).append(":\n")
73+
val value = json?.optString(specName, MISSING_STRING) ?: specName
74+
75+
val valueToAppend = if (specName.endsWith("Date") && value != MISSING_STRING) {
76+
try {
77+
Utils.parseDate(value)
78+
} catch (e: ParseException) {
79+
Log.w(TAG, "${e.message} for $value at offset: ${e.errorOffset}")
80+
context.getString(R.string.document_properties_invalid_date)
81+
}
82+
} else {
83+
value
84+
}
85+
property.append(valueToAppend)
86+
property.setSpan(StyleSpan(Typeface.BOLD), 0, name.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
87+
return property
88+
}
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,15 @@
11
package org.grapheneos.pdfviewer.viewmodel
22

33
import android.content.Context
4-
import android.database.Cursor
5-
import android.graphics.Typeface
64
import android.net.Uri
7-
import android.provider.OpenableColumns
8-
import android.text.SpannableStringBuilder
9-
import android.text.Spanned
10-
import android.text.style.StyleSpan
115
import android.util.Log
126
import androidx.annotation.NonNull
137
import androidx.lifecycle.*
148
import kotlinx.coroutines.Dispatchers
159
import kotlinx.coroutines.launch
16-
import org.grapheneos.pdfviewer.R
17-
import org.grapheneos.pdfviewer.Utils
18-
import org.json.JSONException
19-
import org.json.JSONObject
20-
import java.text.ParseException
2110

2211
private const val TAG = "PdfViewerViewModel"
2312

24-
private const val MISSING_STRING = "-"
25-
2613
private const val STATE_URI = "uri"
2714
private const val STATE_PAGE = "page"
2815
private const val STATE_ZOOM_RATIO = "zoomRatio"
@@ -87,75 +74,4 @@ class PdfViewerViewModel(private val state: SavedStateHandle) : ViewModel() {
8774
}
8875
}
8976
}
90-
91-
companion object DocumentPropertiesParser {
92-
fun parsePropertiesString(
93-
propertiesString: String,
94-
context: Context,
95-
numPages: Int,
96-
uri: Uri?
97-
): ArrayList<CharSequence>? {
98-
val properties = ArrayList<CharSequence>()
99-
val names = context.resources.getStringArray(R.array.property_names);
100-
val cursor: Cursor? = context.contentResolver.query(uri!!, null, null, null, null);
101-
cursor?.let {
102-
it.moveToFirst();
103-
104-
val indexOfName = it.getColumnIndex(OpenableColumns.DISPLAY_NAME)
105-
if (indexOfName >= 0) {
106-
properties.add(
107-
getProperty(null, names[0], it.getString(indexOfName), context)
108-
)
109-
}
110-
111-
val indexOfSize = it.getColumnIndex(OpenableColumns.SIZE)
112-
if (indexOfSize >= 0) {
113-
val fileSize = it.getString(indexOfSize).toLong()
114-
properties.add(
115-
getProperty(null, names[1], Utils.parseFileSize(fileSize), context)
116-
)
117-
}
118-
}
119-
cursor?.close()
120-
121-
val specNames = arrayOf("Title", "Author", "Subject", "Keywords", "CreationDate",
122-
"ModDate", "Producer", "Creator", "PDFFormatVersion", numPages.toString())
123-
try {
124-
val json = JSONObject(propertiesString)
125-
for (i in 2 until names.size) {
126-
properties.add(getProperty(json, names[i], specNames[i - 2], context))
127-
}
128-
129-
Log.d(TAG, "Successfully parsed properties")
130-
return properties
131-
} catch (e: JSONException) {
132-
Log.e(TAG, "Failed to parse properties: ${e.message}", e)
133-
}
134-
return null
135-
}
136-
137-
private fun getProperty(
138-
json: JSONObject?,
139-
name: String,
140-
specName: String,
141-
context: Context
142-
): CharSequence {
143-
val property = SpannableStringBuilder(name).append(":\n")
144-
val value = json?.optString(specName, MISSING_STRING) ?: specName
145-
146-
val valueToAppend = if (specName.endsWith("Date") && value != MISSING_STRING) {
147-
try {
148-
Utils.parseDate(value)
149-
} catch (e: ParseException) {
150-
Log.w(TAG, "${e.message} for $value at offset: ${e.errorOffset}")
151-
context.getString(R.string.document_properties_invalid_date)
152-
}
153-
} else {
154-
value
155-
}
156-
property.append(valueToAppend)
157-
property.setSpan(StyleSpan(Typeface.BOLD), 0, name.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
158-
return property
159-
}
160-
}
16177
}

0 commit comments

Comments
 (0)