Skip to content

add updateRecord logic to LocalListScrollingDataSource #1058

Open
@howudodat

Description

@howudodat

please add updateRecord* functions to LocalListScrollingDataSource similar to LocalListDataSource

	/**
	 * Updates a single record in the data store by replacing it with a new record, updating both the original and filtered lists.
	 *
	 * @param record The new record to replace the existing record.
	 */
	public void updateRecord(T record) {
		updateRecord(original.indexOf(record), record);
	}

	/**
	 * Updates a single record in the data store at a specified index, updating both the original and filtered lists.
	 *
	 * @param index  The index of the record to be updated.
	 * @param record The new record to replace the existing record.
	 */
	public void updateRecord(int index, T record) {
		internalUpdate(index, record, true);
	}

	/**
	 * Updates multiple records in the data store, updating both the original and filtered lists.
	 *
	 * @param records A collection of new records to replace the existing records.
	 */
	public void updateRecords(Collection<T> records) {
		for (T record : records) {
			internalUpdate(original.indexOf(record), record, false);
		}
		load();
	}

	/**
	 * Updates multiple records in the data store starting from a specified index, updating both the original and filtered lists.
	 *
	 * @param startIndex The starting index for updating records.
	 * @param records    A collection of new records to replace the existing records.
	 */
	public void updateRecords(int startIndex, Collection<T> records) {
		for (T record : records) {
			if (startIndex >= original.size()) {
				break;
			}
			internalUpdate(startIndex++, record, false);
		}
		load();
	}

	/**
	 * Internal method to update a single record at a specified index, updating both the original and filtered lists.
	 *
	 * @param index  The index of the record to be updated.
	 * @param record The new record to replace the existing record.
	 * @param load   Specifies whether to trigger a data load after the update.
	 */
	private void internalUpdate(int index, T record, boolean load) {
		if (index >= 0 && index < original.size()) {
			T oldRecord = original.get(index);
			original.set(index, record);
			if (filtered.contains(oldRecord)) {
				filtered.set(filtered.indexOf(oldRecord), record);
			}
			if (load) {
				load();
			}
		}
	}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions