-
-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Álvaro Brey <[email protected]>
- Loading branch information
1 parent
b334e76
commit 091911a
Showing
9 changed files
with
207 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
app/src/main/java/it/niedermann/owncloud/notes/persistence/DirectEditingRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package it.niedermann.owncloud.notes.persistence | ||
|
||
import android.app.Application | ||
import android.content.Context | ||
import com.nextcloud.android.sso.model.SingleSignOnAccount | ||
import io.reactivex.Single | ||
import io.reactivex.schedulers.Schedulers | ||
import it.niedermann.owncloud.notes.persistence.entity.Note | ||
import it.niedermann.owncloud.notes.shared.model.ApiVersion | ||
import it.niedermann.owncloud.notes.shared.model.directediting.DirectEditingInfo | ||
import it.niedermann.owncloud.notes.shared.model.directediting.DirectEditingRequestBody | ||
|
||
// TODO better error handling | ||
class DirectEditingRepository private constructor(private val applicationContext: Context) { | ||
|
||
private val apiProvider: ApiProvider by lazy { ApiProvider.getInstance() } | ||
private val notesRepository: NotesRepository by lazy { | ||
NotesRepository.getInstance( | ||
applicationContext, | ||
) | ||
} | ||
|
||
// TODO check for internet connection, check for directEditing supporting fileId as argument | ||
fun isDirectEditingSupportedByServer(account: SingleSignOnAccount): Single<Boolean> { | ||
val pathSingle = getNotesPath(account) | ||
val textAvailableSingle = getDirectEditingInfo(account) | ||
.map { it.editors.containsKey(SUPPORTED_EDITOR_ID) } | ||
|
||
return Single.zip(pathSingle, textAvailableSingle) { path, textAvailable -> | ||
path.isNotEmpty() && textAvailable | ||
} | ||
} | ||
|
||
private fun getDirectEditingInfo(account: SingleSignOnAccount): Single<DirectEditingInfo> { | ||
val filesAPI = apiProvider.getFilesAPI(applicationContext, account) | ||
return Single.fromCallable { | ||
val call = filesAPI.getDirectEditingInfo() | ||
val response = call.execute() | ||
response.body()?.ocs?.data | ||
?: throw RuntimeException("No DirectEditingInfo available") | ||
}.subscribeOn(Schedulers.io()) | ||
} | ||
|
||
private fun getNotesPath(account: SingleSignOnAccount): Single<String> { | ||
return Single.fromCallable { | ||
val call = notesRepository.getServerSettings(account, ApiVersion.API_VERSION_1_0) | ||
val response = call.execute() | ||
response.body()?.notesPath ?: "" | ||
}.subscribeOn(Schedulers.io()) | ||
} | ||
|
||
fun getDirectEditingUrl( | ||
account: SingleSignOnAccount, | ||
note: Note, | ||
): Single<String> { | ||
return getNotesPath(account) | ||
.flatMap { notesPath -> | ||
val filesAPI = apiProvider.getFilesAPI(applicationContext, account) | ||
Single.fromCallable { | ||
val call = | ||
filesAPI.getDirectEditingUrl( | ||
DirectEditingRequestBody( | ||
path = notesPath, | ||
editorId = SUPPORTED_EDITOR_ID, | ||
fileId = note.remoteId!!, | ||
), | ||
) | ||
val response = call.execute() | ||
response.body()?.ocs?.data?.url | ||
?: throw RuntimeException("No url available") | ||
}.subscribeOn(Schedulers.io()) | ||
} | ||
} | ||
|
||
companion object { | ||
private const val SUPPORTED_EDITOR_ID = "text" | ||
|
||
private var instance: DirectEditingRepository? = null | ||
|
||
/** | ||
* @param applicationContext The application context. Do NOT use a view context to prevent leaks. | ||
*/ | ||
@JvmStatic | ||
fun getInstance(applicationContext: Context): DirectEditingRepository { | ||
require(applicationContext is Application) | ||
if (instance == null) { | ||
instance = DirectEditingRepository(applicationContext) | ||
} | ||
return instance!! | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
app/src/main/java/it/niedermann/owncloud/notes/persistence/sync/FilesAPI.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package it.niedermann.owncloud.notes.persistence.sync | ||
|
||
import it.niedermann.owncloud.notes.shared.model.OcsResponse | ||
import it.niedermann.owncloud.notes.shared.model.OcsUrl | ||
import it.niedermann.owncloud.notes.shared.model.directediting.DirectEditingInfo | ||
import it.niedermann.owncloud.notes.shared.model.directediting.DirectEditingRequestBody | ||
import retrofit2.Call | ||
import retrofit2.http.Body | ||
import retrofit2.http.GET | ||
import retrofit2.http.POST | ||
|
||
interface FilesAPI { | ||
@GET("directEditing?format=json") | ||
fun getDirectEditingInfo(): Call<OcsResponse<DirectEditingInfo>> | ||
|
||
@POST("directEditing/open?format=json") | ||
fun getDirectEditingUrl(@Body body: DirectEditingRequestBody): Call<OcsResponse<OcsUrl>> | ||
} |
9 changes: 9 additions & 0 deletions
9
app/src/main/java/it/niedermann/owncloud/notes/shared/model/OcsUrl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package it.niedermann.owncloud.notes.shared.model | ||
|
||
import com.google.gson.annotations.Expose | ||
|
||
data class OcsUrl( | ||
@Expose | ||
@JvmField | ||
var url: String? = null | ||
) |
18 changes: 18 additions & 0 deletions
18
...main/java/it/niedermann/owncloud/notes/shared/model/directediting/DirectEditingCreator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package it.niedermann.owncloud.notes.shared.model.directediting | ||
|
||
import com.google.gson.annotations.Expose | ||
|
||
data class DirectEditingCreator( | ||
@Expose | ||
val id: String, | ||
@Expose | ||
val editor: String, | ||
@Expose | ||
val name: String, | ||
@Expose | ||
val extension: String, | ||
@Expose | ||
val mimetype: String, | ||
@Expose | ||
val templates: Boolean, | ||
) |
19 changes: 19 additions & 0 deletions
19
.../main/java/it/niedermann/owncloud/notes/shared/model/directediting/DirectEditingEditor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package it.niedermann.owncloud.notes.shared.model.directediting | ||
|
||
import com.google.gson.annotations.Expose | ||
|
||
/** | ||
* Editor for direct editing data model | ||
*/ | ||
data class DirectEditingEditor( | ||
@Expose | ||
val id: String, | ||
@Expose | ||
val name: String, | ||
@Expose | ||
val mimetypes: ArrayList<String>, | ||
@Expose | ||
val optionalMimetypes: ArrayList<String>, | ||
@Expose | ||
val secure: Boolean, | ||
) |
10 changes: 10 additions & 0 deletions
10
...rc/main/java/it/niedermann/owncloud/notes/shared/model/directediting/DirectEditingInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package it.niedermann.owncloud.notes.shared.model.directediting | ||
|
||
import com.google.gson.annotations.Expose | ||
|
||
data class DirectEditingInfo( | ||
@Expose | ||
val editors: Map<String, DirectEditingEditor>, | ||
@Expose | ||
val creators: Map<String, DirectEditingCreator>, | ||
) |
12 changes: 12 additions & 0 deletions
12
.../java/it/niedermann/owncloud/notes/shared/model/directediting/DirectEditingRequestBody.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package it.niedermann.owncloud.notes.shared.model.directediting | ||
|
||
import com.google.gson.annotations.Expose | ||
|
||
data class DirectEditingRequestBody( | ||
@Expose | ||
val path: String, | ||
@Expose | ||
val editorId: String, | ||
@Expose | ||
val fileId: Long, | ||
) |