Skip to content

Commit 91abd3c

Browse files
committed
Convert VideosFragment to Kotlin
1 parent cf60bbc commit 91abd3c

1 file changed

Lines changed: 127 additions & 138 deletions

File tree

Lines changed: 127 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -1,172 +1,160 @@
1-
package ai.elimu.content_provider.ui.video;
2-
3-
import android.os.Bundle;
4-
import android.util.Log;
5-
import android.view.LayoutInflater;
6-
import android.view.View;
7-
import android.view.ViewGroup;
8-
import android.widget.ProgressBar;
9-
import android.widget.TextView;
10-
11-
import androidx.annotation.NonNull;
12-
import androidx.annotation.Nullable;
13-
import androidx.fragment.app.Fragment;
14-
import androidx.lifecycle.Observer;
15-
import androidx.lifecycle.ViewModelProvider;
16-
17-
import com.google.android.material.snackbar.Snackbar;
18-
19-
import java.io.File;
20-
import java.io.FileNotFoundException;
21-
import java.io.FileOutputStream;
22-
import java.io.IOException;
23-
import java.util.List;
24-
import java.util.concurrent.ExecutorService;
25-
import java.util.concurrent.Executors;
26-
27-
import ai.elimu.content_provider.BaseApplication;
28-
import ai.elimu.content_provider.R;
29-
import ai.elimu.content_provider.rest.VideosService;
30-
import ai.elimu.content_provider.room.GsonToRoomConverter;
31-
import ai.elimu.content_provider.room.dao.VideoDao;
32-
import ai.elimu.content_provider.room.db.RoomDb;
33-
import ai.elimu.content_provider.room.entity.Video;
34-
import ai.elimu.content_provider.util.FileHelper;
35-
import ai.elimu.content_provider.util.MultimediaDownloader;
36-
import ai.elimu.model.v2.gson.content.VideoGson;
37-
import retrofit2.Call;
38-
import retrofit2.Callback;
39-
import retrofit2.Response;
40-
import retrofit2.Retrofit;
1+
package ai.elimu.content_provider.ui.video
2+
3+
import ai.elimu.content_provider.BaseApplication
4+
import ai.elimu.content_provider.R
5+
import ai.elimu.content_provider.rest.VideosService
6+
import ai.elimu.content_provider.room.GsonToRoomConverter.getVideo
7+
import ai.elimu.content_provider.room.db.RoomDb
8+
import ai.elimu.content_provider.util.FileHelper.getVideoFile
9+
import ai.elimu.content_provider.util.MultimediaDownloader
10+
import ai.elimu.model.v2.gson.content.VideoGson
11+
import android.os.Bundle
12+
import android.util.Log
13+
import android.view.LayoutInflater
14+
import android.view.View
15+
import android.view.ViewGroup
16+
import android.widget.ProgressBar
17+
import android.widget.TextView
18+
import androidx.fragment.app.Fragment
19+
import androidx.lifecycle.Observer
20+
import androidx.lifecycle.ViewModelProvider
21+
import com.google.android.material.snackbar.Snackbar
22+
import retrofit2.Call
23+
import retrofit2.Callback
24+
import retrofit2.Response
25+
import java.io.FileNotFoundException
26+
import java.io.FileOutputStream
27+
import java.io.IOException
28+
import java.util.concurrent.Executors
4129

4230
//import ai.elimu.content_provider.room.dao.Video_WordDao;
4331
//import ai.elimu.content_provider.room.entity.Video_Word;
44-
45-
public class VideosFragment extends Fragment {
46-
47-
private VideosViewModel videosViewModel;
48-
49-
private ProgressBar progressBar;
50-
51-
private TextView textView;
52-
53-
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
54-
Log.i(getClass().getName(), "onCreateView");
55-
56-
videosViewModel = new ViewModelProvider(this).get(VideosViewModel.class);
57-
View root = inflater.inflate(R.layout.fragment_videos, container, false);
58-
progressBar = root.findViewById(R.id.progress_bar_videos);
59-
textView = root.findViewById(R.id.text_videos);
60-
videosViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
61-
@Override
62-
public void onChanged(@Nullable String s) {
63-
Log.i(getClass().getName(), "onChanged");
64-
textView.setText(s);
32+
class VideosFragment : Fragment() {
33+
private var videosViewModel: VideosViewModel? = null
34+
35+
private var progressBar: ProgressBar? = null
36+
37+
private var textView: TextView? = null
38+
39+
override fun onCreateView(
40+
inflater: LayoutInflater,
41+
container: ViewGroup?,
42+
savedInstanceState: Bundle?
43+
): View? {
44+
Log.i(javaClass.name, "onCreateView")
45+
46+
videosViewModel = ViewModelProvider(this)[VideosViewModel::class.java]
47+
val root = inflater.inflate(R.layout.fragment_videos, container, false)
48+
progressBar = root.findViewById(R.id.progress_bar_videos)
49+
textView = root.findViewById(R.id.text_videos)
50+
videosViewModel!!.text.observe(viewLifecycleOwner, object : Observer<String?> {
51+
override fun onChanged(s: String?) {
52+
Log.i(javaClass.name, "onChanged")
53+
textView?.text = s
6554
}
66-
});
67-
return root;
55+
})
56+
return root
6857
}
6958

70-
@Override
71-
public void onStart() {
72-
Log.i(getClass().getName(), "onStart");
73-
super.onStart();
59+
override fun onStart() {
60+
Log.i(javaClass.name, "onStart")
61+
super.onStart()
7462

7563
// Download Videos from REST API, and store them in the database
76-
BaseApplication baseApplication = (BaseApplication) getActivity().getApplication();
77-
Retrofit retrofit = baseApplication.getRetrofit();
78-
VideosService videosService = retrofit.create(VideosService.class);
79-
Call<List<VideoGson>> videoGsonsCall = videosService.listVideos();
80-
Log.i(getClass().getName(), "videoGsonsCall.request(): " + videoGsonsCall.request());
81-
videoGsonsCall.enqueue(new Callback<List<VideoGson>>() {
82-
83-
@Override
84-
public void onResponse(Call<List<VideoGson>> call, Response<List<VideoGson>> response) {
85-
Log.i(getClass().getName(), "onResponse");
86-
87-
Log.i(getClass().getName(), "response: " + response);
88-
if (response.isSuccessful()) {
89-
List<VideoGson> videoGsons = response.body();
90-
Log.i(getClass().getName(), "videoGsons.size(): " + videoGsons.size());
91-
92-
if (videoGsons.size() > 0) {
93-
processResponseBody(videoGsons);
64+
val baseApplication = activity!!.application as BaseApplication
65+
val retrofit = baseApplication.retrofit
66+
val videosService = retrofit.create(VideosService::class.java)
67+
val videoGsonsCall = videosService.listVideos()
68+
Log.i(javaClass.name, "videoGsonsCall.request(): " + videoGsonsCall.request())
69+
videoGsonsCall.enqueue(object : Callback<List<VideoGson>> {
70+
override fun onResponse(
71+
call: Call<List<VideoGson>>,
72+
response: Response<List<VideoGson>>
73+
) {
74+
Log.i(javaClass.name, "onResponse")
75+
76+
Log.i(javaClass.name, "response: $response")
77+
if (response.isSuccessful) {
78+
val videoGsons = response.body()!!
79+
Log.i(javaClass.name, "videoGsons.size(): " + videoGsons.size)
80+
81+
if (videoGsons.isNotEmpty()) {
82+
processResponseBody(videoGsons)
9483
}
9584
} else {
9685
// Handle error
97-
Snackbar.make(textView, response.toString(), Snackbar.LENGTH_LONG)
98-
.setBackgroundTint(getResources().getColor(R.color.deep_orange_darken_4))
99-
.show();
100-
progressBar.setVisibility(View.GONE);
86+
Snackbar.make(textView!!, response.toString(), Snackbar.LENGTH_LONG)
87+
.setBackgroundTint(resources.getColor(R.color.deep_orange_darken_4))
88+
.show()
89+
progressBar!!.visibility = View.GONE
10190
}
10291
}
10392

104-
@Override
105-
public void onFailure(Call<List<VideoGson>> call, Throwable t) {
106-
Log.e(getClass().getName(), "onFailure", t);
93+
override fun onFailure(call: Call<List<VideoGson>>, t: Throwable) {
94+
Log.e(javaClass.name, "onFailure", t)
10795

108-
Log.e(getClass().getName(), "t.getCause():", t.getCause());
96+
Log.e(javaClass.name, "t.getCause():", t.cause)
10997

11098
// Handle error
111-
Snackbar.make(textView, t.getCause().toString(), Snackbar.LENGTH_LONG)
112-
.setBackgroundTint(getResources().getColor(R.color.deep_orange_darken_4))
113-
.show();
114-
progressBar.setVisibility(View.GONE);
99+
Snackbar.make(textView!!, t.cause.toString(), Snackbar.LENGTH_LONG)
100+
.setBackgroundTint(resources.getColor(R.color.deep_orange_darken_4))
101+
.show()
102+
progressBar!!.visibility = View.GONE
115103
}
116-
});
104+
})
117105
}
118106

119-
private void processResponseBody(List<VideoGson> videoGsons) {
120-
Log.i(getClass().getName(), "processResponseBody");
107+
private fun processResponseBody(videoGsons: List<VideoGson>) {
108+
Log.i(javaClass.name, "processResponseBody")
109+
110+
val executorService = Executors.newSingleThreadExecutor()
111+
executorService.execute(object : Runnable {
112+
override fun run() {
113+
Log.i(javaClass.name, "run")
121114

122-
ExecutorService executorService = Executors.newSingleThreadExecutor();
123-
executorService.execute(new Runnable() {
124-
@Override
125-
public void run() {
126-
Log.i(getClass().getName(), "run");
115+
val roomDb = RoomDb.getDatabase(context)
116+
val videoDao = roomDb.videoDao()
127117

128-
RoomDb roomDb = RoomDb.getDatabase(getContext());
129-
VideoDao videoDao = roomDb.videoDao();
130-
// Video_WordDao video_WordDao = roomDb.video_WordDao();
118+
// Video_WordDao video_WordDao = roomDb.video_WordDao();
131119

132120
// Empty the database table before downloading up-to-date content
133121
// video_WordDao.deleteAll();
134-
videoDao.deleteAll();
135-
// TODO: also delete corresponding video files (only those that are no longer used)
122+
videoDao.deleteAll()
136123

137-
for (VideoGson videoGson : videoGsons) {
138-
Log.i(getClass().getName(), "videoGson.getId(): " + videoGson.getId());
124+
// TODO: also delete corresponding video files (only those that are no longer used)
125+
for (videoGson in videoGsons) {
126+
Log.i(javaClass.name, "videoGson.getId(): " + videoGson.id)
139127

140-
Video video = GsonToRoomConverter.getVideo(videoGson);
128+
val video = getVideo(videoGson)
141129

142130
// Check if the corresponding video file has already been downloaded
143-
File videoFile = FileHelper.getVideoFile(videoGson, getContext());
144-
Log.i(getClass().getName(), "videoFile: " + videoFile);
145-
Log.i(getClass().getName(), "videoFile.exists(): " + videoFile.exists());
131+
val videoFile = getVideoFile(videoGson, context)
132+
Log.i(javaClass.name, "videoFile: $videoFile")
133+
Log.i(javaClass.name, "videoFile.exists(): " + videoFile!!.exists())
146134
if (!videoFile.exists()) {
147135
// Download file
148-
String fileUrl = videoGson.getFileUrl();
149-
Log.i(getClass().getName(), "fileUrl: " + fileUrl);
150-
byte[] bytes = MultimediaDownloader.downloadFileBytes(fileUrl);
151-
Log.i(getClass().getName(), "bytes.length: " + bytes.length);
136+
val fileUrl = videoGson.fileUrl
137+
Log.i(javaClass.name, "fileUrl: $fileUrl")
138+
val bytes = MultimediaDownloader.downloadFileBytes(fileUrl)
139+
Log.i(javaClass.name, "bytes.length: " + bytes.size)
152140

153141
// Store the downloaded file in the external storage directory
154142
try {
155-
FileOutputStream fileOutputStream = new FileOutputStream(videoFile);
156-
fileOutputStream.write(bytes);
157-
} catch (FileNotFoundException e) {
158-
Log.e(getClass().getName(), null, e);
159-
} catch (IOException e) {
160-
Log.e(getClass().getName(), null, e);
143+
val fileOutputStream = FileOutputStream(videoFile)
144+
fileOutputStream.write(bytes)
145+
} catch (e: FileNotFoundException) {
146+
Log.e(javaClass.name, null, e)
147+
} catch (e: IOException) {
148+
Log.e(javaClass.name, null, e)
161149
}
162-
Log.i(getClass().getName(), "videoFile.exists(): " + videoFile.exists());
150+
Log.i(javaClass.name, "videoFile.exists(): " + videoFile.exists())
163151
}
164152

165153
// Store the Video in the database
166-
videoDao.insert(video);
167-
Log.i(getClass().getName(), "Stored Video in database with ID " + video.getId());
154+
videoDao.insert(video)
155+
Log.i(javaClass.name, "Stored Video in database with ID " + video!!.id)
168156

169-
// // Store all the Video's Word labels in the database
157+
// // Store all the Video's Word labels in the database
170158
// Set<WordGson> wordGsons = videoGson.getWords();
171159
// Log.i(getClass().getName(), "wordGsons.size(): " + wordGsons.size());
172160
// for (WordGson wordGson : wordGsons) {
@@ -180,14 +168,15 @@ public class VideosFragment extends Fragment {
180168
}
181169

182170
// Update the UI
183-
List<Video> videos = videoDao.loadAll();
184-
Log.i(getClass().getName(), "videos.size(): " + videos.size());
185-
getActivity().runOnUiThread(() -> {
186-
textView.setText("videos.size(): " + videos.size());
187-
Snackbar.make(textView, "videos.size(): " + videos.size(), Snackbar.LENGTH_LONG).show();
188-
progressBar.setVisibility(View.GONE);
189-
});
171+
val videos = videoDao.loadAll()
172+
Log.i(javaClass.name, "videos.size(): " + videos.size)
173+
activity!!.runOnUiThread {
174+
textView!!.text = "videos.size(): " + videos.size
175+
Snackbar.make(textView!!, "videos.size(): " + videos.size, Snackbar.LENGTH_LONG)
176+
.show()
177+
progressBar!!.visibility = View.GONE
178+
}
190179
}
191-
});
180+
})
192181
}
193182
}

0 commit comments

Comments
 (0)