backlight0815

Untitled

Mar 1st, 2023
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.70 KB | None | 0 0
  1. package com.example.firebaseproject
  2.  
  3. import android.annotation.SuppressLint
  4. import android.os.Bundle
  5. import android.widget.Toast
  6. import android.content.Context
  7. import android.util.Log
  8. import androidx.activity.ComponentActivity
  9. import androidx.activity.compose.setContent
  10. import androidx.compose.foundation.background
  11. import androidx.compose.foundation.layout.*
  12. import androidx.compose.foundation.lazy.LazyColumn
  13. import androidx.compose.foundation.lazy.itemsIndexed
  14. import androidx.compose.material.*
  15. import androidx.compose.runtime.Composable
  16. import androidx.compose.runtime.mutableStateListOf
  17. import androidx.compose.runtime.snapshots.SnapshotStateList
  18. import androidx.compose.ui.Alignment
  19. import androidx.compose.ui.Modifier
  20. import androidx.compose.ui.graphics.Color
  21. import androidx.compose.ui.platform.LocalContext
  22. import androidx.compose.ui.text.TextStyle
  23. import androidx.compose.ui.text.font.FontWeight
  24. import androidx.compose.ui.text.style.TextAlign
  25. import androidx.compose.ui.unit.dp
  26. import androidx.compose.ui.unit.sp
  27. import com.example.firebaseproject.ui.theme.FirebaseProjectTheme
  28. import com.example.firebaseproject.ui.theme.greenColor
  29. import com.google.firebase.firestore.FirebaseFirestore
  30.  
  31. class CourseDetailsActivity : ComponentActivity() {
  32.  
  33. @SuppressLint("UnrememberedMutableState")
  34. override fun onCreate(savedInstanceState: Bundle?) {
  35.  
  36. super.onCreate(savedInstanceState)
  37. setContent {
  38. FirebaseProjectTheme {
  39. // A surface container using the 'background' color from the theme
  40. Surface(
  41. // on below line we are specifying modifier and color for our app
  42. modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background
  43. ) {
  44. // on the below line we are specifying
  45. // the theme as the scaffold.
  46. Scaffold(
  47. // in scaffold we are specifying the top bar.
  48. topBar = {
  49. // inside top bar we are specifying
  50. // background color.
  51. TopAppBar(backgroundColor = greenColor,
  52. // along with that we are
  53. // specifying title for our top bar.
  54. title = {
  55. // in the top bar we are specifying
  56. // tile as a text
  57. Text(
  58. // on below line we are specifying
  59. // text to display in top app bar
  60. text = "GFG",
  61. // on below line we are specifying
  62. // modifier to fill max width
  63. modifier = Modifier.fillMaxWidth(),
  64. // on below line we are specifying
  65. // text alignment
  66. textAlign = TextAlign.Center,
  67. // on below line we are specifying
  68. // color for our text.
  69. color = Color.White
  70. )
  71. })
  72. }) {
  73.  
  74. // on below line creating variable for list of data.
  75. var courseList = mutableStateListOf<Course?>()
  76. // on below line creating variable for freebase database
  77. // and database reference.
  78. var db: FirebaseFirestore = FirebaseFirestore.getInstance()
  79.  
  80. // on below line getting data from our database
  81. db.collection("Courses").get()
  82. .addOnSuccessListener { queryDocumentSnapshots ->
  83. // after getting the data we are calling
  84. // on success method
  85. // and inside this method we are checking
  86. // if the received query snapshot is empty or not.
  87. if (!queryDocumentSnapshots.isEmpty) {
  88. // if the snapshot is not empty we are
  89. // hiding our progress bar and adding
  90. // our data in a list.
  91. // loadingPB.setVisibility(View.GONE)
  92. val list = queryDocumentSnapshots.documents
  93. for (d in list) {
  94. // after getting this list we are passing that
  95. // list to our object class.
  96. val c: Course? = d.toObject(Course::class.java)
  97. // and we will pass this object class inside
  98. // our arraylist which we have created for list view.
  99. courseList.add(c)
  100.  
  101. }
  102. } else {
  103. // if the snapshot is empty we are displaying
  104. // a toast message.
  105. Toast.makeText(
  106. this@CourseDetailsActivity,
  107. "No data found in Database",
  108. Toast.LENGTH_SHORT
  109. ).show()
  110. }
  111. }
  112. // if we don't get any data or any error
  113. // we are displaying a toast message
  114. // that we donot get any data
  115. .addOnFailureListener {
  116. Toast.makeText(
  117. this@CourseDetailsActivity,
  118. "Fail to get the data.",
  119. Toast.LENGTH_SHORT
  120. ).show()
  121. }
  122. // on below line we are calling method to display UI
  123. firebaseUI(LocalContext.current, courseList)
  124. }
  125. }
  126. }
  127. }
  128. }
  129.  
  130.  
  131. @OptIn(ExperimentalMaterialApi::class)
  132. @Composable
  133. fun firebaseUI(context: Context, courseList: SnapshotStateList<Course?>) {
  134.  
  135. // on below line creating a column
  136. // to display our retrieved list.
  137. Column(
  138. // adding modifier for our column
  139. modifier = Modifier
  140. .fillMaxHeight()
  141. .fillMaxWidth()
  142. .background(Color.White),
  143. // on below line adding vertical and
  144. // horizontal alignment for column.
  145. verticalArrangement = Arrangement.Top,
  146. horizontalAlignment = Alignment.CenterHorizontally
  147. ) {
  148. // on below line we are
  149. // calling lazy column
  150. // for displaying listview.
  151. LazyColumn {
  152. // on below line we are setting data
  153. // for each item of our listview.
  154. itemsIndexed(courseList) { index, item ->
  155. // on below line we are creating
  156. // a card for our list view item.
  157. Card(
  158. onClick = {
  159. // inside on click we are
  160. // displaying the toast message.
  161. Toast.makeText(
  162. context,
  163. courseList[index]?.courseName + " selected..",
  164. Toast.LENGTH_SHORT
  165. ).show()
  166. },
  167. // on below line we are adding
  168. // padding from our all sides.
  169. modifier = Modifier.padding(8.dp),
  170.  
  171. // on below line we are adding
  172. // elevation for the card.
  173. elevation = 6.dp
  174. ) {
  175. // on below line we are creating
  176. // a row for our list view item.
  177. Column(
  178. // for our row we are adding modifier
  179. // to set padding from all sides.
  180. modifier = Modifier
  181. .padding(8.dp)
  182. .fillMaxWidth()
  183. ) {
  184. // on below line inside row we are adding spacer
  185. Spacer(modifier = Modifier.width(5.dp))
  186. // on below line we are displaying course name.
  187. courseList[index]?.courseName?.let {
  188. Text(
  189. // inside the text on below line we are
  190. // setting text as the language name
  191. // from our modal class.
  192. text = it,
  193.  
  194. // on below line we are adding padding
  195. // for our text from all sides.
  196. modifier = Modifier.padding(4.dp),
  197.  
  198. // on below line we are adding
  199. // color for our text
  200. color = greenColor,
  201. textAlign = TextAlign.Center,
  202. style = TextStyle(
  203. fontSize = 20.sp, fontWeight = FontWeight.Bold
  204. )
  205. )
  206. }
  207. // adding spacer on below line.
  208. Spacer(modifier = Modifier.height(5.dp))
  209.  
  210. // on below line displaying text for course duration
  211. courseList[index]?.courseDuration?.let {
  212. Text(
  213. // inside the text on below line we are
  214. // setting text as the language name
  215. // from our modal class.
  216. text = it,
  217.  
  218. // on below line we are adding padding
  219. // for our text from all sides.
  220. modifier = Modifier.padding(4.dp),
  221.  
  222. // on below line we are
  223. // adding color for our text
  224. color = Color.Black,
  225. textAlign = TextAlign.Center,
  226. style = TextStyle(
  227. fontSize = 15.sp
  228. )
  229. )
  230. }
  231. // adding spacer on below line.
  232. Spacer(modifier = Modifier.width(5.dp))
  233.  
  234. // on below line displaying text for course description
  235. courseList[index]?.courseDescription?.let {
  236. Text(
  237. // inside the text on below line we are
  238. // setting text as the language name
  239. // from our modal class.
  240. text = it,
  241.  
  242. // on below line we are adding padding
  243. // for our text from all sides.
  244. modifier = Modifier.padding(4.dp),
  245.  
  246. // on below line we are adding color for our text
  247. color = Color.Black,
  248. textAlign = TextAlign.Center,
  249. style = TextStyle(fontSize = 15.sp)
  250. )
  251. }
  252. }
  253. }
  254. }
  255.  
  256. }
  257. }
  258. }
  259. }
Add Comment
Please, Sign In to add comment