-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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: alperozturk <[email protected]>
- Loading branch information
1 parent
8e37c1d
commit 14e6b75
Showing
7 changed files
with
258 additions
and
27 deletions.
There are no files selected for viewing
131 changes: 131 additions & 0 deletions
131
app/src/androidTest/java/com/nextcloud/extensions/OfflineOperationExtensionTests.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,131 @@ | ||
/* | ||
* Nextcloud - Android Client | ||
* | ||
* SPDX-FileCopyrightText: 2024 Alper Ozturk <[email protected]> | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
package com.nextcloud.extensions | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.nextcloud.client.database.entity.OfflineOperationEntity | ||
import com.nextcloud.utils.extensions.getParentPathFromPath | ||
import com.nextcloud.utils.extensions.updatePath | ||
import com.nextcloud.utils.extensions.updatePathsIfParentPathMatches | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertNull | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class OfflineOperationExtensionTests { | ||
|
||
/* | ||
private val entity = OfflineOperationEntity( | ||
1, | ||
null, | ||
null, | ||
OfflineOperationType.CreateFolder, | ||
"/Folder/Folder2/Folder3/", | ||
"Folder", | ||
null | ||
) | ||
@Test | ||
fun testGetParentPathFromPath() { | ||
assert(entity.getParentPathFromPath() == "/Folder/") | ||
} | ||
@Test | ||
fun testUpdatePath() { | ||
assert(entity.updatePath("/MaxPa/") == "/MaxPa/Folder2/Folder3/") | ||
} | ||
@Test | ||
fun testUpdatePathsIfParentPathMatches() { | ||
entity.path = "/MaxPa/Folder2/Folder3/" | ||
val oldPath = "/Folder/Folder2/Folder3/" | ||
assert(entity.updatePathsIfParentPathMatches(oldPath, "/MaxPa/") == "/MaxPa/Folder2/Folder3/") | ||
} | ||
*/ | ||
|
||
@Test | ||
fun testUpdatePathsIfParentPathMatches() { | ||
val entity1 = OfflineOperationEntity(path = "/abc/def/file1/") | ||
val entity2 = OfflineOperationEntity(path = "/xyz/file2/") | ||
|
||
val updatedPath1 = entity1.updatePathsIfParentPathMatches(entity1.path, "/newAbc/") | ||
val updatedPath2 = entity2.updatePathsIfParentPathMatches(entity2.path, "/newAbc/") | ||
|
||
assertEquals("/newAbc/def/file1/", updatedPath1) | ||
assertEquals("/newAbc/file2/", updatedPath2) | ||
} | ||
|
||
@Test | ||
fun testUpdatePathsIfParentPathMatchesWithNullInputs() { | ||
val entity = OfflineOperationEntity(path = "/abc/def/file/") | ||
|
||
val result1 = entity.updatePathsIfParentPathMatches(null, "/newPath/") | ||
val result2 = entity.updatePathsIfParentPathMatches("/oldPath/", null) | ||
|
||
assertNull(result1) | ||
assertNull(result2) | ||
} | ||
|
||
@Test | ||
fun testUpdatePath() { | ||
val entity = OfflineOperationEntity(path = "/abc/def/file/") | ||
val newPath = entity.updatePath("/newAbc/") | ||
assertEquals("/newAbc/def/file/", newPath) | ||
} | ||
|
||
@Test | ||
fun testUpdatePathWithNullInput() { | ||
val entity = OfflineOperationEntity(path = "/abc/def/file/") | ||
val newPath = entity.updatePath(null) | ||
assertNull(newPath) | ||
} | ||
|
||
@Test | ||
fun testGetParentPathFromPath() { | ||
val entity = OfflineOperationEntity(path = "/abc/def/file/") | ||
val parentPath = entity.getParentPathFromPath() | ||
assertEquals("/abc/", parentPath) | ||
} | ||
|
||
@Test | ||
fun testGetParentPathFromPathWithRootPath() { | ||
val entity = OfflineOperationEntity(path = "/") | ||
val parentPath = entity.getParentPathFromPath() | ||
assertEquals("//", parentPath) | ||
} | ||
|
||
@Test | ||
fun testGetParentPathFromPathWithEmptyString() { | ||
val entity = OfflineOperationEntity(path = "") | ||
val parentPath = entity.getParentPathFromPath() | ||
assertEquals("//", parentPath) | ||
} | ||
|
||
@Test | ||
fun testGetParentPathFromPathWithNullPath() { | ||
val entity = OfflineOperationEntity(path = null) | ||
val parentPath = entity.getParentPathFromPath() | ||
assertNull(parentPath) | ||
} | ||
|
||
@Test | ||
fun testUpdatePathWithEmptyPath() { | ||
val entity = OfflineOperationEntity(path = "") | ||
val newPath = entity.updatePath("/newAbc/") | ||
assertNull(newPath) | ||
} | ||
|
||
@Test | ||
fun testUpdatePathsIfParentPathMatchesWithSingleDirectoryPath() { | ||
val entity = OfflineOperationEntity(path = "/abc/") | ||
val updatedPath = entity.updatePathsIfParentPathMatches(entity.path, "/newAbc/") | ||
assertEquals("/newAbc/", updatedPath) | ||
} | ||
} |
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
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
24 changes: 24 additions & 0 deletions
24
app/src/main/java/com/nextcloud/utils/extensions/DateExtensions.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,24 @@ | ||
/* | ||
* Nextcloud - Android Client | ||
* | ||
* SPDX-FileCopyrightText: 2024 Alper Ozturk <[email protected]> | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
package com.nextcloud.utils.extensions | ||
|
||
import android.annotation.SuppressLint | ||
import java.text.SimpleDateFormat | ||
import java.util.Date | ||
|
||
enum class DateFormatPattern(val pattern: String) { | ||
/** | ||
* e.g. 10.11.2024 - 12:44 | ||
*/ | ||
FullDateWithHours("dd.MM.yyyy - HH:mm") | ||
} | ||
|
||
@SuppressLint("SimpleDateFormat") | ||
fun Date.currentDateRepresentation(formatPattern: DateFormatPattern): String { | ||
return SimpleDateFormat(formatPattern.pattern).format(this) | ||
} |
61 changes: 61 additions & 0 deletions
61
app/src/main/java/com/nextcloud/utils/extensions/OfflineOperationExtensions.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,61 @@ | ||
/* | ||
* Nextcloud - Android Client | ||
* | ||
* SPDX-FileCopyrightText: 2024 Alper Ozturk <[email protected]> | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
package com.nextcloud.utils.extensions | ||
|
||
import com.nextcloud.client.database.dao.OfflineOperationDao | ||
import com.nextcloud.client.database.entity.OfflineOperationEntity | ||
|
||
fun OfflineOperationDao.updatePathsIfParentPathMatches(oldPath: String?, newTopDir: String?) { | ||
if (oldPath.isNullOrEmpty() || newTopDir.isNullOrEmpty()) return | ||
|
||
getAll().forEach { | ||
val newPath = it.updatePathsIfParentPathMatches(oldPath, newTopDir) | ||
if (newPath != it.path) { | ||
|
||
// TODO add parent path so it can upload | ||
it.path = newPath | ||
update(it) | ||
} | ||
} | ||
} | ||
|
||
fun OfflineOperationEntity.updatePathsIfParentPathMatches(oldPath: String?, newTopDir: String?): String? { | ||
if (oldPath.isNullOrEmpty() || newTopDir.isNullOrEmpty()) return null | ||
|
||
val topDir = getParentPathFromPath() | ||
val oldTopDir = oldPath.getParentPathFromPath() | ||
return if (topDir == oldTopDir) { | ||
updatePath(newTopDir) | ||
} else { | ||
path | ||
} | ||
} | ||
|
||
fun OfflineOperationEntity.updatePath(newParentPath: String?): String? { | ||
if (newParentPath.isNullOrEmpty() || path.isNullOrEmpty()) return null | ||
|
||
val segments = path!!.trim('/').split('/').toMutableList() | ||
|
||
if (segments.size == 1) { | ||
return newParentPath | ||
} | ||
|
||
segments.removeAt(0) | ||
|
||
return newParentPath + segments.joinToString(separator = "/") + "/" | ||
} | ||
|
||
fun OfflineOperationEntity.getParentPathFromPath(): String? { | ||
return path?.getParentPathFromPath() | ||
} | ||
|
||
private fun String?.getParentPathFromPath(): String { | ||
val trimmedPath = this?.trim('/') | ||
val firstDir = trimmedPath?.split('/')?.firstOrNull() ?: "" | ||
return "/$firstDir/" | ||
} |
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