From 7f5b2cc78b4cad64ce150e51625abb4a7720d8ab Mon Sep 17 00:00:00 2001 From: "Murugan, Vimal" Date: Tue, 10 Nov 2020 02:04:42 +0530 Subject: [PATCH] scroll to row index logic added --- .../Templates/ListPageTemplateModel.swift | 4 ++++ .../Templates/MoleculeListTemplate.swift | 21 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/MVMCoreUI/Atomic/Templates/ListPageTemplateModel.swift b/MVMCoreUI/Atomic/Templates/ListPageTemplateModel.swift index 14be7809..ba464f68 100644 --- a/MVMCoreUI/Atomic/Templates/ListPageTemplateModel.swift +++ b/MVMCoreUI/Atomic/Templates/ListPageTemplateModel.swift @@ -18,6 +18,7 @@ import Foundation } public var molecules: [ListItemModelProtocol & MoleculeModelProtocol]? public var line: LineModel? + public var scrollToRowIndex: Int? /// This template requires content. func validateModelHasContent() throws { @@ -45,6 +46,7 @@ import Foundation private enum CodingKeys: String, CodingKey { case molecules case line + case scrollToRowIndex } //-------------------------------------------------- @@ -55,6 +57,7 @@ import Foundation let typeContainer = try decoder.container(keyedBy: CodingKeys.self) molecules = try typeContainer.decodeModelsIfPresent(codingKey: .molecules) line = try typeContainer.decodeIfPresent(LineModel.self, forKey: .line) + scrollToRowIndex = try typeContainer.decodeIfPresent(Int.self, forKey: .scrollToRowIndex) try super.init(from: decoder) try validateModelHasContent() } @@ -64,6 +67,7 @@ import Foundation var container = encoder.container(keyedBy: CodingKeys.self) try container.encodeModelsIfPresent(molecules, forKey: .molecules) try container.encode(line, forKey: .line) + try container.encode(scrollToRowIndex, forKey: .scrollToRowIndex) } } diff --git a/MVMCoreUI/Atomic/Templates/MoleculeListTemplate.swift b/MVMCoreUI/Atomic/Templates/MoleculeListTemplate.swift index 57760022..4b6b565e 100644 --- a/MVMCoreUI/Atomic/Templates/MoleculeListTemplate.swift +++ b/MVMCoreUI/Atomic/Templates/MoleculeListTemplate.swift @@ -81,6 +81,27 @@ open class MoleculeListTemplate: ThreeLayerTableViewController, TemplateProtocol super.handleNewData() } + open override func viewDidAppear(_ animated: Bool) { + super.viewDidAppear(animated) + //Handle scroll + handleScrollToSpecificRow() + } + + + //-------------------------------------------------- + // MARK: - Handle scroll to spefic row + //-------------------------------------------------- + func handleScrollToSpecificRow() { + guard let index = templateModel?.scrollToRowIndex, + loadObject?.pageDataFromCache == false, + let table = tableView, + index < tableView(table, numberOfRowsInSection:0) else { return } + let indexPath = IndexPath(row: index, section: 0) + tableView?.scrollToRow(at: indexPath, at: .middle, animated: false) + //Reset for without refresh page not needed + templateModel?.scrollToRowIndex = nil + } + //-------------------------------------------------- // MARK: - Table View //--------------------------------------------------