@@ -35,47 +35,49 @@ class ImagesFragment : Fragment() {
3535
3636 private var textView: TextView ? = null
3737
38+ private val TAG = ImagesFragment ::class .java.name
39+
3840 override fun onCreateView (
3941 inflater : LayoutInflater ,
4042 container : ViewGroup ? ,
4143 savedInstanceState : Bundle ?
4244 ): View ? {
43- Log .i(javaClass.name , " onCreateView" )
45+ Log .i(TAG , " onCreateView" )
4446
4547 imagesViewModel = ViewModelProvider (this ).get(ImagesViewModel ::class .java)
4648 val root = inflater.inflate(R .layout.fragment_images, container, false )
4749 progressBar = root.findViewById(R .id.progress_bar_images)
4850 textView = root.findViewById(R .id.text_images) as ? TextView
4951 imagesViewModel!! .text.observe(viewLifecycleOwner, object : Observer <String ?> {
5052 override fun onChanged (s : String? ) {
51- Log .i(javaClass.name , " onChanged" )
53+ Log .i(TAG , " onChanged" )
5254 textView?.text = s
5355 }
5456 })
5557 return root
5658 }
5759
5860 override fun onStart () {
59- Log .i(javaClass.name , " onStart" )
61+ Log .i(TAG , " onStart" )
6062 super .onStart()
6163
6264 // Download Images from REST API, and store them in the database
6365 val baseApplication = activity!! .application as BaseApplication
6466 val retrofit = baseApplication.retrofit
6567 val imagesService = retrofit.create(ImagesService ::class .java)
6668 val imageGsonsCall = imagesService.listImages()
67- Log .i(javaClass.name , " imageGsonsCall.request(): " + imageGsonsCall.request())
69+ Log .i(TAG , " imageGsonsCall.request(): " + imageGsonsCall.request())
6870 imageGsonsCall.enqueue(object : Callback <List <ImageGson >> {
6971 override fun onResponse (
7072 call : Call <List <ImageGson >>,
7173 response : Response <List <ImageGson >>
7274 ) {
73- Log .i(javaClass.name , " onResponse" )
75+ Log .i(TAG , " onResponse" )
7476
75- Log .i(javaClass.name , " response: $response " )
77+ Log .i(TAG , " response: $response " )
7678 if (response.isSuccessful) {
7779 val imageGsons = response.body()!!
78- Log .i(javaClass.name , " imageGsons.size(): " + imageGsons.size)
80+ Log .i(TAG , " imageGsons.size(): " + imageGsons.size)
7981
8082 if (imageGsons.size > 0 ) {
8183 processResponseBody(imageGsons)
@@ -90,9 +92,9 @@ class ImagesFragment : Fragment() {
9092 }
9193
9294 override fun onFailure (call : Call <List <ImageGson >>, t : Throwable ) {
93- Log .e(javaClass.name , " onFailure" , t)
95+ Log .e(TAG , " onFailure" , t)
9496
95- Log .e(javaClass.name , " t.getCause():" , t.cause)
97+ Log .e(TAG , " t.getCause():" , t.cause)
9698
9799 // Handle error
98100 Snackbar .make(textView!! , t.cause.toString(), Snackbar .LENGTH_LONG )
@@ -104,12 +106,12 @@ class ImagesFragment : Fragment() {
104106 }
105107
106108 private fun processResponseBody (imageGsons : List <ImageGson >) {
107- Log .i(javaClass.name , " processResponseBody" )
109+ Log .i(TAG , " processResponseBody" )
108110
109111 val executorService = Executors .newSingleThreadExecutor()
110112 executorService.execute(object : Runnable {
111113 override fun run () {
112- Log .i(javaClass.name , " run" )
114+ Log .i(TAG , " run" )
113115
114116 val roomDb = RoomDb .getDatabase(context)
115117 val imageDao = roomDb.imageDao()
@@ -121,60 +123,60 @@ class ImagesFragment : Fragment() {
121123
122124 // TODO: also delete corresponding image files (only those that are no longer used)
123125 for (imageGson in imageGsons) {
124- Log .i(javaClass.name , " imageGson.getId(): " + imageGson.id)
126+ Log .i(TAG , " imageGson.getId(): " + imageGson.id)
125127
126128 val image = GsonToRoomConverter .getImage(imageGson)
127129
128130 // Check if the corresponding image file has already been downloaded
129131 val imageFile = FileHelper .getImageFile(imageGson, context)
130- Log .i(javaClass.name , " imageFile: $imageFile " )
131- Log .i(javaClass.name , " imageFile.exists(): " + imageFile.exists())
132+ Log .i(TAG , " imageFile: $imageFile " )
133+ Log .i(TAG , " imageFile.exists(): " + imageFile.exists())
132134 if (! imageFile.exists()) {
133135 // Download file bytes
134136 val baseApplication = activity!! .application as BaseApplication
135137 val downloadUrl = if (imageGson.bytesUrl.startsWith(" http" ))
136138 imageGson.bytesUrl
137139 else
138140 baseApplication.baseUrl + imageGson.bytesUrl
139- Log .i(javaClass.name , " downloadUrl: $downloadUrl " )
141+ Log .i(TAG , " downloadUrl: $downloadUrl " )
140142 val bytes = MultimediaDownloader .downloadFileBytes(downloadUrl)
141- Log .i(javaClass.name , " bytes.length: " + bytes.size)
143+ Log .i(TAG , " bytes.length: " + bytes.size)
142144
143145 // Store the downloaded file in the external storage directory
144146 try {
145147 val fileOutputStream = FileOutputStream (imageFile)
146148 fileOutputStream.write(bytes)
147149 } catch (e: FileNotFoundException ) {
148- Log .e(javaClass.name , null , e)
150+ Log .e(TAG , null , e)
149151 } catch (e: IOException ) {
150- Log .e(javaClass.name , null , e)
152+ Log .e(TAG , null , e)
151153 }
152- Log .i(javaClass.name , " imageFile.exists(): " + imageFile.exists())
154+ Log .i(TAG , " imageFile.exists(): " + imageFile.exists())
153155 }
154156
155157 // Store the Image in the database
156158 imageDao.insert(image)
157- Log .i(javaClass.name , " Stored Image in database with ID " + image.id)
159+ Log .i(TAG , " Stored Image in database with ID " + image.id)
158160
159161 // Store all the Image's Word labels in the database
160162 val wordGsons = imageGson.words
161- Log .i(javaClass.name , " wordGsons.size(): " + wordGsons.size)
163+ Log .i(TAG , " wordGsons.size(): " + wordGsons.size)
162164 for (wordGson in wordGsons) {
163- Log .i(javaClass.name , " wordGson.getId(): " + wordGson.id)
165+ Log .i(TAG , " wordGson.getId(): " + wordGson.id)
164166 val image_Word = Image_Word ()
165167 image_Word.image_id = imageGson.id
166168 image_Word.words_id = wordGson.id
167169 image_WordDao.insert(image_Word)
168170 Log .i(
169- javaClass.name ,
171+ TAG ,
170172 " Stored Image_Word in database. Image_id: " + image_Word.image_id + " , words_id: " + image_Word.words_id
171173 )
172174 }
173175 }
174176
175177 // Update the UI
176178 val images = imageDao.loadAll()
177- Log .i(javaClass.name , " images.size(): " + images.size)
179+ Log .i(TAG , " images.size(): " + images.size)
178180 activity!! .runOnUiThread {
179181 textView!! .text = " images.size(): " + images.size
180182 Snackbar .make(textView!! , " images.size(): " + images.size, Snackbar .LENGTH_LONG )
0 commit comments