@@ -35,47 +35,49 @@ class ImagesFragment : Fragment() {
35
35
36
36
private var textView: TextView ? = null
37
37
38
+ private val TAG = ImagesFragment ::class .java.name
39
+
38
40
override fun onCreateView (
39
41
inflater : LayoutInflater ,
40
42
container : ViewGroup ? ,
41
43
savedInstanceState : Bundle ?
42
44
): View ? {
43
- Log .i(javaClass.name , " onCreateView" )
45
+ Log .i(TAG , " onCreateView" )
44
46
45
47
imagesViewModel = ViewModelProvider (this ).get(ImagesViewModel ::class .java)
46
48
val root = inflater.inflate(R .layout.fragment_images, container, false )
47
49
progressBar = root.findViewById(R .id.progress_bar_images)
48
50
textView = root.findViewById(R .id.text_images) as ? TextView
49
51
imagesViewModel!! .text.observe(viewLifecycleOwner, object : Observer <String ?> {
50
52
override fun onChanged (s : String? ) {
51
- Log .i(javaClass.name , " onChanged" )
53
+ Log .i(TAG , " onChanged" )
52
54
textView?.text = s
53
55
}
54
56
})
55
57
return root
56
58
}
57
59
58
60
override fun onStart () {
59
- Log .i(javaClass.name , " onStart" )
61
+ Log .i(TAG , " onStart" )
60
62
super .onStart()
61
63
62
64
// Download Images from REST API, and store them in the database
63
65
val baseApplication = activity!! .application as BaseApplication
64
66
val retrofit = baseApplication.retrofit
65
67
val imagesService = retrofit.create(ImagesService ::class .java)
66
68
val imageGsonsCall = imagesService.listImages()
67
- Log .i(javaClass.name , " imageGsonsCall.request(): " + imageGsonsCall.request())
69
+ Log .i(TAG , " imageGsonsCall.request(): " + imageGsonsCall.request())
68
70
imageGsonsCall.enqueue(object : Callback <List <ImageGson >> {
69
71
override fun onResponse (
70
72
call : Call <List <ImageGson >>,
71
73
response : Response <List <ImageGson >>
72
74
) {
73
- Log .i(javaClass.name , " onResponse" )
75
+ Log .i(TAG , " onResponse" )
74
76
75
- Log .i(javaClass.name , " response: $response " )
77
+ Log .i(TAG , " response: $response " )
76
78
if (response.isSuccessful) {
77
79
val imageGsons = response.body()!!
78
- Log .i(javaClass.name , " imageGsons.size(): " + imageGsons.size)
80
+ Log .i(TAG , " imageGsons.size(): " + imageGsons.size)
79
81
80
82
if (imageGsons.size > 0 ) {
81
83
processResponseBody(imageGsons)
@@ -90,9 +92,9 @@ class ImagesFragment : Fragment() {
90
92
}
91
93
92
94
override fun onFailure (call : Call <List <ImageGson >>, t : Throwable ) {
93
- Log .e(javaClass.name , " onFailure" , t)
95
+ Log .e(TAG , " onFailure" , t)
94
96
95
- Log .e(javaClass.name , " t.getCause():" , t.cause)
97
+ Log .e(TAG , " t.getCause():" , t.cause)
96
98
97
99
// Handle error
98
100
Snackbar .make(textView!! , t.cause.toString(), Snackbar .LENGTH_LONG )
@@ -104,12 +106,12 @@ class ImagesFragment : Fragment() {
104
106
}
105
107
106
108
private fun processResponseBody (imageGsons : List <ImageGson >) {
107
- Log .i(javaClass.name , " processResponseBody" )
109
+ Log .i(TAG , " processResponseBody" )
108
110
109
111
val executorService = Executors .newSingleThreadExecutor()
110
112
executorService.execute(object : Runnable {
111
113
override fun run () {
112
- Log .i(javaClass.name , " run" )
114
+ Log .i(TAG , " run" )
113
115
114
116
val roomDb = RoomDb .getDatabase(context)
115
117
val imageDao = roomDb.imageDao()
@@ -121,60 +123,60 @@ class ImagesFragment : Fragment() {
121
123
122
124
// TODO: also delete corresponding image files (only those that are no longer used)
123
125
for (imageGson in imageGsons) {
124
- Log .i(javaClass.name , " imageGson.getId(): " + imageGson.id)
126
+ Log .i(TAG , " imageGson.getId(): " + imageGson.id)
125
127
126
128
val image = GsonToRoomConverter .getImage(imageGson)
127
129
128
130
// Check if the corresponding image file has already been downloaded
129
131
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())
132
134
if (! imageFile.exists()) {
133
135
// Download file bytes
134
136
val baseApplication = activity!! .application as BaseApplication
135
137
val downloadUrl = if (imageGson.bytesUrl.startsWith(" http" ))
136
138
imageGson.bytesUrl
137
139
else
138
140
baseApplication.baseUrl + imageGson.bytesUrl
139
- Log .i(javaClass.name , " downloadUrl: $downloadUrl " )
141
+ Log .i(TAG , " downloadUrl: $downloadUrl " )
140
142
val bytes = MultimediaDownloader .downloadFileBytes(downloadUrl)
141
- Log .i(javaClass.name , " bytes.length: " + bytes.size)
143
+ Log .i(TAG , " bytes.length: " + bytes.size)
142
144
143
145
// Store the downloaded file in the external storage directory
144
146
try {
145
147
val fileOutputStream = FileOutputStream (imageFile)
146
148
fileOutputStream.write(bytes)
147
149
} catch (e: FileNotFoundException ) {
148
- Log .e(javaClass.name , null , e)
150
+ Log .e(TAG , null , e)
149
151
} catch (e: IOException ) {
150
- Log .e(javaClass.name , null , e)
152
+ Log .e(TAG , null , e)
151
153
}
152
- Log .i(javaClass.name , " imageFile.exists(): " + imageFile.exists())
154
+ Log .i(TAG , " imageFile.exists(): " + imageFile.exists())
153
155
}
154
156
155
157
// Store the Image in the database
156
158
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)
158
160
159
161
// Store all the Image's Word labels in the database
160
162
val wordGsons = imageGson.words
161
- Log .i(javaClass.name , " wordGsons.size(): " + wordGsons.size)
163
+ Log .i(TAG , " wordGsons.size(): " + wordGsons.size)
162
164
for (wordGson in wordGsons) {
163
- Log .i(javaClass.name , " wordGson.getId(): " + wordGson.id)
165
+ Log .i(TAG , " wordGson.getId(): " + wordGson.id)
164
166
val image_Word = Image_Word ()
165
167
image_Word.image_id = imageGson.id
166
168
image_Word.words_id = wordGson.id
167
169
image_WordDao.insert(image_Word)
168
170
Log .i(
169
- javaClass.name ,
171
+ TAG ,
170
172
" Stored Image_Word in database. Image_id: " + image_Word.image_id + " , words_id: " + image_Word.words_id
171
173
)
172
174
}
173
175
}
174
176
175
177
// Update the UI
176
178
val images = imageDao.loadAll()
177
- Log .i(javaClass.name , " images.size(): " + images.size)
179
+ Log .i(TAG , " images.size(): " + images.size)
178
180
activity!! .runOnUiThread {
179
181
textView!! .text = " images.size(): " + images.size
180
182
Snackbar .make(textView!! , " images.size(): " + images.size, Snackbar .LENGTH_LONG )
0 commit comments