@@ -27,7 +27,7 @@ import java.io.IOException
2727import java.util.concurrent.Executors
2828
2929class VideosFragment : Fragment () {
30- private var videosViewModel: VideosViewModel ? = null
30+ private lateinit var videosViewModel: VideosViewModel
3131 private lateinit var binding: FragmentVideosBinding
3232
3333 override fun onCreateView (
@@ -39,7 +39,7 @@ class VideosFragment : Fragment() {
3939
4040 videosViewModel = ViewModelProvider (this )[VideosViewModel ::class .java]
4141 binding = FragmentVideosBinding .inflate(layoutInflater)
42- videosViewModel!! .text .observe(viewLifecycleOwner, object : Observer <String ?> {
42+ videosViewModel.getText() .observe(viewLifecycleOwner, object : Observer <String ?> {
4343 override fun onChanged (s : String? ) {
4444 Log .i(javaClass.name, " onChanged" )
4545 binding.textVideos.text = s
@@ -53,7 +53,7 @@ class VideosFragment : Fragment() {
5353 super .onStart()
5454
5555 // Download Videos from REST API, and store them in the database
56- val baseApplication = activity!! .application as BaseApplication
56+ val baseApplication = activity? .application as ? BaseApplication ? : return
5757 val retrofit = baseApplication.retrofit
5858 val videosService = retrofit.create(VideosService ::class .java)
5959 val videoGsonsCall = videosService.listVideos()
@@ -67,7 +67,7 @@ class VideosFragment : Fragment() {
6767
6868 Log .i(javaClass.name, " response: $response " )
6969 if (response.isSuccessful) {
70- val videoGsons = response.body()!!
70+ val videoGsons = response.body() ? : return
7171 Log .i(javaClass.name, " videoGsons.size(): " + videoGsons.size)
7272
7373 if (videoGsons.isNotEmpty()) {
@@ -122,29 +122,33 @@ class VideosFragment : Fragment() {
122122 // Check if the corresponding video file has already been downloaded
123123 val videoFile = getVideoFile(videoGson, context)
124124 Log .i(javaClass.name, " videoFile: $videoFile " )
125- Log .i(javaClass.name, " videoFile.exists(): " + videoFile!! .exists())
126- if (! videoFile.exists()) {
127- // Download file
128- val fileUrl = videoGson.fileUrl
129- Log .i(javaClass.name, " fileUrl: $fileUrl " )
130- val bytes = MultimediaDownloader .downloadFileBytes(fileUrl)
131- Log .i(javaClass.name, " bytes.length: " + bytes.size)
132-
133- // Store the downloaded file in the external storage directory
134- try {
135- val fileOutputStream = FileOutputStream (videoFile)
136- fileOutputStream.write(bytes)
137- } catch (e: FileNotFoundException ) {
138- Log .e(javaClass.name, null , e)
139- } catch (e: IOException ) {
140- Log .e(javaClass.name, null , e)
141- }
125+ videoFile?.let {
142126 Log .i(javaClass.name, " videoFile.exists(): " + videoFile.exists())
127+ if (! videoFile.exists()) {
128+ // Download file
129+ val fileUrl = videoGson.fileUrl
130+ Log .i(javaClass.name, " fileUrl: $fileUrl " )
131+ val bytes = MultimediaDownloader .downloadFileBytes(fileUrl)
132+ Log .i(javaClass.name, " bytes.length: " + bytes.size)
133+
134+ // Store the downloaded file in the external storage directory
135+ try {
136+ val fileOutputStream = FileOutputStream (videoFile)
137+ fileOutputStream.write(bytes)
138+ } catch (e: FileNotFoundException ) {
139+ Log .e(javaClass.name, null , e)
140+ } catch (e: IOException ) {
141+ Log .e(javaClass.name, null , e)
142+ }
143+ Log .i(javaClass.name, " videoFile.exists(): " + videoFile.exists())
144+ }
143145 }
144146
145- // Store the Video in the database
146- videoDao.insert(video)
147- Log .i(javaClass.name, " Stored Video in database with ID " + video!! .id)
147+ video?.let {
148+ // Store the Video in the database
149+ videoDao.insert(video)
150+ Log .i(javaClass.name, " Stored Video in database with ID " + video.id)
151+ }
148152
149153 // // Store all the Video's Word labels in the database
150154// Set<WordGson> wordGsons = videoGson.getWords();
@@ -162,7 +166,7 @@ class VideosFragment : Fragment() {
162166 // Update the UI
163167 val videos = videoDao.loadAll()
164168 Log .i(javaClass.name, " videos.size(): " + videos.size)
165- activity!! .runOnUiThread {
169+ activity? .runOnUiThread {
166170 binding.textVideos.text = " videos.size(): " + videos.size
167171 Snackbar .make(binding.textVideos, " videos.size(): " + videos.size, Snackbar .LENGTH_LONG )
168172 .show()
0 commit comments