Is it OK to Move Common ViewController Presentation Logic into Presentation Helper Classes - swift

I'm trying to use layers to make sure I separate everything into it's correct areas in my Swift / iOS / Xcode 6 project. The question, ultimately, is: is it OK / a common practice to move commonly used presentation-level logic to a separate presentation helper class instead of writing it over and over in multiple view controllers with little or no difference?
Here is an example to tie this in and give context:
One of the things I am aiming to do is use a UITableView to display report data. This UITableView will contain 7-10 rows, depending upon the user's preferences (nsuserdefaults). Each row contains a localized string for a label, and some decimal value.
As an example one row could be "Sales made this week: $500.00"
I have a reporting service class that's responsible for talking to the database and getting back / instantiating a report object. This report object contains the raw data for the report, i.e. how much you made this week, this month, this year, etc. Whether the user wants to show all these values or not is irrelevant to the service - it simply gets everything.
So since I have 3 view models that use this same report, I thought it would be wrong to rewrite the same code each time that checks the user's preferences, then creates/binds an array to the UITable and matches the labels with the report values from the object returned by the service.
A better way, I thought, was to create a presentation-level helper class whose job would be to take a report object (the thing I mentioned before that contains the report values), take a user's preferences, and then more generically combine them to create a list of localized strings matched with their respective report values, agnostic to what the view controller wants. Maybe if that requirement changes later (where different view controllers need more customization) I could use flags or different function names within that class.
This way all I have to do is something like
var report = ReportHelper.GenerateReport(reportData, userSettings)
and now report would be an object that could look like this (mock JSON data):
{"Amount made this week":"$100", "Amount made this month":"$500", "Amount made this year": "$10,000"}
And I can use this in any view controller.
The alternate is to just hard code those above values (obviously still pulling localized strings) but I don't know if I adding all those strings + checks based on user preferences + formatting. Seems more elegant to move it away.
Thanks!

I know too little about your problem to provide the definite answer, but basically you have 2 options:
- inheritance
- composition
I personally like inheritance although it is often stated you should choose composition over inheritance.
Your helper sounds somewhat like composition, so that would be the preferred setup. With your specific problem as I understand it, inheritance would lead to a duplication of data, so that is one more argument to choose composition.
So all in all you seem to be about right
Edit:
Inheritance is an "is" relationship, whereas composition is a "has" relationship.
See http://en.wikipedia.org/wiki/Object_composition for more details about composition.
And see http://en.wikipedia.org/wiki/Composition_over_inheritance for more info why and when to choose composition

Related

Implement the Lead conversion using custom button

I will need to create a Custom Button "convert lead" that will perform the same functionality as Standard Button "Convert" when the button is clicked.
What is the best approach to do it..?
That's a very broad question. What exactly you need, what have you tried so far? Do you really need just a button that opens the conversion page or something more?
If you want to somehow recreate it with Apex... Core of the coded solution would be the Database.convertLead method. You don't pass to it whole leads (like to Database.insert for example) but instead just their IDs + bunch of control flags to make it do exactly what you need. Read up about LeadConvert object. And similarly you can get Account/Contact/Opportunity ID from the result object if it succeeded.
I don't think there's programmatic way to access field names & mappings defined by administrator for lead conversion. Maybe you'd need to store this info somehow (in helper object? custom metadata?). So then you'd query the field names from metadata, then query the lead fields and finally display table of fields & mappings to the user.

NSComboBox with alternative ways of writing

