Skip to content

Commit c420d1f

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 5cfe6d2 commit c420d1f

5 files changed

Lines changed: 88 additions & 48 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
@@ -279,6 +279,7 @@ class FileDisplayActivity :
279279
observeWorkerState()
280280
startMetadataSyncForRoot()
281281
handleBackPress()
282+
registerUnifiedSearchReceiver()
282283
}
283284

284285
private fun loadSavedInstanceState(savedInstanceState: Bundle?) {
@@ -732,7 +733,13 @@ class FileDisplayActivity :
732733
val transaction = fragmentManager.beginTransaction()
733734
transaction.addToBackStack(null)
734735
transaction.replace(R.id.left_fragment_container, fragment, TAG_LIST_OF_FILES)
735-
transaction.commit()
736+
737+
if (fragmentManager.isStateSaved) {
738+
transaction.commitAllowingStateLoss()
739+
} else {
740+
transaction.commit()
741+
}
742+
736743
callback.onComplete(true)
737744
} else {
738745
callback.onComplete(false)
@@ -1962,7 +1969,7 @@ class FileDisplayActivity :
19621969
else -> VirtualFolderType.NONE
19631970
}
19641971

1965-
startImagePreview(file, type, file.isDown)
1972+
startImagePreview(file, file.isDown, type)
19661973
} else {
19671974
startImagePreview(file, file.isDown)
19681975
}
@@ -2491,44 +2498,33 @@ class FileDisplayActivity :
24912498
}
24922499
}
24932500

