<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by iMint on Medium]]></title>
        <description><![CDATA[Stories by iMint on Medium]]></description>
        <link>https://medium.com/@chalitta-c-k?source=rss-c4e27bbfe1f9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*fRrlXwmE8xzsnwLHX4YxeQ.jpeg</url>
            <title>Stories by iMint on Medium</title>
            <link>https://medium.com/@chalitta-c-k?source=rss-c4e27bbfe1f9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 09:20:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@chalitta-c-k/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[แชร์ประสบการณ์ทำ AI Agent ครั้งแรกในชีวิต]]></title>
            <link>https://medium.com/kbtg-life/%E0%B9%81%E0%B8%8A%E0%B8%A3%E0%B9%8C%E0%B8%9B%E0%B8%A3%E0%B8%B0%E0%B8%AA%E0%B8%9A%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B8%93%E0%B9%8C%E0%B8%97%E0%B8%B3-ai-agent-%E0%B8%84%E0%B8%A3%E0%B8%B1%E0%B9%89%E0%B8%87%E0%B9%81%E0%B8%A3%E0%B8%81%E0%B9%83%E0%B8%99%E0%B8%8A%E0%B8%B5%E0%B8%A7%E0%B8%B4%E0%B8%95-9fda0dfa946c?source=rss-c4e27bbfe1f9------2</link>
            <guid isPermaLink="false">https://medium.com/p/9fda0dfa946c</guid>
            <category><![CDATA[ai-agent]]></category>
            <category><![CDATA[ollama]]></category>
            <category><![CDATA[how-to]]></category>
            <category><![CDATA[postgresql]]></category>
            <category><![CDATA[n8n]]></category>
            <dc:creator><![CDATA[iMint]]></dc:creator>
            <pubDate>Wed, 04 Jun 2025 10:02:22 GMT</pubDate>
            <atom:updated>2025-06-04T10:02:22.971Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Tbc-hmUCI1IX3Hk-SvSoGQ.png" /></figure><h3><strong>ในยุคที่ไปทางไหนก็มีแต่ AI</strong></h3><p>ช่วงหลังมานี้ ไม่ว่าจะเลื่อนฟีดบนโซเชียล หันไปฟัง Podcast หรือแม้แต่บทสนทนาในร้านกาแฟ คำว่า <strong>AI</strong> ก็โผล่ขึ้นมาทุกที่ กลายเป็นคำที่ทั้งใกล้ตัวและน่าหวาดหวั่นในเวลาเดียวกัน บางคนใช้ AI แค่เวลาถาม ChatGPT เล่น ๆ บางคนถึงขั้นใช้เพื่อสร้างธุรกิจใหม่</p><p>สำหรับผู้เขียนนั้น AI ไม่ได้เป็นของไกลตัวมาตั้งแต่แรก แต่เป็นอะไรที่ค่อย ๆ คืบคลานเข้ามาใกล้ขึ้นเรื่อย ๆ จากตอนแรกที่แค่ลองถามเล่นในแชต กลายเป็นใช้ช่วยสรุปบทความ ช่วยเขียนโค้ด และช่วยคิดไอเดียในวันที่สมองตัน</p><p>จนวันหนึ่งก็เผลอคิดขึ้นมาว่า…</p><blockquote><strong>อยากลองสร้าง AI ดูบ้าง! มันจะเป็นอย่างไรนะ</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*brKfZUqQtfEvIZNs1aGWTw.png" /></figure><h3>จากศูนย์… สู่ “จารย์แชท”</h3><p>แรงบันดาลใจในการทำ AI Agent ของผู้เขียน ไม่ได้เริ่มจากอะไรอลังการมากมาย แค่มีความคิดเล็ก ๆ ที่ผุดขึ้นมาในหัว</p><blockquote>“จะดีแค่ไหน ถ้าเรามี AI ที่รู้จักเราจริง ๆ เหมือนเป็นไดอารี่เดินได้ คอยจำสิ่งที่เราลืม คอยตอบคำถามในแบบที่เข้าใจเราโดยไม่ต้องอธิบายซ้ำ”</blockquote><p>พูดง่าย ๆ คือผู้เขียนอยากมีผู้ช่วยที่เป็นเหมือน <strong>ความทรงจำสำรอง</strong> ของตัวเองนั่นแหละ ไม่ใช่แค่ AI ที่ตอบแบบทั่วไป แต่เป็น AI ที่รู้จักเราจริง ๆ</p><p>จังหวะนั้นเองที่องค์กรเริ่มพูดถึงเรื่อง AI Agent กันเยอะขึ้น บางคนลงมือทำ Agent แบบ High-code เท่ ๆ แต่พอลองเปิดดูโค้ด เอ่อ… ขอยาดถอนหายใจแป๊บนึง แบบว่าเข้าใจนะ แต่มันไม่เข้าใจเราน่ะ 😅 ทำให้เราเริ่มคิดว่าจะมีวิธีไหนที่ Low-code หรือ No-code กว่านี้ไหม อยากทำให้ได้เร็ว ไม่ต้องไถเอกสารหรือเรียน TensorFlow ก่อนถึงจะเริ่มได้</p><p>สำหรับความรู้พื้นฐานตอนนั้นเหรอ <strong>ศูนย์สนิท</strong> ค่ะคุณผู้อ่าน เมื่อไม่มีพื้นหลังด้าน AI ใด ๆ ทั้งสิ้น เราก็ทำสิ่งที่ดูฉลาดสุดในยุคนี้</p><blockquote>เปิด ChatGPT แล้วพิมพ์ว่า สอนเราสร้าง AI Agent หน่อยค่ะจารย์</blockquote><p>ใช่แล้ว ผู้เขียนเริ่มต้นเรียนรู้ AI จาก AI เองเลย! เรียกได้ว่า <strong>ลูกศิษย์จารย์แชท รุ่นบุกเบิก</strong> ก็ว่าได้ 😂</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cOQBA5XyLTJfKwQz95E4PA.png" /></figure><h3>เส้นทางการลงมือทำ: จาก “จารย์แชท” สู่สายลุย AI Agent (แบบไม่ต้องเขียนโค้ด)</h3><p>หลังจากได้แรงบันดาลใจเต็มกระเป๋า และได้รับการแนะนำขั้นพื้นฐานจากจารย์แชท ChatGPT แบบที่เรียกว่า “ปูทางให้ทุกก้าว” ก็ถึงเวลาลงมือจริง สิ่งแรกที่เราทำคือการตั้งโจทย์ง่าย ๆ ให้กับตัวเองว่า</p><blockquote>“อยากสร้าง AI ที่รู้จักตัวเรา ตอบคำถามเกี่ยวกับเราได้ โดยไม่ต้องเป็นนักวิทยาศาสตร์ข้อมูลระดับโลก”</blockquote><h4>เริ่มต้นจากเครื่องตัวเองกับ Ollama 🔍</h4><p>ก่อนจะวิ่งไปเล่นของใหญ่ ผู้เขียนเริ่มจากสิ่งที่ใกล้ตัวที่สุด นั่นคือเครื่องคอมของตัวเอง ทำให้ได้เจอกับ <strong>Ollama</strong> ที่ช่วยให้เราสามารถรันโมเดล Local อย่าง Llama, Mistral, Phi และ Gemma ได้แบบง่าย ๆ แค่พิมพ์คำสั่ง Ollama run [Model] เท่านั้น ก็ได้ AI มานั่งอยู่ในเครื่องเรา</p><p>จากเดิมที่ไม่รู้ว่าโมเดลแต่ละตัวต่างกันยังไง พอได้ลองใช้จริง ก็เริ่มเข้าใจฟีล</p><ul><li>บางตัวตอบฉลาดแต่พูดจาห่างเหิน 😶</li><li>บางตัวพูดเหมือนเพื่อนแต่ไม่ค่อยรู้เรื่อง 😂</li><li>บางตัวชอบตอบสั้น ๆ เหมือนกลัวเราเบื่อ 😆</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FmjQH2e11GaGXZjBGlJf-A.png" /></figure><h3><strong>ลองเล่น Ollama ประตูบานแรกสู่โลก AI</strong></h3><p>1. โหลด Docker มาติด Desktop หรือใครอยากลองโหลด Ollama โดยตรงก็ได้เหมือนกัน แต่วิธีนี้ง่ายสุด</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Au4n-RThbljP133qXft0GQ.png" /></figure><p>2. ค้นหาคำว่า ollama/ollama ใน Docker ทำการ Pull ลงมาและกดรัน</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gZAyWjFX1jlqvYbVLhqZvg.png" /></figure><p>3. ในเว็บไซต์ของ Ollama จะมีโมเดลให้เราเลือกใช้หลากหลายตัว แต่ละตัวจะเก่งแต่ละด้าน เราสามารถเข้าไปอ่านรายละเอียดข้างในได้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IYO8tSnmXweMyMyBAjH3Pw.png" /></figure><p>4. หลังจากกด Run เราจะได้หน้าตา Terminal ซึ่งไม่มี UI ให้ เราสามารถใช้คำสั่งดังนี้</p><ul><li><strong>Ollama ps</strong> เพื่อดูว่ามีโมเดลไหนกำลังทำงานอยู่</li><li><strong>Ollama list </strong>เพื่อดูว่าเรามีโมเดลไหนบ้างที่โหลดมาใช้งานแล้ว</li><li><strong>Ollama run [Model]</strong> เพื่อ Pull โมเดลลงมารันใช้ในเครื่อง เช่น ollama run llama3.2 ซึ่งเป็นโมเดลของ Meta เจ้าของเฟสบุ๊คที่เราคุ้นเคยกันดี</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dk5vgxu8mxNzNawtTD1UMQ.png" /></figure><p>5. ลองเริ่มคุยกับเจ้า llama3.2 ดูว่าเป็นยังไง เมื่อเห็นว่าน้องไม่มีความทรงจำใด ๆ เราเริ่มหาเครื่องมือที่ไม่ต้องเขียนโค้ดเป็นร้อยบรรทัดให้ปวดหัว จนเจอของเด็ดเข้าให้</p><h4>เครื่องมือคู่ใจ Low-code จริง ไม่จกตา 🛠️</h4><ul><li><strong>Make.com</strong> เครื่องมือลากวางอันเป็นที่รักของสาย No-code ทั้งหลาย ใช้ต่อ API และเชื่อมระบบได้ง่าย ๆ เหมือนต่อเลโก้ เดิมทีผู้เขียนเคยสนใจ Make.com เพราะหน้าตาน่ารักน่าใช้ แต่หลังลองใช้จริง ก็ต้องถอยออกมาอย่างช้า ๆ เพราะต้องเสียเงินถ้าจะใช้ฟีเจอร์เจ๋ง ๆ หรือรันบน Server ตัวเองแบบจริงจัง</li><li><strong>n8n </strong>เป็นเครื่องมือ Workflow Automation ที่สามารถติดตั้งเองในเครื่องผ่าน Docker ได้ง่าย ๆ ใช้ฟรีไม่จำกัดจำนวน Workflow หรือจำนวน Run</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*DAbnojUflnQUqwlE.png" /><figcaption><a href="https://github.com/n8n-io/n8n">https://github.com/n8n-io/n8n</a></figcaption></figure><p>สิ่งที่ทำให้ n8n โดดเด่นมาก ๆ คือ</p><ul><li>เชื่อมกับ API ได้แทบทุกอย่าง</li><li>มี Block สำเร็จรูปเยอะมาก ทั้ง HTTP Request, Python, JS, AI Node, Webhook ฯลฯ</li><li>ใช้ Logic แบบ Flowchart เข้าใจง่าย เหมือนวางสายไฟแผงวงจร</li><li>สามารถเขียนโค้ดเพิ่มใน Block ที่เป็นโค้ด หากเราอยากทำการ Classify ข้อมูลบางอย่าง</li></ul><p><strong>และที่เด็ดสุดคือใน Community ของ n8n มีคนทำ AI Workflow Template แจกไว้เยอะมาก</strong> เราสามารถกดดูตัวอย่าง Workflow ของคนอื่น แล้วค่อย ๆ ดัดแปลงให้เป็นของตัวเองได้เลย บางอันดีไซน์อย่างกับเทพทำ แต่เราเอามาใช้ได้ง่ายมาก แค่ลาก วาง แล้วใส่ API Key</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fe2ES_RiOqfhOWGcHYB0EA.png" /><figcaption><a href="https://blog.n8n.io/">https://blog.n8n.io/</a></figcaption></figure><p>ทั้งนี้ก่อนจะลงมือทำ เรายังขาดหนึ่งสิ่งที่เป็นหัวใจหลักในการจดจำหรือเป็นเหมือนสมองที่จะให้ข้อมูลกับเราได้ตรงตามความต้องการ นั่นคือ…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ulCY0s285SDsvtu-N0LpWg.png" /></figure><h4>ความจำของ AI ใช้ Qdrant + PostgreSQL 🧠</h4><p>ได้ AI และเครื่องมือควบคุมแล้ว มาถึงเรื่อง “ความจำ” ของ Agent กันบ้าง ผู้เขียนเลือกใช้ <strong>Qdrant</strong> เป็น Vector Store ที่ใช้เก็บความรู้หรือฐานข้อมูลต่าง ๆ แล้วใช้ <strong>PostgreSQL</strong> เพื่อจัดเก็บ Context, Log หรือข้อมูลที่เป็นเชิงโครงสร้างของความจำที่ใช้ในการสนทนาก่อนหน้า ที่เลือกไม่ใช้ LangChain แบบเต็ม ๆ เพราะบางทีก็ดู Overkill ไปหน่อยและใช้เวลาในการเรียนรู้สูง เลยเลือกหยิบแนวคิดแบบ RAG มาปรับใช้เองใน n8n เรียกได้ว่า <strong>ประยุกต์ให้เหมาะกับทรัพยากรและเวลาเรียนรู้ของตัวเอง</strong> มากที่สุด</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P_Z9rD4hPoflZ_A3RwOpjQ.png" /></figure><h4>แนวคิด RAG: ให้ AI จำได้เหมือนมีสมุดโน้ตติดตัว 📒</h4><p>ตอนแรกที่ลองให้ AI ตอบคำถามจากตัวโมเดลอย่างเดียว (เช่น ใน Ollama) ก็เจอปัญหาง่าย ๆ แต่เจ็บลึกมาก เช่น</p><blockquote>ตอบเหมือนคนเพิ่งรู้จักกันทุกครั้ง ทั้ง ๆ ที่เราคุยกันมาเป็นอาทิตย์แล้ว!</blockquote><p>จึงเริ่มศึกษาแนวทางที่เรียกว่า <strong>RAG </strong>หรือชื่อเต็ม ๆ ว่า <strong>Retrieval-Augmented Generation</strong> ซึ่งเป็นแนวคิดที่ว่า</p><blockquote>แทนที่ AI จะต้อง “จำทุกอย่างเอง” (ซึ่งเป็นไปไม่ได้ เพราะ Context Window ของโมเดลมีจำกัด) ให้มัน “ไปค้นข้อมูลที่เกี่ยวข้องก่อน แล้วค่อยตอบ”</blockquote><p>ถ้าเปรียบเทียบให้เห็นภาพง่าย ๆ</p><ul><li>ให้ AI ตอบลอย ๆ = เหมือนคนตอบโดยไม่มีโน้ต ไม่มีข้อมูลประกอบ</li><li>ใช้ RAG = เหมือน AI มีสมุดโน้ตอยู่เล่มหนึ่ง ก่อนตอบอะไรก็เปิดสมุดโน้ตดูความรู้หรือสิ่งที่เคยรู้มาก่อน แล้วค่อยตอบ</li></ul><p>ซึ่งระบบที่ผู้เขียนทำจะออกแบบไว้เป็นดังนี้</p><ul><li>ข้อมูลทั้งหมด (ความรู้ส่วนตัว, บันทึก, บทสนทนาเก่า ฯลฯ) จะถูก <strong>แปลงเป็น Vector </strong>และจัดเก็บไว้ใน <strong>Qdrant</strong></li><li>เวลามีคำถามใหม่เข้ามา AI จะ <strong>ค้นหาข้อมูลที่ใกล้เคียง</strong> จาก Qdrant ก่อน</li><li>แล้วค่อยนำข้อมูลนั้นมา “ประกบ” กับคำถาม ส่งเข้าโมเดลใน Ollama เพื่อให้มันตอบได้แม่นยำขึ้น</li></ul><blockquote>RAG = ให้ AI “ไม่ต้องฉลาดล้วน ๆ” แต่ “ฉลาดแบบมีพจนานุกรมส่วนตัว”</blockquote><blockquote>ไม่ต้องจำหมด แต่จำได้ว่าต้องไปเปิดหน้าไหน</blockquote><p>ข้อดีของวิธีนี้</p><ul><li>ตอบคำถามได้แม่นขึ้น</li><li>รองรับความรู้เฉพาะตัว เช่น ข้อมูลของเราเองที่ไม่มีในอินเทอร์เน็ต</li><li>ขยาย Context ได้แบบไม่ต้องรันโมเดลใหญ่ (ประหยัดแรม ประหยัดไฟ)</li></ul><h3>เริ่ม!! ลงมือทำ</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*knTvl8Oy4aG7HkU2nCcngQ.png" /></figure><p>1. เริ่ม Workflow ด้วยการค้นหา Template ใน n8n ที่พอมีคนทำ ๆ ไว้บ้างแล้วในช่องค้นหา AI Agent</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m9spYVxHN9pH5M7oJ_f8DA.png" /><figcaption><a href="https://n8n.io/workflows/">https://n8n.io/workflows/</a></figcaption></figure><p>2. จะกดใช้ Use for free แล้วมาปรับใช้ก็ได้ ส่วนผู้เขียนเลือกใช้วิธีการดู Template เป็นแนวทาง แล้วสร้าง Workflow ของเราเอง</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zZlGkSsTTTkL6AJzQaCZWg.png" /></figure><p>3. จาก Docker ที่เราโหลดมาลองเล่น Ollama เราจะใช้วิธีเดียวกันในการลง n8n ในเครื่องของเรา ไปที่ช่องค้นหา แล้วค้นคำว่า n8n, Postgres, และ Qdrant ทำการลงให้ครบทั้งหมด เพราะเราจะใช้ Postgres และ Qdrant ในการทำความจำของ AI</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ijX_VPFDnCujGc6j4C1swg.png" /></figure><p>4. เมื่อลงครบทั้งหมด ก็จะได้ออกมาหน้าตาแบบนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bGCvE6B6D740qEZzXo3arQ.png" /></figure><p>5. ทำการเปิด Web Browser แล้วเปิดไปที่พอร์ต 5678 ซึ่งถูกรันในเครื่องของเรา <a href="http://localhost:5678/home/workflows">http://localhost:5678/home/workflows</a> โดยครั้งแรกจะมีการสร้าง Account ขึ้น (ผู้เขียนได้ทำการสร้างเรียบร้อยแล้ว 😄) กด Create Workflow ได้เลย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mcpA7XrAuUkxsTKmltAC2Q.png" /></figure><p>6. ใน Workflow จะมีปุ่ม + ทางขวามือ ให้เราเลือกเครื่องมือมาใช้ดังรูป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Og5nebACkysNWEnoXXwpBw.png" /></figure><p>7. ผู้เขียนเลือกใช้ Node Chat ในการรับ Input หรือใครจะใช้ Webhook เผื่อนำไปต่อ LINE, Facebook, Telegram หรืออื่น ๆ ก็สามารถทำได้ จากนั้นใช้ Edit Fields ในการคัดแยก Parameter ว่าเราจะใช้ Parameter ตัวไหนเป็น Input ของเรา</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nlrwntkyRRwEeWW-oDFJsQ.png" /></figure><p>8. สำหรับ AI Agent เลือก Config เป็น Conversational Agent เลือกใส่ Prompt ข้างล่าง โดยกำหนดให้ตอบเป็นภาษาไทย และเพิ่ม Prompt ให้กับระบบ โดยให้ดึงข้อมูลมาจากฐานข้อมูล (ซึ่งจะมี RAG มารองรับการสร้างข้อมูลเฉพาะ ตรงนี้เดี๋ยวเล่าให้ฟังนะ)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q8YAvsR1tA56jeT44OBcCw.png" /></figure><p>9. สร้างสมองให้กับ Agent โดยเลือก <strong>Chat Model</strong> เป็น llama3.2 (อย่าลืมไปรัน Ollama ใน Docker ด้วยนะ เดี๋ยวหาไม่เจอ)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kyjydZsmWL-hOtzzbUiB2w.png" /></figure><p>10. สร้างความทรงจำให้แชทเป็น Postgres โดยเราสามารถตั้งชื่อตารางของเรา (อย่าลืมไปรัน Postgres ใน Docker ด้วยเหมือนกันนะ)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*40pUQP1eTORESHLld7mf9w.png" /></figure><p>11. สร้างฐานข้อมูล Vector เฉพาะเจาะจงที่เราอยากให้ AI ไปดึงคำตอบมาจากการทำ RAG ของเรา โดยเราต้อง Prompt ให้ดึงข้อมูลมาจากฐานข้อมูล Vector ซึ่งเราใช้เป็น Qdrant</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kGwJKo7p4TYu_WLjnHQczg.png" /></figure><p>12. ให้ LLM สร้างคำตอบที่ดึงมาจากฐานข้อมูลด้วยโมเดล Gemma 2 (เรียกว่าเพิ่มความฉลาดให้กับเจ้า AI)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k6Upv3gas4Inh8WSC9AcPw.png" /></figure><p>ใช้เวลาไม่นานก็ได้ Agent ตัวแรกของออกมาแบบเบสิกมาก ๆ ต่อไปเรามาทำ RAG ที่จะดึงข้อมูลของเรามาไว้ในฐานข้อมูล Vector (Qdrant) กัน</p><p>1. สร้าง Workflow อันใหม่ขึ้นมา โดยให้เป็น RAG ที่เราจะสร้าง</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SxfnR396XY5Bajf_oOPSXg.png" /></figure><p>จากรูปจะเห็นว่าเรามีการสร้าง Google Drive ขึ้นมา ในนั้นเราได้นำไฟล์ Excel ที่เป็นข้อมูลไปวาง ให้ระบบทำการดึงและแตกไฟล์นำไปเก็บที่ฐานข้อมูล Vector ของ Qdrant และใช้โมเดล Embedding เป็นสมองในการอ่านภาษา ซึ่งเราสามารถหาในเว็บไซต์ Ollama ที่เป็น Embedding มาใช้ได้</p><p>2. กดรัน Test Workflow เพื่อดึงข้อมูลจาก Google Drive ของเรามา แล้วเปิดที่ <a href="http://localhost:6333/dashboard#/collections">http://localhost:6333/dashboard#/collections</a> ซึ่งเป็น Qdrant ฐานข้อมูลของเรา เพื่อดูว่ามีข้อมูลของเราเข้าไปแล้วหรือไม่ (ข้อมูลที่จำลองใส่เข้าไปเป็นข้อมูลแมว 2 ตัวของผู้เขียนเอง 🐈🐈)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cUbPyfx5XBo1dACSc36H-g.png" /></figure><p>3. กลับไปที่ Workflow AI Agent หลักของเรา กดรัน Workflow และทำการแชทถามเจ้า AI ของเราได้เลย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aHOcfw2-H8qhonljIQXOOA.png" /><figcaption>แสดงผลงาน AI ที่บอกข้อมูลเฉพาะของเราได้จริงๆ</figcaption></figure><blockquote>เมื่อถามว่าดอลล่านิสัยเป็นอย่างไร (***ดอลล่าเป็นชื่อแมวของผู้เขียนเอง) AI ตอบกลับมาว่า “ดอลล่าเป็นแมวน่ารัก และชอบหลับทั้งวัน แต่ถ้าเกิดอะไรขึ้นกับเธอ เธอก็สามารถดุร้ายได้ นั่นคือความน่ารักและความเป็นแมวที่แท้จริงของเธอ!🐈”</blockquote><p>แน่นอนว่าเส้นทางไม่ได้โรยด้วยกลีบกุหลาบ แต่โรยด้วย Error แดง ๆ 😅 เจอปัญหาตั้งแต่ Vector ไม่เข้า, Context หาย ไปจนถึง Agent เงียบไปเลยแบบไม่บอกลา แต่ข้อดีของการมี AI เป็นครูคือ <strong>ไม่เคยดุเราเลยสักครั้ง</strong> ผิดกี่ครั้งก็ยังตอบแบบใจเย็นเสมอ ต่างจากชีวิตจริงมาก 😂</p><h3>บทเรียนที่ได้เรียนรู้: AI ไม่ใช่เรื่องไกลตัว ถ้ากล้าลอง</h3><p>หลังจากเดินทางร่วมกับ AI Agent ตัวแรกของผู้เขียน ทั้งเรียน ทั้งลอง ทั้งงมหา Bug และถาม ChatGPT จนรู้สึกเหมือนมันเป็นพี่เลี้ยงเด็ก ก็ได้เรียนรู้อะไรหลายอย่างที่ไม่ใช่แค่เทคนิค แต่รวมถึงมุมมองต่อ AI ที่เปลี่ยนไปแบบสิ้นเชิง</p><h4>1. เริ่มจากสิ่งใกล้ตัว และเรียนรู้แบบ “ขำ ๆ แต่ได้เรื่อง”</h4><p>ผู้เขียนไม่ได้เริ่มจาก TensorFlow หรือเขียน Python 500 บรรทัด แต่เริ่มจากการถาม ChatGPT ว่า “ฉันอยากมีผู้ช่วยที่รู้จักฉัน ต้องเริ่มยังไงดี?” ซึ่งกลายเป็นจุดเริ่มต้นของการเรียนรู้ที่ <strong>ไม่เครียดแต่ได้ผล</strong></p><blockquote>ทุกครั้งที่ติด ไม่ได้รู้สึกว่า “เราไม่เก่งพอ” แต่จะคิดว่า “โอเค ถาม AI สิ เดี๋ยวมันบอกทางให้เอง” ซึ่งมันก็บอกจริง ๆ</blockquote><h4>2. เครื่องมือ Low-Code / No-Code นั้นทรงพลังมาก</h4><p>ตอนแรกผู้เขียนคิดว่า “การสร้าง AI Agent ต้องเป็นเรื่องของ Dev สายเทพเท่านั้น” แต่พอได้ลองใช้ n8n ก็เหมือนเปิดประตูอีกบาน การเขียน Logic ด้วยการลาก Node ต่อ Flowchart เหมือนเล่นเกมต่อท่อ <strong>เข้าใจง่าย ใช้ได้จริง และขยายต่อได้เยอะมาก</strong> ยิ่งเมื่อรู้ว่ามี Community ที่แจก Workflow ตัวอย่าง ก็ยิ่งเหมือนมีพิมพ์เขียวให้เราเอาไปปรับใช้ได้เลย</p><blockquote>จากไม่รู้จะเริ่มยังไง ก็แค่ดูคนอื่นทำ แล้วลองบิด ๆ ปรับ ๆ ตาม</blockquote><h4>3. AI ที่ดีไม่ต้องฉลาดที่สุด แค่ “เข้าใจเรา” ก็พอแล้ว</h4><p>โมเดลบางตัวใน Ollama อาจไม่ได้ฉลาดเท่า GPT-4 หรือ Claude 3 แต่มันก็เพียงพอที่จะเป็น “ผู้ช่วยที่รู้จักเรา” เมื่อเราจัดการระบบดี เชื่อมกับ Qdrant และมี Logic คอยควบคุมด้วย n8n ตัว AI Agent ของเราก็สามารถตอบคำถามจากความทรงจำส่วนตัวได้เหมือนเพื่อนสนิท</p><blockquote>ไม่ต้องตอบทุกอย่างเก่ง แค่ตอบเรื่องของเราได้ถูกก็สุดยอดแล้ว</blockquote><h4>4. เริ่มก่อน แล้วจะเก่งเอง</h4><p>ลองเริ่มจากศูนย์ แล้วค่อย ๆ เติมวันละนิด มันก็จะกลายเป็น 1 → 2 → 3 ไปเรื่อย ๆ</p><blockquote>อ้าว ทำ Agent ใช้งานเองได้แล้วนี่หว่า!</blockquote><p>สุดท้ายนี้ <strong>AI อาจจะไม่ใช่ทุกคำตอบในชีวิต</strong> แต่มันคือเครื่องมือที่<strong>ถ้าเราใช้ถูกที่ถูกทาง จะทำให้ชีวิตง่ายขึ้นกว่าที่เคย</strong></p><blockquote><em>See you next time 👋</em></blockquote><p><em>สำหรับใครที่ชื่นชอบบทความนี้ อย่าลืมกดติดตาม </em><a href="https://www.medium.com/kbtg-life"><em>Medium: KBTG Life</em></a><em> เรามีสาระความรู้และเรื่องราวดีๆ จากชาว KBTG พร้อมเสิร์ฟให้ที่นี่ที่แรก</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9fda0dfa946c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/kbtg-life/%E0%B9%81%E0%B8%8A%E0%B8%A3%E0%B9%8C%E0%B8%9B%E0%B8%A3%E0%B8%B0%E0%B8%AA%E0%B8%9A%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B8%93%E0%B9%8C%E0%B8%97%E0%B8%B3-ai-agent-%E0%B8%84%E0%B8%A3%E0%B8%B1%E0%B9%89%E0%B8%87%E0%B9%81%E0%B8%A3%E0%B8%81%E0%B9%83%E0%B8%99%E0%B8%8A%E0%B8%B5%E0%B8%A7%E0%B8%B4%E0%B8%95-9fda0dfa946c">แชร์ประสบการณ์ทำ AI Agent ครั้งแรกในชีวิต</a> was originally published in <a href="https://medium.com/kbtg-life">KBTG Life</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[มาทำความรู้จักกับ Ktlint กัน]]></title>
            <link>https://medium.com/kbtg-life/%E0%B8%A1%E0%B8%B2%E0%B8%97%E0%B8%B3%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%88%E0%B8%B1%E0%B8%81-ktlint-%E0%B8%81%E0%B8%B1%E0%B8%99-5b497079631f?source=rss-c4e27bbfe1f9------2</link>
            <guid isPermaLink="false">https://medium.com/p/5b497079631f</guid>
            <category><![CDATA[ktlint]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[lint]]></category>
            <dc:creator><![CDATA[iMint]]></dc:creator>
            <pubDate>Tue, 11 Jun 2024 04:19:02 GMT</pubDate>
            <atom:updated>2024-07-01T04:42:00.578Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*b5_HS35bo_RXCz6W.png" /><figcaption><a href="https://pinterest.github.io/ktlint/latest/">https://pinterest.github.io/ktlint/latest/</a></figcaption></figure><p>การเขียนโค้ดก็เหมือนการเขียนหนังสือหนึ่งเล่ม ที่จะต้องประกอบไปด้วยโครงเรื่อง เนื้อเรื่อง การเรียบเรียงถ้อยคำ การจัดรูปแบบ เพื่อให้ผู้ที่เข้ามาอ่านหนังสือเล่มนั้นอ่านง่ายและเข้าใจง่าย</p><p>นอกจากโครงสร้างการเขียน เช่น MVC, MVP, MVVM หรือ MVI อีกสิ่งที่สำคัญไม่น้อยไปกว่ากันในการเขียนโค้ดคือการเรียบเรียงและการจัดรูปแบบ เพื่อให้ง่ายต่อการเข้าใจของผู้ที่มาอ่านหรือเขียนโค้ดต่อจากเรา และให้การเขียนโค้ดเป็นไปในทิศทางเดียวกัน</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*JRFoXZvrpWlN4TRsrLS9Qg.jpeg" /><figcaption>Generated by AI</figcaption></figure><p>และสิ่งที่เข้ามาตอบโจทย์ในส่วนนี้คือ Lint เครื่องมือที่ทำหน้าที่ตรวจสอบและบังคับใช้รูปแบบโค้ด ซึ่งในปัจจุบันสำหรับ Android เราก็มี Ktlint ในรูปแบบเครื่องมือที่ใช้คำสั่งผ่าน Command Line ช่วยตรวจสอบ Format โค้ดของภาษา Kotlin</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*qmMlNeDCtkrhIY6LzxT-QA.jpeg" /><figcaption>Generated by AI</figcaption></figure><h3>นำ Ktlint มาใช้ในโปรเจค</h3><p>การนำ Ktlint มาใช้ในโปรเจคนั้นมีอยู่ด้วยกัน 2 วิธี วิธีแรกคือการใช้ Extension จะทำให้ Ktlint อยู่บนเครื่องของผู้ใช้งาน ส่วนวิธีที่สองคือการฝัง Ktlint ลงในโปรเจคของเราเลย</p><h4><strong>1. การใช้ Extension</strong></h4><p><strong>การใช้ Extension ผ่านการลง Brew</strong> เป็นข้อมูลจากเอกสาร Office ของ Ktlint ซึ่งสามารถลงผ่าน Brew และใช้งาน Ktlint ผ่าน Terminal หรือ Command Line ได้เลย</p><p><a href="https://pinterest.github.io/ktlint/latest/quick-start/">Quick start</a></p><pre>brew install ktlint</pre><pre>ktlint --format<br># or<br>ktlint -F</pre><p><strong>การใช้ Extension ผ่าน Android Studio</strong> เป็นอีกวิธีที่สะดวกมากๆ เข้าไปที่ Setting ที่เมนู Plugins จากนั้นค้นหาคำว่า Ktint แล้วทำการติดตั้งได้เลย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hS2JAmYJnDm9kH7ZjNeF8w.png" /></figure><p>วิธีใช้งานก็ไม่ได้ยากอย่างที่คิด เพียงเข้าไปที่ Refactor แล้วมองหาเมนู Ktlint Format</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vnLWxlAZB0GMk_pZ" /><figcaption><a href="https://plugins.jetbrains.com/plugin/15057-ktlint">https://plugins.jetbrains.com/plugin/15057-ktlint</a></figcaption></figure><blockquote>ทั้งนี้การใช้วิธีดังกล่าวจะไม่ได้ทำให้ Ktlint ติดไปกับโปรเจคของเรา แต่จะอยู่แค่ในเครื่องผู้ใช้งานเท่านั้น</blockquote><h4><strong>2.การฝัง Ktlint ในโปรเจค</strong></h4><p>วิธีนี้จะเป็นที่นิยมของเหล่า Developer ส่วนใหญ่ เพราะเมื่อเรา Clone โปรเจคไปลงที่เครื่องอื่นๆ เราจะสามารถใช้ Ktlint ได้อย่างสะดวกและเป็นไปในทิศทางเดียวกันกับที่ทีมพัฒนากำหนด</p><p>ที่มาของ Library ตัวนี้มาจากการพัฒนาของ <a href="https://github.com/JLLeitschuh/ktlint-gradle">GitHub: JLLeitschuh/ktlint-gradle</a> ต่อยอดมาจาก Ktlint Office ที่ผูกติดกับ Gradle</p><h3><strong>วิธีใช้ Ktlint Gradle</strong></h3><p>เพิ่ม Library ลงใน app/build.gradle โดยสามารถเข้าไปเช็ค Current Version ได้ที่ <a href="https://github.com/JLLeitschuh/ktlint-gradle">GitHub: JLLeitschuh/ktlint-gradle</a></p><pre>plugins {<br>  id(&quot;org.jlleitschuh.gradle.ktlint&quot;) version &quot;&lt;current_version&gt;&quot;<br>}<br><br>repositories {<br>  // Required to download KtLint<br>  mavenCentral()<br>}</pre><p>เพิ่มขอบเขตการใช้งาน Ktlint ลงใน app/build.gradle โดยสามารถเข้าไปเช็ค Current Version ได้ที่ <a href="https://pinterest.github.io/ktlint/latest/">Features — Ktlint (pinterest.github.io)</a></p><pre>ktlint {<br>    version = &quot;&lt;current_version&gt;&quot;<br>    android = true<br>    ignoreFailures = false<br>    enableExperimentalRules = true<br>    reporters {<br>        reporter(ReporterType.PLAIN)<br>        reporter(ReporterType.CHECKSTYLE)<br>        reporter(ReporterType.SARIF)<br>    }<br>}</pre><p>เมื่อทำการเพิ่ม Library แล้ว เราจะสามารถรันคำสั่งในการตรวจสอบโค้ดและทำการ Format โค้ดด้วยคำสั่งนี้</p><pre>./gradlew ktlintCheck <br>./gradlew ktlintFormat</pre><p>หลังจากที่ทำการรันคำสั่งเรียบร้อย จะสามารถดูได้ว่ามีการรันที่ Success หรือ Fail หาก Fail จะมีการบอกว่า Fail ที่อะไรบ้าง ดังรูป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xjoD0JeE8XGVIlBTqCgQcQ.png" /></figure><p>มีไฟล์ Report การตรวจสอบโค้ดอยู่ที่ไฟล์ Build ซึ่งเราสามารถเข้าไปตรวจสอบข้อผิดพลาดของ Style Code ที่เกิดขึ้นได้ ดังรูป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/397/1*VtVLRy68jk1d9619lNUCsA.png" /></figure><p>ในไฟล์ ktlintMainSourceSetCheck.xml จะเป็นรายงานที่เราสามารถนำ Source ไปใช้ในการ Disable Rule หรือ Suppress Rule ที่เราไม่ต้องการได้ด้วย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GqDHiOpIHlBAqSZe6-OhUQ.png" /></figure><h3>การกำหนดกฎรูปแบบโค้ด .editorconfig</h3><p>เราสามารถกำหนดรูปแบบโค้ดที่ต้องการด้วยไฟล์ .editorconfig โดย Ktlint จะทำการตรวจสอบการ Format ที่ไม่ถูกต้อง ช่วยให้โค้ดของเราสอดคล้องกับกฎและดูสะอาดขึ้น</p><h4><strong>Disable Rule</strong></h4><p>เป็นการปิดการบังคับไม่ให้ตรวจสอบกฎของ Format โค้ดนี้ เช่น หากเราต้องการบังคับไม่ให้ตรวจสอบกฎการ Import Wildcard เราสามารถเพิ่มกฎดังกล่าวในไฟล์ .editorconfig ดังรูป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/867/1*P2OQQRu_C4nXlL5X7__o7g.png" /></figure><p>เราสามารถดูกฎที่จะนำมาเขียน Disable Rule ได้จากไฟล์รายงานที่กล่าวข้างต้น ด้วยการนำ Source จากรายงานมา Disable Rule ตามวิธีการเขียนนี้</p><pre>ktlint_standard_&lt;name standard of source&gt; = disabled</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BEVkNhSsitVVcU5TA6HVEA.png" /></figure><p>ยกตัวอย่างจากไฟล์รายงานนี้ เราต้องการ Disable Rule ของ Comment-spacing ก็สามารถเพิ่มการ Disable Rule ในไฟล์ .editorconfig ได้ตามนี้</p><pre>ktlint_standard_comment-spacing = disabled</pre><h4><strong>Suppress Rule</strong></h4><p>เป็นการมองข้ามกฎบังคับ ไม่ให้ตรวจสอบบรรทัดหรือไฟล์นั้นๆ โดยเราสามารถเพิ่ม Annotation บน Function ที่ต้องการให้มองข้าม ซึ่งการเขียนชื่อสำหรับการ Suppress Rule จะเป็นการนำชื่อของ Source จากไฟล์รายงานมาใช้ ดังรูป</p><pre>@Suppress(&quot;ktlint:standard:&lt;name standard of source&gt;&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/495/1*euUZ1p1EWyILeMOZ3PUFGA.png" /></figure><h3>การทำ Script เพื่อให้รัน Ktlint ได้ง่ายๆ</h3><p>ก่อนหน้านี้ผู้เขียนเคยเขียนบทความการทำ Script ไว้รัน Emulator สามารถอ่านบทความดังกล่าวที่ลิงก์ด้านล่าง</p><p><a href="https://chalitta-c-k.medium.com/%E0%B9%80%E0%B8%9B%E0%B8%B4%E0%B8%94-emulator-%E0%B9%81%E0%B8%AD%E0%B8%99%E0%B8%94%E0%B8%A3%E0%B8%AD%E0%B8%A2%E0%B8%94%E0%B9%8C%E0%B9%82%E0%B8%94%E0%B8%A2%E0%B9%84%E0%B8%A1%E0%B9%88%E0%B9%80%E0%B8%9B%E0%B8%B4%E0%B8%94-android-studio-%E0%B8%97%E0%B8%B3%E0%B9%84%E0%B8%94%E0%B9%89%E0%B8%AD%E0%B8%A2%E0%B9%88%E0%B8%B2%E0%B8%87%E0%B9%84%E0%B8%A3-3dcc5f9b4a85">เปิด emulator แอนดรอยด์โดยไม่เปิด Android Studio ทำได้อย่างไร</a></p><p>ในส่วนนี้จะใช้หลักการเดียวกัน เริ่มจากเปิด Terminal ขึ้นมา จากนั้นพิมพ์คำสั่งสำหรับเขียนไฟล์ Script</p><pre>vi {NAME}.sh</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*479QAJQol7SuK0cikRDYng.png" /></figure><p>กด i (INSERT) เพื่อทำการพิมพ์คำสั่ง</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/354/1*KrsYzPWZWS1z4R0eQ2Em5g.png" /></figure><p>พิมพ์คำสั่งของ Ktlint ลงไป จากนั้นกด esc และ :wq เพื่อเขียนและออกจากไฟล์</p><pre>./gradlew ktlintCheck <br>./gradlew ktlintFormat</pre><p>ใน Android Studio เราสามารถกด Play (สีเขียว) เพื่อรันคำสั่ง Ktlint ได้เลย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/489/1*ZCxnEuVytyukIL_MziirTA.png" /></figure><p>จะเห็นว่าเราสามารถรัน Ktlint ได้อย่างง่าย เพียงแค่รัน Script ที่เราได้ทำไว้ ทำให้โค้ดของเราดูสะอาด อ่านง่าย เป็นไปในทิศทางรูปแบบเดียวกัน ทั้งยังง่ายต่อการบริหารจัดการโค้ดของเราต่อไปในอนาคต</p><h3><strong>สรุป</strong></h3><p>Android Developer โดยส่วนใหญ่แล้วจะนิยมใช้ Ktlint ที่ฝังไว้กับโปรเจคของ <a href="https://github.com/JLLeitschuh/ktlint-gradle">GitHub: JLLeitschuh/ktlint-gradle</a> เพราะง่ายต่อการนำโค้ดที่อยู่บน Git ไปรันบน Local เครื่องนั้นๆ แถมตัว Ktlint ก็ยังสามารถรันได้โดยยึดหลักการเดียวกันที่ได้ทำไว้แล้วใน Gradle และ .editorconfig ยิ่งเราทำ Script ไว้ในไฟล์โปรเจค ก็จะยิ่งทำให้การรัน Ktlint สะดวกมากขึ้น</p><p>นอกจากนี้เรายังสามารถนำ Script ที่ทำไว้ไปใช้ใน Pipeline ของ CI/CD ได้อีกด้วย ทุกครั้งที่มีการรัน Pipeline ใน CI/CD ก็จะช่วยให้มีการตรวจเช็ครูปแบบโค้ดของเราผ่าน Ktlint ก่อนจะ Build โปรเจคนั่นเอง</p><p>ก่อนจากกันไป ขอทิ้งท้ายด้วย Trick ดีๆ อีกซักเล็กน้อย…</p><p>เราสามารถใช้ Ktlint กำหนดค่าโปรเจคให้จัดรูปแบบโค้ด Kotlin โดยอัตโนมัติก่อนที่จะสร้างโปรเจคได้ เพียงแค่เพิ่มบรรทัดต่อไปนี้ลงในไฟล์ build.gradle</p><pre>tasks.getByPath(&quot;preBuild&quot;).dependsOn(&quot;ktlintCheck&quot;).dependsOn(&quot;ktlintFormat&quot;)</pre><blockquote>See you next time 👋</blockquote><p><em>สำหรับใครที่ชื่นชอบบทความนี้ อย่าลืมกดติดตาม </em><a href="https://www.medium.com/kbtg-life"><em>Medium: KBTG Life</em></a><em> เรามีสาระความรู้และเรื่องราวดีๆ จากชาว KBTG พร้อมเสิร์ฟให้ที่นี่ที่แรก</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5b497079631f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/kbtg-life/%E0%B8%A1%E0%B8%B2%E0%B8%97%E0%B8%B3%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%88%E0%B8%B1%E0%B8%81-ktlint-%E0%B8%81%E0%B8%B1%E0%B8%99-5b497079631f">มาทำความรู้จักกับ Ktlint กัน</a> was originally published in <a href="https://medium.com/kbtg-life">KBTG Life</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Security Keys on Android]]></title>
            <link>https://chalitta-c-k.medium.com/security-keys-on-android-7cf2a00a13e4?source=rss-c4e27bbfe1f9------2</link>
            <guid isPermaLink="false">https://medium.com/p/7cf2a00a13e4</guid>
            <category><![CDATA[android-ndk]]></category>
            <category><![CDATA[security-key]]></category>
            <category><![CDATA[ssl-pinning]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[proguard]]></category>
            <dc:creator><![CDATA[iMint]]></dc:creator>
            <pubDate>Wed, 04 Nov 2020 18:50:07 GMT</pubDate>
            <atom:updated>2020-11-04T18:50:07.230Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*09zsh2WwbcKHs0JIH29T9Q.png" /></figure><p>ความปลอดภัยของแอพพลิเคชั้นเป็นเรื่องสำคัญอย่างมากในปัจจุบัน เพราะเป็นสิ่งที่ทำให้ผู้ใช้มั่นใจว่าหากมาใช้แอพพลิเคชั้นของเราแล้วจะปกป้องข้อมูลของผู้ใช้ ไม่ยอมให้แฮกเกอร์มือดีมาฉกฉวยข้อมูลอันล้ำค่าของผู้ใช้ได้ ดังนั้นในบนความนี้จะขอพูดถึง 3 เรื่องซึ่งเป็นพื้นฐานในการทำความปลอดภัยให้กับแอพ ดังนี้</p><ul><li>Certificate Pinning</li><li>ProGuard</li><li>NDK (Native Development Kit)</li></ul><h3>Certificate<strong> Pinning</strong></h3><p>คือการทำให้ทุกครั้งที่เชื่อมต่อ server ต้องผ่านใบอนุญาตที่ได้รับอนุญาตไว้เท่านั้น ถ้าไม่เช่นนั้นจะไม่สามารถเชื่อมต่อได้ เพราะเป็นการป้องกันไม่ให้แฮกเกอร์มือสมัครเล่นมาทำการขโมยข้อมูล หรือเปลี่ยนแปลงข้อมูลของผู้ใช้ก่อนส่งไปยัง server เช่นการทำธุรกรรมทางการเงิน แฮกเกอร์อาจจะแอบเปลี่ยนแปลงจำนวนเงิน หรือฉกเอาข้อมูลทางธุรกรรมทางการเงินเราได้ ในกรณีที่แอพไม่ได้ทำการเข้ารหัสข้อมูลไว้ ( Encrypt นั่นเอง ในที่นี่ขอไม่พูดถึง ไว้จะมาแชร์ให้ในบทความหน้านะคร้าบบบ)</p><p>การทำ Certificate Pinning เราจะต้องรู้ว่า server ที่เราใช้ในการเรียก API มี Certificate อะไร เพื่อที่จะนำใบอนุญาตที่ได้ไปกำหนดค่าในแอพของเรา วิธีที่เรียกใช้ในการดู Certificate ผู้เขียนเลือกใช้ 2 วิธีคือ</p><ol><li>Termial คือทำการรัน script openssl ผ่าน termial โดยใช้ชุดคำสั่งด้านล่าง และรัน <a href="https://gist.github.com/ChalittaKhampachua/3771a4ca987fb5b92515fae08a1fc425">script</a> ผ่านคำสั่ง และตามด้วย server ที่เราต้องการ Certificate</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/567/1*Wn9wq2zrIlBZXLSJStWxWA.png" /><figcaption>certs.sh</figcaption></figure><pre>username$ ./certs.sh www.google.com</pre><p>2. Website โดยเว็ปที่ใช้ในการดู Certificate คือ <a href="https://www.ssllabs.com/ssltest/">https://www.ssllabs.com/ssltest/</a> ซึ่งเราสามารถใส่ hostname server ของเราลงไป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rX-QcOQMWn9ZHWQl1c6Gow.png" /><figcaption>ตัวอย่างเว็ปไซต์ Google</figcaption></figure><p>จากนั้นเลือก server ที่เราใช้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xQ5U64eurFapdAl0ajLq7w.png" /><figcaption>ตัวอย่าง Server ของเว็ปไซต์ Google</figcaption></figure><p>จากนั้นเราจะเห็น Certificate server โดยเราสามารถเลือกใช้ Certificate ไหนก็ได้โดยที่ดูจากวันหมดอายุเป็นหลัก หากเลือกวันหมดอายุน้อยๆ ก็ต้องขยันมาเปลี่ยนรหัส Certificate ที่แอพด้วยนะคร้าบ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NU6_A-Evb_dye01G4a8g1w.png" /><figcaption>ตัวอย่าง Certificate ของเว็ปไซต์ Google</figcaption></figure><h3><strong>วิธีทำ </strong>Certificate<strong> Pinning ผ่าน Retrofit</strong></h3><pre>val certPinner = CertificatePinner.Builder()<br>    .add(<strong><em>HOST_URL_1</em></strong>, &quot;sha256/&quot;+ <strong><em>SSL_SHA256_1</em></strong>)<br>    .add(<strong><em>HOST_URL_2</em></strong>, &quot;sha256/&quot;+ <strong><em>SSL_SHA256_2</em></strong>)<br>    .build()</pre><pre>val okHttpClient = OkHttpClient.Builder()<br>    .certificatePinner(certPinner)<br>    .build()</pre><pre>Retrofit.Builder()<br>    .baseUrl(<strong><em>BASE_URL</em></strong>)<br>    .addConverterFactory(GsonConverterFactory.create(get()))<br>    .client(okHttpClient)<br>    .build()</pre><p>ตัว CertificatePinner จะเป็นตัวที่กำหนดใบอนุญาตการติดต่อกับ server ของเราไว้ จากนั้นนำไปใส่ที่ OKHttpClient ตัวกลางที่เราใช้ในการติดต่อ server และเรียก ใช้งาน Retrofit ได้ตามปกติ จะสังเกตุว่ามีการใช้ sha256/ ก่อนที่จะใส่รหัสใบอนุญาตเพราะจะเป็นการระบุรหัสเราว่าเป็นประเภท SHA256 นั่งเอง</p><h3>วิธีทำ Certificate Pinning</h3><p>วิธีการนี้เป็นวิธีการสร้าง certificata pinning โดยการสร้าง config ไฟล์ xml ขึ้น ดังนี้</p><ol><li>เพิ่มการตั้งค่า networkSecurityConfig ผ่านไฟล์ xml/network_security_config โดยทำการแก้ไข้ที่ AndroidManifest.xml</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ATqO1kIAdMIv5cdRYnlTZw.png" /></figure><p>2. สร้างไฟล์ network_security_config.xml ไว้ใน res/xml โดยเราจะกำหนดโดเมน และรหัสใบอนุญาตประเภท SHA256</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z-rxohUkd76-f5hL4MY2QQ.png" /></figure><p>ใครอยากอ่านรายละเอียดเชิงลึกเชิญได้ที่ลิงค์ด้านล่างนี้เลยคับ</p><p><a href="https://developer.android.com/training/articles/security-config">Network security configuration | Android Developers</a></p><h3><strong>วิธีการตรวจสอบ </strong>Certificate<strong> Pinning</strong></h3><p>มีเครื่องมือต่างๆที่ใช้ในการ sniff ข้อมูลเช่น proxyman, burp suite เป็นต้น ซึ่งเป็นเครื่องมือที่ใช้ตรวจสอบ proxy นั่นเอง (HTTP Debugging Proxy) ซึ่งในที่นี่ผู้เขียนใช้ proxyman ในการตรวจสอบ หากยังสามารถดักจับข้อมูลได้อยู่แสดงว่าทำ Certificate Pinning ไม่สำเร็จนะคร้าบบบบ (รู้นะ คิดไรอยู่! 😎 อย่านำโปรแกรมไปใช้ในทางที่ผิดนะคร้าบ ผิดกฎหมายนะฮะ )</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FQhAjPq9Ft4o_e71YDY_mg.png" /><figcaption><a href="https://proxyman.io/">Proxyman Tool</a></figcaption></figure><h3><strong>ProGuard</strong></h3><p>คือการป้องกันโค้ดของเราด้วยการทำให้โค้ด obfuscation หรือวุ่นวายสับสนนั่นเอง เพื่อป้องกันการ decomplier หรือ reverse engineer จากแฮกเกอร์มือสมัครเล่น โดยทาง google บอกว่า</p><blockquote>เครื่องมือ ProGuard จะย่อขนาดเพื่อเพิ่มประสิทธิภาพและทำให้โค้ดของคุณสับสนโดยการลบโค้ดที่ไม่ได้ใช้ออกและเปลี่ยนชื่อคลาส, ฟิลด์, และเมธอดด้วยชื่อที่คลุมเครือ ผลลัพธ์ที่ได้คือไฟล์. apk จะมีขนาดเล็กลงซึ่งยากต่อการทำวิศวกรรมย้อนกลับ</blockquote><p>เช่นเมื่อมีแฮกเกอร์ได้นำ APK ของแอพไปเข้าโปรแกรมเพื่อแตกไฟล์โค้ดจาก APK แฮกเกอร์ก็จะเห็นโค้ดของเราหมดเลยว่าเขียนอะไรบ้าง มี key สำคัญอะไรบ้าง เพราะสามารถนำโค้ดของเราไปปลอมแปลง หรือนำ key สำคัญในโค้ดไปใช้เพื่อดักจับข้อมูลผู้ใช้ได้</p><h3><strong>วิธีทำ ProGuard</strong></h3><pre>buildTypes <strong>{<br>    </strong>release <strong>{<br>        </strong>minifyEnabled true<br>        shrinkResources true<br>        proguardFiles getDefaultProguardFile(<br>                &#39;proguard-android-optimize.txt&#39;),<br>                &#39;proguard-rules.pro&#39;<br>    <strong>}<br>}</strong></pre><p>เพิ่มแก้ไข้โค้ดในส่วนของไฟล์ build.gradle โดยมีรายละเอียดดังนี้</p><p>minifyEnabled เปิดใช้งานการย่อขนาดโค้ด, การทำให้ยุ่งเหยิง, และการปรับให้เหมาะสมสำหรับประเภทบิวด์ release ของโปรเจ็กต์เท่านั้น</p><p>shrinkResources เปิดใช้งานการลดขนาด resources ซึ่งดำเนินการโดย Android Gradle plugin.</p><p>proguardFiles รวมไฟล์กฎของ ProGuard ที่ใช้ร่วมกับ Android Gradle plugin</p><p>โดยไฟล์ proguard-rules.pro ซึ่งเป็นไฟล์ที่ใช้เขียนกฎของ ProGuard มีไว้สำหรับบังคับให้ห้ามตัดไฟล์อันไหนทิ้งในโค้ด ซึ่งหากไม่กำหนดจะทำให้ตัดโค้ดบ้างส่วนของเราทิ้งและเกิด error ขึ้นมาได้</p><pre>-keep class com.mcodeapps.project.model.** {*;}</pre><pre># Retrofit<br>-dontwarn retrofit2.**<br>-keep class retrofit2.** { *; }<br>-keepattributes Signature<br>-keepattributes Exceptions<br>-keepattributes *Annotation*<br><br># Material<br>-dontwarn com.google.android.material.**</pre><p>-keep class รักษา class และ object ใน class ที่ระบุ</p><p>-dontwarn อย่าเตือนเกี่ยวกับการอ้างอิงที่ไม่ได้รับการแก้ไข</p><p>-keepattributes รักษา attribute ที่ระบุ</p><p>ซึ่งกฎของการเขียน ProGuard ก็มีเยอะมากสามารถอ่าน<a href="https://www.guardsquare.com/en/products/proguard/manual/refcard">เพิ่มเติม</a>ได้ นอกจากนี้ใครที่ทำ Crashlytics ไว้ก็อย่าลืมที่จะอัพโหลดไฟล์ mapping.txt ขึ้นไปด้วย ถ้าไม่เช่นนั้นจะอ่านโค้ดที่รายงานผ่าน Crashlytics ไม่รู้เรื่องนะฮะ</p><h3><strong>วิธีการตรวจสอบ ProGuard</strong></h3><p>โดยส่วนใหญ่แล้วนักพัฒนาก็จะสามารถเลือกที่ Build &gt; Analyze APK… ก็จะแสดงการตรวจสอบว่า APK ของเราใช้งานได้จริงหรือไม่</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*zAcb_3Jzfsrm3pd1-y9gDQ.png" /></figure><p>และถ้าสังเกตุจะเห็นว่าชื่อบ้าง class หรือชื่อบ้างตัวแปรจะอ่านไม่รู้เรื่อง (c.a.a.a) โดยจะเปลี่ยนชื่อด้วยวิธีการแทนที่ด้วยตัวอักษรภาษาอังกฤษa-z ซึ่งถือว่าเป็นการทำ ProGuard สมบรูณ์</p><p>แต่ก็ยังมีบ้าง class ที่ ProGuard ไม่สามารถแปลงชื่อคลาสได้คือ คลาสที่ถูกประกาศไว้ใน <strong>AndroidManifest.xml </strong>จะยังมีการเก็บชื่อคลาสไว้อยู่ (activities, services, providers, receivers, application, instrumentation) เพื่อป้องกันไม่ให้ระบบพัง</p><p>แต่เราก็ยังสามารถทำ <a href="https://www.guardsquare.com/en/products/proguard/manual/usage#obfuscationoptions">Manually ProGuard</a> ได้เช่นกันโดยจะกำหนดให้ทำการเปลี่ยนแปลงชื่อ pagage ให้สับสน แต่ว่าวิธีนี้ก็ต้องระวังเพราะอาจจะทำให้โปรแกรมไม่สามารถแมพไฟล์กับ ProGuard ได้</p><pre>-repackageclasses &#39;&#39;<br>-allowaccessmodification</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*9obvpkSl3q_dJwsSXdOt_w.png" /><figcaption>ตัวอย่างการทำ manually proguard</figcaption></figure><p>เห็นแบบนี้อย่าสบายใจไป เพราะเวลาเราทำการ decomplier ผ่าน java decomplier ก็ยังคงเจอชื่อคลาสที่ถูกประกาศไว้ใน <strong>AndroidManifest.xml </strong>อยู่ แต่ชื่อตัวแปรที่อยู่ในคลาสจะถูกแทนที่ด้วยการ obfuscate และก็ไม่ได้หมายความว่า value ของตัวแปรนั่นจะถูกทำ obfuscate</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*bRBDdeldWaAuOVB4WP8PLg.png" /><figcaption>รูปที่แสดงให้เห็นว่าค่าของตัวแปรยังคงอยู่</figcaption></figure><p>จากรูปจะเห็นได้ว่าเมื่อทำการ decomplier แล้วยังเห็นค่าคีย์สำคัญอยู่ ซึ่งก็ยังทำให้แอพพลิเคชั้นยังไม่ปลอยภัย 100 %</p><blockquote>จึงเป็นที่มาว่าทำไมผู้เขียนถึงหาวิธีที่จะทำความปลอดภัยให้กับคีย์สำคัญ โดยการใช้ NDK เข้ามาช่วยปกปิดค่าของตัวแปรนั่นเอง</blockquote><h3><strong>NDK (Native Development Kit)</strong></h3><p>Native Development Kit (NDK) เป็นชุดเครื่องมือที่ช่วยให้ใช้โค้ด C และ C ++ กับ Android และมีไลบรารีแพลตฟอร์มที่สามารถใช้เพื่อจัดการ native activities และเข้าถึงส่วนประกอบของอุปกรณ์ทางกายภาพ เช่น sensors และ touch input</p><p>แอนดรอยใช้ <a href="https://cmake.org/">Cmake</a> เครื่องมือในการ compile ตัว Native libraries ซึ่งก็จะเป็นตัวที่เราจะใช้ในการพัฒนา ซึ่งการเขียนโค้ดด้วย C++ ก็เป็นส่วนหนึ่งที่จะทำให้คีย์สำคัญของเรา (sensitive data) หายากขึ้นจากการ decomplier หรือปลอยภัยมากขึ้นกว่าเดิม</p><h3>วิธีทำ NDK</h3><ol><li>เลือกสร้างโปรเจคแบบ Native C++</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*fuvQPz8LJHscjQfxpYYZ6w.png" /></figure><p>2. จะสังเกตุว่าที่ไฟล์ build.gradle มีการเพิ่มโค้ดของ cmake เข้ามาใช้ในโปรเจค ซึ่งแสดงว่าการสร้างโปรเจค NDK ทำงานได้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*DkQuJhcHOxmDXcpSwb5fKg.png" /></figure><p>3. จากนั้นนทำการสร้างไฟล์ native-lib.cpp ขึ้นมาและทำการเขียนโค้ดเพิ่มคีย์ที่เราอยากจะใช้ทำให้ปลอดภัยลงไป โดยที่ข้างหน้าชื่อฟังก์ชั้นที่เราใช้จะประกอบไปด้วย ชื่อคลาส . ชื่อฟังก์ชั้นที่เราต้องการ ดังรูป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6A9blEXZFg0MIUQPzq5XQA.png" /></figure><p>4. จากนั้นไปยังหน้า activity ที่เราต้องการเรียกใช้ โดยวิธีการเรียกใช้ จะต้องทำการเรียกใช้แบบโหลดไลบารี่ native-lib ที่เราได้เขียนไว้ในภาษา c++ มาใช้ก่อน จากนั้นทำการ external ฟังก์ชั้นที่ได้เขียนใน native-lib ออกมา และนำไปเรียกใช้ได้เลย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/832/1*MLzpS3w5JtI4_igqyGaKfQ.png" /></figure><h3>วิธีการตรวจสอบ NDK</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TEB5lOhtiSM50iHL4S-FRw.png" /></figure><p>จากภาพฝั่งซ้ายมือคือโค้ดที่ได้ทำการ NDK เรียบร้อยแล้วเมื่อทำการ decomplier และทำการค้นหาคีย์สำคัญจะสังเกตุว่าไม่พบคีย์นั่นเลย</p><p>จากภาพฝั่งขวามือคือโค้ดที่ไม่ได้ทำการ NDK และทำการ decomplier จะสังเกตุว่าเราเห็นคีย์สำคัญของเราอยู่ และการ decomplier แบบที่ผู้เขียนใช้เป็นการ decomplier เชิงลึกที่สามารถแกะมาจาก ProGuard ที่ใช้การ rename จากตัว p และ q ดังรูปประกอบด้านล่าง</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*bRBDdeldWaAuOVB4WP8PLg.png" /></figure><p>ซึ่งก็จะเห็นได้ว่าวิธีการสร้างความปลอดภัยให้กับ sensitive data ทำได้โดยใช้วิธีการ NDK</p><p>การ decomplier หรือ reverse engineer มีให้เลือกหลากหลายวิธีแล้วแต่ความถนัดความเหมาะสม และความปลอยภัยของแอพนั่นๆ หากใครเลือกใช้เว็ปไซต์ก็ควรระวังเรื่องการเอา apk ที่ใช้อัพขึ้นไป อาจจะทำให้แอพที่ทำ security มาก็ไม่มีประโยชน์นะค้าบ</p><p>จากบทความข้างบน นั่นก็เป็นองค์ความรู้เรื่องความปลอดภัยอันน้อยนิดที่ตกผลึกมาให้ทุกคนได้อ่านกัน ถ้าหากใครชอบบทความนี้อย่างลืมกดไลท์ กดแชร์ และกดติดตามเพื่อจะไม่พลาดคลิปใหม่ๆของพวกเราด้วยนะค้าบบบ (เห้ย! พูดเป็นยูทูปเปอร์ไปได้ 555) ไว้เจอกันในบทความหน้านะคับ :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7cf2a00a13e4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[เปิด emulator แอนดรอยด์โดยไม่เปิด Android Studio ทำได้อย่างไร]]></title>
            <link>https://chalitta-c-k.medium.com/%E0%B9%80%E0%B8%9B%E0%B8%B4%E0%B8%94-emulator-%E0%B9%81%E0%B8%AD%E0%B8%99%E0%B8%94%E0%B8%A3%E0%B8%AD%E0%B8%A2%E0%B8%94%E0%B9%8C%E0%B9%82%E0%B8%94%E0%B8%A2%E0%B9%84%E0%B8%A1%E0%B9%88%E0%B9%80%E0%B8%9B%E0%B8%B4%E0%B8%94-android-studio-%E0%B8%97%E0%B8%B3%E0%B9%84%E0%B8%94%E0%B9%89%E0%B8%AD%E0%B8%A2%E0%B9%88%E0%B8%B2%E0%B8%87%E0%B9%84%E0%B8%A3-3dcc5f9b4a85?source=rss-c4e27bbfe1f9------2</link>
            <guid isPermaLink="false">https://medium.com/p/3dcc5f9b4a85</guid>
            <category><![CDATA[commands]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[emulator]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[terminal]]></category>
            <dc:creator><![CDATA[iMint]]></dc:creator>
            <pubDate>Wed, 20 Nov 2019 06:59:33 GMT</pubDate>
            <atom:updated>2019-11-20T10:52:56.900Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7oISEMX93MHqMLTULfP3VQ.png" /></figure><p>หลายครั้งที่เราพัฒนาโปรแกรม แล้วต้องการเปิด emulator หรือเครื่องจำลองมือถือ เพื่อทำการทดสอบ หรือเพื่อต้องการดูผลลัพธิ์ของโค้ดที่เขียนไป แต่ไม่ต้องการเปิด android studio เพื่อทำการสร้าง emulator ขึ้นมา โดยในบทความนี้จะสอนวิธีที่จะทำให้เปิด emulator ได้อย่างง่าย แบ่งออกเป็น 2 วิธีด้วยกัน</p><h3>เปิดจาก Terminal</h3><p>เข้าไปยังโฟลเดอร์ที่ SDK ที่มี emulator อยู่</p><pre>cd ~/Library/Android/sdk/emulator</pre><p>จากนั้นเช็คว่ามี emulator ตัวไหนที่ถูกสร้างไว้หรือไม่</p><pre>./emulator -list-avds</pre><p>จากนั้นเราเลือกตัว emulator ที่เราต้องการรันขึ้นมา</p><pre>./emulator -avd {AVD_NAME}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9MJx1022U5evUqBnTlGGpw.png" /></figure><h3>ทำ Script</h3><p>เป็นการสร้าง Script ขึ้นมาเพื่อรันคำสั่งที่เราใช้ใน terminal เพื่อให้ง่ายต่อการใช้งาน โดยที่เราไม่ต้องใช้คำสั่งใน terminal เยอะ<br>เริ่มจากเปิด terminal ขึ้นมาจากนั้นพิมพ์คำสั่งเพื่อต้องการเขียนไฟล์ script</p><pre>vi {NAME}.sh</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Zc-sKeSsdBH9pUfmh2duZA.png" /></figure><p>จากนั้นจะได้หน้าตาคลายกระดาษเพื่อเขียนคำสั่งลงไป โดยคลิก i เพื่อทำการ insert คำสั่งเมื่อพิมพ์เสร็จให้กด esc เพื่อออกจากคำสั่ง insert และใช้ :w เพื่อทำการ write และใช้คำสั่ง :q เพื่อ quit ออกจากไฟล์การเขียน</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i1nUXl4Gs1MBOLMDmGVuHw.png" /></figure><p>กด i และทำการพิมพ์คำสั้ง ตามด้วยชื่อ emulator ที่เราต้องการ</p><pre>~/Library/Android/sdk/tools/emulator -avd Nexus_5X_API_27</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1jSbdpVULfXe9d5FvZiHjg.png" /></figure><p>จากนั้นกด esc และ :wq เพื่อออกจากไฟล์ และทำการรันคำสั่งนั้นขึ้นมาด้วย</p><pre>./{NameFile}</pre><p>ยกตัวอย่างเช่นตั้งชื่อไฟล์ว่า emulator.sh</p><pre>./emulatar.sh</pre><p>เพียงเท่านี้เราก็จะได้คำสั่งที่สามารถใช้งานได้ตลอดเวลา เมื่อต้องการเปิด emulator แล้ว</p><h3>Trick</h3><p>สำหรับใครที่ต้องการปรับแต่งคำสั่งก็สามารถทำได้เช่นเดียวกัน ดังรูปเลยจ้า</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EEtqVyGiA0JLPdvX4h8bCg.png" /></figure><p>เป็นการปรับแต่งโดย จะทำการแสดงรายการทั้งหมดที่มี emulator ที่ถูกสร้างขึ้นก่อน จากนั้นทำการรับค่าด้วยตัวแปร name จากคำสั่ง read name แล้วทำการรัน emulator ตัวนั้นขึ้นมา</p><h3>สำหรับ emulator IOS</h3><p>เพียงพิมพ์คำสั่งเดียวใน terminal ก็จะสามารถสร้าง emulator ออกมาได้แล้วนะคับ</p><pre>open -a Simulator</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3dcc5f9b4a85" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[เขียนโค้ดแอนดรอยด์ให้มีคุณภาพยังไง?SonarQube ช่วยคุณได้]]></title>
            <link>https://chalitta-c-k.medium.com/%E0%B9%80%E0%B8%82%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B9%82%E0%B8%84%E0%B9%89%E0%B8%94%E0%B9%81%E0%B8%AD%E0%B8%99%E0%B8%94%E0%B8%A3%E0%B8%AD%E0%B8%A2%E0%B8%94%E0%B9%8C%E0%B9%83%E0%B8%AB%E0%B9%89%E0%B8%A1%E0%B8%B5%E0%B8%84%E0%B8%B8%E0%B8%93%E0%B8%A0%E0%B8%B2%E0%B8%9E%E0%B8%A2%E0%B8%B1%E0%B8%87%E0%B9%84%E0%B8%87-sonarqube-%E0%B8%8A%E0%B9%88%E0%B8%A7%E0%B8%A2%E0%B8%84%E0%B8%B8%E0%B8%93%E0%B9%84%E0%B8%94%E0%B9%89-c32f508ab45e?source=rss-c4e27bbfe1f9------2</link>
            <guid isPermaLink="false">https://medium.com/p/c32f508ab45e</guid>
            <category><![CDATA[sonar]]></category>
            <category><![CDATA[sonarqube]]></category>
            <category><![CDATA[sonarscanner]]></category>
            <category><![CDATA[sonarqube-คือ]]></category>
            <category><![CDATA[เขียนโค้ดให้มีคุณภาพ]]></category>
            <dc:creator><![CDATA[iMint]]></dc:creator>
            <pubDate>Thu, 20 Jun 2019 12:58:06 GMT</pubDate>
            <atom:updated>2019-06-20T12:58:06.832Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/904/1*SO4D0CjNvzvZq2FySkOD5g@2x.png" /></figure><p>การเขียนโค้ดให้ออกมาได้โดยโปรแกรมที่มีประสิทธิภาพ และเจอ bug น้อยที่สุด คือการเขียนโค้ดที่มีแบบแผน โดยแบ่งออกได้ดังนี้</p><ul><li><strong>code pattern</strong> เป็นการเขียนโค้ดตามแบบแผนทั้งทีม เพื่อให้ง่ายต่อการจัดการโดยกำหนดหลักการในการเขียนขึ้นแต่ละโปรเจค เช่นการกำหนดการประกาศตัวแปร และการกำหนดการเขียนแบบไหน (design pattern) อาทิ MVC, MVP, MVVM เป็นต้น</li><li><strong>code quality </strong>เป็นการตรวจสอบโค้ดว่ามีประสิทธิภาพขนาดไหน เขียนออกมาดีหรือไม่ ซึ่งในที่นี้เราจะพูดถึงเครื่องมือช่วยตรวจสอบโค้ด sonarqube เป็นหลัก</li><li><strong>code review </strong>เป็นการรีวิวโค้ด เครื่องมือที่ช่วยให้คนในทีมเข้าใจโค้ดของแต่ละคน พร้อมกับดูว่าโค้ดที่จะเอาไปรวมกับคนอื่นเขียนได้หรือไม่ เช่น gitlab จะมีเครื่องมือ merge request ที่จะส่ง request ให้คนในทีมดูก่อนที่จะทำการรวมโค้ดนั่นเอง</li><li><strong>unit test</strong> เป็นการเขียนเทสที่จะช่วยลดบัคที่เกิดจากการเขียนโค้ดของเรา</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cKOmggPLpm-u3utinIz19Q.png" /></figure><h3><strong>คุณภาพโค้ด ( code quality )</strong></h3><p>การเขียนโค้ดที่มีคุณภาพเป็นสิ่งที่สำคัญสำหรับการพัฒนาโปรแกรมขึ้นมาให้สมบรูณ์และมีประสิทธิภาพสูง นอกจากจะลดบัคแล้วยังทำให้โค้ดอ่านง่าย ง่ายต่อการพัฒนาต่อยอด และง่ายต่อการจัดการแก้ไข้อีกด้วย</p><p>เครื่องมือที่ช่วยในการตรวจสอบโค้ดแบบอัตโนมัติก็คือ SonarQube และ SonarLint ซึ่งจะใช้ควบคู่กัน</p><ul><li><strong>SonarLint</strong> เป็นเครื่องมือที่ช่วยตรวจสอบโค้ดว่าเขียนเป็นไปตามมาตรฐานหรือไม่ และให้คำแนะนำให้แก้ไขให้เป็นไปตามรูปแบบมาตรฐาน</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/670/1*ozbl-yT173buej31oz1Mjw.png" /></figure><ul><li><strong>SonarQube </strong>ตรวจสอบข้อผิดพลาด(Bugs) ค้นหาโค้ดที่แย่และซับซ้อน(Code Smells) ค้นหาช่องโหว่ต่างๆ (Vulnerabilities) และประมวลผลเป็นเปอร์เซ็นต์ให้เห็นในรูปแบบ report นอกจากนี้ยังรองรับการตรวจสอบภาษามากกว่า 25 ภาษาเช่น Java, Kotlin, PHP, C#, Python, Swift, C/C++, Javascript เป็นต้น</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*l81gAeDXxuzaQa8_-YJ-NQ@2x.png" /></figure><h3><strong>ติดตั้ง SonarLint</strong></h3><p>ใน android studio จะมีให้เพิ่ม plugin สำหรับ SonarLint ซึ่งสามารถเพิ่มได้โดยคลิกที่ Android Studio (File &gt; Settings บนวินโดว์)&gt; Preferences &gt; Plugin จากนั้นพิมพ์ในช่องค้นหาว่า sonarlint และทำการ install</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8LEpTiJAPjUmxoMNmW0e8g.png" /></figure><p>มาลองดูผลจากการติดตั้ง SonarLint ซึ่งตัวนี้จะช่วยตรวจสอบว่าโค้ดเราควรเขียนยังไงดังรูป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*brgF2OKq73DW1USXKST5bw.png" /></figure><p>จะเห็นว่าจากเดิมโค้ดเป็น loop for ตัว sonarlint ก็แนะนำให้เราใช้ loop while แทน เป็นต้น จะเห็นว่ามันเจ๋งมาก และฉลาดสุดๆ ซึ่งเราสามารถดูกดการใช้ หรือมาตราฐานของโค้ดที่ควรจะเป็นเพิ่มเติมได้ที่</p><pre><a href="https://rules.sonarsource.com/kotlin">https://rules.sonarsource.com/kotlin</a></pre><h3>ติดตั้ง SonarQube</h3><p>การติดตั้ง SonarQube ที่เป็น open source สามารถตั้งเครื่องให้เป็น server เองโดยไม่เสียเงินทำได้ดังนี้</p><ul><li>ทำการดาวน์โหลด source code server จากเว็ป <a href="https://www.sonarqube.org/downloads/">https://www.sonarqube.org/downloads/</a> . โดยเลือกเป็นแบบ SonarQube Community Edition</li><li>จากนั้นทำการ unzip แล้วทำการ copy ไฟล์ไปวางที่</li></ul><pre>#Mac OS<br>/opt/sonarqube</pre><pre>#Windows OS<br>c:/sonarqube</pre><ul><li>ทำการรัน SonarQube ให้เป็น server ด้วยคำสั่ง</li></ul><pre>#Mac OS<br>$cd Opt/sonarqube/bin/macosx-universal-64/</pre><pre>$./sonar.sh start</pre><pre>#Windows OS<br>$c:/sonarqube/bin/windows-x86–64/StartSonar.bat</pre><ul><li>ไปที่ลิงค์เพื่อเช็คเซอร์เวอร์ <a href="http://localhost:9000/">http://localhost:9000</a></li><li>ทำการ login ด้วย <br>username : admin <br>password : admin</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Nnuv1c1a-DF6otDzvzzh7w.png" /></figure><ul><li>คลิกสร้างโปรเจคที่เราจะทำการ scanner project นั่นๆ</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gSqPS7MSQ6co_JzfdPQk4g.png" /></figure><p>จะเห็นว่าเราตั้งโปรเจคและเซอร์เวอร์เรียบร้อยแล้ว จากนั้นจะต้องติดตั้ง sonar scanner เพื่อทำการสแกนโค้ดแล้วดึกไปประมวลผลที่เว็ปว่าโค้ดเรามีคุณภาพขนาดไหน</p><h3>ติดตั้ง SonarScanner</h3><p>SonarScanner จะทำการสแกนโค้ดของเราทั้งหมด ใน Mac OS จะใช้ brew เป็นตัวช่วยติดตั้งเพื่อให้ง่ายต่อการ set path ทำได้ดังนี้</p><ul><li>ทำการลง brew ก่อนจากนั้นทำการลง sonar scanner ด้วยคำสั่ง</li></ul><pre>$ruby -e “$(curl -fsSL <a href="https://raw.githubusercontent.com/Homebrew/install/master/install">https://raw.githubusercontent.com/Homebrew/install/master/install</a>)&quot; &lt; /dev/null 2&gt; /dev/null</pre><pre>$brew install sonar-scanner</pre><ul><li>ในส่วนของ Windows OS จะทำการ<a href="https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner">ดาวน์โหลด</a> และทำการแตกไฟล์ นำไฟล์ไปว่างไว้ที่ c:/sonar-scanner แล้วทำการ <a href="https://www.addictivetips.com/windows-tips/set-path-environment-variables-in-windows-10/">set path environment</a></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-4b9MSN5bL3S5jMoUCptlQ.jpeg" /><figcaption><a href="https://www.addictivetips.com/windows-tips/set-path-environment-variables-in-windows-10/">https://www.addictivetips.com/windows-tips/set-path-environment-variables-in-windows-10/</a></figcaption></figure><ul><li>ทำการสร้างไฟล์ sonar-project.properties ไว้ที่ข้างนอกสุดของโฟเดอร์โปรเจค</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/1*JvrDUCsz-mntiB0NGF-rUg.png" /></figure><p>จากนั้นทำการเพิ่มโค้ดลงไปในไฟล์ sonar-project.properties โดยใส่คีย์โปรเจคที่เราได้สร้างไว้ และชื่อโปรเจคที่สร้างขึ้น นอกจากนี้ต้องใส่ path ที่เก็บ source ของเราให้ถูกต้องในที่นี้เก็บไว้ที่ app/src/main</p><pre> sonar.projectKey=YOUR_KEY<br> sonar.projectName=YOUR_PROJECT_NAME<br> sonar.projectVersion=1.0<br> sonar.sources=app/src/main<br> sonar.host.url=<a href="http://localhost:9000/">http://localhost:9000</a><br> sonar.sourceEncoding=UTF-8</pre><ul><li>ทำการรันคำสั่ง sonar-scanner ใน terminal ของโปรเจคเรา</li></ul><pre>$sonar-scanner</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q3NsYFYFbdeRIdC49rJAaA.png" /></figure><ul><li>เปิดดูที่ลิงค์ <a href="http://localhost:9000/">http://localhost:9000</a> เพื่อดูรายงานผลจากการสแกนโค้ด</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V1CJLKYXOtPl33eqaUgMwQ.png" /></figure><p>ซึ่งก็จะแสดงผลลัพธิ์จากการสแกน โดยจากรูปจะเห็นว่าโค้ดเขียนมาไม่มีบัคแต่เขียนโค้ดโดยมีกลิ่นตุๆ โค้ดยุ่งเหยิง และเขียนโค้ดซ้ำกันเยอะ ซึ่งจะทำให้เรากลับไปแก้ไขโค้ดให้มีประสิทธิภาพมากยิ่งขึ้น</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V4KE8QH4nhoF1Jgv8Fn-VQ.png" /></figure><h3>ของแถม</h3><p>การลง sonar scanner ลงครั้งเดียวสามารถนำไปใช้ได้กับอีกหลายภาษาได้เลยซึ่งมีประโยชน์มากๆ ผู้เขียนได้นำไปลองกับ react native หรือว่าภาษา javascript ก็สามารถสแกนได้เช่นกัน แล้วพบกันใหม่ในบทความหน้า</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c32f508ab45e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ติดตั้ง React Native บน Mac]]></title>
            <link>https://chalitta-c-k.medium.com/%E0%B8%95%E0%B8%B4%E0%B8%94%E0%B8%95%E0%B8%B1%E0%B9%89%E0%B8%87-react-native-%E0%B8%9A%E0%B8%99-mac-289b698e8d4c?source=rss-c4e27bbfe1f9------2</link>
            <guid isPermaLink="false">https://medium.com/p/289b698e8d4c</guid>
            <category><![CDATA[mac]]></category>
            <category><![CDATA[ติดตั้ง-react-native-mac]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[install-react-native]]></category>
            <category><![CDATA[install-react-native-mac]]></category>
            <dc:creator><![CDATA[iMint]]></dc:creator>
            <pubDate>Tue, 07 May 2019 18:25:30 GMT</pubDate>
            <atom:updated>2019-06-19T08:09:15.446Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*sTGi0uHG97h1nNkbwiqmdg.png" /></figure><p>ในบทความก่อนหน้านี้ได้เขียนการ<a href="https://medium.com/@chalitta.c.k/%E0%B8%95%E0%B8%B4%E0%B8%94%E0%B8%95%E0%B8%B1%E0%B9%89%E0%B8%87-react-native-%E0%B8%9A%E0%B8%99-windows-ac256a3e77ce">ติดตั้ง react native บน windows</a> ไปแล้วนะครับ รอบนี้เราจะมาอธิบายวิธีติดตั้ง react native บน mac กันบ้างนะครับ</p><h3>เครื่องมือในการพัฒนาแอพพลิเคชั่น</h3><p>เครื่องมือที่จำเป็นในการพัฒนา React Native เพื่อให้สามารถแก้ไข้โค้ดและรันโค้ดได้</p><ol><li><a href="https://nodejs.org/en/">Node JS</a> เนื่องจาก React Native ใช้ JavaScript ในการพัฒนา</li><li><a href="https://code.visualstudio.com/">Visual Studio Code</a> เป็น Editer ที่ผู้เขียนเลือกใช้ในการแก้ไขโค้ด ซึ่งผู้อ่านสามารถเลือกใช้ Editer ตัวอื่นได้ตามใจชอบ</li><li><a href="https://developer.apple.com/xcode/">XCode</a> เป็น Editer ที่ใช้ในการรัน emulator ios</li></ol><h3>เริ่มต้นติดตั้ง React Native</h3><p>อันดับแรกทำการติดตั้งโปรแกรมที่กล่าวมาข้างต้น คือ node js ซึ่งเลือกที่เป็น recommended for most users และทำการติดตั้ง ส่วนเครื่องมือที่ใช้ในการพัฒนาสามารถเลือกใช้ได้ตามความถนัด ในที่นี้ผู้เขียนเลือกใช้ visual studio code แต่หากใครใช้ atom ในการพัฒนาก็สามารถใช้ได้เช่นกัน เครืองมือตัวสุดท้ายคือ xcode เป็นเครื่องมือสำคัญในการรันแอพพลิเคชั้นเพราะตัวนี้จะทำการสร้าง iphone ในเราได้ทดลองแอพที่เราพัฒนาขึ้นมา</p><p>จากนั้นเปิดตัว Terminal ขึ้นมา ทำการติดตั้ง Watchman เป็นเครื่องมือที่ใช้สำหรับจับตาดูการเปลี่ยนแปลงไฟล์ต่างๆ</p><pre>brew install node <br>brew install watchman</pre><p>จากนั้นทำการลง react native ด้วยคำสั่งต่อไปนี้</p><pre>npm install -g react-native-cli</pre><p>ทำการเช็คเวอร์ชั้น react native</p><pre>react-native -v</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O9EK3ENw20tgsFCJ6GvwaA.png" /></figure><p>จากนั้นทำการสร้างโปรเจคขึ้นมาด้วยคำสั่ง ในที่นี้ผู้เขียนใช้ชื่อโปรเจคว่า TestReactNative</p><pre>react-native init TestReactNative</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FMdh8rBbVdExv2HKGFXhvw.png" /></figure><p>จากนั้นเข้าไปในโปรเจคที่เราสร้างขึ้น และทำการเช็คเวอร์ชั่น react native อีกครั้ง</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uKsz4WPzUtg1ENi_2qRCsA.png" /></figure><p>แต่ก่อนที่เราจะทำการรันโปรเจคก็ต้องทำการตั้งค่า xcode ก่อน โดยเปิด Xcode จากนั้นเลือก Preferences จากเมนู Xcode ไปที่ Location และไปที่ Command Line Tools โดยเลือกเวอร์ชันล่าสุดในรายการดร็อปดาวน์</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tPZgWHwx8iCz_nRHNJQe8A.png" /></figure><p>จากนั้นทำการรันโปรเจคด้วยคำสั่ง</p><pre>react-native run-ios</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RRd5BL7OpPr6zsgpePKD-g.png" /></figure><p>การแก้ไข้โค้ดก็ทำได้โดยการเปิด โปรแกรม VS Code ทำการเลือก File &gt; Open folder และเลือกไฟล์โปรเจค React Native</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2CKTokJYg_ecXrIX9ob-KQ.png" /></figure><h3>Trick</h3><p>สำหรับใครที่ใช้ต้องการรันบน emulator บนแอนดรอยด์แต่ไม่ต้องการเปิด Android Studio สามารถทำได้ด้วยการสร้าง emulator จากตัว terminal ได้ด้วยคำสั่งต่อไปนี้</p><p>เข้าไปยังโฟลเดอร์ที่ SDK ที่มี emulator อยู่</p><pre>cd ~/Library/Android/sdk/emulator</pre><p>จากนั้นเช็คว่ามี emulator ตัวไหนที่ถูกสร้างไว้หรือไม่</p><pre>./emulator -list-avds</pre><p>จากนั้นเราเลือกตัว emulator ที่เราต้องการรันขึ้นมา</p><pre>./emulator -avd {AVD_NAME}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=289b698e8d4c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[เปลี่ยน Font บน Android ด้วย Font Family]]></title>
            <link>https://chalitta-c-k.medium.com/%E0%B9%80%E0%B8%9B%E0%B8%A5%E0%B8%B5%E0%B9%88%E0%B8%A2%E0%B8%99-font-%E0%B8%9A%E0%B8%99-android-%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2-font-family-86f2e2a94388?source=rss-c4e27bbfe1f9------2</link>
            <guid isPermaLink="false">https://medium.com/p/86f2e2a94388</guid>
            <category><![CDATA[font-family]]></category>
            <category><![CDATA[fonts]]></category>
            <category><![CDATA[android-fonts]]></category>
            <category><![CDATA[change-font-android]]></category>
            <dc:creator><![CDATA[iMint]]></dc:creator>
            <pubDate>Thu, 28 Feb 2019 08:19:32 GMT</pubDate>
            <atom:updated>2021-10-30T11:47:46.748Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u88JzWsVL-7je8lRzkIEMA.png" /></figure><p>Font เป็นตัวดึงดูดให้แอพดูน่าสนใจ และกลมเกลือนกับดีไซต์นั่นๆ จากนักพัฒนาหลายท่านคงเคย Custom font ซึ่งมีหลายขั้นตอนกว่าจะออกมาเป็นฟอร์ตที่ต้องการ ดังนั้นทางผู้เขียนจึงอยากหาวิธีที่ง่ายที่สุดในการเปลี่ยน Font ไปพร้อมกับการวาด UI ในไฟล์ xml และพบว่า Font family คือสิ่งที่ลงตัวที่สุด</p><p>Font ที่ผู้เขียนเลือกใช้มี 3 Font โดยมีลักษณะที่แตกต่างกันไป ผู้อ่านสามารถหาโหลด Font ที่ถูกลิขสิทธิ์ทั้งฟรีและเสียเงินได้ตามเว็ปไซต์ต่างๆเพื่อนำมาใช้ในแอพของเรา</p><h3>เริ่มสร้าง Font Family</h3><p>เมื่อสร้างโปรเจคแอนดรอยด์ขึ้นมาละทำการเปลี่ยนมุมมองไฟล์แอนดรอยด์เป็น project จะแสดงโฟลเดอร์ดังต่อไปนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/654/1*v4MTD7w223z2gZMMxd1EvA.png" /></figure><p>จะสังเกตุว่ามีโพลเดอร์ res ปรากฎอยู่ ทำการคลิกขวาที่โฟลเดอร์แล้วเลือก New &gt; Android Resource Directory</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nFbKQbMI3-If2oUMMtUekg.png" /></figure><p>เมื่อทำการคลิกแล้วจะแสดงหน้าต่าง New Resource Directory ทำการเลือก Resource type เป็น font ดังรูป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1010/1*klAALMjN8giifzSigQjmwg.png" /></figure><p>โฟลเดอร์ฟอร์ตจะถูกสร้างขึ้น ให้ทำการนำฟอร์ตที่ต้องการจะใช้นำมาว่างลงในโฟลเดอร์และทำการสร้าง ไฟล์ font.xml ขึ้นมา ไฟล์ฟอร์ตที่นำมาว่างควรเป็นไฟล์ที่มีนามสกุลเป็น .ttf เพื่อลดปัญหาฟอร์ตไม่ทำงาน หรือใช้งานไม่ได้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/414/1*H5i6w3LDZGLaYLpp_xEMJw.png" /></figure><p>ในไฟล์ font.xml ภายในจะเป็นโค้ดที่ให้สามารถเลือกใช้ฟอร์ตได้ใน android เวอร์ชั่นตำ่ ซึ่งจะมีโค้ดดังต่อไปนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/898/1*bFTuVFcAx9MJOfKNnPeYDA.png" /></figure><p>เลือก font ที่จะใช้เป็นฟอร์ตหลักลงบน fontStyle ที่เป็น normal เท่านี้เราก็มี font family ที่พร้อมใช้งานได้แล้ว</p><h3>เริ่มใช้ Font Family</h3><p>วิธีที่นำ Font Family ไปใช้ก็แสนจะง่ายครับ โดยนำไปใช้กับการวาด UI ได้เลยโดยไม่ต้องโค้ดให้วุ่นวาย เช่น การสร้าง text ตัวหนึ่งโดยให้เปลี่ยนฟอร์ตให้เป็นไปตามฟอร์ตที่เราต้องการ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/740/1*aquVEPB77FWBSu01zl4rKg.png" /></figure><p>คำสั่งที่เพิ่มเข้ามาคือ fontFamily และเลือกใช้ในโฟลเดอร์ font ที่เราสร้างขึ้นมาและเลือกใช้ฟอร์ตที่เราต้องการได้เลย</p><p>นอกจากนี้เราสามารถเลือกใช้คำสั่ง fontFamily โดยเลือกใช้ font จากไฟล์ xml ได้เลยนะครับ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*ZCR15If_Mt8cEJv5zqRSNg.png" /></figure><p>แต่การใส่ font ทุกๆ TextView ก็ทำให้บางครั้งเราอาจจะหลงลืมใส่ไป ดังนั่นจึงมีวิธีที่ง่ายกว่าในการกำหนด font ให้ทั้งแอพเราเลย โดยไม่ต้องไปเพิ่มลงบน TextView หรือ View ที่มีการแสดง Text แต่ละอัน เพียงกำหนดในไฟล์ style.xml หรือ themes.xml ตรง style หลักที่เราใช้ในการ config style ของทั้งแอพ ดังภาพ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/756/1*Fr6PiQR5g2XDgqeGVcBVRA.png" /></figure><p>เพียงเท่านี้เราก็จะได้แอพที่มีฟอร์ตที่น่าดึงดูดผู้ใช้ให้เข้ามาทดลองใช้แอพของเราได้แล้วนะครับ</p><p>ปล. ใครกำลังหา font ฟรีและสวยๆ ก็เข้าไปส่องได้ที่ <a href="https://fonts.google.com/">Google Fonts</a> ได้เลย</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=86f2e2a94388" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Debug React Native ด้วย Chrome]]></title>
            <link>https://chalitta-c-k.medium.com/debug-react-native-%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2-chrome-11eb42d19e9f?source=rss-c4e27bbfe1f9------2</link>
            <guid isPermaLink="false">https://medium.com/p/11eb42d19e9f</guid>
            <category><![CDATA[debug-react-native]]></category>
            <category><![CDATA[debug-chrome]]></category>
            <category><![CDATA[debug]]></category>
            <category><![CDATA[react-native]]></category>
            <dc:creator><![CDATA[iMint]]></dc:creator>
            <pubDate>Sat, 04 Aug 2018 10:21:50 GMT</pubDate>
            <atom:updated>2019-10-09T08:07:13.414Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*ETacl82xtvTeffPsvqXVcQ.jpeg" /></figure><p>การ debug ของ react native มีหลากหลายรูปแบบในบทความของผู้พัฒนาชาวต่างประเทศ ผู้เขียนได้ทดลองใช้มาหลายรูปแบบ แล้วพบว่าวิธีที่ง่ายที่สุดในการ debug code คือการ debug ผ่าน chrome ซึ่งเป็นวิธีพื้นฐานสำหรับการ debug react native</p><h3>Debug ด้วย Emulator</h3><p>สำหรับการ debug code ด้วยการใช้อุปการจำลอง หรือที่เรียกกันว่า emulator จะทำง่ายกว่าการ debug ผ่านเครื่องที่ใช้งานจริง เพราะไม่จำเป็นต้องมาเซ็ตค่าต่างๆให้วุ่นวาย โดยการรันผ่าน emulator ได้เขียนไว้แล้วในบทความก่อนหน้า หากใครยังไม่ได้อ่าน ลองอ่านดูที่ <a href="https://medium.com/@chalitta.c.k/%E0%B8%95%E0%B8%B4%E0%B8%94%E0%B8%95%E0%B8%B1%E0%B9%89%E0%B8%87-react-native-%E0%B8%9A%E0%B8%99-windows-ac256a3e77ce">ติดตั้ง React Native บน Windows</a></p><p>ซึ่งวิธีที่ใช่ในการ debug คือให้ทำการเปิด emulator ขึ้นมาก่อนจากนั้นรัน react native ด้วยคำสั่ง react-native run-android ดังรูป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w4Uz9pUVWNuxsGUQxtSoyQ.png" /></figure><ul><li>npm start ใช้สำหรับรัน node js เพื่อใช้ในการคอมไฟล์โค้ด</li><li>react-native run-android ใช้สำหรับรันโค้ด react native</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/296/1*tx-D_0CvXYHXcQBdXr9YRw.png" /></figure><p>เมื่อรัน emulator เสร็จเรียบร้อยทำการกด ctrl + m จะปรากฎเมนูให้เลือก Debug JS Remotely</p><p>เปิด google chrome เพื่อทำการ debug โดยใส่ url ดังต่อไปนี้</p><pre><a href="http://localhost:8081/debugger-ui/">http://localhost:8081/debugger-ui/</a></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/948/1*UvygAG8S8kSTKWPChMgCyA.png" /></figure><p>จากนั่นทำการเปิด debug โดยเข้าไปที่แทบเมนู เลือก More tools และเลือก developer tools หรือหากต้องการเข้าถึงง่ายๆให้กด Ctrl + Shift + I เมื่อได้จะปรากฎหน้าตาดังรูป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Rs5uvDZLKLGbiRJqbnF4Pw.png" /></figure><p>หากทำการเชื่อมต่อกับ emulator ไม่ได้ให้ทำการ kill process ของแอพพลิเคชั่นก่อน แล้วทำการเข้ามาใหม่อีกครั้ง</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*DEspoqEKskOKhSEXWy4uug.gif" /></figure><h3>Debug ด้วย Device จริง</h3><p>การ debug ด้วยอุปกรณ์ที่ใช้งานจริงจะทำให้เห็นข้อผิดพลาดที่เกิดขึ้นจากการใช้งานจริง และยังสามารถดู UI ที่อยู่บนอุปกรณ์จริงด้วย</p><p>ซึ่งวิธี debug ผ่านอุปกรณ์จริงไม่ได้ต่างจากการ debug ผ่าน emulator มากเท่าไร การรันผ่านอุปกรณ์จริงมีดังนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KkT5hXOg4CCoe0os3G3shQ.png" /></figure><ul><li>npm start ใช้สำหรับรัน node js เพื่อใช้ในการคอมไฟล์โค้ด</li><li>react-native run-android ใช้สำหรับรันโค้ด react native</li><li>เข้าไปที่ path C:\Users\USERNAME\AppData\Local\Android\Sdk\platform-tools เพื่อทำการเปิดพอร์ทการเชื่อมต่อด้วยคำสั่ง</li></ul><pre>adb reverse tcp:8081 tcp:8081</pre><p>เมื่อรัน device เสร็จเรียบร้อยทำการเปิดเมนูด้วยคำสั่ง</p><pre>adb shell input keyevent 82</pre><p>ให้เลือก Debug JS Remotely เพื่อทำการเปิดโหมด debug</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*PpO3FoZkSVW7Jj9sIkHQxg.png" /></figure><p>เปิด google chrome เพื่อทำการ debug โดยใส่ url ดังต่อไปนี้</p><pre><a href="http://localhost:8081/debugger-ui/">http://localhost:8081/debugger-ui/</a></pre><p>หากทำการเชื่อมต่อกับ device ไม่ได้ให้ทำการ kill process ของแอพพลิเคชั่นก่อน แล้วทำการเข้ามาใหม่อีกครั้ง จากนั้นทำการเปิดพอร์ทใหม่อีกครั้งด้วยคำสั่ง adb reverse tcp:8081 tcp:8081</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*Yf3xbxKVFMXaylWlbXi2VQ.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=11eb42d19e9f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[การปัดข้าง (Swipe) ListView Android]]></title>
            <link>https://chalitta-c-k.medium.com/%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B8%9B%E0%B8%B1%E0%B8%94%E0%B8%82%E0%B9%89%E0%B8%B2%E0%B8%87-swipe-listview-android-d9e25a6e14?source=rss-c4e27bbfe1f9------2</link>
            <guid isPermaLink="false">https://medium.com/p/d9e25a6e14</guid>
            <category><![CDATA[สไลด์listview]]></category>
            <category><![CDATA[การปัดข้าง]]></category>
            <category><![CDATA[listview]]></category>
            <category><![CDATA[swipelistview]]></category>
            <category><![CDATA[สไลด์ลิสวิว]]></category>
            <dc:creator><![CDATA[iMint]]></dc:creator>
            <pubDate>Wed, 11 Apr 2018 04:40:48 GMT</pubDate>
            <atom:updated>2018-04-11T04:40:48.097Z</atom:updated>
            <content:encoded><![CDATA[<p>จากการที่ทำ ListView และอยากให้ ListView ปัดไปด้านข้างได้ในแต่ละ List จึงไปหา Library มาลองใช้หลากหลายตัว และพบว่ามี Library ตัวหนึ่งที่ใช้งานง่าย และเข้าใจง่าย จึงนำมาเขียนเป็นความรู้ให้แก่ผู้ที่หลงเข้า</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*K3iX2yT9fQzC603gHMVkWg.png" /></figure><h3>เริ่มต้นการสร้างโปรเจค</h3><p>โปรเจคนี้เป็นการเขียนด้วย ListView ที่มีการ Custom ListView เข้ามาด้วยเพื่อให้ง่ายต่อผู้ที่จะนำโค้ดไปใช้ศึกษาต่อ เริ่มต้นโดยการสร้างโปรเจค ใส่ชื่อ ใส่ชื่อแพคเกจ ให้เรียบร้อยจากนั้นไปที่ไฟล์ build.gradle ที่อยู่ในไฟลเดอร์ app</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/314/1*v-LQ05Vf1T34XR1f3DGZzQ.png" /></figure><p>เมื่อเข้าไปจะทำการเพิ่ม Library ที่ใช้ในการคอมไฟล์ตัว swipe listview นั่นก็คือ</p><pre>implementation &#39;com.baoyz.swipemenulistview:library:1.3.0&#39;</pre><p>เนื่องจากตั้งแต่ปี 2018 ทาง android studio ได้เปลี่ยนคำว่า compile ไปเป็น implementation ฉะนั้นจึงไม่ต้องแปลกใจว่าทำไมไม่ใช้ compile จากนั้นเพิ่มลงไปใน dependencies ดังนี้</p><pre>dependencies {<br>    implementation &#39;com.baoyz.swipemenulistview:library:1.3.0&#39;<br>}</pre><p>แต่เนื่องด้วย library ตัวนี้ถูกพัฒนาเมื่อ 2 ปีที่แล้วเป็นระยะเวลานานพอสมควร ซึ่งทำให้เราต้องเพิ่ม library ที่สนับสนุนการใช้งาน support-v4 ลงไป</p><pre>dependencies {<br>    implementation &#39;com.android.support:support-v4:27.1.0&#39;<br>    implementation &#39;com.baoyz.swipemenulistview:library:1.3.0&#39;<br>}</pre><p>จากนั้นทำการ Sync Now เพื่อเพิ่ม library ตัวนี้ลงไปในโปรเจค</p><h3>สร้าง ListView ด้วย Library</h3><p>เริ่มด้วยการวาด Layout ที่ไฟล์ activity_main.xml โดยจะใช้ library เป็นตัวเปิดและตัวปิด ซึ่งคำสั่งด้านในจะเหมือนกับ ListView ปกติ</p><pre>&lt;com.baoyz.swipemenulistview.SwipeMenuListView<br>    android:id=&quot;@+id/swipeListView&quot;<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;wrap_content&quot;<br>    android:divider=&quot;@color/colorAccent&quot;<br>    android:dividerHeight=&quot;0.5dp&quot;&gt;<br>&lt;/com.baoyz.swipemenulistview.SwipeMenuListView&gt;</pre><h3>วาด Layout Custom ListView</h3><p>สร้าง Layout ที่ต้องการจะ Custom ให้กับ ListView ซึ่งจะอยู่ที่ไฟล์ custom_listview.xml โดยแต่ละ List ของจะออกมาหน้าตาเหมือนดังในรูป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/245/1*ndN6wMu5TNazKl_axlLRBg.png" /></figure><p>ประกอบไปด้วยรูปทางซ้ายมือสุด (ImageView) และมี Name และ Description (TextView) มีโค้ดดังต่อไปนี้</p><pre>&lt;LinearLayout<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;wrap_content&quot;<br>    android:background=&quot;@color/gray&quot;<br>    android:orientation=&quot;horizontal&quot;&gt;<br><br>    &lt;ImageView<br>        android:id=&quot;@+id/imageView&quot;<br>        android:layout_width=&quot;100dp&quot;<br>        android:layout_height=&quot;100dp&quot;<br>        android:padding=&quot;2dp&quot;<br>        android:scaleType=&quot;fitXY&quot;<br>        android:layout_marginTop=&quot;10dp&quot;<br>        android:layout_marginBottom=&quot;10dp&quot;<br>        android:layout_marginLeft=&quot;10dp&quot;<br>        android:background=&quot;@color/white&quot;/&gt;<br><br>    &lt;RelativeLayout<br>        android:id=&quot;@+id/spaceText&quot;<br>        android:layout_width=&quot;0dp&quot;<br>        android:layout_height=&quot;match_parent&quot;<br>        android:layout_weight=&quot;0.8&quot;<br>        android:layout_marginLeft=&quot;5dp&quot;<br>        android:layout_marginTop=&quot;10dp&quot;<br>        android:layout_marginBottom=&quot;10dp&quot;&gt;<br><br>        &lt;TextView<br>            android:id=&quot;@+id/text_header&quot;<br>            android:layout_width=&quot;wrap_content&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            android:text=&quot;Name&quot;<br>            android:typeface=&quot;monospace&quot;<br>            android:textStyle=&quot;bold&quot;<br>            android:textColor=&quot;#000000&quot;<br>            android:layout_marginTop=&quot;5dp&quot;<br>            android:textSize=&quot;16sp&quot;<br>            android:layout_marginLeft=&quot;5dp&quot;<br>            android:ellipsize=&quot;end&quot;<br>            android:maxLines=&quot;1&quot;/&gt;<br><br>        &lt;TextView<br>            android:id=&quot;@+id/text_description&quot;<br>            android:layout_width=&quot;wrap_content&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            android:layout_below=&quot;@+id/text_header&quot;<br>            android:text=&quot;Description&quot;<br>            android:textSize=&quot;12sp&quot;<br>            android:typeface=&quot;monospace&quot;<br>            android:ellipsize=&quot;end&quot;<br>            android:maxLines=&quot;2&quot;<br>            android:layout_marginLeft=&quot;5dp&quot;<br>            android:layout_marginTop=&quot;5dp&quot;/&gt;<br><br>    &lt;/RelativeLayout&gt;<br><br>&lt;/LinearLayout&gt;</pre><h3>เขียนโค้ดการใช้งาน Swipe ListView</h3><p>ในการเขียนโค้ดที่ไฟล์ MainActivity.java โดยในโค้ดประกอบด้วย 3 ส่วนหลักด้วยกันคือ การสร้างข้อมูลให้กับ ListView , การสร้าง Swipe และ การ Custom ให้กับ ListView</p><p>อันดับแรกจะต้องประกาศการใช้งาน ListView ไว้ที่ onCreate</p><pre>@Override<br>protected void onCreate(Bundle savedInstanceState) {<br>    super.onCreate(savedInstanceState);<br>    setContentView(R.layout.<em>activity_main</em>);<br><br>    swipeListView = findViewById(R.id.<em>swipeListView</em>);<br><br>}</pre><h4>การสร้างข้อมูลให้กับ ListView</h4><p>การสร้างข้อมูลให้กับ ListView โดยข้อมูลจะประกอบไปด้วย name และ photo โดยใช้ ArrayList ในการเก็บข้อมูล ส่วนในข้อมูล photo ในโปรเจคนี้จะทำการนำรูปมาจากโฟเดอร์ Drawable ซึ่งหากต้องการเปลี่ยนรูปก็สามารถนำรูปที่ต้องการมาว่างที่โฟเดอร์ Drawable ได้เลยหรือหากว่าใครมี Url ของรูปก็สามารถนำข้อมูลมาดัดแปลงที่ ArrayList photo ได้เลย</p><pre>private ArrayList&lt;String&gt; name = new ArrayList&lt;String&gt;();<br>private ArrayList&lt;Integer&gt; photo = new ArrayList&lt;Integer&gt;();</pre><pre>....</pre><pre>private void setDataListView() {<br>    name.add(&quot;android studio&quot;);<br>    name.add(&quot;xcode&quot;);<br>    name.add(&quot;xamarin&quot;);<br>    name.add(&quot;react native&quot;);<br>    name.add(&quot;ionic&quot;);<br>    name.add(&quot;phonegap&quot;);<br>    name.add(&quot;node js&quot;);<br>    name.add(&quot;visual studio code&quot;);<br>    name.add(&quot;cordova&quot;);<br>    name.add(&quot;flutter&quot;);<br><br>    photo.add(R.drawable.<em>android</em>);<br>    photo.add(R.drawable.<em>xcode</em>);<br>    photo.add(R.drawable.<em>xamarin</em>);<br>    photo.add(R.drawable.<em>react</em>);<br>    photo.add(R.drawable.<em>ionic</em>);<br>    photo.add(R.drawable.<em>phonegap</em>);<br>    photo.add(R.drawable.<em>nodejs</em>);<br>    photo.add(R.drawable.<em>vscode</em>);<br>    photo.add(R.drawable.<em>cordova</em>);<br>    photo.add(R.drawable.<em>flutter</em>);<br><br>}</pre><h4>การสร้าง Swipe</h4><p>การสร้าง Swipe ListView คือจะทำการสร้างโดยใช้งาน Library ที่ลงไว้แล้วข้างต้นซึ่งสามารถปรับแต่งขนาด รูป และข้อมูลได้ตามต้องการ</p><pre>private void setSwipeListView() {<br>    SwipeMenuCreator creator = new SwipeMenuCreator() {<br><br>    @Override<br>    public void create(SwipeMenu menu) {<br>    // create &quot;delete&quot; item<br>    SwipeMenuItem deleteItem = new SwipeMenuItem(MainActivity.this);</pre><pre>    // set item background<br>    deleteItem.setBackground(<br>           new ColorDrawable(getResources().getColor(R.color.<em>red</em>)));</pre><pre>    // set item width<br>    deleteItem.setWidth(170);</pre><pre>    // set a icon<br>    deleteItem.setIcon(R.drawable.<em>ic_bin</em>);</pre><pre>    // set item title<br>    deleteItem.setTitle(&quot;Delete&quot;);</pre><pre>    // set item title fontsize<br>    deleteItem.setTitleSize(14);</pre><pre>    // set item title font color<br>    deleteItem.setTitleColor(getResources().getColor(R.color.<em>gray</em>));</pre><pre>    // add to menu<br>    menu.addMenuItem(deleteItem);<br>    }</pre><pre>  };<br><br>   // set creator<br>   swipeListView.setMenuCreator(creator);<br>}</pre><h4>การ Custom ให้กับ ListView</h4><p>การ Custom ListView คือเราจะนำ Layout ที่วาดไว้ด้านบนมาใส่ใน ListView ซึ่งสร้างด้วย class ชื่อ CustomListView และประกาศเครื่องมือที่ใช้ไว้ที่ class ชื่อ ViewHolder ส่วนการแสดงผลจะให้ holder.image แสดงรูปจาก ArrayList ข้อมูล photo และให้ holder.header แสดงผลข้อมูลจาก ArrayList ข้อมูล Name และให้ holder.description แสดงข้อมูลลำดับที่ของ List นั่นๆ</p><pre>private class CustomListView extends BaseAdapter {<br><br>    private Context mContext;<br>    private LayoutInflater mInflater;<br>    private CustomListView(Context context) {<br>        mContext = context;<br>        mInflater = LayoutInflater.<em>from</em>(mContext);<br>    }<br><br>    @Override<br>    public int getCount() {<br>        return name.size();<br>    }<br><br>    @Override<br>    public Object getItem(int position) {<br>        return null;<br>    }<br><br>    @Override<br>    public long getItemId(int position) {<br>        return position;<br>    }<br><br>    @Override<br>    public View getView(final int position, View convertView, ViewGroup parent) {<br><br>        ViewHolder holder = null;<br>        if(convertView == null) {<br>            convertView = mInflater.inflate(R.layout.<em>custom_listview</em>, null);<br>            holder = new ViewHolder();<br>            holder.image = (ImageView) convertView.findViewById(R.id.<em>imageView</em>);<br>            holder.header = (TextView) convertView.findViewById(R.id.<em>text_header</em>);<br>            holder.description = (TextView) convertView.findViewById(R.id.<em>text_description</em>);<br><br>            convertView.setTag(holder);<br>        }<br>        else{<br>            holder = (ViewHolder) convertView.getTag();<br>        }<br><br>        holder.header.setText(name.get(position));<br>        holder.description.setText(String.<em>valueOf</em>(&quot;No. &quot; + position));<br>        holder.image.setImageResource(photo.get(position));<br><br>        return convertView;<br>    }<br>}<br>private class ViewHolder {<br>    ImageView image;<br>    TextView header;<br>    TextView description;<br>}</pre><p>สุดท้ายเมื่อทำการคลิก Delete ให้โชว์ป๊อปอัพว่าจะลบหรือไม่ หากคลิกตกลงก็จะทำการลบ List นั่นไป</p><pre>swipeListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {<br>    @Override<br>    public boolean onMenuItemClick(<br>          final int position, <br>          SwipeMenu menu, <br>          int index) {<br>        final Dialog dialog = new Dialog(MainActivity.this);<br>        dialog.getWindow();<br>        dialog.requestWindowFeature(Window.<em>FEATURE_NO_TITLE</em>);<br>        dialog.getWindow().setBackgroundDrawable(<br>             new ColorDrawable(android.graphics.Color.<em>TRANSPARENT</em>));<br>        dialog.setContentView(R.layout.<em>dialog_data</em>);</pre><pre>        TextView dialog_title = (TextView)dialog.findViewById(R.id.<em>dialog_title</em>);<br>        dialog_title.setText(String.<em>valueOf</em>(&quot;Delete List&quot;));<br><br>        TextView dialog_description = (TextView)dialog.findViewById(R.id.<em>dialog_description</em>);<br>        dialog_description.setText(String.<em>valueOf</em>(&quot;You want delete this&quot;+name.get(position)+&quot;?&quot;));<br><br>        Button buttonCancel = (Button)dialog.findViewById(R.id.<em>buttonCancel</em>);<br>        buttonCancel.setOnClickListener(new View.OnClickListener() {<br>            public void onClick(View v) {<br>                dialog.cancel();<br>            }<br>        });<br><br>        Button buttonOK = (Button)dialog.findViewById(R.id.<em>buttonOK</em>);<br>        buttonOK.setOnClickListener(new View.OnClickListener() {<br>            public void onClick(View v) {<br>                //remove data in list<br>                name.remove(position);<br>                photo.remove(position);<br>                adapter.notifyDataSetChanged();  //updata listview<br><br>                dialog.cancel();<br>            }<br>        });<br><br>        dialog.show();<br><br>        return false;<br>    }<br>});</pre><p>สุดท้ายและท้ายสุด อย่าลืมที่จะเรียกการใช้งานจาก custom listview ด้วย</p><pre>@Override<br>protected void onCreate(Bundle savedInstanceState) {<br>    super.onCreate(savedInstanceState);<br>    setContentView(R.layout.<em>activity_main</em>);<br><br>    swipeListView = findViewById(R.id.<em>swipeListView</em>);<br><br>    //create data to listview<br>    setDataListView();<br><br>    //create swipe menu listview<br>    setSwipeListView();<br><br>    adapter = new CustomListView(this);<br>    swipeListView.setAdapter(adapter);<br><br>}</pre><h4>Output :</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*7md53KUMbwaWrCnEvFKDkQ.gif" /></figure><h4>Source code :</h4><p>🚩 <a href="https://github.com/ChalittaKhampachua/SwipeListView.git">https://github.com/ChalittaKhampachua/SwipeListView.git</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d9e25a6e14" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ติดตั้ง React Native บน Windows]]></title>
            <link>https://chalitta-c-k.medium.com/%E0%B8%95%E0%B8%B4%E0%B8%94%E0%B8%95%E0%B8%B1%E0%B9%89%E0%B8%87-react-native-%E0%B8%9A%E0%B8%99-windows-ac256a3e77ce?source=rss-c4e27bbfe1f9------2</link>
            <guid isPermaLink="false">https://medium.com/p/ac256a3e77ce</guid>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[react-native-windows]]></category>
            <category><![CDATA[react-native-thai]]></category>
            <category><![CDATA[install-react-native]]></category>
            <category><![CDATA[ติดตั้ง-react-native]]></category>
            <dc:creator><![CDATA[iMint]]></dc:creator>
            <pubDate>Thu, 16 Nov 2017 10:03:23 GMT</pubDate>
            <atom:updated>2019-05-07T17:28:04.133Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*n3jAd7UYfAH8GSEo0vGPtQ.jpeg" /></figure><p>React Native ถูกใช้ในการพัฒนา cross-platform mobile apps มีวัตถุประสงค์เพื่อต้องการให้แอพพลิเคชั่นที่ถูกพัฒนาสามารถใช้ได้ทั้งในระบบ android และ ios หรือเรียกง่ายๆว่า “ยิงปืนนัดเดียวได้นกสองตัว” นั้นเอง เป็นแนวคิดที่ผู้ใช้งานระบบปฎิบัติการ windows สามารถพัฒนาแอพพลิเคชั่นบน ios ได้ ซึ่งทำลายกำแพงข้อจำกัดของนักพัฒนาไปได้เยอะ</p><p>React Native เป็น Framwork ที่ถูกพัฒนาโดย Facebook จะทำงานด้วยชุดคำสั่ง JavaScript แต่ทั้งนี้การพัฒนาแอพพลิเคชั้นแบบ cross-platform ยังเจอปัญหาหลักๆนั่นคือ library บ้างตัวไม่สนับสนุนอีกด้วย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*AZfK4q5o0F2frfOl3ESqRg.jpeg" /></figure><h3>เครื่องมือในการพัฒนาแอพพลิเคชั่น</h3><p>เครื่องมือที่จำเป็นในการพัฒนา React Native เพื่อให้สามารถแก้ไข้โค้ดและรันโค้ดได้ ซึ่งในที่นี้ผู้เขียนจะเน้นเฉพาะเจาะจงสำหรับผู้ใช้งาน windows เท่านั้น โดยจะต้องติดตั้งเครื่องมือดังจะกล่าวต่อไปนี้</p><ol><li><a href="https://nodejs.org/en/">Node JS</a> เนื่องจาก React Native ใช้ JavaScript ในการพัฒนา</li><li><a href="https://code.visualstudio.com/">Visual Studio Code</a> เป็น Editer ที่ผู้เขียนเลือกใช้ในการแก้ไขโค้ด ซึ่งผู้อ่านสามารถเลือกใช้ Editer ตัวอื่นได้ตามใจชอบ</li></ol><h3>เริ่มต้นติดตั้ง React Native</h3><p>จากที่กล่าวไว้ข้างต้น จะเห็นว่าจะต้องดาวน์โหลด Node JS เป็นอันดับแรกมีขั้นตอนดังนี้</p><ol><li>ดาวน์โหลด Node JS จากเว็ปไซต์ <a href="https://nodejs.org/en/">คลิกที่</a>นี้ และทำการติดตั้ง เลือกติดตั้งเวอร์ชั้นที่มีคนใช้เยอะที่สุด (Recommended For Most Users) เนื่องจากเวอร์ชั้นใหม่อาจจะไม่ support กับ npm</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*91zPcXp-n9yROraQHExRxw.jpeg" /></figure><p>2. ดาวน์โหลด Visual Studio Code จากเว็ปไซต์ <a href="https://code.visualstudio.com/">คลิกที่นี้</a> และทำการติดตั้ง</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3rEHkJ1RUsOA56dDm44faw.png" /></figure><p>3. ดาวน์โหลด JDK ของ java ในเว็ปไซต์ <a href="http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html">คลิกที่นี้</a> ตอนนี้ JDK ของผู้เขียนใช้ 1.8.0_151 และเลือกระบบปฎิบัติการ Windows x64 หรือ Windows x86 ตาม base ของแต่ละเครื่อง</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/697/1*u7mJGa8yW4sYbkYyedyUdA.png" /></figure><p>4. ทำการติดตั้ง JDK ให้เรียบร้อย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/1*oiRsSCdQZLT5u2GJJ6GiFg.png" /></figure><p>5. จากนั้นทำการตั้งค่า Path ที่อยู่ของ JDK โดยคลิกขวาที่ MyComputer เลือก Properties</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/465/1*JxP6KZhE7ybDiSasFXwOMQ.png" /></figure><p>6. เลือก Advanced system settings จากนั้นเลือก Environment Variables</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/548/1*HKOonEtIw4Tx9wc3pm74Gg.png" /></figure><p>7. คลิก New เพื่อจะใส่ Path JDK</p><p>8. สร้าง Path JAVA_HOME ด้วย Path ดังรูป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/755/1*OkW-Gj7flA1j3I71JSLw8w.png" /></figure><p>9. สร้างโฟลเดอร์ เพื่อใช้ในการเก็บโปรเจค ในที่นี้ผู้เขียนใช้ชื่อว่า “React Native” ซึ่งอยู่ใน D:\Work_Project\</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3RU3trm1ZxCgmuSfXfgvfA.png" /></figure><p>10. ทำการเปิด command Prompt หรือ cmd นั่นคือทำการ click search แล้วพิมพ์คำว่า “cmd”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/528/1*slUqS5EzxHeTwJR52sYE8w.png" /></figure><p>11. จากนั้นทำการเข้าไปที่โฟลเดอร์ที่เราได้สร้างไว้ด้วยคำสั่ง pushd D:\Work_Project\ReactNative</p><p>12. ทำการตรวจสอบเวอร์ชั้น Node JS ที่ได้ลงไปแล้วข้างต้น ด้วยคำสั่ง node -v หากเป็นเวอร์ชั้น V.9.2.0 จะไม่ support npm ให้เปลี่ยนไปลง <a href="https://nodejs.org/dist/v8.9.1/node-v8.9.1-x64.msi">Node JS V.8.9.1</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dIKqVxBpy9ZfuBID6037aw.png" /></figure><p>13. ทำการตรวจสอบเวอร์ชั้น npm ด้วยคำสั่ง npm -v</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vIsfiwzQhIx5g0UDtxAs1g.png" /></figure><p>14. จากนั้นทำการลง npm เวอร์ชั้นล่าสุดด้วยคำสั่ง npm install -g npm และทำการลง React Native ด้วยคำสั่ง npm install -g react-native-cli</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*24t-O1N342hcw2udY5l1cQ.png" /></figure><p>15. เมื่อลงสำเร็จทำการตรวจสอบเวอร์ชั้น React Native ด้วยคำสั่ง react-native -v</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zrt0JrSa0RgwRpF1hiNjSA.png" /></figure><p>16. สร้างโปรเจค React Native ในที่นี้ผู้เขียนจะให้ชื่อโปรเจคว่า ReactNativeProject ด้วยคำสั่ง react-native init ReactNativeProject</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D8QS_8OHBpHBULMskqjhrw.png" /></figure><p>17. จากนั้นทำการเปิด Emulator หากใครที่มี android sudio ก็สามารถใช้ตัว Emulator ของ android sudio ได้ แต่จะต้องทำการลง SDK โดยทางเว็ป React Native ได้ระบุไว้ <a href="https://facebook.github.io/react-native/docs/getting-started.html">คลิกที่นี้</a> โดยจะใช้ Android 6.0 (Marshmallow) ในการรันโปรเจคดังรูป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/31/1*iniKDJuR9LnwHjdvuF2xDQ.png" /></figure><p>คลิก icon SDK Manager</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xj1Ynt3kD2jBXUYR_ubmiQ.png" /></figure><p>18. จากนั้นทำการลง SDK Tools ที่ 23.0.1 หากใครมองไม่เห็น Packege Details ให้คลิก Show Packege Details ที่ล่างขวาสุด</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oUroF0yVKwP34pfMh8k2VQ.png" /></figure><p>19. จากนั้นทำการสร้าง Emulator ด้วยการคลิด AVD Manager</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/32/1*vKX2CHtq3YvIvA8kcJT4KA.png" /></figure><p>20. เลือก API 23 และ เลือก Image x86_64 ดังรูป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*loRoT86Y7wIv5MiABXHJPA.png" /></figure><p>21. คลิก Start Device</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jEQ7RL8lv3FtDy-V7JqTXA.png" /></figure><p>22. จะได้ Device Android ดังรูป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/558/1*F_I8VQMPHr5eX0HEU56mSQ.png" /></figure><p>23. จากนั้นทำการรันโปรเจค React Native ด้วยคำสั่ง react-native run-android</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*unIo3FsSj7ACDidUuul4JA.png" /></figure><p>24. เมื่อรันสำเร็จจะปรากฎดังรูป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7pJTt16AYp6YfmpVD4YgRw.png" /></figure><p>25. ในส่วนของ Device จะเห็นว่าถ้ารันสำเร็จจะปรากฎดังรูป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/561/1*BrCK7upsJcPQrKjXCq3PAA.png" /></figure><p>26. หากต้องการแก้โค้ดให้เปิดโปรแกรม VS Code เพื่อทำการแก้ไข้โค้ด</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yLcRLRYKF9DvswrCVoJ-Mw.png" /></figure><p>27. ทำการเลือก File &gt; Open folder และเลือกไฟล์โปรเจค React Native</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g3R8vuewbo6nLvOm85VSqw.png" /></figure><p>28. จากนั้นเลือก App.js เพื่อทำการแก้ไข้โค้ด ดังตัวอย่างผู้เขียนแก้ไข้คำเป็นคำว่า Welcome to React Native By USERNAME แล้วกด Save</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T0Cn2IpIwNsNfVz2-rgF2g.png" /></figure><p>29. กด double คลิก R เพื่อทำการโหลดอีกครั้ง</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/465/1*JbpR8iou45crn6QIchFo5A.png" /></figure><p>30. หากต้องการให้ทำการ Reload Auto ให้คลิก Ctrl+M และทำการเลือก Enable Live Reload เมื่อแก้โค้ดก็จะเห็นผลลัพธ์ทันที</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/464/1*Er2U-GfsjRfp0m-BRqdURQ.png" /></figure><h3>สรุป</h3><p>จากการที่ผู้เขียน เขียน Android Native มาก่อน จะเห็นได้ชัดเลยว่าการรัน React Native จะไวและเห็นผลลัพธ์ได้ทันที ในขณะที่ Android Native รอนานเป็นนาที แต่การเขียน Android Native ก็ยังเป็นการพัฒนาแอพพลิเคชั้นที่มีประสิทธิภาพสูงกว่า React Native อยู่ดี แต่ทั้งนี้ทั้งนั้นก็ขึ้นอยู่กับว่าเป็นโปรเจคที่มีความซับซ้อนมากแค่ไหน ควรเลือกใช้ให้เหมาะสม แล้วเจอกันใหม่ในบทความ Coding React Native นะจ๊ะ 😊</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ac256a3e77ce" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>