use library, remove mock data

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-12-16 16:42:49 +01:00 committed by Alper Öztürk
parent ba9377fdae
commit 7353992e65
3 changed files with 58 additions and 71 deletions

View file

@ -31,6 +31,8 @@ import android.widget.LinearLayout;
import com.elyeproj.loaderviewlibrary.LoaderImageView;
import com.nextcloud.android.common.ui.theme.utils.ColorRole;
import com.nextcloud.android.lib.resources.recommendations.GetRecommendationsRemoteOperation;
import com.nextcloud.android.lib.resources.recommendations.Recommendation;
import com.nextcloud.client.account.User;
import com.nextcloud.client.database.entity.OfflineOperationEntity;
import com.nextcloud.client.jobs.upload.FileUploadHelper;
@ -82,7 +84,6 @@ import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
@ -145,6 +146,8 @@ public class OCFileListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol
private final long headerId = UUID.randomUUID().getLeastSignificantBits();
private final SyncedFolderProvider syncedFolderProvider;
private final ArrayList<Recommendation> recommendedFiles;
public OCFileListAdapter(
Activity activity,
@NonNull User user,
@ -154,7 +157,9 @@ public class OCFileListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol
OCFileListFragmentInterface ocFileListFragmentInterface,
boolean argHideItemOptions,
boolean gridView,
final ViewThemeUtils viewThemeUtils) {
final ViewThemeUtils viewThemeUtils,
final ArrayList<Recommendation> recommendedFiles) {
this.recommendedFiles = recommendedFiles;
this.ocFileListFragmentInterface = ocFileListFragmentInterface;
this.activity = activity;
this.preferences = preferences;
@ -412,6 +417,7 @@ public class OCFileListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol
);
}
case VIEW_TYPE_HEADER -> {
// TODO add height if recommended files is empty
ListHeaderBinding binding = ListHeaderBinding.inflate(
LayoutInflater.from(parent.getContext()),
parent,
@ -431,61 +437,20 @@ public class OCFileListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol
ocFileListFragmentInterface.isLoading() ? View.VISIBLE : View.GONE);
} else if (holder instanceof OCFileListHeaderViewHolder headerViewHolder) {
String text = currentDirectory.getRichWorkspace();
final var recommendedFiles = headerViewHolder.getBinding().recommendedFilesRecyclerView;
final LinearLayoutManager layoutManager = new LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false);
recommendedFiles.setLayoutManager(layoutManager);
// TODO use actual data
ArrayList<Recommendation> mockData = new ArrayList<>(Arrays.asList(
new Recommendation(
2124L,
System.currentTimeMillis(),
"Document1",
"/documents",
"pdf",
"application/pdf",
true,
"Recently opened"
),
new Recommendation(
2130L,
System.currentTimeMillis() - 3600000,
"Image1",
"/pictures",
"jpg",
"image/jpeg",
true,
"Frequently viewed"
),
new Recommendation(
2131L,
System.currentTimeMillis() - 7200000,
"Presentation1",
"/presentations",
"pptx",
"application/vnd.ms-powerpoint",
false,
"Shared with you"
),
new Recommendation(
2126L,
System.currentTimeMillis() - 7200000,
"Presentation1",
"/presentations",
"pptx",
"application/vnd.ms-powerpoint",
false,
"Shared with you"
))
);
final var adapter = new RecommendedFilesAdapter(activity, mockData, ocFileListDelegate, this, mStorageManager);
recommendedFiles.setAdapter(adapter);
PreviewTextFragment.setText(headerViewHolder.getHeaderText(), text, null, activity, true, true, viewThemeUtils);
headerViewHolder.getHeaderView().setOnClickListener(v -> ocFileListFragmentInterface.onHeaderClicked());
ViewExtensionsKt.setVisibleIf(headerViewHolder.getBinding().recommendedFilesLayout, !recommendedFiles.isEmpty());
if (!recommendedFiles.isEmpty()) {
final var recommendedFilesRecyclerView = headerViewHolder.getBinding().recommendedFilesRecyclerView;
final LinearLayoutManager layoutManager = new LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false);
recommendedFilesRecyclerView.setLayoutManager(layoutManager);
final var adapter = new RecommendedFilesAdapter(activity, recommendedFiles, ocFileListDelegate, this, mStorageManager);
recommendedFilesRecyclerView.setAdapter(adapter);
}
} else {
ListViewHolder gridViewHolder = (ListViewHolder) holder;
OCFile file = getItem(position);
@ -817,13 +782,15 @@ public class OCFileListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol
return false;
}
// TODO add or condition for recommended files
if (!recommendedFiles.isEmpty()) {
return true;
}
if (currentDirectory.getRichWorkspace() == null) {
return false;
}
return !TextUtils.isEmpty(currentDirectory.getRichWorkspace().trim()) || true;
return !TextUtils.isEmpty(currentDirectory.getRichWorkspace().trim());
}
/**

View file

@ -12,23 +12,12 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.nextcloud.android.lib.resources.recommendations.Recommendation
import com.owncloud.android.databinding.RecommendedFilesListItemBinding
import com.owncloud.android.datamodel.FileDataStorageManager
import com.owncloud.android.datamodel.OCFile
import com.owncloud.android.utils.DisplayUtils
// TODO delete mock data
data class Recommendation(
val id: Long,
val timestamp: Long,
val name: String,
val directory: String,
val extension: String,
val mimeType: String,
val hasPreview: Boolean,
val reason: String
)
class RecommendedFilesAdapter(
private val context: Context,
private val recommendations: List<Recommendation>,

View file

@ -15,6 +15,7 @@
package com.owncloud.android.ui.fragment;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
@ -41,6 +42,8 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.nextcloud.android.lib.resources.files.ToggleFileLockRemoteOperation;
import com.nextcloud.android.lib.resources.recommendations.GetRecommendationsRemoteOperation;
import com.nextcloud.android.lib.resources.recommendations.Recommendation;
import com.nextcloud.android.lib.richWorkspace.RichWorkspaceDirectEditingRemoteOperation;
import com.nextcloud.client.account.User;
import com.nextcloud.client.account.UserAccountManager;
@ -72,6 +75,8 @@ import com.owncloud.android.datamodel.VirtualFolderType;
import com.owncloud.android.datamodel.e2e.v2.decrypted.DecryptedFolderMetadataFile;
import com.owncloud.android.lib.common.Creator;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.OwnCloudClientFactory;
import com.owncloud.android.lib.common.accounts.AccountUtils;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC;
@ -151,6 +156,7 @@ import androidx.media3.common.util.UnstableApi;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import kotlin.collections.CollectionsKt;
import static com.owncloud.android.datamodel.OCFile.ROOT_PATH;
import static com.owncloud.android.ui.dialog.setupEncryption.SetupEncryptionDialogFragment.SETUP_ENCRYPTION_DIALOG_TAG;
@ -249,6 +255,7 @@ public class OCFileListFragment extends ExtendedListFragment implements
protected MenuItemAddRemove menuItemAddRemoveValue = MenuItemAddRemove.ADD_GRID_AND_SORT_WITH_SEARCH;
private List<MenuItem> mOriginalMenuItems = new ArrayList<>();
private ArrayList<Recommendation> recommendedFiles = new ArrayList<>();
@Override
public void onCreate(Bundle savedInstanceState) {
@ -383,7 +390,7 @@ public class OCFileListFragment extends ExtendedListFragment implements
mOnlyFoldersClickable = args != null && args.getBoolean(ARG_ONLY_FOLDERS_CLICKABLE, false);
mFileSelectable = args != null && args.getBoolean(ARG_FILE_SELECTABLE, false);
mLimitToMimeType = args != null ? args.getString(ARG_MIMETYPE, "") : "";
fetchRecommendedFiles();
setAdapter(args);
mHideFab = args != null && args.getBoolean(ARG_HIDE_FAB, false);
@ -432,6 +439,29 @@ public class OCFileListFragment extends ExtendedListFragment implements
listDirectory(MainApp.isOnlyOnDevice(), false);
}
private void fetchRecommendedFiles() {
new Thread(() -> {{
try {
User user = accountManager.getUser();
final var client = OwnCloudClientFactory.createNextcloudClient(user.toPlatformAccount(), requireActivity());
final var result = new GetRecommendationsRemoteOperation().execute(client);
if (result.isSuccess()) {
recommendedFiles.addAll(result.getResultData());
requireActivity().runOnUiThread(new Runnable() {
@SuppressLint("NotifyDataSetChanged")
@Override
public void run() {
mAdapter.notifyDataSetChanged();
}
});
}
} catch (Exception e) {
Log_OC.d(TAG,"Error caught at fetchRecommendedFiles");
}
}}).start();
}
protected void setAdapter(Bundle args) {
boolean hideItemOptions = args != null && args.getBoolean(ARG_HIDE_ITEM_OPTIONS, false);
@ -444,7 +474,8 @@ public class OCFileListFragment extends ExtendedListFragment implements
this,
hideItemOptions,
isGridViewPreferred(mFile),
viewThemeUtils
viewThemeUtils,
recommendedFiles
);
setRecyclerViewAdapter(mAdapter);