Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit b866384

Browse files
theScrabiabelgardep
authored andcommitted
update GetRemoteShareesOperation to use moshi
1 parent 1c08a94 commit b866384

File tree

1 file changed

+17
-35
lines changed

1 file changed

+17
-35
lines changed

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareesOperation.kt

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,15 @@ import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod
3535
import com.owncloud.android.lib.common.operations.RemoteOperation
3636
import com.owncloud.android.lib.common.operations.RemoteOperationResult
3737
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK
38+
import com.owncloud.android.lib.resources.CommonOcsResponse
39+
import com.owncloud.android.lib.resources.shares.responses.ShareeOcsResponse
40+
import com.squareup.moshi.JsonAdapter
41+
import com.squareup.moshi.Moshi
42+
import com.squareup.moshi.Types
3843
import org.json.JSONArray
3944
import org.json.JSONObject
4045
import timber.log.Timber
46+
import java.lang.reflect.Type
4147
import java.net.URL
4248
import java.util.ArrayList
4349

@@ -80,7 +86,7 @@ class GetRemoteShareesOperation
8086
* @param perPage maximum number of results in a single page
8187
*/
8288
(private val searchString: String, private val page: Int, private val perPage: Int) :
83-
RemoteOperation<ArrayList<JSONObject>>() {
89+
RemoteOperation<ShareeOcsResponse>() {
8490

8591
private fun buildRequestUri(baseUri: Uri) =
8692
baseUri.buildUpon()
@@ -92,32 +98,18 @@ class GetRemoteShareesOperation
9298
.appendQueryParameter(PARAM_PER_PAGE, perPage.toString())
9399
.build()
94100

95-
private fun parseResponse(response: String): Array<JSONArray> {
96-
val respJSON = JSONObject(response)
97-
val respOCS = respJSON.getJSONObject(NODE_OCS)
98-
val respData = respOCS.getJSONObject(NODE_DATA)
99-
val respExact = respData.getJSONObject(NODE_EXACT)
100-
val respExactUsers = respExact.getJSONArray(NODE_USERS)
101-
val respExactGroups = respExact.getJSONArray(NODE_GROUPS)
102-
val respExactRemotes = respExact.getJSONArray(NODE_REMOTES)
103-
val respPartialUsers = respData.getJSONArray(NODE_USERS)
104-
val respPartialGroups = respData.getJSONArray(NODE_GROUPS)
105-
val respPartialRemotes = respData.getJSONArray(NODE_REMOTES)
106-
return arrayOf(
107-
respExactUsers,
108-
respExactGroups,
109-
respExactRemotes,
110-
respPartialUsers,
111-
respPartialGroups,
112-
respPartialRemotes
113-
)
101+
private fun parseResponse(response: String): ShareeOcsResponse? {
102+
val moshi = Moshi.Builder().build()
103+
val type: Type = Types.newParameterizedType(CommonOcsResponse::class.java, ShareeOcsResponse::class.java)
104+
val adapter: JsonAdapter<CommonOcsResponse<ShareeOcsResponse>> = moshi.adapter(type)
105+
return adapter.fromJson(response)!!.ocs.data
114106
}
115107

116108
private fun onResultUnsuccessful(
117109
method: GetMethod,
118110
response: String?,
119111
status: Int
120-
): RemoteOperationResult<ArrayList<JSONObject>> {
112+
): RemoteOperationResult<ShareeOcsResponse> {
121113
Timber.e("Failed response while getting users/groups from the server ")
122114
if (response != null) {
123115
Timber.e("*** status code: $status; response message: $response")
@@ -139,19 +131,15 @@ class GetRemoteShareesOperation
139131
return data
140132
}
141133

142-
private fun onRequestSuccessful(response: String?): RemoteOperationResult<ArrayList<JSONObject>> {
134+
private fun onRequestSuccessful(response: String?): RemoteOperationResult<ShareeOcsResponse> {
135+
val result = RemoteOperationResult<ShareeOcsResponse>(OK)
143136
Timber.d("Successful response: $response")
144-
145-
// Parse the response
146-
val jsonResults = parseResponse(response!!)
147-
137+
result.data = parseResponse(response!!)
148138
Timber.d("*** Get Users or groups completed ")
149-
val result = RemoteOperationResult<ArrayList<JSONObject>>(OK)
150-
result.data = flattenResultData(jsonResults)
151139
return result
152140
}
153141

154-
override fun run(client: OwnCloudClient): RemoteOperationResult<ArrayList<JSONObject>> {
142+
override fun run(client: OwnCloudClient): RemoteOperationResult<ShareeOcsResponse> {
155143
val requestUri = buildRequestUri(client.baseUri)
156144

157145
val getMethod = GetMethod(URL(requestUri.toString()))
@@ -191,12 +179,6 @@ class GetRemoteShareesOperation
191179
private const val VALUE_ITEM_TYPE = "file" // to get the server search for users / groups
192180

193181
// JSON Node names
194-
private const val NODE_OCS = "ocs"
195-
private const val NODE_DATA = "data"
196-
private const val NODE_EXACT = "exact"
197-
private const val NODE_USERS = "users"
198-
private const val NODE_GROUPS = "groups"
199-
private const val NODE_REMOTES = "remotes"
200182
const val NODE_VALUE = "value"
201183
const val PROPERTY_LABEL = "label"
202184
const val PROPERTY_SHARE_TYPE = "shareType"

0 commit comments

Comments
 (0)