Skip to content

Commit

Permalink
Remove Presenters in favor of viewmodels
Browse files Browse the repository at this point in the history
  • Loading branch information
yousuf-haque committed Dec 5, 2016
1 parent d017468 commit ecc1a67
Show file tree
Hide file tree
Showing 26 changed files with 281 additions and 280 deletions.
15 changes: 15 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,24 @@ dependencies {
compile 'com.roughike:bottom-bar:1.3.5'

compile 'io.reactivex:rxjava:1.0.10'
compile 'com.jakewharton.rxbinding:rxbinding-kotlin:0.4.0'
compile 'com.jakewharton.rxbinding:rxbinding-appcompat-v7-kotlin:0.4.0'
compile 'com.jakewharton.rxbinding:rxbinding-design-kotlin:0.4.0'
compile 'com.jakewharton.rxrelay:rxrelay:1.2.0'

kapt 'com.android.databinding:compiler:1.0-rc4'

compile 'com.trello:rxlifecycle:1.0'

// If you want to bind to Android-specific lifecycles
compile 'com.trello:rxlifecycle-android:1.0'

// If you want pre-written Activities and Fragments you can subclass as providers
compile 'com.trello:rxlifecycle-components:1.0'

// If you want to use Kotlin syntax
compile 'com.trello:rxlifecycle-kotlin:1.0'


}

Expand Down

This file was deleted.

16 changes: 0 additions & 16 deletions app/src/main/kotlin/com/yohaq/titan/presenters/base/Presenter.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import kotlinx.android.synthetic.main.activity_create_exercise.*
import javax.inject.Inject


