mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-31 10:27:45 +00:00 
			
		
		
		
	clean run method by braking down into small functions
This commit is contained in:
		
							parent
							
								
									8a04231473
								
							
						
					
					
						commit
						4f72f93f8b
					
				| @ -28,12 +28,14 @@ | ||||
| 
 | ||||
| package com.owncloud.android.lib.resources.shares | ||||
| 
 | ||||
| import android.net.Uri | ||||
| import com.owncloud.android.lib.common.OwnCloudClient | ||||
| import com.owncloud.android.lib.common.http.HttpConstants | ||||
| import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod | ||||
| import com.owncloud.android.lib.common.operations.RemoteOperation | ||||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult | ||||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK | ||||
| import org.json.JSONArray | ||||
| import org.json.JSONObject | ||||
| import timber.log.Timber | ||||
| import java.net.URL | ||||
| @ -80,30 +82,17 @@ class GetRemoteShareesOperation | ||||
|     (private val searchString: String, private val page: Int, private val perPage: Int) : | ||||
|     RemoteOperation<ArrayList<JSONObject>>() { | ||||
| 
 | ||||
|     override fun run(client: OwnCloudClient): RemoteOperationResult<ArrayList<JSONObject>> { | ||||
|         var result: RemoteOperationResult<ArrayList<JSONObject>> | ||||
| 
 | ||||
|         try { | ||||
|             val requestUri = client.baseUri | ||||
|             val uriBuilder = requestUri.buildUpon() | ||||
|     private fun buildRequestUri(baseUri: Uri) = | ||||
|         baseUri.buildUpon() | ||||
|             .appendEncodedPath(OCS_ROUTE) | ||||
|             .appendQueryParameter(PARAM_FORMAT, VALUE_FORMAT) | ||||
|             .appendQueryParameter(PARAM_ITEM_TYPE, VALUE_ITEM_TYPE) | ||||
|             .appendQueryParameter(PARAM_SEARCH, searchString) | ||||
|             .appendQueryParameter(PARAM_PAGE, page.toString()) | ||||
|             .appendQueryParameter(PARAM_PER_PAGE, perPage.toString()) | ||||
|             .build() | ||||
| 
 | ||||
|             val getMethod = GetMethod(URL(uriBuilder.build().toString())) | ||||
| 
 | ||||
|             getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) | ||||
| 
 | ||||
|             val status = client.executeHttpMethod(getMethod) | ||||
|             val response = getMethod.getResponseBodyAsString() | ||||
| 
 | ||||
|             if (isSuccess(status)) { | ||||
|                 Timber.d("Successful response: $response") | ||||
| 
 | ||||
|                 // Parse the response | ||||
|     private fun parseResponse(response: String): Array<JSONArray> { | ||||
|         val respJSON = JSONObject(response) | ||||
|         val respOCS = respJSON.getJSONObject(NODE_OCS) | ||||
|         val respData = respOCS.getJSONObject(NODE_DATA) | ||||
| @ -114,7 +103,7 @@ class GetRemoteShareesOperation | ||||
|         val respPartialUsers = respData.getJSONArray(NODE_USERS) | ||||
|         val respPartialGroups = respData.getJSONArray(NODE_GROUPS) | ||||
|         val respPartialRemotes = respData.getJSONArray(NODE_REMOTES) | ||||
|                 val jsonResults = arrayOf( | ||||
|         return arrayOf( | ||||
|             respExactUsers, | ||||
|             respExactGroups, | ||||
|             respExactRemotes, | ||||
| @ -122,38 +111,67 @@ class GetRemoteShareesOperation | ||||
|             respPartialGroups, | ||||
|             respPartialRemotes | ||||
|         ) | ||||
| 
 | ||||
|                 val data = ArrayList<JSONObject>() // For result data | ||||
|                 for (i in 0..5) { | ||||
|                     for (j in 0 until jsonResults[i].length()) { | ||||
|                         val jsonResult = jsonResults[i].getJSONObject(j) | ||||
|                         data.add(jsonResult) | ||||
|                         Timber.d("*** Added item: ${jsonResult.getString(PROPERTY_LABEL)}") | ||||
|                     } | ||||
|     } | ||||
| 
 | ||||
|                 result = RemoteOperationResult(OK) | ||||
|                 result.data = data | ||||
| 
 | ||||
|                 Timber.d("*** Get Users or groups completed ") | ||||
| 
 | ||||
|             } else { | ||||
|                 result = RemoteOperationResult(getMethod) | ||||
|     private fun onResultUnsuccessful( | ||||
|         method: GetMethod, | ||||
|         response: String?, | ||||
|         status: Int | ||||
|     ): RemoteOperationResult<ArrayList<JSONObject>> { | ||||
|         Timber.e("Failed response while getting users/groups from the server ") | ||||
|         if (response != null) { | ||||
|             Timber.e("*** status code: $status; response message: $response") | ||||
|         } else { | ||||
|             Timber.e("*** status code: $status") | ||||
|         } | ||||
|             } | ||||
|         } catch (e: Exception) { | ||||
|             result = RemoteOperationResult(e) | ||||
|             Timber.e(e, "Exception while getting users/groups") | ||||
|         return RemoteOperationResult(method) | ||||
|     } | ||||
| 
 | ||||
|     private fun flattenResultData(jsonResults: Array<JSONArray>):ArrayList<JSONObject> { | ||||
|         val data = ArrayList<JSONObject>() // For result data | ||||
|         for (i in 0..jsonResults.size) { | ||||
|             for (j in 0 until jsonResults[i].length()) { | ||||
|                 val jsonResult = jsonResults[i].getJSONObject(j) | ||||
|                 data.add(jsonResult) | ||||
|                 Timber.d("*** Added item: ${jsonResult.getString(PROPERTY_LABEL)}") | ||||
|             } | ||||
|         } | ||||
|         return data | ||||
|     } | ||||
| 
 | ||||
|     private fun onRequestSuccessful(response: String?): RemoteOperationResult<ArrayList<JSONObject>> { | ||||
|         Timber.d("Successful response: $response") | ||||
| 
 | ||||
|         // Parse the response | ||||
|         val jsonResults = parseResponse(response!!) | ||||
| 
 | ||||
|         Timber.d("*** Get Users or groups completed ") | ||||
|         val result = RemoteOperationResult<ArrayList<JSONObject>>(OK) | ||||
|         result.data = flattenResultData(jsonResults) | ||||
|         return result | ||||
|     } | ||||
| 
 | ||||
|     override fun run(client: OwnCloudClient): RemoteOperationResult<ArrayList<JSONObject>> { | ||||
|         val requestUri = buildRequestUri(client.baseUri) | ||||
| 
 | ||||
|         val getMethod = GetMethod(URL(requestUri.toString())) | ||||
|         getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) | ||||
| 
 | ||||
|         return try { | ||||
|             val status = client.executeHttpMethod(getMethod) | ||||
|             val response = getMethod.getResponseBodyAsString() | ||||
| 
 | ||||
|             if (!isSuccess(status)) { | ||||
|                 onResultUnsuccessful(getMethod, response, status) | ||||
|             } else { | ||||
|                 onRequestSuccessful(response) | ||||
|             } | ||||
|         } catch (e: Exception) { | ||||
|             Timber.e(e, "Exception while getting users/groups") | ||||
|             RemoteOperationResult(e) | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     private fun isSuccess(status: Int) = status == HttpConstants.HTTP_OK | ||||
| 
 | ||||
|     companion object { | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user