Notes
![]() ![]() Notes - notes.io |
// API - Model
class Movies : ArrayList<MoviesItem>()
data class MoviesItem(
@SerializedName("Actors")
val actors: String,
@SerializedName("Awards")
val awards: String,
@SerializedName("ComingSoon")
val comingSoon: Boolean,
@SerializedName("Country")
val country: String,
@SerializedName("Director")
val director: String,
@SerializedName("Genre")
val genre: String,
@SerializedName("Images")
val images: List<String>,
@SerializedName("imdbID")
val imdbID: String,
@SerializedName("imdbRating")
val imdbRating: String,
@SerializedName("imdbVotes")
val imdbVotes: String,
@SerializedName("Language")
val language: String,
@SerializedName("Metascore")
val metascore: String,
@SerializedName("Plot")
val plot: String,
@SerializedName("Poster")
val poster: String,
@SerializedName("Rated")
val rated: String,
@SerializedName("Released")
val released: String,
@SerializedName("Response")
val response: String,
@SerializedName("Runtime")
val runtime: String,
@SerializedName("Title")
val title: String,
@SerializedName("totalSeasons")
val totalSeasons: String,
@SerializedName("Type")
val type: String,
@SerializedName("Writer")
val writer: String,
@SerializedName("Year")
val year: String
)
// Service
// Service - Class
interface MoviesService {
@GET("saniyusuf/406b843afdfb9c6a86e25753fe2761f4/raw/523c324c7fcc36efab8224f9ebb7556c09b69a14/Film.JSON")
suspend fun getMoviesList(): Response<Movies>
}
// Retrofit - Instance
class RetrofitInstance {
companion object{
val BASE_URL = "https://gist.githubusercontent.com/"
val interceptor = HttpLoggingInterceptor().apply {
this.level = HttpLoggingInterceptor.Level.BODY
}
val client = OkHttpClient().newBuilder().apply {
this.addInterceptor(interceptor)
this.connectTimeout(20, TimeUnit.SECONDS)
this.readTimeout(25, TimeUnit.SECONDS)
this.writeTimeout(30, TimeUnit.SECONDS)
}.build()
fun getInstance(): Retrofit{
return Retrofit
.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create(GsonBuilder().create()))
.build()
}
}
}
// View
class MainActivity : AppCompatActivity() {
lateinit var moviesService: MoviesService
lateinit var mContext: Context
var moviesList: Movies? = null
private val viewModel by viewModels<MainViewModel> {
object : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(p0: Class<T>): T {
return MainViewModel(moviesService) as T
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
moviesService = RetrofitInstance.getInstance().create(MoviesService::class.java)
setContent {
PopularMoviesTheme {
// A surface container using the 'background' color from the theme
Surface(color = MaterialTheme.colors.background) {
PopularMovieMainScreen()
}
}
}
viewModel.getMovies()
}
@Composable
fun PopularMovieMainScreen() {
Column() {
TopAppBar(title = { Text(text = "Top Movie Apps") })
MoviesItems(viewModel.moviesData)
}
}
@Composable
fun MoviesItems(moviesData: LiveData<Movies>) {
val movies by moviesData.observeAsState(kotlin.collections.emptyList())
if(movies.isEmpty()){
CircularProgressIndicator(
color = teal200,
strokeWidth = 2.dp,
modifier = Modifier.gravity(Alignment.CenterHorizontally)
)
}
LazyColumnFor(items = movies, itemContent = { movie: MoviesItem ->
PopularMoviesListItem(movie = movie)
})
}
@Composable
fun PopularMoviesListItem(movie: MoviesItem) {
androidx.compose.foundation.layout.Column() {
Card(
shape = RoundedCornerShape(8.dp),
modifier = Modifier
.padding(all = 16.dp)
.fillMaxWidth()
.height(height = 375.dp)
) {
Column {
fetchImage(movie.images.get(0))
Text(text = movie.title, modifier = Modifier.padding(all = 16.dp), style = MaterialTheme.typography.h4)
Row(
modifier = Modifier.padding(all = 8.dp).fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween
) {
Text(
text = "Release Date: ${movie.released}",
style = MaterialTheme.typography.subtitle2
)
Text(
text = "Vote: ${movie.imdbRating}/10",
style = MaterialTheme.typography.subtitle2
)
}
Text(
text = movie.plot,
modifier = Modifier.padding(all = 8.dp),
maxLines = 5,
overflow = TextOverflow.Ellipsis,
style = MaterialTheme.typography.caption
)
}
}
}
}
@Composable
fun fetchImage(url: String) {
// This holds our current image, and will be updated by the
// launchInComposition lambda below
var image by remember(url) { mutableStateOf<ImageAsset?>(null) }
// launchInComposition will automatically launch a coroutine to execute
// the given block. If the `url` changes, any previously launched coroutine
// will be cancelled, and a new coroutine launched.
launchInComposition(url) {
Picasso.get()
.load(url)
.into(object : Target {
override fun onBitmapLoaded(bitmap: Bitmap, from: Picasso.LoadedFrom) {
image = bitmap.asImageAsset()
}
override fun onBitmapFailed(e: Exception, errorDrawable: Drawable?) {
// Handle the error drawable.
}
override fun onPrepareLoad(placeHolderDrawable: Drawable?) {
// Handle the placeholder drawable.
}
})
}
if (image != null) {
Image(
asset = image!!,
contentScale = ContentScale.FillWidth,
modifier = Modifier
.preferredHeight(height = 200.dp)
.fillMaxWidth()
,
alignment = Alignment.TopCenter
)
}
// Return the state-backed image property
// return image
}
@Preview
@Composable
fun DefaultPreview(){
val movie = MoviesItem(
title = "Avatar",
year = "2009",
rated = "PG-13",
released = "18 Dec 2009",
runtime = "162 min",
imdbRating = "7.9",
images = listOf(
"https://images-na.ssl-images-amazon.com/images/M/MV5BMjEyOTYyMzUxNl5BMl5BanBnXkFtZTcwNTg0MTUzNA@@._V1_SX1500_CR0,0,1500,999_AL_.jpg",
"https://images-na.ssl-images-amazon.com/images/M/MV5BMjEyOTYyMzUxNl5BMl5BanBnXkFtZTcwNTg0MTUzNA@@._V1_SX1500_CR0,0,1500,999_AL_.jpg"
),
actors = "",
awards = "",
comingSoon = true,
country = "",
director = "",
genre = "",
imdbID = "",
imdbVotes = "",
language = "",
metascore = "",
plot = "",
poster = "",
response = "",
totalSeasons = "",
type = "",
writer = "",
)
PopularMoviesListItem(movie)
}
}
// ViewModel
class MainViewModel(private val moviesService: MoviesService) : ViewModel() {
private val _moviesData = MutableLiveData<Movies>()
val moviesData: LiveData<Movies>
get() = _moviesData
fun getMovies(){
viewModelScope.launch {
try {
val movies = moviesService.getMoviesList().body()
_moviesData.value = movies
} catch (e: Exception){
Log.e("API", "Data not fetched")
e.printStackTrace()
}
}
}
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team