Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
apikyanrobert committed Aug 28, 2018
1 parent bfdfaf3 commit d451a31
Show file tree
Hide file tree
Showing 32 changed files with 873 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
*.iml
.gradle
/local.properties
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
.DS_Store
/build
/captures
.externalNativeBuild
Binary file added .idea/caches/build_file_checksums.ser
Binary file not shown.
29 changes: 29 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/markdown-navigator/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions abstractMvp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
35 changes: 35 additions & 0 deletions abstractMvp/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 28



defaultConfig {
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

repositories {
mavenCentral()
}
21 changes: 21 additions & 0 deletions abstractMvp/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package robertapikyan.com.abstractmvp;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();

assertEquals("robertapikyan.com.abstractmvp.test", appContext.getPackageName());
}
}
1 change: 1 addition & 0 deletions abstractMvp/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest package="robertapikyan.com.abstractmvp" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.robertapikyan.abstractmvp.presentation

import com.robertapikyan.abstractmvp.presentation.presenter.IPresenterHolder
import com.robertapikyan.abstractmvp.presentation.presenter.IPresenterLifecycleHandler
import com.robertapikyan.abstractmvp.presentation.presenter.Presenter
import com.robertapikyan.abstractmvp.presentation.presenter.PresenterProxy
import com.robertapikyan.abstractmvp.presentation.view.IView
import com.robertapikyan.abstractmvp.presentation.view.IViewActionDispatcher
import com.robertapikyan.abstractmvp.presentation.view.IViewActionObserver
import com.robertapikyan.abstractmvp.presentation.view.ViewHolder

class Mvp {

/**
* One of the important interface for MVP library.
* This is the starting point for MVP implementation
* Inherit from this Factory, and provide your custom implementations
* for IViewActionDispatcher, IViewActionObserver, IPresenterHolder, IPresenterLifecycleHandler ...
*/
interface Factory<V : IView, P : Presenter<V>> {
fun getView(): V
fun getViewActionDispatcher(): IViewActionDispatcher<V>
fun getViewActionObserver(): IViewActionObserver<V>
fun getPresenter(): P
fun getPresenterHolder(): IPresenterHolder<V, P>
fun getPresenterLifecycleHandler(): IPresenterLifecycleHandler
}

companion object {
/**
* Use this method in order to receive your presenter instance
* based on you Factory implementation
*/
fun <V : IView, P : Presenter<V>> from(factory: Factory<V, P>): P {
// View
val view = factory.getView()
val viewHolder = ViewHolder<V>()

// Presenter
val presenter by lazy { factory.getPresenter() }
val presenterHolder = factory.getPresenterHolder()
val presenterProxy by lazy { PresenterProxy(presenterHolder.get()) }
val presenterLifecycleHandler by lazy { factory.getPresenterLifecycleHandler() }

// Action dispatcher and observable
val viewActionObserver = factory.getViewActionObserver()
val viewActionDispatcher by lazy { factory.getViewActionDispatcher() }

// holds state whatever presenter is created or restored
val isPresenterCreated = !presenterHolder.hasPresenter()

// if holder is empty, we create new presenter instance
if (!presenterHolder.hasPresenter()) {
presenterHolder.put(presenter)
}

// set the viewHolder instance,
// in order to clear it when view scope is going to be destroyed
presenterProxy.viewHolder = viewHolder

viewHolder.putView(view)

// pass the presenterProxy instance as PresenterLifecycle
presenterLifecycleHandler.onCreate(presenterProxy)

viewActionObserver.onCreate(viewHolder)

// now if presenter is created we set viewActionObserver instance to viewActionDispatcher,
// pass the viewActionDispatcher instance to presenter,
// and call presenter.onCreate(). At this point presenter is ready
if (isPresenterCreated) {
viewActionDispatcher.setViewActionObserver(viewHolder, viewActionObserver)
presenter._viewActionDispatcher = viewActionDispatcher
presenter.onCreate()
} else {
// if presenter instance is restored, we just set viewActionObserver to viewActionDispatcher,
// and call presenter.onRestore()
presenterHolder.get()
._viewActionDispatcher
.setViewActionObserver(viewHolder, viewActionObserver)
presenterHolder.get().onRestore()
}

// return the presenter instance
return presenterHolder.get()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.robertapikyan.abstractmvp.presentation.presenter

import com.robertapikyan.abstractmvp.presentation.view.IView

/**
* IPresenterHolder represent's holder container for Presenter instance,
* It receive's presenter instance via put() method, and provide's the same instance
* via get() method
* The main point of this class is to inherit from android lifecycle persistence objects such as
* Loaders and ViewModels and every time provide the same presenter instance
*/
interface IPresenterHolder<V : IView, P : Presenter<V>> {
fun hasPresenter(): Boolean
fun put(presenter: P)
fun get(): P
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.robertapikyan.abstractmvp.presentation.presenter

/**
* IPresenterLifecycle is Presenter lifecycle representation
*/
interface IPresenterLifecycle {

/**
* onViewAttach, will be called with activity setViewActionObserver
*/
fun onViewAttach()

/**
* onViewStop, will be called with activity onStart
*/
fun onViewStart()

/**
* onViewStop, will be called with activity onStop
*/
fun onViewStop()

/**
* onViewDetach, will be called with activity onDestroy
*/
fun onViewDetach()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.robertapikyan.abstractmvp.presentation.presenter

/**
* IPresenterLifecycleHandler is responsible for presenter lifecycle handling
* Receive presenter lifecycle instance via setViewActionObserver method,
* All lifecycle methods will be called by PresenterLifecycleHandler except Lifecycle::setViewActionObserver method,
* this method will be called by MVP framework
*/
interface IPresenterLifecycleHandler {
fun onCreate(presenterLifecycle: IPresenterLifecycle)
}
Loading

0 comments on commit d451a31

Please sign in to comment.