Avatar

Dave's Blog

@daveanthony / daveanthony.tumblr.com

This is Dave Thomas' BLOG
Avatar

Android Studio - Live Template - @StringDef, @IntDef

Today I created a useful Android Studio live template for auto generating @StringDef and @IntDef in java source code, that I would like to share with you.  My team does not use Enums in our Java android code for efficiency reasons. Instead constants are typically used in their place. Android Typedef Annotations are great for making sure the use of the constants stay consistent in your code base. They can be used to annotate function parameters to restrict the parameter to only accept Stings or ints that are of that Typedef.

The Typedef Annotations can be a little bit on the lengthy side code wise, so having a live template to quickly generate them is helpful.

The idea is we can write our constants, and then just generate the Typedef annotation interface based on those constants.

Goto preferences in your IntelliJ application:

image

In your preferences select Live Templates under Editor:

Expand Android in the Live Templates window and click the + sign to add a new live template

Select Live Template:

image

Now first let’s create our @StringDef template. Fill out your abbreviation to be stringdef. This can be another value of your liking if you wish. Typically this is what you would type to start your live template. 

( note: discussed later, this template will be triggered via the option+command+j hotkey ).

Next let’s fill out the Template text with the following code:

$SELECTION$ $END$
@Retention(RetentionPolicy.SOURCE) @StringDef({$enums$}) public @interface $name$ {}
The $SELECTION$ variable indicates that we would like to capture the current selected text in the editor. This allows us to use this selected value to generate our enum names inside of @StringDef. We will be selecting our constants that we want add an @StringDef too.

$END$ Indicates that our template code to be inserted comes after our selection.

$enums$ is a placeholder for the enum values we will generate, and $name$ will be filled out by us to name our new interface for restricting input for these enums.

Next you will notice some red text saying: No applicable context. Define 

Click on Define. 

Then select Java.

We are almost there! We just need to generate the enums inside @StringDef. Click on Edit variables.

We are going to use the Expression field to generate the value for our enums variable.

A little bit of groovy code will give us the behaviour we want:

groovyScript("_1.findAll(/String ([A-Za-z0-9_]+)/) { match, constant -> constant }.join(', '); ", SELECTION)

Paste this code into the Expression field for the enums variable, and select Skip if defined for enums.

Now click apply on your preferences pane and you are ready to try out this template!

To use the template you need to select some enums for it to generate an interface for. Then press option+command+l (control+alt+j) for the surround with live template action. If your hotkeys are different you can find this command via shift+command+a (a search for commands by name). After pressing option+command+j, select stringdef. Type an interface name and press tab. Vola! You are done.

Repeat these steps using what I taught you here to create a live template for @IntDef. You’ll need to replace @StringDef with @IntDef and in the groovy snippet in the regular expression, String with int.

Note: I ran into a bug while trying to write this blog post. I was unable to paste text into the Expression field and have it stay. If you run into this problem you can add the live template via an XML file in your settings folder for the IDE. Using this handy github repo: https://github.com/keyboardsurfer/idea-live-templates I was able to figure out where to put the live template file. For me it was in ~/Library/Preferences/AndroidStudio2.2/templates. The version number of Android Studio may be different for you.

Here is this live template as an XML file for importing:

Avatar

Displaying WordPress blog posts on a static front page along with page content

Recently I rewrote a website I was working on as Wordpress site from just HTML&PHP and simple custom CMS. The site's layout had a front page with two columns. One column was the intro to the site, and the other column included the website's news posts.

I wanted to recreate this in Wordpress and have the blog posts show up on one column on the right, and then have static content on the left. As hard as I could google, couldn't find anything that covered my issue.

Nothing is impossible, and really the solution I came up with was fairly easy to do. It is well documented how to create a static page... ( and simple enough to figure out ) so I created a static front page, with the left column content. Then I created a theme file for the front page. Calling the theme file front-page.php will make it the default for your theme. Inside front-page.php I created two divs for the two columns and styled it the same as the original site. Inside the first div, the left one, I did the obligatory:

the_post(); the_content();

To display the static page's content.

Then inside the second div for the right side, I did this to grab all the blog posts:

foreach ( get_posts() as $post ) {

setup_postdata( $post );

the_title(); the_content(); //Get creative here ;)

}

The get_posts() function will return all of the blog posts in an array. You can pass it options such as numberposts, to choose how many you get. Look it up in the Wordpress codex to see all the options. 

