MainActivity.kt 1.61 KB
Newer Older
1 2 3 4
package fr.chenry.android.freshrss.activities

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
5 6 7 8
import androidx.navigation.NavController
import androidx.navigation.Navigation
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
9 10
import fr.chenry.android.freshrss.*
import fr.chenry.android.freshrss.utils.*
11 12
import nl.komponents.kovenant.deferred
import nl.komponents.kovenant.resolve
13 14 15
import nl.komponents.kovenant.ui.failUi
import nl.komponents.kovenant.ui.successUi

16

17
class MainActivity: AppCompatActivity() {
18
    private val deferred = deferred<Unit, Exception>()
19 20 21 22
    private val navigation: NavController by lazy {
        Navigation.findNavController(this, R.id.main_activity_host_fragment)
    }
    private val appBarConfiguration by lazy {AppBarConfiguration(navigation.graph)}
23 24

    init {
25 26 27 28 29
        FreshRSSApplication.application.refresherService.whenNotNull {
            it.refresh().successUi {deferred.resolve()}.failUi(deferred::reject)
        }.whenNull {
            deferred.reject(Exception("${FreshRSSApplication::class.qualifiedName}: Service ${RefresherService::class.qualifiedName} not bound"))
        }
30 31 32 33 34
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
35
        setupActionBarWithNavController(navigation, appBarConfiguration)
36 37 38 39
    }

    override fun onStart() {
        super.onStart()
40
        deferred.promise failUi {this.e(it)}
41 42
    }

43
    override fun onSupportNavigateUp() = navigation.navigateUp() || super.onSupportNavigateUp()
44
}