Skip to content

Commit 39d01e3

Browse files
committed
extract non-repository logic
Signed-off-by: alperozturk <[email protected]>
1 parent 1a0c9e9 commit 39d01e3

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed

app/src/main/java/it/niedermann/owncloud/notes/share/repository/ShareRepository.kt

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ import it.niedermann.owncloud.notes.share.model.toOCShare
2222
import it.niedermann.owncloud.notes.shared.model.ApiVersion
2323
import it.niedermann.owncloud.notes.shared.model.Capabilities
2424
import it.niedermann.owncloud.notes.shared.model.NotesSettings
25+
import it.niedermann.owncloud.notes.shared.util.StringConstants
2526
import it.niedermann.owncloud.notes.shared.util.extensions.getErrorMessage
2627
import it.niedermann.owncloud.notes.shared.util.extensions.toExpirationDateString
2728
import org.json.JSONObject
2829
import java.util.Date
2930

30-
class ShareRepository(private val applicationContext: Context, private val account: SingleSignOnAccount) {
31+
class ShareRepository(
32+
private val applicationContext: Context,
33+
private val account: SingleSignOnAccount
34+
) {
3135

3236
private val tag = "ShareRepository"
3337
private val gson = Gson()
@@ -48,14 +52,25 @@ class ShareRepository(private val applicationContext: Context, private val accou
4852
val notesPathResponseResult = getNotesPathResponseResult() ?: return null
4953
val notesPath = notesPathResponseResult.notesPath
5054
val notesSuffix = notesPathResponseResult.fileSuffix
51-
return "/" + notesPath + "/" + note.title + notesSuffix
55+
return StringConstants.PATH + notesPath + StringConstants.PATH + note.title + notesSuffix
5256
}
5357

5458
fun getShareEntitiesForSpecificNote(note: Note): List<ShareEntity> {
5559
val path = getNotePath(note)
5660
return notesRepository.getShareEntities(path)
5761
}
5862

63+
private fun getExpirationDate(chosenExpDateInMills: Long): String? {
64+
if (chosenExpDateInMills == -1L) {
65+
return null
66+
}
67+
68+
return Date(chosenExpDateInMills).toExpirationDateString()
69+
}
70+
71+
fun getCapabilities(): Capabilities = notesRepository.capabilities
72+
73+
// region API calls
5974
fun getSharesForNotesAndSaveShareEntities() {
6075
val notesPathResponseResult = getNotesPathResponseResult() ?: return
6176
val notesPath = notesPathResponseResult.notesPath
@@ -69,7 +84,7 @@ class ShareRepository(private val applicationContext: Context, private val accou
6984
if (call != null) {
7085
val respOCS = call["ocs"] as? LinkedTreeMap<*, *>
7186
val respData = respOCS?.getList("data")
72-
respData?.forEach { data ->
87+
respData?.forEach { data ->
7388
val map = data as? LinkedTreeMap<*, *>
7489
val id = map?.get("id") as? String
7590
val note = map?.get("note") as? String
@@ -117,7 +132,11 @@ class ShareRepository(private val applicationContext: Context, private val accou
117132
perPage: Int
118133
): ArrayList<JSONObject> {
119134
val shareAPI = apiProvider.getShareAPI(applicationContext, account)
120-
val call = shareAPI.getSharees(search = searchString, page = page.toString(), perPage = perPage.toString())
135+
val call = shareAPI.getSharees(
136+
search = searchString,
137+
page = page.toString(),
138+
perPage = perPage.toString()
139+
)
121140
return if (call != null) {
122141
val respOCS = call["ocs"] as? LinkedTreeMap<*, *>
123142
val respData = respOCS?.get("data") as? LinkedTreeMap<*, *>
@@ -127,13 +146,15 @@ class ShareRepository(private val applicationContext: Context, private val accou
127146
val respExactGroups = respExact?.getList("groups")
128147
val respExactRemotes = respExact?.getList("remotes")
129148
val respExactEmails = respExact?.getList("emails")
130-
val respExactCircles = respExact?.takeIf { it.containsKey("circles") }?.getList("circles")
149+
val respExactCircles =
150+
respExact?.takeIf { it.containsKey("circles") }?.getList("circles")
131151
val respExactRooms = respExact?.takeIf { it.containsKey("rooms") }?.getList("rooms")
132152

133153
val respPartialUsers = respData?.getList("users")
134154
val respPartialGroups = respData?.getList("groups")
135155
val respPartialRemotes = respData?.getList("remotes")
136-
val respPartialCircles = respData?.takeIf { it.containsKey("circles") }?.getList("circles")
156+
val respPartialCircles =
157+
respData?.takeIf { it.containsKey("circles") }?.getList("circles")
137158
val respPartialRooms = respData?.takeIf { it.containsKey("rooms") }?.getList("rooms")
138159

139160
val jsonResults = listOfNotNull(
@@ -202,24 +223,15 @@ class ShareRepository(private val applicationContext: Context, private val accou
202223
)
203224
}
204225

205-
private fun getExpirationDate(chosenExpDateInMills: Long): String? {
206-
if (chosenExpDateInMills == -1L) {
207-
return null
208-
}
209-
210-
return Date(chosenExpDateInMills).toExpirationDateString()
211-
}
212-
213-
fun getCapabilities(): Capabilities = notesRepository.capabilities
214-
215226
fun getShares(remoteId: Long): List<OCShare>? {
216227
val shareAPI = apiProvider.getShareAPI(applicationContext, account)
217228
val call = shareAPI.getShares(remoteId)
218229
val response = call.execute()
219230

220231
return try {
221232
if (response.isSuccessful) {
222-
val result = response.body()?.ocs?.data ?: throw RuntimeException("No shares available")
233+
val result =
234+
response.body()?.ocs?.data ?: throw RuntimeException("No shares available")
223235
result.toOCShare()
224236
} else {
225237
Log_OC.d(tag, "Failed to getShares: ${response.errorBody()?.string()}")
@@ -233,7 +245,7 @@ class ShareRepository(private val applicationContext: Context, private val accou
233245

234246
fun sendEmail(shareId: Long, requestBody: SharePasswordRequest): Boolean {
235247
val shareAPI = apiProvider.getShareAPI(applicationContext, account)
236-
val call = shareAPI.sendEmail(shareId , requestBody)
248+
val call = shareAPI.sendEmail(shareId, requestBody)
237249
val response = call.execute()
238250

239251
return try {
@@ -309,7 +321,7 @@ class ShareRepository(private val applicationContext: Context, private val accou
309321
response.isSuccessful
310322
} catch (e: Exception) {
311323
Log_OC.d(tag, "Exception while updating share", e)
312-
false
324+
false
313325
}
314326
}
315327

@@ -331,7 +343,7 @@ class ShareRepository(private val applicationContext: Context, private val accou
331343
val notesSuffix = notesPathResponseResult.fileSuffix
332344

333345
val requestBody = CreateShareRequest(
334-
path = "/" + notesPath + "/" + note.title + notesSuffix,
346+
path = StringConstants.PATH + notesPath + StringConstants.PATH + note.title + notesSuffix,
335347
shareType = shareType.value,
336348
shareWith = shareWith,
337349
publicUpload = publicUpload,
@@ -343,7 +355,8 @@ class ShareRepository(private val applicationContext: Context, private val accou
343355
val shareAPI = apiProvider.getShareAPI(applicationContext, account)
344356
val call = shareAPI.addShare(request = requestBody)
345357
val response = call.execute()
346-
val defaultErrorMessage = applicationContext.getString(R.string.note_share_activity_cannot_created)
358+
val defaultErrorMessage =
359+
applicationContext.getString(R.string.note_share_activity_cannot_created)
347360

348361
return try {
349362
if (response.isSuccessful) {
@@ -385,4 +398,5 @@ class ShareRepository(private val applicationContext: Context, private val accou
385398
false
386399
}
387400
}
401+
// endregion
388402
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package it.niedermann.owncloud.notes.shared.util
2+
3+
object StringConstants {
4+
const val PATH = "/"
5+
}

0 commit comments

Comments
 (0)