mirror of
https://github.com/nextcloud/android.git
synced 2024-11-23 05:35:39 +03:00
Refactor
Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
parent
d596d2f11e
commit
11a27eb74a
5 changed files with 61 additions and 31 deletions
|
@ -13,6 +13,7 @@ import android.content.Context
|
|||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.provider.CalendarContract
|
||||
import com.nextcloud.utils.extensions.createdAt
|
||||
import com.nextcloud.utils.extensions.showToast
|
||||
import com.owncloud.android.R
|
||||
import com.owncloud.android.lib.common.SearchResultEntry
|
||||
|
@ -22,21 +23,21 @@ import com.owncloud.android.utils.PermissionUtil.checkSelfPermission
|
|||
class CalendarEventManager(private val context: Context) {
|
||||
|
||||
fun openCalendarEvent(searchResult: SearchResultEntry, listInterface: UnifiedSearchListInterface) {
|
||||
val createdAt = searchResult.attributes["createdAt"]?.toLongOrNull()?.times(1000L)
|
||||
val haveReadCalendarPermission = checkSelfPermission(context, Manifest.permission.READ_CALENDAR)
|
||||
val eventId: Long? = if (haveReadCalendarPermission && createdAt != null) {
|
||||
if (!haveReadCalendarPermission) {
|
||||
listInterface.checkPermission(searchResult)
|
||||
return
|
||||
}
|
||||
|
||||
val createdAt = searchResult.createdAt()
|
||||
val eventId: Long? = if (createdAt != null) {
|
||||
getCalendarEventId(searchResult.title, createdAt)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
if (eventId == null) {
|
||||
val messageId = if (haveReadCalendarPermission) {
|
||||
R.string.unified_search_fragment_calendar_event_cannot_be_found_on_device
|
||||
} else {
|
||||
R.string.unified_search_fragment_calendar_permission_needed_redirecting_web
|
||||
}
|
||||
context.showToast(messageId)
|
||||
context.showToast(R.string.unified_search_fragment_calendar_event_cannot_be_found_on_device)
|
||||
listInterface.onSearchResultClicked(searchResult)
|
||||
} else {
|
||||
val uri: Uri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventId)
|
||||
|
|
|
@ -12,6 +12,7 @@ import android.content.Context
|
|||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.provider.ContactsContract
|
||||
import com.nextcloud.utils.extensions.displayName
|
||||
import com.nextcloud.utils.extensions.showToast
|
||||
import com.owncloud.android.R
|
||||
import com.owncloud.android.lib.common.SearchResultEntry
|
||||
|
@ -21,21 +22,21 @@ import com.owncloud.android.utils.PermissionUtil.checkSelfPermission
|
|||
class ContactManager(private val context: Context) {
|
||||
|
||||
fun openContact(searchResult: SearchResultEntry, listInterface: UnifiedSearchListInterface) {
|
||||
val displayName = searchResult.attributes["displayName"]
|
||||
val haveReadContactsPermission = checkSelfPermission(context, Manifest.permission.READ_CONTACTS)
|
||||
val contactIds = if (haveReadContactsPermission && displayName != null) {
|
||||
if (!haveReadContactsPermission) {
|
||||
listInterface.checkPermission(searchResult)
|
||||
return
|
||||
}
|
||||
|
||||
val displayName = searchResult.displayName()
|
||||
val contactIds = if (displayName != null) {
|
||||
getContactId(displayName)
|
||||
} else {
|
||||
listOf()
|
||||
}
|
||||
|
||||
if (contactIds.isEmpty()) {
|
||||
val messageId = if (haveReadContactsPermission) {
|
||||
R.string.unified_search_fragment_contact_cannot_be_found_on_device
|
||||
} else {
|
||||
R.string.unified_search_fragment_contact_permission_needed_redirecting_web
|
||||
}
|
||||
context.showToast(messageId)
|
||||
context.showToast(R.string.unified_search_fragment_contact_cannot_be_found_on_device)
|
||||
listInterface.onSearchResultClicked(searchResult)
|
||||
} else {
|
||||
val uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, contactIds.first().toString())
|
||||
|
|
|
@ -25,3 +25,7 @@ fun SearchResultEntry.getType(): SearchResultEntryType {
|
|||
SearchResultEntryType.Unknown
|
||||
}
|
||||
}
|
||||
|
||||
fun SearchResultEntry.createdAt(): Long? = attributes["createdAt"]?.toLongOrNull()?.times(1000L)
|
||||
|
||||
fun SearchResultEntry.displayName(): String? = attributes["displayName"]
|
||||
|
|
|
@ -10,6 +10,8 @@ package com.owncloud.android.ui.fragment
|
|||
import android.Manifest
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.view.LayoutInflater
|
||||
import android.view.Menu
|
||||
import android.view.MenuInflater
|
||||
|
@ -101,7 +103,7 @@ class UnifiedSearchFragment :
|
|||
lateinit var viewThemeUtils: ViewThemeUtils
|
||||
|
||||
private var listOfHiddenFiles = ArrayList<String>()
|
||||
|
||||
private var searchResultEntry: SearchResultEntry? = null
|
||||
private var showMoreActions = false
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
|
@ -135,7 +137,6 @@ class UnifiedSearchFragment :
|
|||
|
||||
setupFileDisplayActivity()
|
||||
setupAdapter()
|
||||
checkPermissions()
|
||||
}
|
||||
|
||||
@Deprecated("Deprecated in Java")
|
||||
|
@ -144,28 +145,51 @@ class UnifiedSearchFragment :
|
|||
setupSearchView(item)
|
||||
}
|
||||
|
||||
private val contactPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted: Boolean ->
|
||||
if (!isGranted) {
|
||||
DisplayUtils.showSnackMessage(binding.root, R.string.unified_search_fragment_contact_permission_needed)
|
||||
}
|
||||
}
|
||||
override fun checkPermission(searchResultEntry: SearchResultEntry) {
|
||||
this.searchResultEntry = searchResultEntry
|
||||
|
||||
private val calendarPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted: Boolean ->
|
||||
if (!isGranted) {
|
||||
DisplayUtils.showSnackMessage(binding.root, R.string.unified_search_fragment_calendar_permission_needed)
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkPermissions() {
|
||||
if (!checkSelfPermission(requireActivity(), Manifest.permission.READ_CONTACTS)) {
|
||||
DisplayUtils.showSnackMessage(
|
||||
binding.root,
|
||||
R.string.unified_search_fragment_contact_permission_needed_redirecting_web
|
||||
)
|
||||
contactPermissionLauncher.launch(Manifest.permission.READ_CONTACTS)
|
||||
}
|
||||
|
||||
if (!checkSelfPermission(requireActivity(), Manifest.permission.READ_CALENDAR)) {
|
||||
DisplayUtils.showSnackMessage(
|
||||
binding.root,
|
||||
R.string.unified_search_fragment_calendar_permission_needed_redirecting_web
|
||||
)
|
||||
calendarPermissionLauncher.launch(Manifest.permission.READ_CALENDAR)
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("MagicNumber")
|
||||
private fun triggerOnSearchClick() {
|
||||
searchResultEntry?.let {
|
||||
Handler(Looper.getMainLooper()).postDelayed({
|
||||
onSearchResultClicked(it)
|
||||
}, 1500)
|
||||
}
|
||||
}
|
||||
|
||||
private val contactPermissionLauncher =
|
||||
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted: Boolean ->
|
||||
if (!isGranted) {
|
||||
DisplayUtils.showSnackMessage(binding.root, R.string.unified_search_fragment_contact_permission_needed)
|
||||
triggerOnSearchClick()
|
||||
}
|
||||
}
|
||||
|
||||
private val calendarPermissionLauncher =
|
||||
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted: Boolean ->
|
||||
if (!isGranted) {
|
||||
DisplayUtils.showSnackMessage(binding.root, R.string.unified_search_fragment_calendar_permission_needed)
|
||||
triggerOnSearchClick()
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupSearchView(item: MenuItem) {
|
||||
(item.actionView as? SearchView?)?.run {
|
||||
// Required to align with TextView width.
|
||||
|
|
|
@ -11,7 +11,7 @@ import com.owncloud.android.lib.common.SearchResultEntry
|
|||
import com.owncloud.android.ui.unifiedsearch.ProviderID
|
||||
|
||||
interface UnifiedSearchListInterface {
|
||||
|
||||
fun checkPermission(searchResultEntry: SearchResultEntry)
|
||||
fun onSearchResultClicked(searchResultEntry: SearchResultEntry)
|
||||
fun onLoadMoreClicked(providerID: ProviderID)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue