Category: Coding

  • Mac OS X – Sublime Text 2 key bindings suddenly stopped working (SOLVED)

    https://www.sublimetext.com/docs/2/revert.html

    I’m using a Mac Mini with OS X El Capitan (v. 10.11) with Sublime Text v.2.0.2.

    Today, Sublime Text shortcuts like Cmd + / (line comment), Cmd + D (multiple selection), Cmd + F (find) and others stopped working. I had no problem with those key bindings before until today.

    Based on research, I found that it could be caused by packages installed to Sublime Text. Aha! It could be the GoToClass package that I just installed today! I uninstalled it since I was desperate and the package didn’t work as I need it to anyway. Still no luck, my favorite Sublime Text shortcuts still didn’t work.

    I decided to revert my Sublime Text into a clean install. That fixed my key bindings that stopped working.

    I’ll show you how.

    First, quit Sublime Text 2.

    Then remove the “Sublime Text 2” folder that’s located at ~/Library/Application Support/. You may try to do so by running this command in the terminal:

    FYI: The backslashes are required to escape the white spaces in file/folder names. rm is the command for removing files/folders. The -rf flag means that we want to remove the folder we specified and all its contents recursively.

    Once deleted, run Sublime Text 2 again. You’ll notice that it’s back to its freshly installed state. 🙂

  • Laravel 5 RESTful API with Basic Auth: PHPUnit Testing

    I’ve been using PHPUnit to test my RESTful API. PHPUnit is ready to use in Laravel.

    In my Laravel-based RESTful API, I have an endpoint /api/users which returns JSON data when you send a GET request. The API will only return such data if the client sends the corrent username and password (Basic Authentication). The JSON data looks like this:

    I’m gonna show you how to create a PHPUnit test for this kind of JSON API data in Laravel 5.

    To create a test case, use this command:

    This will create a new test case named ApiUsersTest.php in your directory /tests

    Paste the following code in it.

    Edit the test case as needed.

  • Android M runtime permissions utility class


    Hello world, I prepared a PermissionsUtil.java class and it’s on GitHub.

    Check it out: AndroidPermissionsUtil

    Why use it? It will help by letting you write less code as you add conditions to your Android app that supports Android 6.0 (Marshmallow). The utility class defines helper methods that were used in the following example.

    Sample usage of Android M PermissionUtils

    On the activity where you need to check for runtime permissions, add these:

    And add the callback function to the same activity:

    Feel free to use in on any project. And please let me know if you have suggestions on how to improve that Android M runtime permissions utility class.

  • Android app codes for Google Maps with clustering

    I prepared Android app codes with Google Maps and clustering feature. Feel free to use these as basis for your new projects.

    They are on GitHub:

    Android map clustering with default markers

    [1] Default Map Markers:
    catzie/SimpleAndroidMapClustering

     

     

    I simply took the necessary classes and res files from Android Maps Utility demo, and then placed them into bare Android Studio projects for simplicity. For me, it’s easier to start a new “maps with clustering” project on simple apps like these, rather than downloading the entire Android Maps Utility Demo, picking out the necessary files, and fixing errors before getting started each time. 😆

     

  • Android Maps: Get current location coordinates with Google API Client Builder

    To get your current location via Google Map in an Android app, this was how we do it:

    Stop. Don’t use that! The method GoogleMap.getMyLocation() is deprecated. Instead, we need to use FusedLocationApi and the callback onLocationChanged().

    Here’s an example code that works for me ( consider copying the contents of onResume() into onStart() ):

    Code source: How to get current location of device on google maps api v2, getMyLocation() method is deprecated

  • Bash: rename all files in directory, replacing all hyphens (-) with underscores (_)

    I don’t want to waste time doing things manually when I can run a script that will help me save time and effort.

    When using Linux or Mac command line (bash?), if you want to rename all files in your present directory and replace all hyphens - with underscores _, run this command:

    And if you want to do the reverse, i.e., replace all underscores _ with hyphens -, run this command instead:

    To remove substring from file names, (e.g., remove @3x string), run this:

    I had to do this after importing image files with hyphens in their names into my Android project. I can’t use images with hyphens in their names.

  • Android Studio: add Google Map from empty activity (dev note)

    //Update: I prepared a simple Android app that features a Google Map with markers that form clusters together when zoomed out. Check it out: Simple Android Map Clustering on GitHub.

    This “dev note” of mine will give you a rough idea on how to add a Google Map to your Android up from an empty activity, not from a pre-made Google Maps activity.

    I won’t be able to write everything in detail here because I’m in a hurry, and so I’m calling this article a “dev note” instead of tutorial. 🙂 This dev note can be considered a “guide” too.

    This dev note was based on Android Studio v.2.1 running on MAC OS X

    From your Android Studio’s SDK Manager, install Google Play Services.

    Add this as child of <application> tag in AndroidManifest.xml:

    Later we’ll replace YOUR_API_KEY with a key from Google API Console.

    Also add these to your AndroidManifest.xml as direct child of manifest tag:

    We need to get an API KEY for the value attribute of this meta data. Let’s get one from the Google API Console — create a new project there and then search for Google Maps Android API. Enable it.

    After enabling it, you’ll find a way to get an API key that we need from Google API Console. Go to Credentials -> Create Credentials -> API Key -> Android Key. Before we are given the API key, we need to provide Google API Console a SHA1 certificate fingerprint from our computer and our app’s package name.

    To generate SHA1 certificate fingerprint on a Mac, type this on the terminal:

    On Windows, try this command:

    When prompted for a keystore password, enter the word debug as your password.

    Copy the SHA1 certificate fingerprint and then paste it into Google API Console. Enter your package name as well (e.g. com.yourdomain.projname). You’ll be given an API Key in return.

    Note: My Android Studio says ‘In the Google Developer Console (https://console.developers.google.com), ensure that the “Google Maps Android API v2” is enabled.’ but I can’t find the option for V2.

    Remember the meta-data tag we added in the AndroidManifest.xml? Replace the value with the API key that you just copied from Google API Console.

    Resource: https://developers.google.com/maps/documentation/android-api/signup

    Open app/build.gradle file and add the following as dependency:

    Sync your project with Gradle files.

    This was one of the tutorials I followed: GOOGLE MAPS ANDROID API V2 TUTORIAL USING ANDROID STUDIO and I based my XML plus Java codes on the author’s.

    My files in the end…

    Here were the codes of my Android app when I finally made the Google Map show up in the activity:

    app/build.gradle

    build.gradle

    app/src/main/res/layout/activity_main.xml

    app/src/main/java/net/catzie/maptest/MainActivity.java

    app/src/main/AndroidManifest.xml

    …even though the Google Map now shows up on my Android activity, I get these errors that I’m not able to fix yet:

  • Laravel update from v.5.0.35 to v.5.2.32 hiccups and solutions

    I tried to update Laravel 5 today and here were the steps I had to take.

    First checked my Laravel version before the update:

    Edited composer.json in my laravel’s root folder like such:

    Run this on command line:

    You might need to delete root folder’s composer.lock file before the update can actually begin.

    I got an error which I later fixed. Scroll to the bottom of the following message.

    So, Illuminate\Routing\ControllerServiceProvider’ could not be found.

    What I did was remove the following line from config/app.php:

    New error upon running php artisan –version:

    I created a folder named “cache” under bootstrap folder to solve that one.

    Tried to check my Laravel version again, but this was the new error:

    Solution was this:

    Then, finally!

    Oops, I can’t view my Laravel site on the web browser. It gives the following error:

    I opened App/Http/Controllers/Controller.php and replaced all instances of DispatchesCommands with DispatchesJobs.

    Next web browser error:

    So I replaced the content of my config/auth.php file with the following:

    Another error… Oh will this end soon?

    …which was caused by this code in my blade file:

    I simply passed a string “/” to the url() method to fix it. Then, ta-da! I could browse my Laravel site again via web browser.

  • Laravel 5: Create controller under new folder and routing

    So here I am on a Sunday, trying to make progress with a personal Laravel 5 site I need to work on. As I bear with the noise outside in the street (some crazy neighbors set up a swimming pool IN THE STREET! :oops:), I’m trying to figure out how too create a new Laravel 5 controller and place it inside a sub-folder.

    Thanks to a StackOverflow answer as my guide, it’s now working. Here, I’ll share how I did it. Just replace “New” with whatever name you want.

    First, create NewController.php on this folder: app\Http\Controllers\New\

    Then the first part of the code should be like:

    My call for the blade file in index() looks like this:

    In app\Http\routes.php, add this:

    $router->get(‘/new’, ‘New\PostsController@index’);

    Last, run composer dump-autoload or php artisan dump

    Guide: Laravel Controller Subfolder routing

  • Android Development: Permission denied when running ./gradlew in Mac

    I’m using a Mac at the moment to work on my Android app projects.

    For some reason I need to run ./gradlew in the terminal, but I was getting this error:

    -bash: ./gradlew: Permission denied

    To fix this, what I did was run the following commands:

    After those commands, my Mac downloaded numerous files and it took an hour to finish because of our poor Internet connection in the workplace. 🙁

    After an hour and a half, I was able to finally use the command ./gradlew on the Mac Mini. Phew!