class CreateExerciseActivity : AppCompatActivity(), CreateExerciseView {
class CreateExerciseActivity : AppCompatActivity() {

companion object {

Expand All @@ -19,25 +19,23 @@ class CreateExerciseActivity : AppCompatActivity(), CreateExerciseView {
}

@Inject
lateinit var presenter: CreateExercisePresenter
lateinit var viewModel: CreateExerciseViewModel

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_create_exercise)

DaggerCreateExerciseComponent.create().inject(this)
presenter.attachView(this)

create_exercise_submit_button.setOnClickListener { handleSubmit() }
}

private fun handleSubmit() {
presenter.createExercise(exercise_name_input.text.toString())
viewModel.createExercise(exercise_name_input.text.toString())
finish()
}

override fun onDestroy() {
super.onDestroy()
presenter.detachView()
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.yohaq.titan.ui.createExerciseScreen

import com.jakewharton.rxrelay.PublishRelay
import com.yohaq.titan.data.interactors.CreateExerciseInteractor
import rx.Observable
import rx.functions.Action1
import javax.inject.Inject

/**
* Created by yousufhaque on 6/8/16.
*/
class CreateExerciseViewModel
@Inject
constructor(private val createExerciseInteractor: CreateExerciseInteractor) {

val onNameUpdate : Action1<CharSequence>
val onCreateExercise : Action1<Unit>

val nameObservable : Observable<CharSequence>
val createExerciseObservable : Observable<Unit>

init {
val nameRelay = PublishRelay.create<CharSequence>()
val createExerciseRelay = PublishRelay.create<Unit>()

onNameUpdate = nameRelay.asAction()
onCreateExercise = createExerciseRelay.asAction()

nameObservable = nameRelay.asObservable()
createExerciseObservable = createExerciseRelay.asObservable()
}


fun createExercise(name: String) {
createExerciseInteractor.createExercise(name)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.trello.rxlifecycle.kotlin.bindToLifecycle
import com.yohaq.titan.R
import com.yohaq.titan.data.models.Exercise
import com.yohaq.titan.data.models.WorkoutSet
import com.yohaq.titan.databinding.ExerciseItemBinding
import com.yohaq.titan.databinding.WorkoutSetItemBinding
import com.yohaq.titan.ui.mainScreen.exerciseCatalog.ExerciseCatalogPresenter
import com.yohaq.titan.ui.mainScreen.exerciseCatalog.ExerciseCatalogView
import com.yohaq.titan.ui.mainScreen.exerciseCatalog.ExerciseCatalogViewModel
import com.yohaq.titan.ui.mainScreen.exerciseCatalog.adapters.ExerciseViewModel
import com.yohaq.titan.ui.mainScreen.exerciseCatalog.adapters.ExercisesAdapter
import kotlinx.android.synthetic.main.activity_create_workout.*
import java.util.*
import javax.inject.Inject

class CreateWorkoutActivity : AppCompatActivity(), CreateWorkoutView, ExerciseCatalogView {
class CreateWorkoutActivity : AppCompatActivity() {

//TODO Implement state retention of added sets on config change

Expand All @@ -32,10 +32,10 @@ class CreateWorkoutActivity : AppCompatActivity(), CreateWorkoutView, ExerciseCa
}

@Inject
lateinit var createWorkoutPresenter: CreateWorkoutPresenter
lateinit var createWorkoutViewModel: CreateWorkoutViewModel

@Inject
lateinit var exerciseCatalogPresenter: ExerciseCatalogPresenter
lateinit var exerciseCatalogViewModel: ExerciseCatalogViewModel


@Inject
Expand All @@ -54,6 +54,7 @@ class CreateWorkoutActivity : AppCompatActivity(), CreateWorkoutView, ExerciseCa
choose_exercise_list.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
exercisesAdapter.onExerciseClick = { exercise -> handleExerciseSelection(exercise) }
choose_exercise_list.adapter = exercisesAdapter
exercisesAdapter.bindObservable(exerciseCatalogViewModel.exercisesObservable).bindToLifecycle(choose_exercise_list).subscribe()


selected_exercise.setOnClickListener { handleExerciseDeselection() }
Expand All @@ -65,22 +66,18 @@ class CreateWorkoutActivity : AppCompatActivity(), CreateWorkoutView, ExerciseCa
handleExerciseDeselection()


createWorkoutPresenter.attachView(this)
exerciseCatalogPresenter.attachView(this)
}

override fun onDestroy() {
super.onDestroy()
createWorkoutPresenter.detachView()
exerciseCatalogPresenter.detachView()
}


private fun handleAddSetButtonClick() {
var newSet = WorkoutSet(
val newSet = WorkoutSet(
weight = input_weight.text.toString().toFloat(),
reps = input_reps.text.toString().toInt())
var workoutSetItemBinding = DataBindingUtil.inflate<WorkoutSetItemBinding>(layoutInflater, R.layout.workout_set_item, workout_set_list, true)
val workoutSetItemBinding = DataBindingUtil.inflate<WorkoutSetItemBinding>(layoutInflater, R.layout.workout_set_item, workout_set_list, true)

//Seems like an IDE bug: https://youtrack.jetbrains.com/issue/KT-12402
@Suppress("MISSING_DEPENDENCY_CLASS")
Expand All @@ -98,13 +95,10 @@ class CreateWorkoutActivity : AppCompatActivity(), CreateWorkoutView, ExerciseCa

private fun handleAddExerciseButtonClick() {

createWorkoutPresenter.createWorkout(Date(), selectedExercise!!, sets)
createWorkoutViewModel.createWorkout(Date(), selectedExercise!!, sets)
finish()
}

override fun showExercises(exercises: List<Exercise>) {
exercisesAdapter.updateExercises(exercises)
}

private fun handleExerciseSelection(exercise: Exercise) {

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.yohaq.titan.ui.createWorkoutScreen

import com.jakewharton.rxrelay.PublishRelay
import com.yohaq.titan.data.interactors.CreateWorkoutInteractor
import com.yohaq.titan.data.models.Exercise
import com.yohaq.titan.data.models.WorkoutSet
import rx.Observable
import rx.functions.Action1
import java.util.*
import javax.inject.Inject

/**
* Created by yousufhaque on 6/8/16.
*/
class CreateWorkoutViewModel
@Inject constructor(private val createWorkoutInteractor: CreateWorkoutInteractor) {

val onDateUpdate : Action1<Date>
val onExerciseUpdate : Action1<Exercise>
val onSetListUpdate: Action1<List<WorkoutSet>>
val onCreateWorkout: Action1<Unit>

private val dateObservable : Observable<Date>
private val exerciseObservable : Observable<Exercise>
private val setListObservable : Observable<List<WorkoutSet>>
private val createWorkoutObservable : Observable<Unit>

init {
val dateRelay = PublishRelay.create<Date>()
val exerciseRelay = PublishRelay.create<Exercise>()
val setListRelay = PublishRelay.create<List<WorkoutSet>>()
val createWorkoutRelay = PublishRelay.create<Unit>()

onDateUpdate = dateRelay.asAction()
onExerciseUpdate = exerciseRelay.asAction()
onSetListUpdate = setListRelay.asAction()
onCreateWorkout = createWorkoutRelay.asAction()


dateObservable = dateRelay.asObservable()
exerciseObservable = exerciseRelay.asObservable()
setListObservable = setListRelay.asObservable()
createWorkoutObservable = createWorkoutRelay.asObservable()

}


fun createWorkout(dateCreated: Date, exercise: Exercise, sets: List<WorkoutSet>) {
createWorkoutInteractor.createWorkout(dateCreated, exercise, sets)
}
}
Loading

0 comments on commit ecc1a67

Please sign in to comment.