본문 바로가기

Android Studio

[Android Studio] 코틀린으로 뉴스 리더 어플 만들기2

오프라인 상태의 앱을 이용할 때 캐쉬를 관리하기 위해 paper DB 사용

and add swipe to refresh to list of news source & fetch all news source data

Reload website function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
private fun loadWebSiteSource(isRefresh:Boolean){
 
        if(!isRefresh)
        {
            val cache = Paper.book().read<String>("cache")
            if(cache != null && !cache.isBlank() && cache != "null")
            {
                val webSite = Gson().fromJson<WebSite>(cache, WebSite::class.java)
                adapter = ListSourceAdapter(baseContext, webSite)
                adapter.notifyDataSetChanged()
                recycler_view_source_news.adapter = adapter
            }
            else
            {
                //Load website and write cache
                dialog.show()
                //Fetch new data
                mService.sources.enqueue(object:retrofit2.Callback<WebSite>{
                    override fun onFailure(call: Call<WebSite>, t: Throwable) {
                        Toast.makeText(baseContext,"Failed", Toast.LENGTH_SHORT).show()
                    }
 
                    override fun onResponse(call: Call<WebSite>, response: Response<WebSite>) {
                        adapter = ListSourceAdapter(baseContext,response!!.body()!!)
                        adapter.notifyDataSetChanged()
                        recycler_view_source_news.adapter = adapter
 
                        //Save to cache
                        Paper.book().write("cache", Gson().toJson(response!!.body()!!))
 
                        dialog.dismiss()
                    }
                })
 
            }
        }
        else
        {
            swipe_to_refresh.isRefreshing=true
            //Fetch new data
            mService.sources.enqueue(object:retrofit2.Callback<WebSite>{
                override fun onFailure(call: Call<WebSite>, t: Throwable) {
                    Toast.makeText(baseContext,"Failed", Toast.LENGTH_SHORT).show()
                }
 
                override fun onResponse(call: Call<WebSite>, response: Response<WebSite>) {
                    adapter = ListSourceAdapter(baseContext,response!!.body()!!)
                    adapter.notifyDataSetChanged()
                    recycler_view_source_news.adapter = adapter
 
                    //Save to cache
                    Paper.book().write("cache", Gson().toJson(response!!.body()!!))
 
                    swipe_to_refresh.isRefreshing=false
                }
            })
        }
    }
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

 

 

menifest에서 Internet permission 추가

1
<uses-permission android:name="android.permission.INTERNET"/>
 

 

 

picasso를 이용해 이미지 로딩하기

 

kenburn 효과 주기

 

 

 

 

어플리케이션 구현

 

 

 

 

참고 영상

https://www.youtube.com/watch?v=tDPuUzl6fQk&list=PLaoF-xhnnrRXZ95JGpey0BLF3kIX72wuP&index=5