-
Notifications
You must be signed in to change notification settings - Fork 183
/
CourseActivity.kt
164 lines (133 loc) · 6.38 KB
/
CourseActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.codingblocks.cbonlineapp
import android.content.Context
import android.os.Bundle
import android.widget.ProgressBar
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.ahmadrosid.svgloader.SvgLoader
import com.codingblocks.cbonlineapp.Utils.ProgressBarAnimation
import com.codingblocks.cbonlineapp.Utils.retrofitCallback
import com.codingblocks.cbonlineapp.adapters.SectionsDataAdapter
import com.codingblocks.onlineapi.Clients
import com.codingblocks.onlineapi.models.Sections
import com.ethanhua.skeleton.Skeleton
import com.ethanhua.skeleton.SkeletonScreen
import com.google.android.youtube.player.YouTubeInitializationResult
import com.google.android.youtube.player.YouTubePlayer
import com.google.android.youtube.player.YouTubePlayerSupportFragment
import io.github.inflationx.viewpump.ViewPumpContextWrapper
import kotlinx.android.synthetic.main.activity_course.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.jetbrains.anko.AnkoLogger
import org.jetbrains.anko.info
import org.jetbrains.anko.toast
class CourseActivity : AppCompatActivity(), AnkoLogger {
lateinit var skeletonScreen: SkeletonScreen
lateinit var courseId: String
lateinit var courseName: String
lateinit var progressBar: Array<ProgressBar?>
companion object {
val YOUTUBE_API_KEY = "AIzaSyAqdhonCxTsQ5oQ-tyNaSgDJWjEM7UaEt4"
}
private lateinit var youtubePlayerInit: YouTubePlayer.OnInitializedListener
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_course)
courseId = intent.getStringExtra("courseId")
courseName = intent.getStringExtra("courseName")
title = courseName
progressBar = arrayOf(courseProgress1, courseProgress2, courseProgress3, courseProgress4, courseProgress5)
setSupportActionBar(toolbar)
supportActionBar?.setHomeButtonEnabled(true)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
skeletonScreen = Skeleton.bind(courseRootView)
.shimmer(true)
.angle(20)
.duration(1200)
.load(R.layout.item_skeleton_course)
.show()
val service = Clients.onlineV2PublicClient
Clients.onlineV2PublicClient.courseById(courseId).enqueue(retrofitCallback { t, resp ->
resp?.body()?.let { it ->
skeletonScreen.hide()
coursePageTitle.text = courseName
coursePageSubtitle.text = it.subtitle
coursePageSummary.text = it.summary
SvgLoader.pluck()
.with(this)
.load(it.logo, coursePageLogo)
showpromoVideo(it.promoVideo)
fetchRating()
val sections = it.runs?.get(0)?.sections
val sectionsList = ArrayList<Sections>()
val sectionAdapter = SectionsDataAdapter(ArrayList())
rvExpendableView.layoutManager = LinearLayoutManager(this)
rvExpendableView.adapter = sectionAdapter
sections!!.forEachIndexed { index, section ->
GlobalScope.launch(Dispatchers.Main) {
val request = service.getSections(section.id!!)
val response = request.await()
if (response.isSuccessful) {
val value = response.body()!!
value.order = index
sectionsList.add(value)
if (sectionsList.size == sections.size) {
sectionsList.sortBy { it.order }
sectionAdapter.setData(sectionsList)
}
} else {
toast("Error ${response.code()}")
}
}
}
}
})
// fetchRating()
}
private fun showpromoVideo(promoVideo: String?) {
// val frameVideo = "<html><iframe width=\"420\" height=\"315\" src=\"$promoVideo\" frameborder=\"0\" allowfullscreen></iframe></body></html>"
//
// displayYoutubeVideo.webViewClient = object : WebViewClient() {
// override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
// return false
// }
// }
// val webSettings = displayYoutubeVideo.settings
// webSettings.javaScriptEnabled = true
// displayYoutubeVideo.loadData(frameVideo, "text/html", "utf-8")
youtubePlayerInit = object : YouTubePlayer.OnInitializedListener {
override fun onInitializationFailure(p0: YouTubePlayer.Provider?, p1: YouTubeInitializationResult?) {
}
override fun onInitializationSuccess(p0: YouTubePlayer.Provider?, youtubePlayerInstance: YouTubePlayer?, p2: Boolean) {
if (!p2) {
info { "VIDEO_ID" + promoVideo!!.substring(30, 41) }
youtubePlayerInstance?.loadVideo(promoVideo!!.substring(30, 41))
}
}
}
val youTubePlayerSupportFragment = supportFragmentManager.findFragmentById(R.id.displayYoutubeVideo) as YouTubePlayerSupportFragment?
youTubePlayerSupportFragment!!.initialize(YOUTUBE_API_KEY, youtubePlayerInit)
}
fun fetchRating() {
Clients.api.getCourseRating(courseId).enqueue(retrofitCallback { throwable, response ->
response?.body().let { it?.apply {
coursePageRatingCountTv.text = count.toString() + " Rating"
coursePageRatingTv.text = rating + " out of 5 stars"
coursePageRatingBar.rating = rating?.toFloat()!!
for (i in 0 until progressBar.size) {
progressBar[i]?.max = it.count * 1000
progressBar[i]?.progress = it.stats[i].toInt() * 1000
val anim = ProgressBarAnimation(progressBar[i], 0F, it.stats[i].toInt() * 1000F)
anim.duration = 1500
progressBar[i]?.startAnimation(anim)
}
}
}
})
}
override fun attachBaseContext(newBase: Context) {
super.attachBaseContext(ViewPumpContextWrapper.wrap(newBase))
}
}