override fun onCreate()

in SleepSampleKotlin/app/src/main/java/com/android/example/sleepsamplekotlin/MainActivity.kt [71:116]


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        mainViewModel.subscribedToSleepDataLiveData.observe(this) { newSubscribedToSleepData ->
            if (subscribedToSleepData != newSubscribedToSleepData) {
                subscribedToSleepData = newSubscribedToSleepData
            }
        }

        // Adds observers on LiveData from [SleepRepository]. Data is saved to the database via
        // [SleepReceiver] and when that data changes, we get notified of changes.
        // Note: The data returned is Entity versions of the sleep classes, so they don't contain
        // all the data, as I just saved the minimum to show it's being saved.
        mainViewModel.allSleepSegments.observe(this) { sleepSegmentEventEntities ->
            Log.d(TAG, "sleepSegmentEventEntities: $sleepSegmentEventEntities")

            if (sleepSegmentEventEntities.isNotEmpty()) {
                // Constructor isn't accessible for [SleepSegmentEvent], so we just output the
                // database table version.
                sleepSegmentOutput = sleepSegmentEventEntities.joinToString {
                    "\t$it\n"
                }
                updateOutput()
            }
        }

        mainViewModel.allSleepClassifyEventEntities.observe(this) {
                sleepClassifyEventEntities ->
            Log.d(TAG, "sleepClassifyEventEntities: $sleepClassifyEventEntities")

            if (sleepClassifyEventEntities.isNotEmpty()) {
                // Constructor isn't accessible for [SleepClassifyEvent], so we just output the
                // database table version.
                sleepClassifyOutput = sleepClassifyEventEntities.joinToString {
                    "\t$it\n"
                }
                updateOutput()
            }
        }

        sleepPendingIntent =
            SleepReceiver.createSleepReceiverPendingIntent(context = applicationContext)
    }