@@ -35,9 +35,15 @@ import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod
35
35
import com.owncloud.android.lib.common.operations.RemoteOperation
36
36
import com.owncloud.android.lib.common.operations.RemoteOperationResult
37
37
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
38
43
import org.json.JSONArray
39
44
import org.json.JSONObject
40
45
import timber.log.Timber
46
+ import java.lang.reflect.Type
41
47
import java.net.URL
42
48
import java.util.ArrayList
43
49
@@ -80,7 +86,7 @@ class GetRemoteShareesOperation
80
86
* @param perPage maximum number of results in a single page
81
87
*/
82
88
(private val searchString: String , private val page: Int , private val perPage: Int ) :
83
- RemoteOperation <ArrayList < JSONObject > >() {
89
+ RemoteOperation <ShareeOcsResponse >() {
84
90
85
91
private fun buildRequestUri (baseUri : Uri ) =
86
92
baseUri.buildUpon()
@@ -92,32 +98,18 @@ class GetRemoteShareesOperation
92
98
.appendQueryParameter(PARAM_PER_PAGE , perPage.toString())
93
99
.build()
94
100
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
114
106
}
115
107
116
108
private fun onResultUnsuccessful (
117
109
method : GetMethod ,
118
110
response : String? ,
119
111
status : Int
120
- ): RemoteOperationResult <ArrayList < JSONObject > > {
112
+ ): RemoteOperationResult <ShareeOcsResponse > {
121
113
Timber .e(" Failed response while getting users/groups from the server " )
122
114
if (response != null ) {
123
115
Timber .e(" *** status code: $status ; response message: $response " )
@@ -139,19 +131,15 @@ class GetRemoteShareesOperation
139
131
return data
140
132
}
141
133
142
- private fun onRequestSuccessful (response : String? ): RemoteOperationResult <ArrayList <JSONObject >> {
134
+ private fun onRequestSuccessful (response : String? ): RemoteOperationResult <ShareeOcsResponse > {
135
+ val result = RemoteOperationResult <ShareeOcsResponse >(OK )
143
136
Timber .d(" Successful response: $response " )
144
-
145
- // Parse the response
146
- val jsonResults = parseResponse(response!! )
147
-
137
+ result.data = parseResponse(response!! )
148
138
Timber .d(" *** Get Users or groups completed " )
149
- val result = RemoteOperationResult <ArrayList <JSONObject >>(OK )
150
- result.data = flattenResultData(jsonResults)
151
139
return result
152
140
}
153
141
154
- override fun run (client : OwnCloudClient ): RemoteOperationResult <ArrayList < JSONObject > > {
142
+ override fun run (client : OwnCloudClient ): RemoteOperationResult <ShareeOcsResponse > {
155
143
val requestUri = buildRequestUri(client.baseUri)
156
144
157
145
val getMethod = GetMethod (URL (requestUri.toString()))
@@ -191,12 +179,6 @@ class GetRemoteShareesOperation
191
179
private const val VALUE_ITEM_TYPE = " file" // to get the server search for users / groups
192
180
193
181
// 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"
200
182
const val NODE_VALUE = " value"
201
183
const val PROPERTY_LABEL = " label"
202
184
const val PROPERTY_SHARE_TYPE = " shareType"
0 commit comments