Skip to content

Commit cbd1608

Browse files
committed
fix(file-list): when user opens file from unified search and press back
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent 79c73bf commit cbd1608

5 files changed

Lines changed: 88 additions & 47 deletions

File tree

app/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.kt

Lines changed: 59 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ class FileDisplayActivity :
278278
observeWorkerState()
279279
startMetadataSyncForRoot()
280280
handleBackPress()
281+
registerUnifiedSearchReceiver()
281282
}
282283

283284
private fun loadSavedInstanceState(savedInstanceState: Bundle?) {
@@ -731,7 +732,13 @@ class FileDisplayActivity :
731732
val transaction = fragmentManager.beginTransaction()
732733
transaction.addToBackStack(null)
733734
transaction.replace(R.id.left_fragment_container, fragment, TAG_LIST_OF_FILES)
734-
transaction.commit()
735+
736+
if (fragmentManager.isStateSaved) {
737+
transaction.commitAllowingStateLoss()
738+
} else {
739+
transaction.commit()
740+
}
741+
735742
callback.onComplete(true)
736743
} else {
737744
callback.onComplete(false)
@@ -1953,7 +1960,7 @@ class FileDisplayActivity :
19531960
else -> VirtualFolderType.NONE
19541961
}
19551962

1956-
startImagePreview(file, type, file.isDown)
1963+
startImagePreview(file, file.isDown, type)
19571964
} else {
19581965
startImagePreview(file, file.isDown)
19591966
}
@@ -2482,44 +2489,33 @@ class FileDisplayActivity :
24822489
}
24832490
}
24842491

