| 844 | } |
| 845 | |
| 846 | void ComicModel::takeUpdatedData(const QList<ComicItem *> &updatedData, std::function<bool(ComicItem *, ComicItem *)> comparator) |
| 847 | { |
| 848 | int length = _data.size(); |
| 849 | int lengthUpdated = updatedData.size(); |
| 850 | |
| 851 | int i; // index of the internal data |
| 852 | int j; // index of the updated children |
| 853 | for (i = 0, j = 0; i < length && j < lengthUpdated;) { |
| 854 | auto comic = _data.at(i); |
| 855 | auto updatedComic = updatedData.at(j); |
| 856 | |
| 857 | auto sameComic = comic->data(ComicModel::Id) == updatedComic->data(ComicModel::Id); |
| 858 | if (sameComic) { |
| 859 | if (comic->getData() != updatedComic->getData()) { |
| 860 | auto modelIndexToUpdate = index(i, 0, QModelIndex()); |
| 861 | |
| 862 | comic->setData(updatedComic->getData()); |
| 863 | |
| 864 | emit dataChanged(modelIndexToUpdate, modelIndexToUpdate); |
| 865 | } |
| 866 | |
| 867 | i++; |
| 868 | j++; |
| 869 | continue; |
| 870 | } |
| 871 | |
| 872 | auto lessThan = comparator(comic, updatedComic); |
| 873 | |
| 874 | // comic added |
| 875 | if (!lessThan) { |
| 876 | beginInsertRows(QModelIndex(), i, i); |
| 877 | |
| 878 | _data.insert(i, updatedComic); |
| 879 | |
| 880 | endInsertRows(); |
| 881 | |
| 882 | i++; |
| 883 | j++; |
| 884 | length++; |
| 885 | continue; |
| 886 | } |
| 887 | |
| 888 | // comic removed |
| 889 | if (lessThan) { |
| 890 | beginRemoveRows(QModelIndex(), i, i); |
| 891 | |
| 892 | _data.removeAt(i); |
| 893 | |
| 894 | endRemoveRows(); |
| 895 | |
| 896 | length--; |
| 897 | continue; |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | // add remaining comics |
| 902 | for (; j < lengthUpdated; j++) { |
| 903 | beginInsertRows(QModelIndex(), i, i); |
nothing calls this directly
no test coverage detected