2494-
fun startImagePreview(file: OCFile, showPreview: Boolean) {
2495-
val showDetailsIntent = Intent(this, PreviewImageActivity::class.java)
2496-
showDetailsIntent.putExtra(EXTRA_FILE, file)
2497-
showDetailsIntent.putExtra(EXTRA_LIVE_PHOTO_FILE, file.livePhotoVideo)
2498-
showDetailsIntent.putExtra(
2499-
EXTRA_USER,
2500-
user.orElseThrow(Supplier { RuntimeException() })
2501-
)
2502-
if (showPreview) {
2503-
startActivity(showDetailsIntent)
2504-
} else {
2505-
val fileOperationsHelper =
2506-
FileOperationsHelper(this, userAccountManager, connectivityService, editorUtils)
2507-
fileOperationsHelper.startSyncForFileAndIntent(file, showDetailsIntent)
2501+
fun startImagePreview(file: OCFile, preview: Boolean, type: VirtualFolderType? = null) {
2502+
val optionalUser = user
2503+
if (optionalUser.isEmpty) {
2504+
Log_OC.e(TAG, "user is empty cannot preview image")
2505+
return
25082506
}
2509-
}
25102507

2511-
fun startImagePreview(file: OCFile, type: VirtualFolderType?, showPreview: Boolean) {
2512-
val showDetailsIntent = Intent(this, PreviewImageActivity::class.java)
2513-
showDetailsIntent.putExtra(EXTRA_FILE, file)
2514-
showDetailsIntent.putExtra(EXTRA_LIVE_PHOTO_FILE, file.livePhotoVideo)
2515-
showDetailsIntent.putExtra(
2516-
EXTRA_USER,
2517-
user.orElseThrow(Supplier { RuntimeException() })
2518-
)
2519-
showDetailsIntent.putExtra(PreviewImageActivity.EXTRA_VIRTUAL_TYPE, type)
2508+
val intent = Intent(this, PreviewImageActivity::class.java).apply {
2509+
putExtra(EXTRA_FILE, file)
2510+
putExtra(EXTRA_LIVE_PHOTO_FILE, file.livePhotoVideo)
2511+
putExtra(EXTRA_USER, optionalUser.get())
2512+
putExtra(PreviewImageActivity.EXTRA_VIRTUAL_TYPE, type)
2513+
putExtra(PreviewImageActivity.EXTRA_LAST_SEARCH_QUERY, listOfFilesFragment?.lastSearchQuery)
2514+
}
25202515

2521-
if (showPreview) {
2522-
startActivity(showDetailsIntent)
2523-
} else {
2524-
val fileOperationsHelper = FileOperationsHelper(
2525-
this,
2526-
userAccountManager,
2527-
connectivityService,
2528-
editorUtils
2529-
)
2530-
fileOperationsHelper.startSyncForFileAndIntent(file, showDetailsIntent)
2516+
if (preview) {
2517+
startActivity(intent)
2518+
return
25312519
}
2520+
2521+
val operation = FileOperationsHelper(
2522+
this,
2523+
userAccountManager,
2524+
connectivityService,
2525+
editorUtils
2526+
)
2527+
operation.startSyncForFileAndIntent(file, intent)
25322528
}
25332529

25342530
/**
@@ -2791,8 +2787,8 @@ class FileDisplayActivity :
27912787
val virtualType = bundle.get(PreviewImageActivity.EXTRA_VIRTUAL_TYPE) as VirtualFolderType?
27922788
startImagePreview(
27932789
file,
2794-
virtualType,
2795-
true
2790+
true,
2791+
virtualType
27962792
)
27972793
} else {
27982794
startImagePreview(file, true)
@@ -3052,23 +3048,41 @@ class FileDisplayActivity :
30523048
binding.fabMain.visibility = visibility
30533049
}
30543050

3055-
fun showFile(selectedFile: OCFile?, message: String?) {
3051+
private fun registerUnifiedSearchReceiver() {
3052+
val filter = IntentFilter(UNIFIED_SEARCH_EVENT_ACTION)
3053+
LocalBroadcastManager.getInstance(this).registerReceiver(unifiedSearchReceiver, filter)
3054+
}
3055+
3056+
private val unifiedSearchReceiver: BroadcastReceiver = object : BroadcastReceiver() {
3057+
override fun onReceive(context: Context, intent: Intent) {
3058+
val query = intent.getStringExtra(PreviewImageActivity.EXTRA_LAST_SEARCH_QUERY) ?: return
3059+
Handler(Looper.getMainLooper()).post {
3060+
if (!isFinishing && !isDestroyed) {
3061+
performUnifiedSearch(query, null)
3062+
}
3063+
}
3064+
}
3065+
}
3066+
3067+
@JvmOverloads
3068+
fun showFile(selectedFile: OCFile?, message: String?, lastSearchQuery: String? = null) {
30563069
dismissLoadingDialog()
30573070

30583071
getOCFileListFragmentFromFile(object : TransactionInterface {
30593072
override fun onOCFileListFragmentComplete(listOfFiles: OCFileListFragment) {
3060-
if (TextUtils.isEmpty(message)) {
3073+
if (message?.isEmpty() == true) {
30613074
val temp = file
30623075
file = getCurrentDir()
30633076
listOfFiles.listDirectory(getCurrentDir(), temp, MainApp.isOnlyOnDevice())
30643077
updateActionBarTitleAndHomeButton(null)
30653078
} else {
3066-
val view = listOfFiles.view
3067-
if (view != null) {
3068-
DisplayUtils.showSnackMessage(view, message)
3079+
listOfFiles.view?.let {
3080+
DisplayUtils.showSnackMessage(it, message)
30693081
}
30703082
}
3083+
30713084
if (selectedFile != null) {
3085+
listOfFiles.setLastSearchQuery(lastSearchQuery)
30723086
listOfFiles.onItemClicked(selectedFile)
30733087
}
30743088
}
@@ -3133,6 +3147,8 @@ class FileDisplayActivity :
31333147

31343148
const val ACTION_DETAILS: String = "com.owncloud.android.ui.activity.action.DETAILS"
31353149

3150+
const val UNIFIED_SEARCH_EVENT_ACTION = "PHOTO_SEARCH_EVENT"
3151+
31363152
@JvmField
31373153
val REQUEST_CODE__SELECT_CONTENT_FROM_APPS: Int = REQUEST_CODE__LAST_SHARED + 1
31383154

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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import android.content.BroadcastReceiver
1010
import android.content.Context
1111
import android.content.Intent
1212
import android.content.IntentFilter
13-
import android.graphics.drawable.ColorDrawable
1413
import android.os.Bundle
1514
import android.view.MenuItem
1615
import android.view.View
@@ -250,8 +249,17 @@ class PreviewImageActivity :
250249
}
251250

252251
private fun sendRefreshSearchEventBroadcast() {
253-
val intent = Intent(GalleryFragment.REFRESH_SEARCH_EVENT_RECEIVER)
254-
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+
}
255263
}
256264

257265
public override fun onStart() {
@@ -593,6 +601,8 @@ class PreviewImageActivity :
593601
companion object {
594602
val TAG: String = PreviewImageActivity::class.java.simpleName
595603
const val EXTRA_VIRTUAL_TYPE: String = "EXTRA_VIRTUAL_TYPE"
604+
const val EXTRA_LAST_SEARCH_QUERY: String = "EXTRA_LAST_SEARCH_QUERY"
605+
596606
private const val KEY_WAITING_FOR_BINDER = "WAITING_FOR_BINDER"
597607
private const val KEY_SYSTEM_VISIBLE = "TRUE"
598608

0 commit comments

Comments
 (0)