I'm trying to implement a autocomplete function into a small app. I want to give the user the possibility to write down a city (from a long long list of cities) into a NSComboBox. That works fine, as long as the user is using the exact writing of the city inside my array of city names. But if they use, for whatever reason, a different spelling, it fails and the city is not found.
So if the user is looking for "Köln" for example, it's not a problem, but if he is looking for "Cologne" he wouldn't be able to find it.
For this I have, for each city an additional array of alternative spellings.
Now I would love NSComboBox (or any other type of TextField) to look not only in "city.name" but also within the array "city.alternativeNames". The shown value should as well represent what the user is writing.
I don't want to create an entry in the list of the pop-up part of NSComboBox for each alternative name, as that would make the list even longer than it is and would confuse people (cologne alone has 85 different spellings).
Thanks for your suggestions, I'm completely new to NSComboBox.
Interesting problem. I think probably you need to choose a different way to structure your data to make it easier.
Consider the lookup method (matching a string). Structuring your data for this case should account for the preferred spelling of each city (preferred by you, for the scrolled list).
How about a flat array of cities (to allow a simple search based on user spelling without also having to check each possible array of alternates and manage sorting them properly) but each has an optional (can be nil) "preferred spelling" pointer to the "correct" one. When displaying the options in the combo box, show the array filtered by those with no optional preferred spellings plus the currently-typed partial/full completion of the alternate spelling?
So a City has a name property and an optional preferred property. In your case, if Cologne is preferred, the Köln instance would have Cologne set as preferred. Köln would only appear in the list if the user typed it (even partially) and it would automatically be in the correct alpha-sorted position (assuming your cities are kept sorted).
Does this make sense or do I need to rephrase? Haven't had enough coffee this morning. :-)

Game: General Structure and Game Item Structure

I am making a graphically simple 3D game in C++ using DirectX. The main problem I am having is with how to structure some things efficiently. Right now I know my goal for certain areas but not how to ideally perform them.
For instance, right now I am storing all meshes and textures in an Asset class with enumerated definitions pointing to each asset. All meshes and textures are loaded when the game starts by creating an Asset object and initializing it. From there I load meshes and textures to objects by assigning the pointer given by the Asset object. Is this the best way to go about this?
A harder topic is that of items. For the sake of argument I am going to say we are dealing with scenery. Right now my scenery needs the following:
A name
A mesh
Textures (optional)
Flags (such as Destructible, Flammable, etc.)
Status (such as Burning, Locked, Cursed, etc.)
Abilities (things the object actually does, such as becoming damaged when burning)
The first 5 things on this list are simply variables. But abilities are bothering me. The first 5 can all exist in one item class. But what I do not know how to do is somehow attach abilities to the object.
For example; I want the object to have the "Fire Nova" ability. At the start of each turn, the object damages anything near it.
For me, this means each object would need a "Trigger_Phase_TurnStart()" or some similarly named method and would then need a new "Fire Nova" class with its own unique action for that trigger and its own extra variables (Damage, Range, etc). So now I have the base object class and a new Fire Nova class that inherits from it.
But now what if I needed an object that has Fire Nova and Frost Nova and also Slowing Aura and other such abilities. Basically I need a way to add triggered effects to an object without needing a new object class for each of them.
So by the end I would be able to have say:
(pseudo code of the object's components)
name = Elemental Orb
mesh = "Mesh_Sphere"
textures[] = "Tex_Ice", "Tex_Fire"
flags = OF_Destructible
status = SF_Cursed | SF_Burning
abilities[] = Fire_Nova, Frost_Nova, Slowing_Aura
And this orb would simply be the object class with all these attributes. The object would then activate each stored ability's trigger at the appropriate turn phase for any actions to perform.
Right now I am thinking I might need to make a class for each ability possessing every turn-phase or action trigger (inherited from a base ability class) and have them perform the appropriate action when the object calls them from it's array of abilities. Would this be the best way to do this?
As a separate issue. Some flags would require additional variables that would otherwise be unnecessary. For example, Destructible would mean the object would have health whereas without the flag it wouldn't need it, or an Openable item would need an array of contents. What would be a good way to ration these variables to when they are needed? I do not need every wall to have health and an empty contents array for example.
Finally. Assuming the bullet-listed attributes above are all an item needs, how might you suggest I best store them? To clarify, I would like to be able to write:
CreateItem(ITEM_CHAIR);
and have this return the created object with name, mesh, textures, flags, status and abilities. What structure might be suitable for achieving such an end effect?
To summarise:
Does my current Asset storage seem feasible?
What is the best way to create abilities to attach to the object class without making numerous separate object classes?
Is there a way to limit the variables (or at least memory usage) when the associated flag is not present?
What structure or format would be best for storing fixed item definitions?
Sorry if this is a little long winded. If you cannot answer all the questions then an answer to one would still be appreciated. Question two is probably the largest priority for me right now.
Thanks for your time :)
I want only answer question 2 and it looks like you need a decorator pattern. In OOP a decorator pattern is useful when you have many ingredients and wants to decorate an object, for example a pizza. Unfortunately you need to create for each ingredient a separate class hence I think you have the right approach. The good thing is that with the decorator pattern you can wrap the object over and over with abilites classes and call only one method at the end to do all the stuff.