setup_postdata takes a post object and makes it the current post that is affected by all the "the" functions. Ie the_date() or the_content().

And thats all there is to it really... if you know Wordpress well, its probably something you already know how to do. But if your a newbie to it this may help you figure out how to achieve displaing blog posts on any static page.

Avatar

A Year with Yii...

Well it hasn't been a year, since I first was introduced to the framework "Yii", but it seemed like a nice title. More like 8 months

It appears that 8 months is the magical time when you get your first teeth. My daughter's first tooth is making its debut this week. Which means not much sleep for dad. After a week of up all night, I find myself a little insomniac.

Now I sit awake, head spinning with programming thoughts. As I do most nights,  I contemplate on what I am working on and brain storm new ideas as I go over it in my head (usually before falling asleep).

Why not blog? Maybe it will clear some of my thoughts.

 It has dawned on me that I'm actually starting to feel comfortable with Yii. I've had some time to work with it now. Yii is a php framework for rapid web development. I first became acquainted with it in the fall of 2011 during my PHP Level 2 course at BCIT. We had to choose a framework to use in a project and present it to the class. It was a tough decision at the time, there are a lot of PHP frameworks and they are all so daunting. Unlike JavaScript where they pretty much pushed us in the direction of the framework, jQuery (no complaints here about that).  

 I'm glad I chose Yii, not only is it one of the newest, it has turned out to really grow on me and proven to be a powerful tool. I've had some time now, to pursue my interest in the Ruby programming language. And the Yii framework is similar in it's approach to MVC as is Ruby's rails framework. I was a little tempted to switch to Ruby for my projects, but because I'm not at the virtual private server level with any of my projects, it doesn't seem worth it for shared hosting. But because Yii has it similarities to Rails I've settled with being a PHP guy. 

 I can see now Yii has made me a better programmer. I look at some of the PHP I've written before using a framework, and I can see all the short comings so clearly. A little SQL here, a whole lot of procedural programming there, and a dash of HTML because I was in a hurry. When I'm in a hurry with Yii, it is all already separated and structured for me before I start working. 

 Even in other languages, I find myself structuring my projects with a MVC look. I just finished an advanced java class at BCIT. One project was to build a database management application (ie phpmyadmin but desktop). My teacher gave me a good grade and told me "it was heavily over engineered". I took it as criticism at first, until another classmate opened my eyes to the fact that it is better to be over engineered than under. And this played out to be oh so very true, because for the next assignment I needed to extend the first assignment's functionality. With my project nicely laid out in an MVC way, the changes were minimal and the second assignment happened very quickly for me.

 I started using Yii heavily in my personal website. This is where the learning and familiarity began. I created behind the scenes invoicing for customers, if need be.  This required a few models, and opened my eyes to RBAC, role base access control.  When I first used Yii for my PHP level 2 project we cut a lot of corners to get the project done. Doing a lot of things outside of Yii's framework. Had I known that cutting corners only made it harder... But that is how it is when you start learning a framework. You don't know how to do everything with it. Using it in my own site, I took the time to learn the right way. Which in the end was the wrong way I'm sure for some things being that it was my frist time, but that is how you learn.

 I then turned to my company's website, where I work during the day, and tore it down. I replaced it with a brand new Yii site, and included an extension for CMS. Learning more along the way.

Recently I took that company site and have started building a backend to replace some C#.net applicationa I've written for the company. Moving away from the .net desktop application was a personal choice. Hence becoming a mac user, I really didn't like having to virtual box to code.

While I've been working on this project, it finally clicked, and I got it. Me and Yii gelled. Everything started making sense, and I stopped trying to fight the framework, but instead utilized it's power. I was familiar with it and knew what to use.

And, it has made me learn even so much more about PHP! Just like when me and jQuery first had our "Ah ha!" moment, jQuery helped me learn more about JavaScript. I know people think frameworks hide the language and make newbies learn less about programming. But if you really get to know a framework, you start to get to know its inner programming. And if it is written well, you learn more about the language it is written in.

I think that it takes time for a framework to become useful to you. You will fight them at first. It will always feel longer when using a new framework. All the time you will waste reading online documentation just to figure out something so simple to do. Something you could so easily do with out the framework and just your programming language. But if you give a framework time, and learn it well, they will work magic for you and empower you. (don't waste time writing a framework, it's already been done for you)

Avatar

Using jQuery with Yii

I recently was looking for a tutorial on the proper way to use jQuery with yii. Rather than just including it in the head in my main layout, like I would normally do. I noticed that yii uses it quite frequently for a lot of its widgets (date picker for example).

