MainActivity

mail@pastecode.io avatar
unknown
kotlin
2 years ago
3.9 kB
1
Indexable
Never
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private lateinit var mainViewModel: MainViewModel
    private lateinit var storiesAdapter: StoryListAdapter
    lateinit var tokenInfo : String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        setupViewModel()

        binding.apply {
            storiesAdapter = StoryListAdapter()
            rvStories.layoutManager = LinearLayoutManager(this@MainActivity)
            rvStories.adapter = storiesAdapter
        }

        mainViewModel.getUser().observe(this) { user ->
            if (user.isLogin) {
                /*showLoading(true)*/
                tokenInfo = user.token
                getData(user.token)
                /*mainViewModel.getAllStories(user.token)*/
            }
        }

        /*mainViewModel.listStories.observe(this){
            if (it != null) {
                *//*storiesAdapter.loadListUser(it)*//*
                *//*showLoading(false)*//*
            }
        }*/

        mainViewModel.responseMessage.observe(this){
            Toast.makeText(this@MainActivity, it, Toast.LENGTH_SHORT).show()
        }

        /*storiesAdapter.setOnItemClickCallback(object : StoriesAdapter.OnItemClickCallback {
            override fun onItemClicked(data: ListStoryItem) {
                val dataDetail = StoriesDetail(
                    data.name.toString(),
                    data.photoUrl.toString(),
                    data.description.toString(),
                    data.createdAt.toString()
                )

                val moveDetail = Intent(this@MainActivity, DetailStoriesActivity::class.java)
                moveDetail.putExtra(keyDetail, dataDetail)

                startActivity(moveDetail)
            }
        })*/

        binding.fabAdd.setOnClickListener {
            val moveAddStory = Intent(this@MainActivity, AddStoryActivity::class.java)
            moveAddStory.putExtra(token, tokenInfo)
            startActivity(moveAddStory)
        }

    }

    private fun setupViewModel() {
        mainViewModel = ViewModelProvider(
            this,
            ViewModelFactory(UserPreference.getInstance(dataStore))
        )[MainViewModel::class.java]

    }

    private fun showLoading(isLoading: Boolean) {
        binding.progressBar.visibility = if (isLoading) View.VISIBLE else View.GONE
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        val inflater = menuInflater
        inflater.inflate(R.menu.option_menu, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.menu1 -> {
                mainViewModel.logout()
                startActivity(Intent(this, LoginActivity::class.java))
                finish()
                return true
            }
            R.id.menu2 -> {
                startActivity(Intent(Settings.ACTION_LOCALE_SETTINGS))
                return true
            }
            R.id.menu3 -> {
                val moveMaps = Intent(this@MainActivity, MapsActivity::class.java)
                moveMaps.putExtra(token, tokenInfo)
                startActivity(moveMaps)
                return true
            }
            else -> return true
        }
    }

    companion object {
        const val keyDetail = "extra_info"
        const val token = "token_info"
    }

    private fun getData(tokenId: String) {
        mainViewModel.getStory(tokenId).observe(this) {
            storiesAdapter.submitData(lifecycle, it)
        }
    }

}