Pure MVVM Without Code Behind - mvvm

I'm interested in approaches that avoids code in the code behind.
In my opinion, there are some cases where code must be placed in the code behind.
For example: I have a grid with an undefined count of columns. Columns can't be binded. So the easiest way would be to generate the columns in the code behind.
For this case, I can create a new class which inherits from the grid. This new class has a new binding property and code for the column binding. The code is separated in a custom class which can be used in the XAML. And then, I can easy bind the columns to my newly created property. And the view has no code behind.
Is it a good idea? How would you solve such (or similar) problems?
Are there other ways to extract the code from the code behind?
Thanks.

Having a code behind free xaml.cs and moving to code to a new class does not mean its pure MVVM. The idea is you will have all the logic in ViewModel hence a code behind free View, helping in Unit Testing. View specific logic like colors and animation cannot be unittested hence doesnt matter where it resides, in an custom control or a inherited control or directly in xaml.cs. As long as you are testing most of the user interactions and view logic via UnitTesting, you should be happy that you have done a good job.

Related

SwiftUI Control flow build elements

I recently started playing with SwiftUI and though there are some good things too it, so far I am very disappointed. Especially by the fact that it makes it really easy to create large, messy looking code and reminds me of XML ( or HTML ) structure of sorts.
Anyhow, I am running into the following problem that I can't find a solution for anywhere.
I need to draw certain rectangles ( or shapes ) at run time, think about it as generating a horizontal bar-chart in a way. Because the number of rects is dynamic, I need to generate this in a method, however I am getting an error from the compiler with any method I've tried so far.
Whenever I try and use some sort of flow control, like 'for' or 'while' in order to generate elements inside the:
struct ContentView: View {
or :
var body: some View {
I either get:
Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type
or
Closure containing control flow statement cannot be used with function builder 'ViewBuilder'
What would be a better approach for drawing dynamic elements with SwiftUI.
I've seen a ton of examples on dynamic lists, even though that was covered by Apple already on day 1, but nothing that would be about custom elements or if you wanted to create a dynamically set number of HStack or Vstack, that are not formatted as a List.
Thank you!
ViewBuilders are special things and you cannot use certain control flow statements in them.
You can call out to separate (private) functions to do the appropriate control flow and produce the appropriate views.
See this blog post by John Sundell on the Swift 5.1 features that power SwiftUI: https://www.swiftbysundell.com/articles/the-swift-51-features-that-power-swiftuis-api/

Dynamic form with binding in GXT

What would be the best approach to implement a form with a variable number of text fields? I'm thinking something like this:
textField1 (removeButton)
textField2 (removeButton)
textField3 (removeButton)
addNewTextFieldButton
I would like this to bind to a list of strings.
I achieved this with an editable grid with a single column and buttons to add/delete rows.
This component is very well integrated with GWT Editor framework so you can bind your grid to a list of objects using a ListStoreEditor
The best approach would be to use the GWT Editor framework. GXT's fields are very well integrated with the Editor framework.
Here is a very rough example of how you might approach this problem.
You would start by creating one Editor for the thing you are wanting to bind to. In your case, I think a composite which contains a TextField (which is bound to the string) and a button. The button won't actually bind to anything, but you will provide a way for something which uses this class to register a SelectHandler against it. Let's call this editor SubEditor.
Once you create a UI component which is designed to bind to one string, you will next create a ListEditor<String, SubEditor> which will bind to a List<String> which will compose a view, consisting of one SubEditor per each String in the bound list.
You don't really need to create the SubEditor, as you could construct something as simple as what you want within your ListEditor's EditorSource class (read through the tutorials on ListEditors).
Again, I want to emphasize the this is a ROUGH example on how to get started. I hope there is enough information here for you to fill in the pieces.
The following SO question has helped me out a lot:
Using GWT Editors with a complex usecase

Is it possible to have two different EditorDrivers simultaneously editing the same object?

In my app, I have a presenter (Presenter1) which I use to kick off an Editor (EditorView1) which edits a Foo object. This MVP setup is akin to what is described in this answer, https://stackoverflow.com/a/10699346/565863
Now, let's say that I need to create another view (EditorView2 which is kicked off by Presenter2) which also edits a Foo object, but needs to make use of EditorView1.
EditorView1 would be supplied to EditorView2 by Presenter1.
This approach seems sloppy and error prone. Is there another way to do this?
As I was writing this question, I realized a much more clean approach.
The problem with what is described above is that I was intent on re-using the first Presenter, Presenter1.
It would be much cleaner to abstract out the Editor portion of the EditorView1 code into a re-usable Editor widget (Editor1) which could be used by both EditorView1 and EditorView2. Now, I have one presenter, one view, and one EditorDriver. There is no need to juggle nested presenters or multiple EditorDrivers.

CellTree suggestion - AsyncDataProvider add/remove/update

I have issues with GWT CellTree and at this point, I'm wondering if it's really ready for prime time. Maybe I'm not getting the default use-cases??
Most questions that have seen over the Web so far are related to CRUD operations with a CellTree but using a simple ListDataProvider such as GWT - Add and remove nodes in celltree.
In my case, I'm populating the nodes of a CellTree using an AsyncDataProvider.
The nodes are fetched on-demand using a RequestFactory service.
Given a selection, I would like to add child nodes, remove/update the current selection. The GWT TreeViewModel interface is way too basic in my opinion.
From my current understanding, the way to go would be to use a map of DataProviders, keep a reference of the underlying list returned by the remote call and likely a reference to the parent NodeInfo object.
For example to delete the current selection I'd probably do the following:
TreeViewModel model = cellTree.getTreeViewModel();
TreeViewModel.NodeInfo nodeInfo = model.getNodeInfo(selectionFromChangeListener);
CustomNodeInfo parent = ((CustomNodeInfo) nodeInfo).getParent();
parent.getUnderLyingNodeListFromDataProvider().remove(selectionFromChangeListener);
// maybe force refresh using dataProvider???
parent.getDataProvider().refreshDisplayAsInRepopulateData()
Any better suggestion? It looks like it's going to be a challenging task, unless I'm mistaken... It seems a bit overkill though. In Swing it would be very easy to achieve or even in most other Web Frameworks providing Tree widgets.
Would using the default Tree widget and replacing myself the icons with the "loading" image be a more straightforward thing? It looks like the basic Tree allows way more manipulations of nodes as TreeItems.
The CellTree widget seems to be based on the original code of FastTree.
The FastTree already have more or less what I need, without some kind of setUserObject method as in a Swing DefaultMutableTreeNode
For spinning icons, someone already investigated it in the past.
I guess that for now, I'll switch from CellTree to a customized version of FastTree and FastTreeItem.
Out of the box ability to have methods such as getParentItem, remove(current node or specific child node)?? Thank you very much sir...

Loading a Workflow 4 from xaml file and adding it to workflowdesigner

I have created a couple of activities and stored them as XAML.
Opening them in the Workflowdesigner works great and I can Execute them.
Now I would like to create a new Activity and add the activities I created to it.
Basically loading it from the XAML and into the designer as part of another activity/flow.
I have tried adding my activities to the toolbox but the render as dynamicactivity and (understandably) does not work.
Any suggestions?
Is it even possible?
/Jimmy
DynamicActivity and the toolbox were basically not designed to work together that way. The toolbox expects to deal with types, not class instances.
One thing you can do instead is subclass IActivityTemplateFactory and in the Create() function return DynamicActivity. But you will probably have some really weird issues once you try to save a XAML file created which contains dynamic activities. Because in fact the designer doesn't do any special treatment for DynamicActivity, and it will not get serialized as any kind of 'logical reference' to the XAML file you created it from.
Tim