Advertisement
backlight0815

Untitled

Feb 28th, 2023
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.29 KB | None | 0 0
  1. package com.example.firebaseproject
  2.  
  3. import android.annotation.SuppressLint
  4. import android.content.Context
  5. import android.os.Bundle
  6. import android.text.TextUtils
  7. import android.util.Log
  8. import android.widget.Toast
  9. import androidx.activity.ComponentActivity
  10. import androidx.activity.compose.setContent
  11. import androidx.compose.foundation.background
  12. import androidx.compose.foundation.layout.*
  13. import androidx.compose.material.*
  14. import androidx.compose.runtime.Composable
  15. import androidx.compose.runtime.mutableStateOf
  16. import androidx.compose.runtime.remember
  17. import androidx.compose.ui.Alignment
  18. import androidx.compose.ui.Modifier
  19. import androidx.compose.ui.graphics.Color
  20. import androidx.compose.ui.platform.LocalContext
  21. import androidx.compose.ui.text.TextStyle
  22. import androidx.compose.ui.text.style.TextAlign
  23. import androidx.compose.ui.unit.dp
  24. import androidx.compose.ui.unit.sp
  25. import com.example.firebaseproject.ui.theme.FirebaseProjectTheme
  26. import com.example.firebaseproject.ui.theme.greenColor
  27. import com.google.firebase.firestore.CollectionReference
  28. import com.google.firebase.firestore.FirebaseFirestore
  29.  
  30. class MainActivity : ComponentActivity() {
  31. @SuppressLint("UnrememberedMutableState")
  32. override fun onCreate(savedInstanceState: Bundle?) {
  33. super.onCreate(savedInstanceState)
  34. setContent {
  35. FirebaseProjectTheme {
  36. // A surface container using the 'background' color from the theme
  37. Surface(
  38. // on below line we are specifying modifier and color for our app
  39. modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background
  40. ) {
  41. // on the below line we are specifying the theme as the scaffold.
  42. Scaffold(
  43. // in scaffold we are specifying the top bar.
  44. topBar = {
  45. // inside top bar we are specifying background color.
  46. TopAppBar(backgroundColor = greenColor,
  47. // along with that we are specifying
  48. // title for our top bar.
  49. title = {
  50. // in the top bar we are
  51. // specifying tile as a text
  52. Text(
  53. // on below line we are specifying
  54. // text to display in top app bar
  55. text = "GFG",
  56. // on below line we are specifying
  57. // modifier to fill max width
  58. modifier = Modifier.fillMaxWidth(),
  59. // on below line we are
  60. // specifying text alignment
  61. textAlign = TextAlign.Center,
  62. // on below line we are specifying
  63. // color for our text.
  64. color = Color.White
  65. )
  66. })
  67. }) {
  68. // on below line we are calling
  69. // method to display UI
  70. firebaseUI(LocalContext.current)
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }
  77.  
  78. @Composable
  79. fun firebaseUI(context: Context) {
  80.  
  81. // on below line creating variable for course name,
  82. // course duration and course description.
  83. val courseName = remember {
  84. mutableStateOf("")
  85. }
  86.  
  87. val courseDuration = remember {
  88. mutableStateOf("")
  89. }
  90.  
  91. val courseDescription = remember {
  92. mutableStateOf("")
  93. }
  94.  
  95. // on below line creating a column
  96. // to display our retrieved image view.
  97. Column(
  98. // adding modifier for our column
  99. modifier = Modifier
  100. .fillMaxHeight()
  101. .fillMaxWidth()
  102. .background(Color.White),
  103. // on below line adding vertical and
  104. // horizontal alignment for column.
  105. verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally
  106. ) {
  107.  
  108.  
  109. TextField(
  110. // on below line we are specifying
  111. // value for our course name text field.
  112. value = courseName.value,
  113.  
  114. // on below line we are adding on
  115. // value change for text field.
  116. onValueChange = { courseName.value = it },
  117.  
  118. // on below line we are adding place holder
  119. // as text as "Enter your course name"
  120. placeholder = { Text(text = "Enter your course name") },
  121.  
  122. // on below line we are adding modifier to it
  123. // and adding padding to it and filling max width
  124. modifier = Modifier
  125. .padding(16.dp)
  126. .fillMaxWidth(),
  127.  
  128. // on below line we are adding text style
  129. // specifying color and font size to it.
  130. textStyle = TextStyle(color = Color.Black, fontSize = 15.sp),
  131.  
  132. // on below line we are adding
  133. // single line to it.
  134. singleLine = true,
  135. )
  136.  
  137. Spacer(modifier = Modifier.height(10.dp))
  138.  
  139. TextField(
  140. // on below line we are specifying
  141. // value for our course duration text field.
  142. value = courseDuration.value,
  143.  
  144. // on below line we are adding on
  145. // value change for text field.
  146. onValueChange = { courseDuration.value = it },
  147.  
  148. // on below line we are adding place holder
  149. // as text as "Enter your course duration"
  150. placeholder = { Text(text = "Enter your course duration") },
  151.  
  152. // on below line we are adding modifier to it
  153. // and adding padding to it and filling max width
  154. modifier = Modifier
  155. .padding(16.dp)
  156. .fillMaxWidth(),
  157.  
  158. // on below line we are adding text style
  159. // specifying color and font size to it.
  160. textStyle = TextStyle(color = Color.Black, fontSize = 15.sp),
  161.  
  162. // on below line we are adding
  163. // single line to it.
  164. singleLine = true,
  165. )
  166.  
  167. Spacer(modifier = Modifier.height(10.dp))
  168.  
  169. TextField(
  170. // on below line we are specifying
  171. // value for our course description text field.
  172. value = courseDescription.value,
  173.  
  174. // on below line we are adding on
  175. // value change for text field.
  176. onValueChange = { courseDescription.value = it },
  177.  
  178. // on below line we are adding place holder
  179. // as text as "Enter your course description"
  180. placeholder = { Text(text = "Enter your course description") },
  181.  
  182. // on below line we are adding modifier to it
  183. // and adding padding to it and filling max width
  184. modifier = Modifier
  185. .padding(16.dp)
  186. .fillMaxWidth(),
  187.  
  188. // on below line we are adding text style
  189. // specifying color and font size to it.
  190. textStyle = TextStyle(color = Color.Black, fontSize = 15.sp),
  191.  
  192. // on below line we are adding
  193. // single line to it.
  194. singleLine = true,
  195. )
  196.  
  197. Spacer(modifier = Modifier.height(10.dp))
  198.  
  199. // on below line creating button to
  200. // add data to firebase firestore database.
  201. Button(
  202. onClick = {
  203. // on below line we are validating user input parameters.
  204. if (TextUtils.isEmpty(courseName.value.toString())) {
  205. Toast.makeText(context, "Please enter course name", Toast.LENGTH_SHORT).show()
  206. } else if (TextUtils.isEmpty(courseDuration.value.toString())) {
  207. Toast.makeText(context, "Please enter course Duration", Toast.LENGTH_SHORT)
  208. .show()
  209. } else if (TextUtils.isEmpty(courseDescription.value.toString())) {
  210. Toast.makeText(context, "Please enter course descritpion", Toast.LENGTH_SHORT)
  211. .show()
  212. } else {
  213. // on below line adding data to
  214. // firebase firestore database.
  215. addDataToFirebase(
  216. courseName.value,
  217. courseDuration.value,
  218. courseDescription.value, context
  219. )
  220. }
  221. },
  222. // on below line we are
  223. // adding modifier to our button.
  224. modifier = Modifier
  225. .fillMaxWidth()
  226. .padding(16.dp)
  227. ) {
  228. // on below line we are adding text for our button
  229. Text(text = "Add Data", modifier = Modifier.padding(8.dp))
  230. }
  231. }
  232. }
  233.  
  234. fun addDataToFirebase(
  235. courseName: String,
  236. courseDuration: String,
  237. courseDescription: String,
  238. context: Context
  239. ) {
  240. // on below line creating an instance of firebase firestore.
  241. val db: FirebaseFirestore = FirebaseFirestore.getInstance()
  242. //creating a collection reference for our Firebase Firestore database.
  243. val dbCourses: CollectionReference = db.collection("Courses")
  244. //adding our data to our courses object class.
  245. val courses = Course(courseName, courseDescription, courseDuration)
  246.  
  247. //below method is use to add data to Firebase Firestore.
  248. dbCourses.add(courses).addOnSuccessListener {
  249. // after the data addition is successful
  250. // we are displaying a success toast message.
  251. Toast.makeText(
  252. context,
  253. "Your Course has been added to Firebase Firestore",
  254. Toast.LENGTH_SHORT
  255. ).show()
  256.  
  257. }.addOnFailureListener { e ->
  258. // this method is called when the data addition process is failed.
  259. // displaying a toast message when data addition is failed.
  260. Toast.makeText(context, "Fail to add course \n$e", Toast.LENGTH_SHORT).show()
  261. }
  262.  
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement