Skip to content

Commit

Permalink
merged linked attachment changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mickstar committed Jul 9, 2020
2 parents 9e74322 + de107cc commit 00145e3
Show file tree
Hide file tree
Showing 21 changed files with 284 additions and 162 deletions.
26 changes: 13 additions & 13 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ android {
applicationId "com.mickstarify.zooforzotero"
minSdkVersion 21
targetSdkVersion 29
versionCode 34
versionName "2.4a"
versionCode 35
versionName "2.5"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
buildConfigField("String", "zotero_api_key", apikeyProperties['zotero_api_key'])
buildConfigField("String", "zotero_api_secret", apikeyProperties['zotero_api_secret'])
Expand Down Expand Up @@ -55,15 +55,15 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.core:core-ktx:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation "androidx.navigation:navigation-fragment-ktx:2.1.0"
implementation "androidx.navigation:navigation-ui-ktx:2.1.0"
implementation 'com.google.android.material:material:1.0.0'
implementation 'com.google.android.material:material:1.2.0-alpha03'
implementation "androidx.navigation:navigation-fragment-ktx:2.3.0"
implementation "androidx.navigation:navigation-ui-ktx:2.3.0"
implementation 'com.google.android.material:material:1.1.0'
implementation 'com.google.android.material:material:1.3.0-alpha01'
api 'com.google.guava:guava:28.1-jre'
implementation 'oauth.signpost:oauth-signpost:1.2.1.2'
implementation("com.squareup.okhttp3:okhttp:4.2.1")
implementation("com.squareup.okhttp3:okhttp:4.2.2")
implementation('com.squareup.okhttp3:logging-interceptor:4.2.2')
implementation('oauth.signpost:signpost-commonshttp4:1.2.1.2') {
exclude group: 'org.apache.httpcomponents'
Expand All @@ -76,14 +76,14 @@ dependencies {
implementation 'org.jetbrains.anko:anko:0.10.8'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.preference:preference:1.1.0'
implementation 'androidx.preference:preference:1.1.1'
testImplementation 'junit:junit:4.12'
testImplementation "org.mockito:mockito-core:2.28.2"
androidTestImplementation "org.mockito:mockito-core:2.28.2"
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'androidx.gridlayout:gridlayout:1.0.0'
implementation 'com.google.firebase:firebase-analytics:17.2.2'
implementation 'com.google.firebase:firebase-analytics:17.4.3'
implementation 'net.lingala.zip4j:zip4j:2.2.7'
releaseImplementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'

Expand All @@ -96,9 +96,9 @@ dependencies {
debugImplementation 'com.facebook.soloader:soloader:0.8.0'

// Room Dependencies
implementation "androidx.room:room-runtime:2.2.3"
implementation 'androidx.room:room-rxjava2:2.2.3'
kapt "androidx.room:room-compiler:2.2.3"
implementation "androidx.room:room-runtime:2.2.5"
implementation 'androidx.room:room-rxjava2:2.2.5'
kapt "androidx.room:room-compiler:2.2.5"

// Dagger Dependencies
implementation 'com.google.dagger:dagger:2.25.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ interface Contract {
fun refreshItemView()
fun displayGroupsOnActionBar(groups: List<GroupInfo>)
fun openGroup(groupTitle: String)
fun startUploadingAttachment(attachment: Item)
fun stopUploadingAttachment()
fun startUploadingAttachmentProgress(attachment: Item)
fun stopUploadingAttachmentProgress()
fun onResume()
fun createYesNoPrompt(
title: String, message: String, yesText: String, noText: String, onYesClick: () -> Unit,
Expand All @@ -83,6 +83,8 @@ interface Contract {
fun showBasicSyncAnimation()
fun hideBasicSyncAnimation()
fun openTrash()
fun uploadAttachment(item: Item)
fun requestForceResync()
}

interface Model {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.mickstarify.zooforzotero.LibraryActivity.ItemView

import android.app.AlertDialog
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.net.Uri
import android.os.Bundle
Expand All @@ -15,7 +16,7 @@ import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.Fragment
import com.mickstarify.zooforzotero.R
import com.mickstarify.zooforzotero.ZoteroStorage.Database.Item
import org.jetbrains.anko.support.v4.toast
import org.jetbrains.anko.sdk27.coroutines.onLongClick

private const val ARG_ATTACHMENT = "attachment"

Expand Down Expand Up @@ -44,18 +45,19 @@ class ItemAttachmentEntry : Fragment() {
filename.text = attachment?.data?.get("filename") ?: "unknown"
if (linkMode == "linked_file") { // this variant uses title as a filename.
filename.text = "[Linked] ${attachment?.getItemData("title")}"
}else if (linkMode == "linked_url") {

} else if (linkMode == "linked_url") {
filename.text = "[Linked Url] ${attachment?.getItemData("title")}"
layout.setOnClickListener {
val url = attachment?.getItemData("url")
AlertDialog.Builder(context)
.setMessage("Would you like to open this URL: $url")
.setPositiveButton("Yes", {dialog, which ->
.setPositiveButton("Yes", { dialog, which ->
val intent = Intent(Intent.ACTION_VIEW)
intent.setData(Uri.parse(url))
startActivity(intent)
})
.setNegativeButton("No", {_,_ -> })
.setNegativeButton("No", { _, _ -> })
.show()
}
}
Expand All @@ -69,16 +71,40 @@ class ItemAttachmentEntry : Fragment() {
icon.setImageResource(R.drawable.djvu_icon)
} else if (attachment?.getFileExtension() == "epub") {
icon.setImageResource(R.drawable.epub_icon)
} else {
// todo get default attachment icon.
}
layout.setOnClickListener {
if (linkMode == "linked_file") {
toast("This attachment is linked and I cannot download it.")
fileOpenListener?.openLinkedAttachmentListener(
attachment ?: throw Exception("No Attachment given.")
)
} else {
fileOpenListener?.openAttachmentFileListener(
attachment ?: throw Exception("No Attachment given.")
)
}
}

layout.onLongClick {
AlertDialog.Builder(context)
.setTitle("Attachment")
.setItems(
arrayOf("Open", "Force Re-upload"),
object : DialogInterface.OnClickListener {
override fun onClick(dialog: DialogInterface?, item: Int) {
when (item) {
0 -> fileOpenListener?.openAttachmentFileListener(
attachment ?: throw Exception("No Attachment given.")
)
1 -> fileOpenListener?.forceUploadAttachmentListener(
attachment ?: throw Exception("No Attachment given.")
)
}
}

}).show()
}
}
return view
}
Expand All @@ -94,6 +120,8 @@ class ItemAttachmentEntry : Fragment() {

interface OnAttachmentFragmentInteractionListener {
fun openAttachmentFileListener(item: Item)
fun forceUploadAttachmentListener(item: Item)
fun openLinkedAttachmentListener(item: Item)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ class ItemViewFragment : BottomSheetDialogFragment(), NoteInteractionListener {
addTextEntry("Item Type", item.data["itemType"] ?: "Unknown")
addTextEntry("title", item.getTitle())
if (item.creators.isNotEmpty()) {
this.addCreators(item.creators)
this.addCreators(item.getSortedCreators())
} else {
// empty creator.
this.addCreators(listOf(Creator("null", "", "", "")))
this.addCreators(listOf(Creator("null", "", "", "", -1)))
}
for ((key, value) in item.data) {
if (value != "" && key != "itemType" && key != "title") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class LibraryActivity : AppCompatActivity(), Contract.View,

override fun initUI() {
val navigationView = findViewById<NavigationView>(R.id.nav_view_library)
navigationView.setCheckedItem(R.id.my_library)
collectionsMenu = navigationView.menu.addSubMenu(
R.id.group_collections,
Menu.NONE,
Expand Down Expand Up @@ -159,6 +160,9 @@ class LibraryActivity : AppCompatActivity(), Contract.View,
val intent = Intent(this, AttachmentManager::class.java)
startActivity(intent)
}
R.id.force_resync -> {
presenter.requestForceResync()
}

}
return super.onOptionsItemSelected(item)
Expand Down Expand Up @@ -461,6 +465,14 @@ class LibraryActivity : AppCompatActivity(), Contract.View,
presenter.openAttachment(item)
}

override fun forceUploadAttachmentListener(item: Item) {
presenter.uploadAttachment(item)
}

override fun openLinkedAttachmentListener(item: Item) {
presenter.openAttachment(item)
}

override fun onListFragmentInteraction(item: Item?) {
Log.d("zotero", "got onListFragmentInteraction from item ${item?.itemKey}")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,17 @@ class LibraryActivityModel(private val presenter: Contract.Presenter, val contex
/* This is the point of entry when a user clicks an attachment on the UI.
* We must decide whether we want to intitiate a download or just open a local copy. */

// first check to see if we are opening a linked attachment
if (item.data["linkMode"] == "linked_file"){
val intent = attachmentStorageManager.openLinkedAttachment(item)
if (intent != null){
context.startActivity(intent)
} else {
presenter.makeToastAlert("Error, could not find linked attachment ${item.data["path"]}")
}
return
}

// check to see if the attachment exists but is invalid
val attachmentExists: Boolean
try {
Expand Down Expand Up @@ -1034,7 +1045,13 @@ class LibraryActivityModel(private val presenter: Contract.Presenter, val contex
}

override fun uploadAttachment(attachment: Item) {
val md5Key = attachmentStorageManager.calculateMd5(attachment)
val md5Key: String
try {
md5Key = attachmentStorageManager.calculateMd5(attachment)
} catch (e: FileNotFoundException){
presenter.makeToastAlert("Cannot upload attachment. File does not exist.")
return
}
var mtime = attachmentStorageManager.getMtime(attachment)

if (mtime < attachment.getMtime()) {
Expand All @@ -1056,7 +1073,7 @@ class LibraryActivityModel(private val presenter: Contract.Presenter, val contex
.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(
object : CompletableObserver {
override fun onComplete() {
presenter.stopUploadingAttachment()
presenter.stopUploadingAttachmentProgress()
removeFromRecentlyViewed(attachment)
zoteroDB.updateAttachmentMetadata(
attachment.itemKey,
Expand All @@ -1071,7 +1088,7 @@ class LibraryActivityModel(private val presenter: Contract.Presenter, val contex
}

override fun onSubscribe(d: Disposable) {
presenter.startUploadingAttachment(attachment)
presenter.startUploadingAttachmentProgress(attachment)
}

override fun onError(e: Throwable) {
Expand All @@ -1086,16 +1103,18 @@ class LibraryActivityModel(private val presenter: Contract.Presenter, val contex
"error_uploading_attachments_webdav",
bundle
)
presenter.stopUploadingAttachment()
presenter.stopUploadingAttachmentProgress()
}
})
return
} else {
zoteroAPI.updateAttachment(attachment).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()).subscribe(object :
zoteroAPI.updateAttachment(attachment)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object :
CompletableObserver {
override fun onComplete() {
presenter.stopUploadingAttachment()
presenter.stopUploadingAttachmentProgress()
removeFromRecentlyViewed(attachment)
zoteroDB.updateAttachmentMetadata(
attachment.itemKey,
Expand All @@ -1106,18 +1125,19 @@ class LibraryActivityModel(private val presenter: Contract.Presenter, val contex
}

override fun onSubscribe(d: Disposable) {
presenter.startUploadingAttachment(attachment)
presenter.startUploadingAttachmentProgress(attachment)
}

override fun onError(e: Throwable) {
if (e is com.mickstarify.zooforzotero.ZoteroAPI.AlreadyUploadedException) {
if (e is AlreadyUploadedException) {
removeFromRecentlyViewed(attachment)
zoteroDB.updateAttachmentMetadata(
attachment.itemKey,
attachmentStorageManager.calculateMd5(attachment),
attachmentStorageManager.getMtime(attachment),
AttachmentInfo.WEBDAV
).subscribeOn(Schedulers.io()).subscribe()
presenter.makeToastAlert("Attachment already up to date.")
} else if (e is PreconditionFailedException) {
presenter.createErrorAlert(
"Error uploading Attachment",
Expand All @@ -1140,7 +1160,7 @@ class LibraryActivityModel(private val presenter: Contract.Presenter, val contex
Bundle().apply { putString("error_message", e.toString()) }
firebaseAnalytics.logEvent("error_uploading_attachments", bundle)
}
presenter.stopUploadingAttachment()
presenter.stopUploadingAttachmentProgress()
}

})
Expand Down Expand Up @@ -1471,27 +1491,6 @@ class LibraryActivityModel(private val presenter: Contract.Presenter, val contex
}

checkAttachmentStorageAccess()

/*Do to a change in implementation this code will transition webdav configs.*/
if (preferences.firstRunForVersion25()) {
if (preferences.isWebDAVEnabled()) {
val address = preferences.getWebDAVAddress()
val newAddress = if (address.endsWith("/zotero")) {
address
} else {
if (address.endsWith("/")) { // so we don't get server.com//zotero
address + "zotero"
} else {
address + "/zotero"
}
}
preferences.setWebDAVAuthentication(
newAddress,
preferences.getWebDAVUsername(),
preferences.getWebDAVPassword()
)
}
}
}


Expand All @@ -1502,4 +1501,9 @@ class LibraryActivityModel(private val presenter: Contract.Presenter, val contex
fun getCollectionFromKey(collectionKey: String): Collection? {
return zoteroDB.getCollectionById(collectionKey)
}

fun destroyLibrary() {
zoteroDB.clearItemsVersion()
zoteroDatabase.deleteEverything()
}
}
Loading

0 comments on commit 00145e3

Please sign in to comment.