1
1
package com.amrdeveloper.linkhub.data.parser
2
2
3
3
import com.amrdeveloper.linkhub.data.DataPackage
4
+ import com.amrdeveloper.linkhub.data.Folder
4
5
import com.amrdeveloper.linkhub.data.FolderColor
5
6
import com.amrdeveloper.linkhub.data.ImportExportFileType
7
+ import com.amrdeveloper.linkhub.data.Link
8
+ import com.amrdeveloper.linkhub.data.Theme
6
9
import com.amrdeveloper.linkhub.data.source.FolderRepository
7
10
import com.amrdeveloper.linkhub.data.source.LinkRepository
8
11
import com.amrdeveloper.linkhub.util.UiPreferences
9
12
import com.google.gson.Gson
13
+ import com.google.gson.JsonParser
10
14
import com.google.gson.JsonSyntaxException
11
15
12
16
class JsonImportExportFileParser : ImportExportFileParser {
@@ -17,20 +21,53 @@ class JsonImportExportFileParser : ImportExportFileParser {
17
21
linkRepository : LinkRepository
18
22
): Result <DataPackage ?> {
19
23
try {
20
- val dataPackage = Gson ().fromJson(data, DataPackage ::class .java)
24
+ val jsonElement = JsonParser .parseString(data)
25
+ if (! jsonElement.isJsonObject) {
26
+ return Result .success(null )
27
+ }
28
+
29
+ val jsonObject = jsonElement.asJsonObject
30
+ val jsonObjectKeys = jsonObject.keySet()
31
+
32
+ // TODO: Replace with reflection later
33
+ val validKeysSet = setOf (" folders" , " links" , " showClickCounter" , " autoSaving" , " defaultFolderMode" , " theme" )
34
+ if (jsonObjectKeys.size != validKeysSet.size) {
35
+ return Result .success(null )
36
+ }
37
+
38
+ val gson = Gson ()
39
+ var dataPackage = DataPackage ()
40
+
41
+ if (jsonObjectKeys == validKeysSet) {
42
+ dataPackage = gson.fromJson(data, DataPackage ::class .java)
43
+ } else {
44
+ val keysAsList = jsonObjectKeys.toList()
45
+ jsonObject.getAsJsonArray(keysAsList[0 ])?.let { jsonLinks ->
46
+ dataPackage.folders = gson.fromJson(jsonLinks, Array <Folder >::class .java).toList()
47
+ }
48
+
49
+ jsonObject.getAsJsonArray(keysAsList[1 ])?.let { jsonLinks ->
50
+ dataPackage.links = gson.fromJson(jsonLinks, Array <Link >::class .java).toList()
51
+ }
52
+
53
+ dataPackage.showClickCounter = jsonObject[keysAsList[2 ]].asBoolean
54
+ dataPackage.enableAutoSaving = jsonObject[keysAsList[3 ]].asBoolean
55
+ dataPackage.defaultFolderMode = jsonObject[keysAsList[4 ]].asBoolean
56
+
57
+ jsonObject[keysAsList[5 ]]?.asString?.let { jsonTheme ->
58
+ dataPackage.theme = Theme .entries.firstOrNull { it.name.equals(jsonTheme, ignoreCase = true ) }
59
+ }
60
+ }
21
61
22
- val folders = dataPackage.folders
23
62
// This code should be removed after found why it not serialized on some devices (see Issue #23)
24
63
// folderColor field is declared as non nullable type but in this case GSON will break the null safety feature
25
- folders.forEach { if (it.folderColor == null ) it.folderColor = FolderColor .BLUE }
26
- folderRepository.insertFolders(folders)
64
+ dataPackage.folders?.forEach { if (it.folderColor == null ) it.folderColor = FolderColor .BLUE }
27
65
28
- linkRepository.insertLinks(dataPackage.links)
66
+ dataPackage.folders?.let { folderRepository.insertFolders(it) }
67
+ dataPackage.links?.let { linkRepository.insertLinks(it) }
29
68
return Result .success(dataPackage)
30
69
} catch (e: JsonSyntaxException ) {
31
70
return Result .failure(e)
32
- } catch (e: Exception ) {
33
- return Result .failure(e)
34
71
}
35
72
}
36
73
0 commit comments