Parsing multiple XML web file iphone

Let's say i have a xml file with a tag named which contains the number of fields i want to show in my tableView and in another xml file i have the information to be displayed in that tableView.
The question is : Should i create 2 different file in my project (xmlparse1.h and .m + xmlparse2.h and .m) or should i just put all my code in 1 (xmlparse.h + .m) and differenciate which file i am parsing at the moment with a bool or something like that in the code?
I am developing an iphone app on Xcode 4.3 mac os x 10.7.4 if this might change
EDIT: 1st file :
<MenuPrin>
<humidite>82,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0</humidite>
<tempmoy>
189,124,700,700,700,700,700,700,700,700,700,700,700,700,700,700,700,700,700,700
</tempmoy>
</MenuPrin>
2nd File :
<Instal>
<nbrField>2</nbrField>
</Instal>
So it looks like this. So, since nbrField is 2 I would take the 2 first value from tempMoy and humidite and show them in a tableView.
END OF EDIT
If how you're parsing is similar in both instances, to add two sets of files that do effectively the same thing would be extremely inefficient and considered bad practice. It makes a lot more sense to create a parser that can handle the two different files than to write two parsers that can only handle a specific file each.
To more specifically answer your question, I would determine which file you are parsing and behave accordingly, whether you do this with a bool or something else is up to you.
EDIT: Here is the idea that just popped into my head, so if for whatever reason this wont work for you(Like I said its been a while), or someone has a better idea, I'm all ears. You could still have a set of files(.h and .m) that contain the definition of your parser. Your parser could contain within it a two variables of type Object1 and Object2 which are built in such a way that they resemble the data structure that you need to store what you parse once for your first type of file, and a different definition for your second type of file. This way when parsing once you determine which file it is you are reading, it just changes which of these two objects you write into. There are also plenty of variations for how you could set this up, and I can also think of a few cases for what you might be trying to do where this might not work, but there is the idea regardless.
If you think that the two sets of files approach is better for the application you are trying to write and makes more sense to you, given what has been discussed in the comments, it isn't necessarily a bad idea.
In case if u want to create two table view in one view u need to create a single .h & .m file...If u want to create individual table view u need to create two .h & .m...
Try TBXML parser...that is the easiest way and less memory usage for xmlparsing...

Drupal - dynamic options for text_list field

I have a custom node type for which I want to have a field that uses a special combobox based on list_text. When one chooses the type list_text it is normally possible to enter a static list of selectable texts, however, I want this list to be dynamic, i.e. based on the results of a db_query. What is the best way to do this using Drupal 7?
A simple example for clarification: A node of this custom type X contains a field that points to another node, so whenever a node of type X is created I want a combobox that contains all other nodes.
(Best solution would be to only display the combobox during node creation, and no longer during edit. But I could also live with it if the combobox was shown during the edit as well.)
I have tried to customize options_select by defining my own data type and implementing hook_options_list accordingly. The combobox was displayed during creation with the correct values, however, I could not save it.. I have no idea what went wrong there, but on the first submit it would change to a different theme, and when I tried again I got an internal server error. Am I on the right track at all with defining a completely new data type for the field? there surely must be a simpler way?
You're right in that you don't need a new datatype. Here's a good tutorial on how to do this. It's not specifically for D7 but I didn't see much that wasn't still applicable. There may be a better way to do it in D7 specifically but I would love to know it too if so :)
The tutorial linked by allegroconmolto sent me on the right way. Thanks for that.
Here's the simpler way of doing it: tutorial
Basically, it is, as I assumed, a common problem and hence a simple solution for it was included in the webform module by now. It provides a hook_webform_select_options_info which can be used to register a callback method. The callback method is then called each time a corresponding option select of a webform is shown, so that you can easily fill it with the results of a dbquery or anything else. Works like a charm and takes next to no time to implement.