Skip to content

Commit

Permalink
v6
Browse files Browse the repository at this point in the history
  • Loading branch information
Areeb Shaikh committed Nov 18, 2024
1 parent afc89de commit 4b55bd8
Show file tree
Hide file tree
Showing 12 changed files with 322 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .idea/other.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.bignerdranch.android.listr.naughty

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bignerdranch.android.listr.databinding.NaughtyListItemBinding import com.bignerdranch.android.listr.databinding.NiceListItemBinding
import com.bignerdranch.android.listr.nice.NaughtyData


class NaughtyAdapter(val messages: List<NaughtyData>): RecyclerView.Adapter<NaughtyAdapter.NaughtyViewHolder>() {

inner class NaughtyViewHolder(private val binding: NaughtyListItemBinding): RecyclerView.ViewHolder(binding.root) {
fun bind(naughtyData: NaughtyData) {
binding.naughtyMessageText.text = naughtyData.message
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NaughtyViewHolder {
val inflater = LayoutInflater.from(parent.context)
val binding = NaughtyListItemBinding.inflate(inflater, parent, false)
return NaughtyViewHolder(binding)
}

override fun onBindViewHolder(holder: NaughtyViewHolder, position: Int) {
val naughtyData = messages[position]
holder.bind(naughtyData)
}

override fun getItemCount(): Int {
return messages.size
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.bignerdranch.android.listr.nice

data class NaughtyData(val message: String)
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import com.bignerdranch.android.listr.databinding.FragmentNaughtyBinding
import com.bignerdranch.android.listr.nice.NaughtyData
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.ktx.Firebase
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

private const val TAG = "NaughtyFragment"

Expand All @@ -21,6 +29,28 @@ class NaughtyFragment: Fragment() {

private val db = Firebase.firestore

private val randomNaughtyMessages = listOf(
"Better luck next time!",
"Don't be so naughty!",
"What were you thinking?",
"Time to behave!",
"Oops, try again!",
"Caught red-handed!",
"Stay out of mischief!",
"Mind your manners!",
"Reconsider your choices!",
"Not your finest moment!",
"Oops, busted!",
"Try to stay on the nice list!",
"That's not how it's done!",
"Time to clean up your act!",
"Don't push your luck!",
"Naughty, naughty!",
"Whoops, caught you!",
"You’ve been warned!",
"Back to the drawing board!"
)

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand All @@ -33,9 +63,43 @@ class NaughtyFragment: Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

binding.naughtyRecyclerView.layoutManager = LinearLayoutManager(context)

viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
getNaughtyMsgs().collect { messages ->
binding.naughtyRecyclerView.adapter = NaughtyAdapter(messages)
}
}
}

binding.addNaughtyMessage.setOnClickListener() {
addMsg()
}

binding.randomMessageButton.setOnClickListener {
addRandomMsg()
}
}

private fun getNaughtyMsgs(): StateFlow<List<NaughtyData>> {
val listFlow = MutableStateFlow(emptyList<NaughtyData>())

db.collection("naughty")
.addSnapshotListener { snapshot, e ->
if (e != null) {
return@addSnapshotListener
}

if (snapshot != null && snapshot.documents != null) {
listFlow.value = snapshot.documents.map { doc ->
NaughtyData(doc["message"] as String)
}
} else {
Log.d(TAG, "Current Data: null")
}
}
return listFlow
}

private fun addMsg() {
Expand All @@ -53,6 +117,20 @@ class NaughtyFragment: Fragment() {
}
}

private fun addRandomMsg() {
val randomMessage = randomNaughtyMessages.random()
val msg = hashMapOf("message" to randomMessage)

db.collection("naughty")
.add(msg)
.addOnSuccessListener { documentReference ->
Log.d(TAG, "Random naughty message added with ID: ${documentReference.id}")
}
.addOnFailureListener { e ->
Log.w(TAG, "Error adding random naughty message", e)
}
}

override fun onDestroy() {
super.onDestroy()
_binding = null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.bignerdranch.android.listr.nice

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bignerdranch.android.listr.databinding.NiceListItemBinding

class NiceAdapter(val messages: List<NiceData>): RecyclerView.Adapter<NiceAdapter.NiceViewHolder>() {

inner class NiceViewHolder(private val binding: NiceListItemBinding): RecyclerView.ViewHolder(binding.root) {
fun bind(niceData: NiceData) {
binding.niceMessageText.text = niceData.message
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NiceViewHolder {
val inflater = LayoutInflater.from(parent.context)
val binding = NiceListItemBinding.inflate(inflater, parent, false)
return NiceViewHolder(binding)
}

override fun onBindViewHolder(holder: NiceViewHolder, position: Int) {
val niceData = messages[position]
holder.bind(niceData)
}

override fun getItemCount(): Int {
return messages.size
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.bignerdranch.android.listr.nice

data class NiceData(val message: String)
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import com.bignerdranch.android.listr.databinding.FragementNiceBinding
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.ktx.Firebase
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

private const val TAG = "NiceFragment"

Expand All @@ -21,6 +29,29 @@ class NiceFragment: Fragment() {

private val db = Firebase.firestore

private val randomNiceMessages = listOf(
"You're amazing!",
"Keep shining bright!",
"You can do it!",
"Have a fantastic day!",
"Believe in yourself!",
"You're making a difference!",
"You're so kind!",
"You're a star!",
"Your smile lights up the room!",
"You're doing great!",
"Keep up the awesome work!",
"You're so thoughtful!",
"You're an inspiration!",
"Way to go!",
"You're unstoppable!",
"You're so talented!",
"You bring joy to others!",
"You're a wonderful person!",
"Keep spreading positivity!",
"You make the world a better place!"
)

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand All @@ -33,9 +64,45 @@ class NiceFragment: Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

binding.niceRecyclerView.layoutManager = LinearLayoutManager(context)

viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
getNiceMsgs().collect { messages ->
binding.niceRecyclerView.adapter = NiceAdapter(messages)

}
}
}

binding.addNiceMessage.setOnClickListener() {
addMsg()
}

binding.randomMessageButton.setOnClickListener {
addRandomMsg()
}
}

private fun getNiceMsgs(): StateFlow<List<NiceData>> {
val listFlow = MutableStateFlow(emptyList<NiceData>())

db.collection("nice")
.addSnapshotListener { snapshot, e ->
if (e != null) {
return@addSnapshotListener
}

if (snapshot != null && snapshot.documents != null) {
listFlow.value = snapshot.documents.map { doc ->
NiceData(doc["message"] as String)
}
} else {
Log.d(TAG, "Current Data: null")
}
}

return listFlow
}

private fun addMsg() {
Expand All @@ -53,6 +120,20 @@ class NiceFragment: Fragment() {
}
}

private fun addRandomMsg() {
val randomMessage = randomNiceMessages.random()
val msg = hashMapOf("message" to randomMessage)

db.collection("nice")
.add(msg)
.addOnSuccessListener { documentReference ->
Log.d(TAG, "Random message added with ID: ${documentReference.id}")
}
.addOnFailureListener { e ->
Log.w(TAG, "Error adding random message", e)
}
}

override fun onDestroy() {
super.onDestroy()
_binding = null
Expand Down
32 changes: 29 additions & 3 deletions app/src/main/res/layout/fragement_nice.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
android:id="@+id/nice_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="enter nice message"
android:hint="Enter a nice message"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.518"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.147" />

<Button
android:id="@+id/add_nice_message"
Expand All @@ -23,8 +25,32 @@
android:text="Add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.51"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.26" />

<Button
android:id="@+id/random_message_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Random Message"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/nice_message"
app:layout_constraintVertical_bias="0.176" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/nice_recycler_view"
android:layout_width="409dp"
android:layout_height="447dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.614" />
app:layout_constraintVertical_bias="1.0" />

</androidx.constraintlayout.widget.ConstraintLayout>
Loading

0 comments on commit 4b55bd8

Please sign in to comment.