Personalised notes for Tossit App

Photo by Andrew Neel on Unsplash

Personalised notes for Tossit App

    • ViewModel in Android with Kotlin DSL:

      • Define a ViewModel class:

         kotlinCopy code// SharedViewModel.kt
         package com.example.tossit
        
         import androidx.lifecycle.ViewModel
        
         class SharedViewModel : ViewModel() {
             var inputValue: Int = 0
         }
        
        • Use ViewModelProvider to obtain an instance in a Fragment:
        kotlinCopy code// YourFragment.kt
        viewModel = ViewModelProvider(requireActivity()).get(SharedViewModel::class.java)

2 -> Generating Distinct Random Numbers in Kotlin:

  • Use the Fisher-Yates shuffle algorithm to generate distinct random numbers:

      kotlinCopy code// ResultMultiPlayerFragment.kt
      private fun generateDistinctRandomNumbers(maxValue: Int): List<Int> {
          val numbers = (1..maxValue).toMutableList()
          for (i in numbers.size - 1 downTo 1) {
              val j = Random.Default.nextInt(i + 1)
              val temp = numbers[i]
              numbers[i] = numbers[j]
              numbers[j] = temp
          }
          return numbers
      }
    

Certainly! Let's go through each point with corresponding code snippets:

  1. ViewModel in Android with Kotlin DSL:

    • Define a ViewModel class:

        kotlinCopy code// SharedViewModel.kt
        package com.example.tossit
      
        import androidx.lifecycle.ViewModel
      
        class SharedViewModel : ViewModel() {
            var inputValue: Int = 0
        }
      
      • Use ViewModelProvider to obtain an instance in a Fragment:
                    kotlinCopy code// YourFragment.kt
                    viewModel = ViewModelProvider(requireActivity()).get(SharedViewModel::class.java)
  1. Fragment Communication and Navigation in Android:

    • Navigate between fragments using the Navigation component:

        kotlinCopy code// YourFragment.kt
        nextButton.setOnClickListener { view : View ->
            Navigation.findNavController(view).navigate(R.id.action_MultiPlayerFragment_to_resultMultiPlayerFragment)
        }
      
  2. Generating Distinct Random Numbers in Kotlin:

    • Use the Fisher-Yates shuffle algorithm to generate distinct random numbers:

        kotlinCopy code// ResultMultiPlayerFragment.kt
        private fun generateDistinctRandomNumbers(maxValue: Int): List<Int> {
            val numbers = (1..maxValue).toMutableList()
            for (i in numbers.size - 1 downTo 1) {
                val j = Random.Default.nextInt(i + 1)
                val temp = numbers[i]
                numbers[i] = numbers[j]
                numbers[j] = temp
            }
            return numbers
        }
      
  3. Using Random.Default in Kotlin:

    • Generate a random integer using Random.Default.nextInt():

        kotlinCopy code// ResultMultiPlayerFragment.kt
        val randomInt = Random.Default.nextInt(1, 3)
      

      5 -> Android Toast Messages:

      • Display a Toast message:

          kotlinCopy code// YourFragment.kt
          Toast.makeText(requireContext(), "Coin Tossed", Toast.LENGTH_SHORT).sho