From 7353992e65a7e3408dcdc2caca507622e830e159 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Mon, 16 Dec 2024 16:42:49 +0100 Subject: [PATCH] use library, remove mock data Signed-off-by: alperozturk --- .../android/ui/adapter/OCFileListAdapter.java | 81 ++++++------------- .../ui/adapter/RecommendedFilesAdapter.kt | 13 +-- .../ui/fragment/OCFileListFragment.java | 35 +++++++- 3 files changed, 58 insertions(+), 71 deletions(-) diff --git a/app/src/main/java/com/owncloud/android/ui/adapter/OCFileListAdapter.java b/app/src/main/java/com/owncloud/android/ui/adapter/OCFileListAdapter.java index 3643fc0ea7..cb5663f13f 100644 --- a/app/src/main/java/com/owncloud/android/ui/adapter/OCFileListAdapter.java +++ b/app/src/main/java/com/owncloud/android/ui/adapter/OCFileListAdapter.java @@ -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 recommendedFiles; + public OCFileListAdapter( Activity activity, @NonNull User user, @@ -154,7 +157,9 @@ public class OCFileListAdapter extends RecyclerView.Adapter recommendedFiles) { + this.recommendedFiles = recommendedFiles; this.ocFileListFragmentInterface = ocFileListFragmentInterface; this.activity = activity; this.preferences = preferences; @@ -412,6 +417,7 @@ public class OCFileListAdapter extends RecyclerView.Adapter { + // 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 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, diff --git a/app/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java b/app/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java index 7e4be1179a..4a19999267 100644 --- a/app/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java +++ b/app/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java @@ -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 mOriginalMenuItems = new ArrayList<>(); + private ArrayList 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);