Formatting a Binding<String> that's within my TextField - swiftui

I'm using a TextField to collect a User's phone number. I'd like this field to be nicely formatted in a way that, instead of displaying 1234567890 on the frontend, the field is formatted like 123 - 456 - 7890. I've been searching for the last hour or so and am struggling to find any sort of direction to what I'd imagine was a simple task.
Here's a basic idea of what my TextField looks like: TextField("", text: $phoneNumber)

Apple provides a phone number support by default for TextField (but this will not add the '-' between numbers as this is different in different parts of the world - this is my guess):
TextField("", text: $phoneNumber)
.textContentType(.telephoneNumber)
.keyboardType(.phonePad)

I ended up utilizing https://github.com/marmelroy/PhoneNumberKit to get this to work. I used this gist https://gist.github.com/jbnunn/dab656b53f6e4ee2f53730f0b8daee64 to integrate it with SwiftUI.

Related

Select distinct count cloudant/couchdb

I am starting a project using Cloudant.
It's a simple system for logging, so I can track the usage of my apps.
My documents looks like this:
{
app:'name of the app',
type:'page view | login | etc..',
owner:'email_of_the_user',
device: 'iphone | android | etc..', date:
'yyyy-mm-dd'
}
I've tried to do some map reducing and faceted searches, but couldn't find so far the result for what I want.
I want to count the number of distinct documents grouped by same owner, date (yyyy-mm-dd), and app.
[For example, if a the same guy logs in the app twice or 20 times in the same date, it will be counted only once.
I want to count how many single users used an app each day, no matter what's the type of the log, or the device he used.]
If it was SQL, assuming that each key of the document is a column, I would query something like this:
SELECT app, date, count(*) FROM LOGS group by date, owner, app
ant the result would be something like:
'App1', '2015-06-01', 200
'App1', '2015-06-02', 232
'App2', '2015-06-01', 142
'App2', '2015-06-02', 120
How can I get the same result using Cloudant/CouchDB?
You can do this using design documents, as Cesar mentioned. A concrete example would be to create a view where your map function emits the field on where you want to group on, such as:
function(doc) {
emit(doc.email, 1);
}
Then, you select your desired reduce function (such as _count). When viewing this on Cloudant dashboard, make sure you select Reduce as part of the query options. When accessing the view via URL you need to pass the appropriate parameters (reduce=true&group=true).
The documentation on Views here is pretty thorough: https://docs.cloudant.com/creating_views.html
For what you need there is a feature on couldant/couchdb called design document. You can check their documentation for this feature for details or this guide:
http://guide.couchdb.org/draft/design.html
Cloudant documentation:
https://docs.cloudant.com/design_documents.html
Design documents are similar views on the SQL world.
Regards,
We were able to do this in our project using the Cloudant Java API...
https://github.com/cloudant/java-cloudant
You should be able to get this sort of result by creating a view that has a map function like this...
function(doc) {
emit([doc.app, doc.date, doc.owner], 1);
}
The reduce function should look like this:
function(keys, values, rereduce){
if (rereduce){
return sum(values);
} else {
return sum(values);
}
}
Then we used the following query to get the data we wanted.
Database db = ....
db.view(viewName).startKey(startKeys).endKey(endKeys)
.group(true).includeDocs(false).query(castClass)
We supplied the view name and some start and end keys (since we emitted a compound key and we needed to supply a filter) and then used the group method to get the data back as you need it.
Revised..
With this new emit key in the map function you should get results like this:
{[
{[app1, 2015,06,28, john#somewhere.net], 12}, <- john visited 12 times on that day...
{[app1, 2015,06,29, john#somewhere.net], 10},
{[app1, 2015,06,28, ann#somewhere.net], 1}
]}
If you use good start and end keys, the amount of records you're querying will stay small and the number of records you get back is the unique visitors you are seeking. Note that in this scenario you are getting back a bit more than you want, but it does work.

Passing multiple values in a django url with dashes

I am passing several values to a view through my url. Through some blind luck (read experienced debugging (: ) I fixed an issue I had where the view was interpreting the arguments in an unexpected fashion. The old url looked like:
url(r'^explore/(?P<id>[\w\-]+)-(?P<region>[\w\-]+)-(?P<location_name>[-\w]+)-(?P<page>[\w\-]+)/$',
'project.apps.web_feed.views.display_feed', name='display_feed'),
My display_feed view looked like this:
def display_feed(request, id, region, location_name, page):
url_scheme = id + '-' + region + '-' + location_name
print location_name
There are several instances where the value for location_name was two words and the url that was called would look like this:
/explore/90-LA-Los-Angeles-0/
In my display_feed view, the value of location_name would be:
Angeles
This caused obvious problems when trying to query data and display it on the page.
When I rearranged the order of the url to have location_name as the first value like so:
url(r'^explore/(?P<location_name>[-\w]+)-(?P<id>[\w\-]+)-(?P<region>[\w\-]+)-(?P<page>[\w\-]+)/$',
'heylets.apps.web_feed.views.display_search_feed', name='search_feed'),
and updated the view to correspond with the above changes, the logic works swimmingly and the data displays on the page as expected.
I assume there are issues with the structure of the url, but I don't want to chalk this up to inexperience and move on without first understanding what I was doing wrong.
I think the issue is you were putting a ton of info in the same place without a clear delimiter. You were separating your values with a "-", but in the case of a multi-word name you were using "-" as a separator too, which can lead to unexpected results. If you are going to keep the same structure, separate your multi-word names with "_" or something different.
There may be better approaches, like the one points out: querystring.
You can get them on a dict and use them later, and you won't have that issue.
Another approach could be to separate your args with a different char, so your urls look like "/explore/90/LA/Los-Angeles/0/". This is the way it is shown in django tutorials and way cleaner than your approach.

Nintex Forms 2013 - Conditional text

I am using NINTEX FORMS, no previous experience, and I want to set the value of a tag's text based on the value of a key I send through the query string.
What I am trying to accomplish is something like this:
If(fn-GetQueryString(TipoSolicitud) = "X", "Text1", "Text2")
But this doesn't work at all. Anyone knows if this is possible? and How to do it?
Thanks!
You should use "fn-If" instead of "If" in your formula.
The correct formula should be like this:
fn-If(fn-Equals(fn-GetQueryString("TipoSolicitud"),"X"),"equal","not equal")

Regex Input Validation Limit to maximum no. of commas

I am running jQuery Autocomplete with a Laravel form field.
It grabs data from my db
Specialty Area Examples: Real Estate, Mortgage Lenders, Renovation, Buyer's Agent, Listing Agent, Relocation, Short-Sale, Consulting, Local Experts, Refinancing, Architecture, Home Building, Carpentry, Electrical, Engineering, Interior Design, Landscaping, Painting, Plumbing, Appraisal, Commercial Property, Insurance, Legal, Conveyancing,
Users can type in one of the examples and the autocomplete will complete the rest in the field.
I want to limit the user to being allowed to input a maximum of 4 Specialty Area Examples into the form field. So a user can type in for example:
Real Estate, Short-Sale, Consulting, Local Experts
After that the user should not be allowed to input more data. So the maximum number of commas I need to set in the form field is 3.
Try this:
$("#txtBox").keypress(function (e) {
var input = $(this).val() + String.fromCharCode(e.which);
if (input.split(',').length > 4) {
e.preventDefault();
}
});
Demo: http://jsfiddle.net/y6eQF/
This RegEx should do what you want: ([a-zA-Z0-9\-\_\ \'\"]+\,){3}[a-zA-Z0-9\-\_\ \'\"]+
You can also do it with split() as Vinod mentioned. In PHP you have split()/explode() as well.

Custom field / form field (maybe widget?) for storing values for a string format

I wish to create a model where I can store a string formatting pattern as well as accompanying values.
Example:
Pattern = 'Strength deals %d more damage for each %f%% critical chance'
Values = [2, 1.50]
The pattern only has to store ints and floats. My initial thought was to simply create a custom field, validate the values and that would be it. You can see my field here
However, this simply lets me save the list. I still need some way to figure out how many values to validate, check that it matches the other field (a simple CharField). This could be done "manually", but I would like to create a custom form field that would generate X input boxes that match the number of wildcards in the pattern.
Question 1: Is there any way to "link" two fields so that one can act on the value of the other?
Question 2: To create this, I would probably need to create a new form widget, but is this possible? Any hints as to how to start?
This may not exact answer, but writing it in comment is not feasible.
Another option would to store as JSON string in the model. Like
[{ "Pattern": 'Strength deals %d more damage for each %f%% critical chance',
"Values" : [2, 1.50]
}]
Use custom form field to input/output as JSON input. There are quite a few implementation available when searched on google.
With this approach, you can try to validate the inputs, so that format string and number of variables provided matches.