Skip to content

Commit

Permalink
Use interfaceSerializer
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 committed Dec 21, 2023
1 parent 145d6d4 commit 7150507
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.work.Worker
import androidx.work.WorkerParameters
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.nextcloud.client.account.User
import com.nextcloud.client.account.UserAccountManager
import com.nextcloud.java.util.Optional
import com.nextcloud.utils.InterfaceSerializer
import com.nextcloud.utils.InterfaceSerializer.interfaceSerializer
import com.owncloud.android.R
import com.owncloud.android.authentication.AuthenticatorActivity
import com.owncloud.android.datamodel.FileDataStorageManager
Expand Down Expand Up @@ -85,7 +88,6 @@ class FilesDownloadWorker(
companion object {
private val TAG = FilesDownloadWorker::class.java.simpleName

var user: User? = null
const val USER = "USER"
const val FILE = "FILE"
const val BEHAVIOUR = "BEHAVIOUR"
Expand Down Expand Up @@ -144,9 +146,17 @@ class FilesDownloadWorker(
}
}

// FIXME stackoverflow
private fun getUserGson(): Gson {
return GsonBuilder()
.registerTypeAdapter(User::class.java, interfaceSerializer(User::class.java))
.create()
}

private fun getRequestDownloads(): AbstractList<String> {
conflictUploadId = inputData.keyValueMap[CONFLICT_UPLOAD_ID] as Long?
val file = gson.fromJson(inputData.keyValueMap[FILE] as String, OCFile::class.java)
val user = getUserGson().fromJson(inputData.keyValueMap[USER] as String, User::class.java)
val downloadTypeAsString = inputData.keyValueMap[DOWNLOAD_TYPE] as String?
val downloadType = if (downloadTypeAsString != null) {
if (downloadTypeAsString == DownloadType.DOWNLOAD.toString()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,9 +473,8 @@ internal class BackgroundJobManagerImpl(
) {
val gson = Gson()

// FIXME user interface cant serialize and deserialize
val data = workDataOf(
//FilesDownloadWorker.USER to gson.toJson(user),
FilesDownloadWorker.USER to gson.toJson(user),
FilesDownloadWorker.FILE to gson.toJson(ocFile),
FilesDownloadWorker.BEHAVIOUR to behaviour,
FilesDownloadWorker.DOWNLOAD_TYPE to downloadType.toString(),
Expand All @@ -484,8 +483,6 @@ internal class BackgroundJobManagerImpl(
FilesDownloadWorker.CONFLICT_UPLOAD_ID to conflictUploadId,
)

FilesDownloadWorker.user = user

val request = oneTimeRequestBuilder(FilesDownloadWorker::class, JOB_FILES_DOWNLOAD, user)
.setInputData(data)
.build()
Expand Down
56 changes: 56 additions & 0 deletions app/src/main/java/com/nextcloud/utils/InterfaceSerializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Nextcloud Android client application
*
* @author Alper Ozturk
* Copyright (C) 2023 Alper Ozturk
* Copyright (C) 2023 Nextcloud GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.nextcloud.utils;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import java.lang.reflect.Type;

final public class InterfaceSerializer<T> implements JsonSerializer<T>, JsonDeserializer<T> {

private final Class<T> implementationClass;

private InterfaceSerializer(final Class<T> implementationClass) {
this.implementationClass = implementationClass;
}

public static <T> InterfaceSerializer<T> interfaceSerializer(final Class<T> implementationClass) {
return new InterfaceSerializer<>(implementationClass);
}

@Override
public JsonElement serialize(final T value, final Type type, final JsonSerializationContext context) {
final Type targetType = value != null
? value.getClass()
: type;
return context.serialize(value, targetType);
}

@Override
public T deserialize(final JsonElement jsonElement, final Type typeOfT, final JsonDeserializationContext context) {
return context.deserialize(jsonElement, implementationClass);
}
}

0 comments on commit 7150507

Please sign in to comment.