LTHRadioButton

:  ~ 1 min read

I was in need of a radio button recently, and it had to be designed like a Google Material Design radio button. Nothing too fancy, but I wanted to spice it up a little bit, by adding a nice animation. The idea was to:

This is the result:

The library repo can be found here, and it […]


Continue reading →

Adding string attributes slightly easier

:  ~ 1 min read

Say we have a composed string that looks like this:

let date = "22 July, 2017"
let value = "€ 148"
let quantity = 5
let string = "\(quantity) of your items, in value of \(value), have been delivered on \(date)."
// 5 of your items, in value of € 148, have been delivered on 22 July, 2017.

It would be nice to emphasise the important bits, and the usual approach would be to create an NSMutableAttributedString, and to add the required attributes:

let attributedString = NSMutableAttributedString(
  string: string,
  attributes: [
      NSFontAttributeName: UIFont.systemFont(ofSize: 14)
      NSForegroun […]

Continue reading →

Combining protocols

:  ~ 1 min read

Let's say we have a controller that can fetch some data. What would this imply? A loading spinner, the fetching of the data and the update of the UI. We can create a protocol for this, maybe Fetchable:

protocol Fetchable {
  func showSpinner()
  func fetchData()
  func updateUI()
}
[...]
class Controller: UIViewController, Fetchable {
  // showSpinner, fetchData and updateUI are required
}

But showing a spinner and updating the UI could be useful by themselves, so we could extract those into separate protocols:

protocol Loadable {
  func showSpinner()
}

protocol Fetchable {
  func fetchData( […]

Continue reading →

Queueing up async jobs

:  ~ 1 min read

I recently had this problem: at the start of the app there's a call to fetch some reference data, on which other calls depend, but it shouldn't hinder the app launch itself, nor anything else that doesn't depend on it. So, after several approaches, I decided to use dispatch_groups.

First, a struct to abstract a dispatch_queue and a dispatch_group:

struct Queue {
	
  let group = dispatch_group_create()
  let queue: dispatch_queue_t
	
  init(label: String) {
    queue = dispatch_queue_create(label, DISPATCH_QUEUE_SERIAL)
  }
}

This class should have a way to add a closure to the queue, that won […]


Continue reading →

[NJS] Creating a new post

:  ~ 1 min read

Last post in the series is about creating a new post. This wasn't really needed, since the whole point of having my blog in Dropbox was to use any markdown editor I desire, save the file, run the sync command, and have it live, but for the sake of learning, I went ahead and added this feature too.

First, the routes:

router.get("/create/" + process.env.MY_SYNC_KEY, function(req, res) {
  res.render("create-post", {
    title: "Create",
    metadata: "Create a post"
  })
})

router.post("/create", function(req, res) {
  if (req.params.token != process.env.MY_SYNC_KEY) { res.redirect("/"); return }
  const b […]

Continue reading →

[NJS] Dropbox syncing

:  ~ 5 min read

For Dropbox handling I chose a pretty small library, node-dropbox. To use it, I went in Dropbox's developer dashboard and created an access token (instead of using secrets and keys) and saved that in my .env. Then onto the helper:

const dropbox = require('node-dropbox').api(process.env.ACCESS_TOKEN)

function Dropbox() {}

Dropbox.getFolder = function(path) {
  return new Promise(function(resolve) {
    // This will get all the metadata for each file from the folder.
    dropbox.getMetadata(path, function(error, result, folder) {
      if (error) {
        console.log('Getting ' + path + ' failed.') […]

Continue reading →

[NJS] Database handling #3

:  ~ 1 min read

Last time we created a basic database handling functionality, but it was clear we can improve it: posts aren't the only ones that need fetching, and requiring the dbconfig file everywhere (and configuring the object) will become cumbersome. So, a first step would be to create specialized DbConfig objects:

DbConfig.page = function(page) {
  const  config = new DbConfig()
  config.offset = config.limit * (page - 1)

  return config
}

DbConfig.post = function(link) {
  const config       = new DbConfig()
  config.fields      = ['link']
  config.fieldValues = [link]
  config.limit       = 1

  retu […]

Continue reading →

[NJS] Database handling #2

:  ~ 4 min read

Last time I was talking about the DbConfig model and the fetchPosts function, so let's dive in. First, a few examples of using the config:

const Db = require('../lib/db')
const DbConfig = require('../models/dbconfig')

// Fetching one post
const config = new DbConfig()
config.fields      = ['link']
config.fieldValues = [req.baseUrl.substring(1)] // The path without the initial `/`
config.limit       = 1

// Search, meaning to fetch based on a query
const config     = new DBConfig()
config.searching = true // Special logic for creating the query command
config.limit     = 0 // All
config.fields    = […]

Continue reading →

[NJS] Database handling #1

:  ~ 3 min read

With Sinatra, I was using DataMapper as ORM (Object Relational Mapper) for Postgres, and I got a bit spoiled, because it makes things really easy. You first have to define your data mapping:

class Post
  include DataMapper::Resource
  # Set the number of characters for these types of fields, if the DB supports it
  DataMapper::Property::String.length(255)
  DataMapper::Property::Text.length(999999)
  property :id, Serial
  property :title, Text
  property :body, Text
  property :datetime, String
  property :modified, String
  property :link, String
end

And using it is really straightforward:

 […]
Continue reading →

[NJS] Project structure and layout tips

:  ~ 2 min read

In the previous posts I mentioned file locations, but I haven't been explicit on what my structure is, so let's go through it:

- assets
    |___ javascripts (3rd party js)
    |___ stylesheets
    |___ images
    |___ files (random files I want to serve, like my resume)
- lib (let's call them helpers)
    |___ tasks (anything I want to run on Heroku)
    |_ db.js (for communicating with Postgres)
    |_ dropbox.js (for communicating with Dropbox APIs)
    |_ feed.js (just one function to create the feed.xml)
    |_ sitemap.js (just one function to create the sitemap.xml)
- models
    |_ post.js […]

Continue reading →