Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BaseActivity create and apply #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions presentation/src/main/kotlin/com/andremion/theatre/BaseActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.andremion.theatre

import android.databinding.DataBindingUtil
import android.databinding.ViewDataBinding
import android.os.Bundle
import dagger.android.support.DaggerAppCompatActivity

abstract class BaseActivity<T : ViewDataBinding> : DaggerAppCompatActivity() {
lateinit var viewDataBinding: T
abstract val layoutResourceId: Int
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewDataBinding = DataBindingUtil.setContentView(this, layoutResourceId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,27 @@ package com.andremion.theatre.event.detail
import android.arch.lifecycle.ViewModelProvider
import android.arch.lifecycle.ViewModelProviders
import android.content.Intent
import android.databinding.DataBindingUtil
import android.databinding.Observable
import android.os.Bundle
import android.view.Menu
import android.view.ViewGroup
import androidx.view.doOnPreDraw
import com.andremion.theatre.BaseActivity
import com.andremion.theatre.R
import com.andremion.theatre.databinding.ActivityEventBinding
import com.andremion.theatre.event.detail.rating.EventRatingViewModel
import com.andremion.theatre.internal.util.lazyThreadSafetyNone
import com.andremion.theatre.navigation.Navigator
import dagger.android.support.DaggerAppCompatActivity
import javax.inject.Inject

class EventActivity : DaggerAppCompatActivity() {

class EventActivity : BaseActivity<ActivityEventBinding>() {
override val layoutResourceId: Int
get() = R.layout.activity_event
@Inject
lateinit var viewModelFactory: ViewModelProvider.Factory
@Inject
lateinit var navigator: Navigator

private val binder by lazyThreadSafetyNone<ActivityEventBinding> {
DataBindingUtil.setContentView(this, R.layout.activity_event)
}

private val eventDetailViewModel by lazyThreadSafetyNone {
ViewModelProviders.of(this, viewModelFactory).get(EventDetailViewModel::class.java)
}
Expand All @@ -57,11 +53,11 @@ class EventActivity : DaggerAppCompatActivity() {

supportPostponeEnterTransition()

setSupportActionBar(binder.toolbar)
setSupportActionBar(viewDataBinding.toolbar)
supportActionBar!!.setDisplayHomeAsUpEnabled(true)

binder.eventDetailViewModel = eventDetailViewModel
binder.eventRatingViewModel = eventRatingViewModel
viewDataBinding.eventDetailViewModel = eventDetailViewModel
viewDataBinding.eventRatingViewModel = eventRatingViewModel

val event = navigator.getEvent(this)
eventDetailViewModel.loadEventDetail(event)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,27 @@ package com.andremion.theatre.home

import android.arch.lifecycle.ViewModelProvider
import android.arch.lifecycle.ViewModelProviders
import android.databinding.DataBindingUtil
import android.os.Bundle
import android.support.design.widget.Snackbar
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.View.OnClickListener
import com.andremion.theatre.BaseActivity
import com.andremion.theatre.R
import com.andremion.theatre.databinding.ActivityHomeBinding
import com.andremion.theatre.event.type.EventTypeViewModel
import com.andremion.theatre.internal.util.lazyThreadSafetyNone
import dagger.android.support.DaggerAppCompatActivity
import javax.inject.Inject

class HomeActivity : DaggerAppCompatActivity(), OnClickListener {
class HomeActivity : BaseActivity<ActivityHomeBinding>(), OnClickListener {

override val layoutResourceId: Int
get() = R.layout.activity_home

@Inject
lateinit var viewModelFactory: ViewModelProvider.Factory

private val binder by lazyThreadSafetyNone<ActivityHomeBinding> {
DataBindingUtil.setContentView(this, R.layout.activity_home)
}

private val viewModel by lazyThreadSafetyNone {
ViewModelProviders.of(this, viewModelFactory).get(EventTypeViewModel::class.java)
Expand All @@ -48,10 +47,10 @@ class HomeActivity : DaggerAppCompatActivity(), OnClickListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setSupportActionBar(binder.toolbar)
setSupportActionBar(viewDataBinding.toolbar)

binder.viewModel = viewModel
binder.fabClick = this
viewDataBinding.viewModel = viewModel
viewDataBinding.fabClick = this

viewModel.loadEventTypeList()
}
Expand All @@ -69,6 +68,6 @@ class HomeActivity : DaggerAppCompatActivity(), OnClickListener {
}

override fun onClick(v: View?) {
Snackbar.make(binder.root, "Replace with your own action", Snackbar.LENGTH_LONG).show()
Snackbar.make(viewDataBinding.root, "Replace with your own action", Snackbar.LENGTH_LONG).show()
}
}