Migrate BrowserFile to kotlin data class

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
This commit is contained in:
Andy Scherzinger 2022-05-17 22:15:51 +02:00
parent 946df9a2bc
commit 66088a48ec
No known key found for this signature in database
GPG key ID: 6CADC7E3523C308B
7 changed files with 128 additions and 319 deletions

View file

@ -310,7 +310,7 @@ public abstract class MagicPreviewMessageViewHolder extends MessageHolders.Incom
new Handler(context.getMainLooper()).post(() -> {
int resourceId = DrawableUtils
.INSTANCE
.getDrawableResourceIdForMimeType(browserFileList.get(0).mimeType);
.getDrawableResourceIdForMimeType(browserFileList.get(0).getMimeType());
Drawable drawable = ContextCompat.getDrawable(context, resourceId);
image.getHierarchy().setPlaceholderImage(drawable);
});

View file

@ -129,7 +129,7 @@ public class BrowserFileItem extends AbstractFlexibleItem<BrowserFileItem.Browse
}
if (selectionInterface.shouldOnlySelectOneImageFile()) {
if (browserFile.isFile && browserFile.mimeType.startsWith("image/")) {
if (browserFile.isFile() && browserFile.getMimeType().startsWith("image/")) {
holder.binding.selectFileCheckbox.setVisibility(View.VISIBLE);
} else {
holder.binding.selectFileCheckbox.setVisibility(View.GONE);
@ -148,7 +148,7 @@ public class BrowserFileItem extends AbstractFlexibleItem<BrowserFileItem.Browse
context, DrawableUtils.INSTANCE.getDrawableResourceIdForMimeType(browserFile.getMimeType())));
}
if (browserFile.isHasPreview()) {
if (browserFile.getHasPreview()) {
String path = ApiUtils.getUrlForFilePreviewWithRemotePath(activeUser.getBaseUrl(),
browserFile.getPath(),
context.getResources().getDimensionPixelSize(R.dimen.small_item_height));

View file

@ -198,7 +198,7 @@ abstract class BrowserController(args: Bundle) :
recyclerViewItems = ArrayList()
if (davResponse.getData() != null) {
val objectList = davResponse.getData() as List<BrowserFile>
currentPath = objectList[0].getPath()
currentPath = objectList[0].path!!
if (activity != null) {
activity!!.runOnUiThread { setTitle() }
}
@ -256,8 +256,8 @@ abstract class BrowserController(args: Bundle) :
override fun onItemClick(view: View, position: Int): Boolean {
val browserFile = (adapter!!.getItem(position) as BrowserFileItem).model
if ("inode/directory" == browserFile.getMimeType()) {
fetchPath(browserFile.getPath())
if ("inode/directory" == browserFile.mimeType) {
fetchPath(browserFile.path!!)
return true
}
return false

View file

@ -1,309 +0,0 @@
/*
* Nextcloud Talk application
*
* @author Mario Danic
* Copyright (C) 2017-2018 Mario Danic <mario@lovelyhq.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.talk.components.filebrowser.models;
import android.net.Uri;
import android.text.TextUtils;
import com.bluelinelabs.logansquare.annotation.JsonObject;
import com.nextcloud.talk.components.filebrowser.models.properties.NCEncrypted;
import com.nextcloud.talk.components.filebrowser.models.properties.NCPermission;
import com.nextcloud.talk.components.filebrowser.models.properties.NCPreview;
import com.nextcloud.talk.components.filebrowser.models.properties.OCFavorite;
import com.nextcloud.talk.components.filebrowser.models.properties.OCId;
import com.nextcloud.talk.components.filebrowser.models.properties.OCSize;
import org.parceler.Parcel;
import java.io.File;
import java.util.List;
import at.bitfire.dav4jvm.Property;
import at.bitfire.dav4jvm.Response;
import at.bitfire.dav4jvm.property.DisplayName;
import at.bitfire.dav4jvm.property.GetContentType;
import at.bitfire.dav4jvm.property.GetLastModified;
import at.bitfire.dav4jvm.property.ResourceType;
@JsonObject
@Parcel
public class BrowserFile {
public String path;
public String displayName;
public String mimeType;
public long modifiedTimestamp;
public long size;
public boolean isFile;
// Used for remote files
public String remoteId;
public boolean hasPreview;
public boolean favorite;
public boolean encrypted;
public String permissions;
private boolean isAllowedToReShare = false;
public static BrowserFile getModelFromResponse(Response response, String remotePath) {
BrowserFile browserFile = new BrowserFile();
browserFile.setPath(Uri.decode(remotePath));
browserFile.setDisplayName(Uri.decode(new File(remotePath).getName()));
final List<Property> properties = response.getProperties();
for (Property property : properties) {
if (property instanceof OCId) {
browserFile.setRemoteId(((OCId) property).getOcId());
}
if (property instanceof ResourceType) {
browserFile.isFile =
!(((ResourceType) property).getTypes().contains(ResourceType.Companion.getCOLLECTION()));
}
if (property instanceof GetLastModified) {
browserFile.setModifiedTimestamp(((GetLastModified) property).getLastModified());
}
if (property instanceof GetContentType) {
browserFile.setMimeType(((GetContentType) property).getType());
}
if (property instanceof OCSize) {
browserFile.setSize(((OCSize) property).getOcSize());
}
if (property instanceof NCPreview) {
browserFile.setHasPreview(((NCPreview) property).isNcPreview());
}
if (property instanceof OCFavorite) {
browserFile.setFavorite(((OCFavorite) property).isOcFavorite());
}
if (property instanceof DisplayName) {
browserFile.setDisplayName(((DisplayName) property).getDisplayName());
}
if (property instanceof NCEncrypted) {
browserFile.setEncrypted(((NCEncrypted) property).isNcEncrypted());
}
if (property instanceof NCPermission) {
browserFile.setPermissions(((NCPermission) property).getNcPermission());
}
}
if (browserFile.getPermissions() != null && browserFile.getPermissions().contains("R")) {
browserFile.isAllowedToReShare = true;
}
if (TextUtils.isEmpty(browserFile.getMimeType()) && !browserFile.isFile()) {
browserFile.setMimeType("inode/directory");
}
return browserFile;
}
public String getPath() {
return this.path;
}
public String getDisplayName() {
return this.displayName;
}
public String getMimeType() {
return this.mimeType;
}
public long getModifiedTimestamp() {
return this.modifiedTimestamp;
}
public long getSize() {
return this.size;
}
public boolean isFile() {
return this.isFile;
}
public String getRemoteId() {
return this.remoteId;
}
public boolean isHasPreview() {
return this.hasPreview;
}
public boolean isFavorite() {
return this.favorite;
}
public boolean isEncrypted() {
return this.encrypted;
}
public String getPermissions() {
return this.permissions;
}
public boolean isAllowedToReShare() {
return this.isAllowedToReShare;
}
public void setPath(String path) {
this.path = path;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public void setMimeType(String mimeType) {
this.mimeType = mimeType;
}
public void setModifiedTimestamp(long modifiedTimestamp) {
this.modifiedTimestamp = modifiedTimestamp;
}
public void setSize(long size) {
this.size = size;
}
public void setFile(boolean isFile) {
this.isFile = isFile;
}
public void setRemoteId(String remoteId) {
this.remoteId = remoteId;
}
public void setHasPreview(boolean hasPreview) {
this.hasPreview = hasPreview;
}
public void setFavorite(boolean favorite) {
this.favorite = favorite;
}
public void setEncrypted(boolean encrypted) {
this.encrypted = encrypted;
}
public void setPermissions(String permissions) {
this.permissions = permissions;
}
public void setAllowedToReShare(boolean isAllowedToReShare) {
this.isAllowedToReShare = isAllowedToReShare;
}
public boolean equals(final Object o) {
if (o == this) {
return true;
}
if (!(o instanceof BrowserFile)) {
return false;
}
final BrowserFile other = (BrowserFile) o;
if (!other.canEqual((Object) this)) {
return false;
}
final Object this$path = this.getPath();
final Object other$path = other.getPath();
if (this$path == null ? other$path != null : !this$path.equals(other$path)) {
return false;
}
final Object this$displayName = this.getDisplayName();
final Object other$displayName = other.getDisplayName();
if (this$displayName == null ? other$displayName != null : !this$displayName.equals(other$displayName)) {
return false;
}
final Object this$mimeType = this.getMimeType();
final Object other$mimeType = other.getMimeType();
if (this$mimeType == null ? other$mimeType != null : !this$mimeType.equals(other$mimeType)) {
return false;
}
if (this.getModifiedTimestamp() != other.getModifiedTimestamp()) {
return false;
}
if (this.getSize() != other.getSize()) {
return false;
}
if (this.isFile() != other.isFile()) {
return false;
}
final Object this$remoteId = this.getRemoteId();
final Object other$remoteId = other.getRemoteId();
if (this$remoteId == null ? other$remoteId != null : !this$remoteId.equals(other$remoteId)) {
return false;
}
if (this.isHasPreview() != other.isHasPreview()) {
return false;
}
if (this.isFavorite() != other.isFavorite()) {
return false;
}
if (this.isEncrypted() != other.isEncrypted()) {
return false;
}
final Object this$permissions = this.getPermissions();
final Object other$permissions = other.getPermissions();
if (this$permissions == null ? other$permissions != null : !this$permissions.equals(other$permissions)) {
return false;
}
return this.isAllowedToReShare() == other.isAllowedToReShare();
}
protected boolean canEqual(final Object other) {
return other instanceof BrowserFile;
}
public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $path = this.getPath();
result = result * PRIME + ($path == null ? 43 : $path.hashCode());
final Object $displayName = this.getDisplayName();
result = result * PRIME + ($displayName == null ? 43 : $displayName.hashCode());
final Object $mimeType = this.getMimeType();
result = result * PRIME + ($mimeType == null ? 43 : $mimeType.hashCode());
final long $modifiedTimestamp = this.getModifiedTimestamp();
result = result * PRIME + (int) ($modifiedTimestamp >>> 32 ^ $modifiedTimestamp);
final long $size = this.getSize();
result = result * PRIME + (int) ($size >>> 32 ^ $size);
result = result * PRIME + (this.isFile() ? 79 : 97);
final Object $remoteId = this.getRemoteId();
result = result * PRIME + ($remoteId == null ? 43 : $remoteId.hashCode());
result = result * PRIME + (this.isHasPreview() ? 79 : 97);
result = result * PRIME + (this.isFavorite() ? 79 : 97);
result = result * PRIME + (this.isEncrypted() ? 79 : 97);
final Object $permissions = this.getPermissions();
result = result * PRIME + ($permissions == null ? 43 : $permissions.hashCode());
result = result * PRIME + (this.isAllowedToReShare() ? 79 : 97);
return result;
}
public String toString() {
return "BrowserFile(path=" + this.getPath() + ", displayName=" + this.getDisplayName() + ", mimeType=" + this.getMimeType() + ", modifiedTimestamp=" + this.getModifiedTimestamp() + ", size=" + this.getSize() + ", isFile=" + this.isFile() + ", remoteId=" + this.getRemoteId() + ", hasPreview=" + this.isHasPreview() + ", favorite=" + this.isFavorite() + ", encrypted=" + this.isEncrypted() + ", permissions=" + this.getPermissions() + ", isAllowedToReShare=" + this.isAllowedToReShare() + ")";
}
}

View file

@ -0,0 +1,118 @@
/*
* Nextcloud Talk application
*
* @author Mario Danic
* Copyright (C) 2017-2018 Mario Danic <mario@lovelyhq.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.talk.components.filebrowser.models
import android.net.Uri
import android.os.Parcelable
import android.text.TextUtils
import at.bitfire.dav4jvm.Property
import at.bitfire.dav4jvm.Response
import at.bitfire.dav4jvm.property.DisplayName
import at.bitfire.dav4jvm.property.GetContentType
import at.bitfire.dav4jvm.property.GetLastModified
import at.bitfire.dav4jvm.property.ResourceType
import at.bitfire.dav4jvm.property.ResourceType.Companion.COLLECTION
import com.bluelinelabs.logansquare.annotation.JsonObject
import com.nextcloud.talk.components.filebrowser.models.properties.NCEncrypted
import com.nextcloud.talk.components.filebrowser.models.properties.NCPermission
import com.nextcloud.talk.components.filebrowser.models.properties.NCPreview
import com.nextcloud.talk.components.filebrowser.models.properties.OCFavorite
import com.nextcloud.talk.components.filebrowser.models.properties.OCId
import com.nextcloud.talk.components.filebrowser.models.properties.OCSize
import kotlinx.android.parcel.Parcelize
import java.io.File
@Parcelize
@JsonObject
data class BrowserFile(
var path: String? = null,
var displayName: String? = null,
var mimeType: String? = null,
var modifiedTimestamp: Long = 0,
var size: Long = 0,
var isFile: Boolean = false,
// Used for remote files
var remoteId: String? = null,
var hasPreview: Boolean = false,
var isFavorite: Boolean = false,
var isEncrypted: Boolean = false,
var permissions: String? = null,
var isAllowedToReShare: Boolean = false
) : Parcelable {
// This constructor is added to work with the 'com.bluelinelabs.logansquare.annotation.JsonObject'
constructor() : this(null, null, null, 0, 0, false, null, false, false, false, null, false)
companion object {
fun getModelFromResponse(response: Response, remotePath: String): BrowserFile {
val browserFile = BrowserFile()
browserFile.path = Uri.decode(remotePath)
browserFile.displayName = Uri.decode(File(remotePath).name)
val properties = response.properties
for (property in properties) {
mapPropertyToBrowserFile(property, browserFile)
}
if (browserFile.permissions != null && browserFile.permissions!!.contains("R")) {
browserFile.isAllowedToReShare = true
}
if (TextUtils.isEmpty(browserFile.mimeType) && !browserFile.isFile) {
browserFile.mimeType = "inode/directory"
}
return browserFile
}
@Suppress("Detekt.ComplexMethod")
private fun mapPropertyToBrowserFile(property: Property, browserFile: BrowserFile) {
when (property) {
is OCId -> {
browserFile.remoteId = property.ocId
}
is ResourceType -> {
browserFile.isFile = !property.types.contains(COLLECTION)
}
is GetLastModified -> {
browserFile.modifiedTimestamp = property.lastModified
}
is GetContentType -> {
browserFile.mimeType = property.type
}
is OCSize -> {
browserFile.size = property.ocSize
}
is NCPreview -> {
browserFile.hasPreview = property.isNcPreview
}
is OCFavorite -> {
browserFile.isFavorite = property.isOcFavorite
}
is DisplayName -> {
browserFile.displayName = property.displayName
}
is NCEncrypted -> {
browserFile.isEncrypted = property.isNcEncrypted
}
is NCPermission -> {
browserFile.permissions = property.ncPermission
}
}
}
}
}

View file

@ -94,10 +94,10 @@ public class ReadFilesystemOperation {
Log.w("", "Error reading remote path");
}
remoteFiles.add(BrowserFile.getModelFromResponse(rootElement[0],
remoteFiles.add(BrowserFile.Companion.getModelFromResponse(rootElement[0],
rootElement[0].getHref().toString().substring(basePath.length())));
for (Response memberElement : memberElements) {
remoteFiles.add(BrowserFile.getModelFromResponse(memberElement,
remoteFiles.add(BrowserFile.Companion.getModelFromResponse(memberElement,
memberElement.getHref().toString().substring(basePath.length())));
}

View file

@ -88,8 +88,8 @@ public class AlphanumComparator<T> implements Comparator<T>, Serializable {
}
public int compare(BrowserFileItem f1, BrowserFileItem f2) {
String s1 = f1.getModel().path;
String s2 = f2.getModel().path;
String s1 = f1.getModel().getPath();
String s2 = f2.getModel().getPath();
return compare(s1, s2);
}