So I set off to find out. With the info a little thin on it, I was a little ticked to come across a blog tutorial on the subject with improper instructions. Instructions that led to code that did not work at all. Now this could be due to the fact that frameworks change, and the post could be outdated. I did eventually find the correct way to do it, and I feel I should re post it myself just so there is more correct update info on the subject out there.

Please let me know if this ever becomes out of date. The fellow whose tutorial was completely wrong had comments turned off. I dunno but I'd rather people let me know when I'm wrong.

jQuery can be added to your site with one simple command in Yii:

Yii::app()->clientScript->registerCoreScript('jQuery');

Then you can register any custom script of yours one of two ways;

Inline:

Yii::app()->clientScript->registerScript('random-unique-id', 'alert("javascript here!")',CClienScript::POS_READY);

External file:

Yii::app()->clientScript->registerScriptFile('myscriptfile.js', CClientScript::POS_HEAD);

Now one question that I saw asked that wasn't really answered, was where in your code should you put your jQuery register commands?

Well following yii standards, I'd do what they did with the framework. Register the jQuery as you need it in your own widget classes. Using the asset manager class you can package the necessary scripts with your widget. I'd show how to do this too, but maybe another time.

This way the script is only ever loaded when needed, and you aren't mindlessly adding it to all your views to accomplish this. You just load the widget you need, and the code is in one place. And rather than being in the main layout, where it would get loaded every time, it is accessible only when needed.

It may not make sense for simple little jQuery here or there, but I its a good way to package your code for reusability. Think in a modular way.

Avatar

PHP Frameworks

Image
Image
Image
Image
Image
Image
Image

PHP Frameworks are great, they take all the common tasks of a PHP programmer and bundle them in a nice Object Oriented package. Using a framework can help you focus more on the business logic of your web application and less on the plumbing coding that can become very repetitive and is usually very common across many websites.

After completing COMP 2920 at BCIT (the final PHP class in my web software program), it became very apparent to me that I had to start focusing on choosing a PHP framework.

My instructor, Jason, hammered home the importance of using a framework. He made some very good points; 

  • frameworks save you time
  • make your code neater
  • they are tested thoroughly by many people (thousands of users)
  • they have security in mind (maybe some things you never even thought of)
  • the quicker you are at building applications the more competitive you are
  • AND the more money you will make

I estimated 100 hours when I started the fitness trainer site (fitnessprint.com). The design was done when I came on board, and I was mostly just building the customer area, trainer control panel (admin area), plus implementing payments. In the end it took me 300 hours. If I had been more experienced at the time of starting, and more open to using a framework, it would've saved myself a great deal of time.

Using a framework would've ment a complete rewrite of the site, because the site is built on another developers CMS/framework system. (He was smart to do this) but using a framework would've been the smarter way to go. I ended up having to learn his system with no documentation in order to extend the site. Frameworks are nicely documented. I am happy with what I did, since the customer was pleased with my work. But I am still new and learning tons, and using a framework is precious lesson.

Yes, you can build upon your own PHP code and create your own framework for your most common purposes. In the past PHP developers have worked this way as it sped up the development of their sites. But now a days you can be faster by using one of the many community supported PHP frameworks out there. Because unlike your code, the framework is tested by many users constantly and is built on nearly two decades of php experience. It would take you a life time, and you probably wouldn't come close to being as good as a framework.

I learned jQuery pretty quickly after learning javascript. None of the other frameworks, I've tried come close to being jQuery. It's great, hands down the best out there for javascript in my opinion. Having learned a framework in javascript I was open to the idea of a PHP framework (knowing the benefits of using a good framework). When I came to the realization that I wanted to start using a PHP framework, I wanted to find what I felt was jQuery to PHP as jQuery is to javascript.

Well I've used code igniter, I've tried cake, flirted with the agile tool kit, and I've used Zend. But, it appears that my jQuery for PHP is going to be Yii. 

I used Yii for my last COMP 2920 assignment that I did with my lab partner Curtis: 

It was awesome... unlike all the other frameworks, I actually enjoyed using it. Well it may not be the jQuery of PHP in some people's eyes. It definitely is in mine. 

If you haven't tried Yii, I'd give it a considerate look. The auto-code generation and active db models are wicked to use.

You are using an unsupported browser and things might not work as intended. Please make sure you're using the latest version of Chrome, Firefox, Safari, or Edge.