Skip to content

Commit

Permalink
working on #8
Browse files Browse the repository at this point in the history
implementing MVP with dagger and koting
its too hard men
testing CI please squash or ammend
  • Loading branch information
mercuriete committed Nov 14, 2018
1 parent c99dd35 commit 78c2eb0
Show file tree
Hide file tree
Showing 16 changed files with 115 additions and 39 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
android:theme="@style/AppTheme"
android:name="org.mercuriete.musiciantools.MusicianToolsApp">
<activity
android:name=".services.MenuActivity"
android:name=".bpmcalculator.BPMCalculatorActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/java/org/mercuriete/musiciantools/BasePresenter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.mercuriete.musiciantools

interface BasePresenter<T> {

/**
* Binds presenter with a view when resumed. The Presenter will perform initialization here.
*
* @param view the view associated with this presenter
*/
fun takeView(view: T)

/**
* Drops the reference to the view when destroyed
*/
fun dropView()

}
5 changes: 5 additions & 0 deletions app/src/main/java/org/mercuriete/musiciantools/BaseView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.mercuriete.musiciantools

interface BaseView<T> {

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.mercuriete.musiciantools.services
package org.mercuriete.musiciantools.bpmcalculator

import android.os.Bundle
import dagger.android.support.DaggerAppCompatActivity
Expand All @@ -7,18 +7,25 @@ import kotlinx.android.synthetic.main.content_menu.*
import org.mercuriete.musiciantools.R
import javax.inject.Inject

class MenuActivity : DaggerAppCompatActivity() {

class BPMCalculatorActivity : DaggerAppCompatActivity(), BPMCalculatorContract.View {

@Inject
lateinit var bpmCalculatorService: BPMCalculatorService
lateinit var presenter: BPMCalculatorContract.Presenter

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_menu)
setSupportActionBar(toolbar)
presenter.takeView(this)

button.setOnClickListener { _ ->
bpmCalculatorService.beat()
bpmText.text = String.format("%.2f", bpmCalculatorService.getBPM())
presenter.addBeat()
}
}

override fun showBPM(bpm: String) {
bpmText.text = bpm
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.mercuriete.musiciantools.bpmcalculator

import org.mercuriete.musiciantools.BasePresenter
import org.mercuriete.musiciantools.BaseView


interface BPMCalculatorContract {

interface View : BaseView<Presenter> {
fun showBPM(bpm:String)
}

interface Presenter : BasePresenter<View> {
fun addBeat()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.mercuriete.musiciantools.bpmcalculator

import dagger.Provides
import dagger.Module
import javax.inject.Singleton

@Module
class BPMCalculatorModule {

@Provides
fun bpmCalculatorServiceProvider(): BPMCalculatorService {
return BPMCalculatorServiceImpl()
}

@Singleton
@Provides
fun bpmCalculatorPresenterProvider(bpmCalculatorService: BPMCalculatorService): BPMCalculatorContract.Presenter {
return BPMCalculatorPresenter(bpmCalculatorService)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.mercuriete.musiciantools.bpmcalculator

import android.support.annotation.Nullable
import javax.inject.Inject

class BPMCalculatorPresenter : BPMCalculatorContract.Presenter {
@Nullable
private var bpmCalculatorView: BPMCalculatorContract.View? = null

private var bpmCalculatorService: BPMCalculatorService

@Inject
constructor(bpmCalculatorService: BPMCalculatorService) {
this.bpmCalculatorService = bpmCalculatorService
}

override fun addBeat() {
bpmCalculatorService.beat()
bpmCalculatorView?.showBPM(String.format("%.2f", bpmCalculatorService.getBPM()))

}

override fun takeView(view: BPMCalculatorContract.View) {
bpmCalculatorView = view
}

override fun dropView() {
bpmCalculatorView = null
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.mercuriete.musiciantools.services
package org.mercuriete.musiciantools.bpmcalculator

interface BPMCalculatorService {
fun beat()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.mercuriete.musiciantools.services
package org.mercuriete.musiciantools.bpmcalculator

class BPMCalculatorServiceImpl : BPMCalculatorService {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.mercuriete.musiciantools.di

import dagger.Module
import dagger.android.ContributesAndroidInjector
import org.mercuriete.musiciantools.services.MenuActivity
import org.mercuriete.musiciantools.bpmcalculator.BPMCalculatorActivity


/**
Expand All @@ -16,5 +16,5 @@ import org.mercuriete.musiciantools.services.MenuActivity
abstract class ActivityBindingModule {
@ActivityScoped
@ContributesAndroidInjector(modules = [ApplicationModule::class])
internal abstract fun menuActivity(): MenuActivity
internal abstract fun menuActivity(): BPMCalculatorActivity
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package org.mercuriete.musiciantools.di

import org.mercuriete.musiciantools.di.AppComponent

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import javax.inject.Scope;
import java.lang.annotation.Documented
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
import javax.inject.Scope


/**
Expand All @@ -18,4 +15,4 @@ import javax.inject.Scope;
@Documented
@Scope
@Retention(RetentionPolicy.RUNTIME)
annotation class ActivityScoped
annotation class ActivityScoped
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import dagger.android.AndroidInjector
import dagger.android.support.AndroidSupportInjectionModule
import javax.inject.Singleton
import org.mercuriete.musiciantools.MusicianToolsApp
import org.mercuriete.musiciantools.services.BPMCalculatorModule
import org.mercuriete.musiciantools.bpmcalculator.BPMCalculatorModule

/**
* This is a Dagger component. Refer to [MusicianToolsApp] for the list of Dagger components
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".services.MenuActivity">
tools:context=".bpmcalculator.BPMCalculatorActivity">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/content_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".services.MenuActivity"
tools:context=".bpmcalculator.BPMCalculatorActivity"
tools:showIn="@layout/activity_menu">

<TextView
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.mercuriete.musiciantools.services
package org.mercuriete.musiciantools.bpmcalculator

import org.junit.Assert
import org.junit.Test
Expand Down

0 comments on commit 78c2eb0

Please sign in to comment.