Sean's Note: 2月 2017

2017年2月21日 星期二

Node.js 學習筆記 (一)

Node.js 的幾個特點:
1.  Runtime 是建立於 Chrome 的 V8 JavaScript 引擎之上的。
2. Event-driven 和 non-blocking I/O 的模型架構

npm

學 Node.js 不能不知道 npm 這個套件管理程式,npm 如同於 iOS 的 CocoaPods 抑或是 Python 的 pip 和 setuptools,透過一行指令就可以讓你方便下載別人已經寫好的套件,來加速開發應用程式。Node.js 會吸引了廣大的開發者,一部分也是因為 npm 上廣大的社群和擁有許多強大好用的套件。

用什麼編輯器來開發 Node.js?

Atom, Visual Studio Code, Sublime Text 都是不錯的選擇。

如何執行?

安裝好 node 後,只要執行以下的指令,就可以執行已經寫好的 app.js。
node app.js

常用的內建套件

常用的內建套件如:
  • File System - require('fs'),用來讀寫檔案
  • Process - no require needed,process.argv 用來讀取命令列的參數
  • ...etc.
參考更多:https://nodejs.org/dist/latest-v7.x/docs/api/

如何使用第三方套件

有些好用的套件 Node.js 本身不提供,但透過 npm 就可以很方便的下載,使用前必須先將專案初始化:
npm init
此時,專案裡就會產生一個 package.json:
{
  "name": "notes",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Sean",
  "license": "ISC"
}
這時還看不出來這個檔案有什麼用途,一旦我們安裝了一個套件之後
npm install lodash --save
檔案裡就會多了一個 dependencies 的欄位紀錄了這個專案所用到的套件。而下載過的這些套件也是不需要上傳到 repository 的,一起開發的夥伴們只需要下載原始碼,並且在本機端執行 npm install 就可以把所有專案用到的套件給下載下來。
{
  "name": "notes",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Sean",
  "license": "ISC",
  "dependencies": {
    "lodash": "^4.17.4"
  }
}

Lodash

Lodash makes JavaScript easier by taking the hassle out of working with arrays, numbers, objects, strings, etc.

yargs

Yargs helps you build interactive command line tools by parsing arguments and generating an elegant user interface.

request

Request is designed to be the simplest way possible to make http calls. It supports HTTPS and follows redirects by default.

nodemon

nodemon will watch the files in the directory in which nodemon was started, and if any files change, nodemon will automatically restart your node application.

如何除錯 debug?

Node.js 除了可以用 console.log() 的方法來觀察物件的值(對於寫 C/C++ 的人來說就是 printf 大法),也可以透過下斷點 (Break Point) 的方式來逐行除錯,只是不像 iOS 有 XCode,Android 有 Android Studio 一般,有友善的 IDE 介面來幫助除錯,而是透過指令的方式。例如輸入 next(n) 就是在往下一行執行。
node debug app.js

< Debugger listening on 127.0.0.1:5858
connecting to 127.0.0.1:5858 ... ok
break in app.js:1
> 1 console.log("Starting...");
  2
  3 const fs = require('fs');
debug> n
< Starting...
break in app.js:3
  1 console.log("Starting...");
  2
> 3 const fs = require('fs');
  4 const notes = require('./notes');
  5 const _ = require('lodash');
debug>


2017年2月13日 星期一

How to scroll TextView smoothly to bottom

 
   private void scrollToTop() {
        textViewLog.scrollTo(0, 0);
    }

   private void scrollToBottom() {
        final int scrollAmount = textViewLog.getLineCount() * textViewLog.getLineHeight() - (textViewLog.getBottom() - textViewLog.getTop());
        textViewLog.scrollTo(0, Math.max(0, scrollAmount));
    }
不過 TextView 的 scroll 並不是很 smooth,可以用 ScrollView 包覆 TextView 的方式來解決,並改用 ScrollView 的 Scroll 方法。
 
    private void scrollToTop() {
        scrollView.scrollTo(0, 0);
    }

    private void scrollToBottom() {
        scrollView.fullScroll(ScrollView.FOCUS_DOWN);
    }