/** * Instantiate a new item with the current values in the ViewModel, disregarding the * validation state. This can be used to create a copy of the item if you need to * send it off to the server side for validation before actually committing. * * To be able to instantiate a copy, we need to inspect the class of `item`, * so this will only work if the ViewModel has an assigned item. * * The current implementation is resource hungry and wasteful, this should be optimized. */ fun itemWithCurrentValues(): T { val copy = (item as Any).javaClass.newInstance() val viewModel = javaClass.newInstance() viewModel.item = copy as T val currentFacades = propertyMap.keys.sortedBy { it.name } val newFacades = viewModel.propertyMap.keys.sortedBy { it.name } // Copy values from current ViewModel to the new currentFacades.zip(newFacades).forEach { it.second.value = it.first.value } // Commit the new viewmodel to push changes into the copy viewModel.commit() return copy }