mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2026-11-26 11:56:43 -08:00
fix: Add button and onDatabaseLoaded #2541
This commit is contained in:
6 files changed
+85
-84
No files matched your search
@@ -272,7 +272,7 @@ class EntryActivity : DatabaseLockActivity() {
|
||||
toolbar?.changeControlColor(onToolbarColor)
|
||||
collapsingToolbarLayout?.changeTitleColor(onToolbarColor)
|
||||
// Manage FAB visibility
|
||||
editFab?.visibility = if (!mDatabaseReadOnly
|
||||
editFab?.visibility = if (!entryState.isReadOnly
|
||||
&& entryState.showFloatingActionButton) View.VISIBLE else View.GONE
|
||||
// Assign history dedicated view
|
||||
historyView?.visibility = if (entryState.showHistoryView) View.VISIBLE else View.GONE
|
||||
|
||||
@@ -458,7 +458,7 @@ class EntryEditActivity : DatabaseLockActivity() {
|
||||
|
||||
override fun onDatabaseRetrieved(database: ContextualDatabase) {
|
||||
super.onDatabaseRetrieved(database)
|
||||
mEntryEditViewModel.loadTemplateEntry(database)
|
||||
mEntryEditViewModel.onDatabaseLoaded(database)
|
||||
mTemplatesSelectorAdapter?.apply {
|
||||
iconDrawableFactory = database.iconDrawableFactory
|
||||
notifyDataSetChanged()
|
||||
|
||||
@@ -183,13 +183,13 @@ class GroupActivity : DatabaseLockActivity() {
|
||||
}
|
||||
|
||||
override fun onQueryTextChange(newText: String?): Boolean {
|
||||
mGroupViewModel.searchText(mDatabase, newText)
|
||||
mGroupViewModel.searchText(newText)
|
||||
return true
|
||||
}
|
||||
}
|
||||
private val mOnSearchFiltersChangeListener = object : ((SearchParameters) -> Unit) {
|
||||
override fun invoke(searchParameters: SearchParameters) {
|
||||
mGroupViewModel.searchWithParameters(mDatabase, searchParameters)
|
||||
mGroupViewModel.searchWithParameters(searchParameters)
|
||||
}
|
||||
}
|
||||
private val mOnSearchActionExpandListener = object : MenuItem.OnActionExpandListener {
|
||||
@@ -530,9 +530,7 @@ class GroupActivity : DatabaseLockActivity() {
|
||||
mGroupViewModel.viewEvent.collect { event ->
|
||||
when (event) {
|
||||
is GroupViewModel.GroupEvent.OpenGroup -> {
|
||||
mDatabase?.let { database ->
|
||||
mGroupViewModel.loadGroup(database, event.groupId)
|
||||
}
|
||||
mGroupViewModel.loadGroup(event.groupId)
|
||||
}
|
||||
is GroupViewModel.GroupEvent.OpenEntry -> {
|
||||
mDatabase?.let { database ->
|
||||
|
||||
@@ -87,7 +87,7 @@ class EntryEditViewModel: NodeEditViewModel() {
|
||||
|
||||
private var initialized = false
|
||||
|
||||
fun loadTemplateEntry(database: ContextualDatabase) {
|
||||
fun onDatabaseLoaded(database: ContextualDatabase) {
|
||||
mDatabase = database
|
||||
allowCustomFields = database.allowEntryCustomFields() == true
|
||||
allowOTP = database.allowOTP == true
|
||||
|
||||
@@ -134,6 +134,7 @@ class EntryViewModel(application: Application): AndroidViewModel(application) {
|
||||
it.copy(
|
||||
loaded = true,
|
||||
entryInfo = entryInfo,
|
||||
isReadOnly = database.isReadOnly,
|
||||
showFloatingActionButton = !isHistory,
|
||||
showHistoryView = isHistory,
|
||||
entryHistory = if (!isHistory)
|
||||
@@ -307,6 +308,7 @@ class EntryViewModel(application: Application): AndroidViewModel(application) {
|
||||
data class EntryViewState(
|
||||
val loaded: Boolean = false,
|
||||
val entryInfo: EntryInfo? = null,
|
||||
val isReadOnly: Boolean = true,
|
||||
val showFloatingActionButton: Boolean = false,
|
||||
val showHistoryView: Boolean = false,
|
||||
val toolbarColor: Int = 0,
|
||||
|
||||
@@ -148,64 +148,66 @@ class GroupViewModel(application: Application): AndroidViewModel(application) {
|
||||
|
||||
fun loadGroup() {
|
||||
if (mSearchState != null) {
|
||||
loadSearch(mDatabase)
|
||||
} else loadMainGroup(mDatabase)
|
||||
loadSearch()
|
||||
} else loadMainGroup()
|
||||
}
|
||||
|
||||
private fun loadMainGroup(
|
||||
database: ContextualDatabase?,
|
||||
groupState: GroupState? = mMainGroupState
|
||||
) {
|
||||
viewModelScope.launch {
|
||||
withContext(Dispatchers.IO) {
|
||||
_groupUIState.update { groupState ->
|
||||
groupState.copy(loaded = false)
|
||||
}
|
||||
val groupId = groupState?.groupId
|
||||
val showFromPosition = groupState?.firstVisibleItem
|
||||
val group = if (groupId != null) {
|
||||
database?.getGroupInfoById(groupId)
|
||||
} else {
|
||||
database?.getRootGroupInfo()
|
||||
}
|
||||
if (group != null) {
|
||||
mRecyclingBinIsCurrentGroup = group.isRecycleBin(database)
|
||||
mainGroup = group
|
||||
// Save group state
|
||||
mMainGroupState = GroupState(group.nodeId, showFromPosition)
|
||||
isCurrentGroupIsRoot = group.isRoot(mDatabase) == true
|
||||
isCurrentGroupIsSearch = false
|
||||
mSearchState = null
|
||||
// Breadcrumbs
|
||||
val breadcrumbs = mDatabase?.getBreadcrumbsFrom(
|
||||
groupId = group.nodeId,
|
||||
recursiveNumberOfEntries = mRecursiveNumberEntries,
|
||||
nodeFilter = mNodeFilter
|
||||
) ?: listOf()
|
||||
// Children
|
||||
val children = mDatabase?.getSortedChildrenOf(
|
||||
parentId = group.nodeId,
|
||||
recursiveNumberOfEntries = mRecursiveNumberEntries,
|
||||
nodeFilter = mNodeFilter
|
||||
) ?: listOf()
|
||||
// To enable add button
|
||||
val addGroupEnabled = mDatabase?.allowAddGroupIn(group = group) ?: false
|
||||
val addEntryEnabled = mDatabase?.allowAddEntryIn(group = group) ?: false
|
||||
mAddGroupOrEntryAllowed = addGroupEnabled || addEntryEnabled
|
||||
withContext(Dispatchers.Main) {
|
||||
_groupUIState.update { groupState ->
|
||||
groupState.copy(
|
||||
loaded = true,
|
||||
breadcrumbs = breadcrumbs,
|
||||
group = group,
|
||||
children = children,
|
||||
showAddGroupButton = addGroupEnabled,
|
||||
showAddEntryButton = addEntryEnabled,
|
||||
showAddNodeButton = mAddNodeButtonAllowed
|
||||
)
|
||||
}
|
||||
showFromPosition?.let {
|
||||
_viewEvent.emit(GroupEvent.ShowPosition(showFromPosition))
|
||||
mDatabase?.let { database ->
|
||||
withContext(Dispatchers.IO) {
|
||||
_groupUIState.update { groupState ->
|
||||
groupState.copy(loaded = false)
|
||||
}
|
||||
val groupId = groupState?.groupId
|
||||
val showFromPosition = groupState?.firstVisibleItem
|
||||
val group = if (groupId != null) {
|
||||
database.getGroupInfoById(groupId)
|
||||
} else {
|
||||
database.getRootGroupInfo()
|
||||
}
|
||||
if (group != null) {
|
||||
mRecyclingBinIsCurrentGroup = group.isRecycleBin(database)
|
||||
mainGroup = group
|
||||
// Save group state
|
||||
mMainGroupState = GroupState(group.nodeId, showFromPosition)
|
||||
isCurrentGroupIsRoot = group.isRoot(mDatabase) == true
|
||||
isCurrentGroupIsSearch = false
|
||||
mSearchState = null
|
||||
// Breadcrumbs
|
||||
val breadcrumbs = mDatabase?.getBreadcrumbsFrom(
|
||||
groupId = group.nodeId,
|
||||
recursiveNumberOfEntries = mRecursiveNumberEntries,
|
||||
nodeFilter = mNodeFilter
|
||||
) ?: listOf()
|
||||
// Children
|
||||
val children = mDatabase?.getSortedChildrenOf(
|
||||
parentId = group.nodeId,
|
||||
recursiveNumberOfEntries = mRecursiveNumberEntries,
|
||||
nodeFilter = mNodeFilter
|
||||
) ?: listOf()
|
||||
// To enable add button
|
||||
val addGroupEnabled = database.allowAddGroupIn(group = group)
|
||||
val addEntryEnabled = database.allowAddEntryIn(group = group)
|
||||
mAddGroupOrEntryAllowed = addGroupEnabled || addEntryEnabled
|
||||
withContext(Dispatchers.Main) {
|
||||
_groupUIState.update { groupState ->
|
||||
groupState.copy(
|
||||
loaded = true,
|
||||
breadcrumbs = breadcrumbs,
|
||||
group = group,
|
||||
children = children,
|
||||
isReadOnly = database.isReadOnly,
|
||||
showAddGroupButton = addGroupEnabled,
|
||||
showAddEntryButton = addEntryEnabled,
|
||||
showAddNodeButton = mAddNodeButtonAllowed
|
||||
)
|
||||
}
|
||||
showFromPosition?.let {
|
||||
_viewEvent.emit(GroupEvent.ShowPosition(showFromPosition))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -214,7 +216,6 @@ class GroupViewModel(application: Application): AndroidViewModel(application) {
|
||||
}
|
||||
|
||||
fun loadGroup(
|
||||
database: ContextualDatabase?,
|
||||
groupId: GroupId?
|
||||
) {
|
||||
// Save the last not virtual group and it's position
|
||||
@@ -223,29 +224,28 @@ class GroupViewModel(application: Application): AndroidViewModel(application) {
|
||||
mPreviousGroupsStates.add(it)
|
||||
}
|
||||
}
|
||||
loadMainGroup(database, GroupState(groupId, firstVisibleItem = 0))
|
||||
loadMainGroup(GroupState(groupId, firstVisibleItem = 0))
|
||||
}
|
||||
|
||||
private fun loadSearch(
|
||||
database: ContextualDatabase?,
|
||||
searchState: SearchState? = mSearchState,
|
||||
) {
|
||||
viewModelScope.launch {
|
||||
withContext(Dispatchers.IO) {
|
||||
searchState?.let {
|
||||
withContext(Dispatchers.Main) {
|
||||
_groupUIState.update { groupState ->
|
||||
groupState.copy(loaded = false)
|
||||
mDatabase?.let { database ->
|
||||
withContext(Dispatchers.IO) {
|
||||
searchState?.let {
|
||||
withContext(Dispatchers.Main) {
|
||||
_groupUIState.update { groupState ->
|
||||
groupState.copy(loaded = false)
|
||||
}
|
||||
}
|
||||
}
|
||||
val searchParameters = searchState.searchParameters
|
||||
val showFromPosition = searchState.firstVisibleItem
|
||||
val group = database?.createSearchGroupInfo(
|
||||
searchParameters = searchParameters,
|
||||
fromGroup = mMainGroupState?.groupId,
|
||||
max = SearchHelper.MAX_SEARCH_ENTRY
|
||||
)
|
||||
if (group != null) {
|
||||
val searchParameters = searchState.searchParameters
|
||||
val showFromPosition = searchState.firstVisibleItem
|
||||
val group = database.createSearchGroupInfo(
|
||||
searchParameters = searchParameters,
|
||||
fromGroup = mMainGroupState?.groupId,
|
||||
max = SearchHelper.MAX_SEARCH_ENTRY
|
||||
)
|
||||
isCurrentGroupIsRoot = false
|
||||
isCurrentGroupIsSearch = true
|
||||
mSearchState = searchState
|
||||
@@ -256,6 +256,7 @@ class GroupViewModel(application: Application): AndroidViewModel(application) {
|
||||
loaded = true,
|
||||
group = group,
|
||||
children = children,
|
||||
isReadOnly = database.isReadOnly,
|
||||
showAddNodeButton = false
|
||||
)
|
||||
}
|
||||
@@ -263,8 +264,8 @@ class GroupViewModel(application: Application): AndroidViewModel(application) {
|
||||
_viewEvent.emit(GroupEvent.ShowPosition(showFromPosition))
|
||||
}
|
||||
}
|
||||
}
|
||||
} ?: Log.e(TAG, "Search state is null")
|
||||
} ?: Log.e(TAG, "Search state is null")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -326,20 +327,20 @@ class GroupViewModel(application: Application): AndroidViewModel(application) {
|
||||
}
|
||||
}
|
||||
|
||||
fun searchText(database: ContextualDatabase?, text: String?) {
|
||||
fun searchText(text: String?) {
|
||||
if (text != null && !mLockSearchListeners) {
|
||||
mSearchState?.let { searchState ->
|
||||
searchState.searchParameters.searchQuery = text
|
||||
loadSearch(database, searchState)
|
||||
loadSearch(searchState)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun searchWithParameters(database: ContextualDatabase?, searchParameters: SearchParameters) {
|
||||
fun searchWithParameters(searchParameters: SearchParameters) {
|
||||
mSearchState?.let { searchState ->
|
||||
searchParameters.searchQuery = searchState.searchParameters.searchQuery
|
||||
searchState.searchParameters = searchParameters
|
||||
loadSearch(database, searchState)
|
||||
loadSearch(searchState)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -605,7 +606,6 @@ class GroupViewModel(application: Application): AndroidViewModel(application) {
|
||||
fun loadPreviousGroup() {
|
||||
if (previousGroupExists()) {
|
||||
return loadMainGroup(
|
||||
database = mDatabase,
|
||||
groupState = mPreviousGroupsStates.removeAt(mPreviousGroupsStates.lastIndex)
|
||||
)
|
||||
}
|
||||
@@ -657,6 +657,7 @@ class GroupViewModel(application: Application): AndroidViewModel(application) {
|
||||
val breadcrumbs: List<SortedGroupInfo>? = null,
|
||||
val group: GroupInfo? = null,
|
||||
val children: List<SortedNodeInfo>? = null,
|
||||
val isReadOnly: Boolean = true,
|
||||
val showAddNodeButton: Boolean = false,
|
||||
val showAddGroupButton: Boolean = false,
|
||||
val showAddEntryButton: Boolean = false
|
||||
|
||||
Reference in new issue
Block a user