2485-
fun startImagePreview(file: OCFile, showPreview: Boolean) {
2486-
val showDetailsIntent = Intent(this, PreviewImageActivity::class.java)
2487-
showDetailsIntent.putExtra(EXTRA_FILE, file)
2488-
showDetailsIntent.putExtra(EXTRA_LIVE_PHOTO_FILE, file.livePhotoVideo)
2489-
showDetailsIntent.putExtra(
2490-
EXTRA_USER,
2491-
user.orElseThrow(Supplier { RuntimeException() })
2492-
)
2493-
if (showPreview) {
2494-
startActivity(showDetailsIntent)
2495-
} else {
2496-
val fileOperationsHelper =
2497-
FileOperationsHelper(this, userAccountManager, connectivityService, editorUtils)
2498-
fileOperationsHelper.startSyncForFileAndIntent(file, showDetailsIntent)
2492+
fun startImagePreview(file: OCFile, preview: Boolean, type: VirtualFolderType? = null) {
2493+
val optionalUser = user
2494+
if (optionalUser.isEmpty) {
2495+
Log_OC.e(TAG, "user is empty cannot preview image")
2496+
return
24992497
}
2500-
}
25012498

2502-
fun startImagePreview(file: OCFile, type: VirtualFolderType?, showPreview: Boolean) {
2503-
val showDetailsIntent = Intent(this, PreviewImageActivity::class.java)
2504-
showDetailsIntent.putExtra(EXTRA_FILE, file)
2505-
showDetailsIntent.putExtra(EXTRA_LIVE_PHOTO_FILE, file.livePhotoVideo)
2506-
showDetailsIntent.putExtra(
2507-
EXTRA_USER,
2508-
user.orElseThrow(Supplier { RuntimeException() })
2509-
)
2510-
showDetailsIntent.putExtra(PreviewImageActivity.EXTRA_VIRTUAL_TYPE, type)
2499+
val intent = Intent(this, PreviewImageActivity::class.java).apply {
2500+
putExtra(EXTRA_FILE, file)
2501+
putExtra(EXTRA_LIVE_PHOTO_FILE, file.livePhotoVideo)
2502+
putExtra(EXTRA_USER, optionalUser.get())
2503+
putExtra(PreviewImageActivity.EXTRA_VIRTUAL_TYPE, type)
2504+
putExtra(PreviewImageActivity.EXTRA_LAST_SEARCH_QUERY, listOfFilesFragment?.lastSearchQuery)
2505+
}
25112506

2512-
if (showPreview) {
2513-
startActivity(showDetailsIntent)
2514-
} else {
2515-
val fileOperationsHelper = FileOperationsHelper(
2516-
this,
2517-
userAccountManager,
2518-
connectivityService,
2519-
editorUtils
2520-
)
2521-
fileOperationsHelper.startSyncForFileAndIntent(file, showDetailsIntent)
2507+
if (preview) {
2508+
startActivity(intent)
2509+
return
25222510
}
2511+
2512+
val operation = FileOperationsHelper(
2513+
this,
2514+
userAccountManager,
2515+
connectivityService,
2516+
editorUtils
2517+
)
2518+
operation.startSyncForFileAndIntent(file, intent)
25232519
}
25242520

25252521
/**
@@ -2782,8 +2778,8 @@ class FileDisplayActivity :
27822778
val virtualType = bundle.get(PreviewImageActivity.EXTRA_VIRTUAL_TYPE) as VirtualFolderType?
27832779
startImagePreview(
27842780
file,
2785-
virtualType,
2786-
true
2781+
true,
2782+
virtualType
27872783
)
27882784
} else {
27892785
startImagePreview(file, true)
@@ -3043,23 +3039,41 @@ class FileDisplayActivity :
30433039
binding.fabMain.visibility = visibility
30443040
}
30453041

3046-
fun showFile(selectedFile: OCFile?, message: String?) {
3042+
private fun registerUnifiedSearchReceiver() {
3043+
val filter = IntentFilter(UNIFIED_SEARCH_EVENT_ACTION)
3044+
LocalBroadcastManager.getInstance(this).registerReceiver(unifiedSearchReceiver, filter)
3045+
}
3046+
3047+
private val unifiedSearchReceiver: BroadcastReceiver = object : BroadcastReceiver() {
3048+
override fun onReceive(context: Context, intent: Intent) {
3049+
val query = intent.getStringExtra(PreviewImageActivity.EXTRA_LAST_SEARCH_QUERY) ?: return
3050+
Handler(Looper.getMainLooper()).post {
3051+
if (!isFinishing && !isDestroyed) {
3052+
performUnifiedSearch(query, null)
3053+
}
3054+
}
3055+
}
3056+
}
3057+
3058+
@JvmOverloads
3059+
fun showFile(selectedFile: OCFile?, message: String?, lastSearchQuery: String? = null) {
30473060
dismissLoadingDialog()
30483061

30493062
getOCFileListFragmentFromFile(object : TransactionInterface {
30503063
override fun onOCFileListFragmentComplete(listOfFiles: OCFileListFragment) {
3051-
if (TextUtils.isEmpty(message)) {
3064+
if (message?.isEmpty() == true) {
30523065
val temp = file
30533066
file = getCurrentDir()
30543067
listOfFiles.listDirectory(getCurrentDir(), temp, MainApp.isOnlyOnDevice())
30553068
updateActionBarTitleAndHomeButton(null)
30563069
} else {
3057-
val view = listOfFiles.view
3058-
if (view != null) {
3059-
DisplayUtils.showSnackMessage(view, message)
3070+
listOfFiles.view?.let {
3071+
DisplayUtils.showSnackMessage(it, message)
30603072
}
30613073
}
3074+
30623075
if (selectedFile != null) {
3076+
listOfFiles.setLastSearchQuery(lastSearchQuery)
30633077
listOfFiles.onItemClicked(selectedFile)
30643078
}
30653079
}
@@ -3124,6 +3138,8 @@ class FileDisplayActivity :
31243138

31253139
const val ACTION_DETAILS: String = "com.owncloud.android.ui.activity.action.DETAILS"
31263140

3141+
const val UNIFIED_SEARCH_EVENT_ACTION = "PHOTO_SEARCH_EVENT"
3142+
31273143
@JvmField
31283144
val REQUEST_CODE__SELECT_CONTENT_FROM_APPS: Int = REQUEST_CODE__LAST_SHARED + 1
31293145

app/src/main/java/com/owncloud/android/ui/adapter/UnifiedSearchItemViewHolder.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class UnifiedSearchItemViewHolder(
5454

5555
val entryType = entry.getType()
5656
viewThemeUtils.platform.colorImageView(binding.thumbnail, ColorRole.PRIMARY)
57+
5758
GlideHelper.loadIntoImageView(
5859
context,
5960
nextcloudClient,

app/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
import java.util.Iterator;
128128
import java.util.List;
129129
import java.util.Objects;
130+
import java.util.Optional;
130131
import java.util.Set;
131132

132133
import javax.inject.Inject;
@@ -225,6 +226,7 @@ public class OCFileListFragment extends ExtendedListFragment implements
225226
protected String mLimitToMimeType;
226227
private FloatingActionButton mFabMain;
227228
public static boolean isMultipleFileSelectedForCopyOrMove = false;
229+
private String lastSearchQuery = null;
228230

229231
@Inject DeviceInfo deviceInfo;
230232

@@ -253,6 +255,15 @@ public void onCreate(Bundle savedInstanceState) {
253255
searchFragment = currentSearchType != null && isSearchEventSet(searchEvent);
254256
}
255257

258+
public void setLastSearchQuery(@Nullable String value) {
259+
lastSearchQuery = value;
260+
}
261+
262+
@Nullable
263+
public String getLastSearchQuery() {
264+
return lastSearchQuery;
265+
}
266+
256267
@Override
257268
public void onResume() {
258269
// Don't handle search events if we're coming back from back stack

app/src/main/java/com/owncloud/android/ui/fragment/UnifiedSearchFragment.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ class UnifiedSearchFragment :
209209
// Because this fragment is opened with TextView onClick on the previous screen
210210
maxWidth = Integer.MAX_VALUE
211211
viewThemeUtils.androidx.themeToolbarSearchView(this)
212-
setQuery(vm.query.value ?: initialQuery, false)
212+
setQuery(getLastSearchQuery(), false)
213213
setOnQueryTextListener(this@UnifiedSearchFragment)
214214
isIconified = false
215215
clearFocus()
@@ -362,12 +362,14 @@ class UnifiedSearchFragment :
362362
if (showFileActions) {
363363
fda.showFileActions(file)
364364
} else {
365-
fda.showFile(file, "")
365+
fda.showFile(file, "", getLastSearchQuery())
366366
}
367367
}
368368
}
369369
}
370370

371+
private fun getLastSearchQuery(): String? = vm.query.value ?: initialQuery
372+
371373
private fun setupAdapter() {
372374
val syncedFolderProvider = SyncedFolderProvider(requireContext().contentResolver, appPreferences, clock)
373375
val gridLayoutManager = GridLayoutManager(requireContext(), 1)

app/src/main/java/com/owncloud/android/ui/preview/PreviewImageActivity.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,17 @@ class PreviewImageActivity :
249249
}
250250

251251
private fun sendRefreshSearchEventBroadcast() {
252-
val intent = Intent(GalleryFragment.REFRESH_SEARCH_EVENT_RECEIVER)
253-
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
252+
val broadcastManager = LocalBroadcastManager.getInstance(this)
253+
254+
val photoSearchIntent = Intent(GalleryFragment.REFRESH_SEARCH_EVENT_RECEIVER)
255+
broadcastManager.sendBroadcast(photoSearchIntent)
256+
257+
intent.getStringExtra(EXTRA_LAST_SEARCH_QUERY)?.let {
258+
val unifiedSearchIntent = Intent(FileDisplayActivity.UNIFIED_SEARCH_EVENT_ACTION).apply {
259+
putExtra(EXTRA_LAST_SEARCH_QUERY, it)
260+
}
261+
broadcastManager.sendBroadcast(unifiedSearchIntent)
262+
}
254263
}
255264

256265
public override fun onStart() {
@@ -591,6 +600,8 @@ class PreviewImageActivity :
591600
companion object {
592601
val TAG: String = PreviewImageActivity::class.java.simpleName
593602
const val EXTRA_VIRTUAL_TYPE: String = "EXTRA_VIRTUAL_TYPE"
603+
const val EXTRA_LAST_SEARCH_QUERY: String = "EXTRA_LAST_SEARCH_QUERY"
604+
594605
private const val KEY_WAITING_FOR_BINDER = "WAITING_FOR_BINDER"
595606
private const val KEY_SYSTEM_VISIBLE = "TRUE"
596607

0 commit comments

Comments
 (0)