Skip to content

Commit

Permalink
Merge pull request #67 from Wolox/view-binding
Browse files Browse the repository at this point in the history
View binding
  • Loading branch information
lmiotti authored Jan 4, 2021
2 parents 507bd11 + 67ff9ef commit f47bb5b
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 30 deletions.
9 changes: 6 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.61'
ext.kotlin_version = '1.3.72'
repositories {
google()
jcenter()
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath 'com.dicedmelon.gradle:jacoco-android:0.1.4'
classpath 'com.android.tools.build:gradle:4.0.2'
classpath 'com.hiya:jacoco-android:0.2'
classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.8.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
Expand All @@ -21,6 +22,8 @@ allprojects {
google()
jcenter()
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
maven { url "https://plugins.gradle.org/m2/" }

}
}

Expand Down
9 changes: 6 additions & 3 deletions core/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'jacoco-android'
apply plugin: 'com.hiya.jacoco-android'
apply plugin: 'com.github.kt3k.coveralls'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 29
buildToolsVersion '29.0.2'
buildToolsVersion '29.0.3'
testOptions.unitTests.includeAndroidResources = true

compileOptions {
Expand All @@ -32,6 +31,10 @@ android {
lintOptions {
abortOnError false
}

buildFeatures {
dataBinding true
}
}

jacocoAndroidUnitTestReport {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import android.view.MenuItem
import androidx.annotation.CallSuper
import androidx.annotation.IdRes
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
import ar.com.wolox.wolmo.core.fragment.IWolmoFragment
Expand All @@ -37,21 +39,23 @@ import javax.inject.Inject
/**
* A base [DaggerAppCompatActivity] that implements Wolmo's custom lifecycle.
*/
abstract class WolmoActivity : DaggerAppCompatActivity() {
abstract class WolmoActivity<V : ViewDataBinding> : DaggerAppCompatActivity() {

@Inject
lateinit var toastFactory: ToastFactory
@Inject
lateinit var permissionManager: PermissionManager

var binding: V? = null

/**
* Handles the custom lifecycle of Wolmo's Activity. It provides a set of callbacks to structure
* the different aspects of the Activities initialization.
*/
@CallSuper
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(layout())
binding = DataBindingUtil.setContentView(this, layout())
if (handleArguments(intent.extras) == true) {
init()
populate()
Expand All @@ -62,6 +66,11 @@ abstract class WolmoActivity : DaggerAppCompatActivity() {
}
}

override fun onDestroy() {
super.onDestroy()
binding = null
}

/**
* This method provides a way for populating the view with a layout defined in an XML resource
* by returning the ID of the layout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package ar.com.wolox.wolmo.core.di.modules

import android.util.SparseArray
import androidx.databinding.ViewDataBinding
import ar.com.wolox.wolmo.core.fragment.WolmoFragmentHandler
import ar.com.wolox.wolmo.core.permission.PermissionListener
import ar.com.wolox.wolmo.core.presenter.BasePresenter
Expand Down Expand Up @@ -50,7 +51,7 @@ class DefaultModule {
@Provides
fun providesDefaultWolmoFragmentHandler(toastFactory: ToastFactory,
logger: Logger,
basePresenter: BasePresenter<Any>): WolmoFragmentHandler<*> {
return WolmoFragmentHandler(toastFactory, logger, basePresenter)
basePresenter: BasePresenter<Any>): WolmoFragmentHandler<*,*> {
return WolmoFragmentHandler<ViewDataBinding, BasePresenter<Any>>(toastFactory, logger, basePresenter)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.databinding.ViewDataBinding;

import java.io.File;

Expand All @@ -45,7 +46,7 @@
* @deprecated use {@link ar.com.wolox.wolmo.core.util.GetImageHelper} instead.
*/
@Deprecated
public abstract class GetImageFragment<T extends BasePresenter<?>> extends WolmoFragment<T> {
public abstract class GetImageFragment<V extends ViewDataBinding, T extends BasePresenter<?>> extends WolmoFragment<V, T> {

private static final String[] CAMERA_PERMISSIONS = new String[] {
Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import androidx.annotation.CallSuper
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.FragmentManager
import ar.com.wolox.wolmo.core.permission.PermissionManager
import ar.com.wolox.wolmo.core.presenter.BasePresenter
Expand All @@ -48,21 +49,24 @@ import javax.inject.Inject
* inflating the view returned by [layout].
* The presenter is created on [onCreate] if [handleArguments] returns true.
*/
abstract class WolmoBottomSheetDialogFragment<P : BasePresenter<*>> : BottomSheetDialogFragment(),
abstract class WolmoBottomSheetDialogFragment<V : ViewDataBinding, P : BasePresenter<*>> : BottomSheetDialogFragment(),
IWolmoFragment {

@Inject
lateinit var permissionManager: PermissionManager

@Inject
lateinit var fragmentHandler: WolmoFragmentHandler<P>
lateinit var fragmentHandler: WolmoFragmentHandler<V, P>

/** Invoked when thee dialog is attached. */
var onAttached: (() -> Unit)? = null

val presenter: P
get() = fragmentHandler.presenter

protected val binding: V?
get() = fragmentHandler.binding

@CallSuper
override fun onAttach(context: Context) {
AndroidSupportInjection.inject(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import androidx.annotation.CallSuper
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.FragmentManager
import ar.com.wolox.wolmo.core.permission.PermissionManager
import ar.com.wolox.wolmo.core.presenter.BasePresenter
Expand All @@ -45,18 +46,21 @@ import javax.inject.Inject
* inflating the view returned by [layout].
* The presenter is created on [onCreate] if [handleArguments] returns true.
*/
abstract class WolmoDialogFragment<P : BasePresenter<*>> : DaggerAppCompatDialogFragment(),
abstract class WolmoDialogFragment<V : ViewDataBinding, P : BasePresenter<*>> : DaggerAppCompatDialogFragment(),
IWolmoFragment {

@Inject
lateinit var permissionManager: PermissionManager

@Inject
lateinit var fragmentHandler: WolmoFragmentHandler<P>
lateinit var fragmentHandler: WolmoFragmentHandler<V, P>

val presenter: P
get() = fragmentHandler.presenter

val binding: V?
get() = fragmentHandler.binding

override fun onCreateDialog(savedInstanceState: Bundle?) = super.onCreateDialog(savedInstanceState).apply {
window?.run {
requestFeature(Window.FEATURE_NO_TITLE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.CallSuper
import androidx.databinding.ViewDataBinding
import ar.com.wolox.wolmo.core.permission.PermissionManager
import ar.com.wolox.wolmo.core.presenter.BasePresenter
import dagger.android.support.DaggerFragment
Expand All @@ -36,10 +37,10 @@ import javax.inject.Inject
* by [layout]. The presenter is created on [onCreate] if [handleArguments] returns true.
* This class defines default implementations for most of the methods on [IWolmoFragment].
*/
abstract class WolmoFragment<P : BasePresenter<*>> : DaggerFragment(), IWolmoFragment {
abstract class WolmoFragment<V : ViewDataBinding, P : BasePresenter<*>> : DaggerFragment(), IWolmoFragment {

@Inject
lateinit var fragmentHandler: WolmoFragmentHandler<P>
lateinit var fragmentHandler: WolmoFragmentHandler<V, P>

@Inject
lateinit var permissionManager: PermissionManager
Expand All @@ -51,6 +52,9 @@ abstract class WolmoFragment<P : BasePresenter<*>> : DaggerFragment(), IWolmoFra
val presenter: P
get() = fragmentHandler.presenter

val binding: V?
get() = fragmentHandler.binding

@CallSuper
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;

import javax.inject.Inject;
Expand All @@ -41,7 +43,7 @@
* This class is used to separate Wolox Fragments logic so that different subclasses of
* Fragment can implement MVP without re-writing this.
*/
public final class WolmoFragmentHandler<T extends BasePresenter> {
public final class WolmoFragmentHandler<V extends ViewDataBinding, T extends BasePresenter> {

private Fragment mFragment;
private IWolmoFragment mWolmoFragment;
Expand All @@ -51,6 +53,7 @@ public final class WolmoFragmentHandler<T extends BasePresenter> {
private boolean mVisible;

private @NonNull T mPresenter;
private @Nullable V mBinding;
private ToastFactory mToastFactory;
private Logger mLogger;

Expand Down Expand Up @@ -105,7 +108,8 @@ void onCreate(@NonNull IWolmoFragment wolmoFragment) {
* </ul><p>
*/
View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
return inflater.inflate(mWolmoFragment.layout(), container, false);
mBinding = DataBindingUtil.inflate(inflater, mWolmoFragment.layout(), container, false);
return mBinding.getRoot();
}

/**
Expand All @@ -132,6 +136,11 @@ public T getPresenter() {
return mPresenter;
}

@Nullable
public V getBinding() {
return mBinding;
}

private void onVisibilityChanged() {
if (!mCreated) return;
if (mFragment.isResumed() && mMenuVisible && !mVisible) {
Expand Down Expand Up @@ -176,5 +185,6 @@ void setMenuVisibility(boolean visible) {
*/
void onDestroyView() {
getPresenter().detachView();
mBinding = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class GetImageFragmentTest {
static final int CAMERA_ERROR_RES_ID = 1234;
static final String CAMERA_FILENAME = "Filename";

private WolmoFragmentHandler<BasePresenter<?>> mWolmoFragmentHandlerMock;
private WolmoFragmentHandler<?, BasePresenter<?>> mWolmoFragmentHandlerMock;
private PermissionManager mPermissionManagerMock;
private ImageProvider mImageProviderMock;
private WolmoFileProvider mWolmoFileProviderMock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

public class WolmoBottomSheetDialogFragmentTest {

private WolmoFragmentHandler<BasePresenter<?>> mWolmoFragmentHandlerMock;
private WolmoFragmentHandler<?, BasePresenter<?>> mWolmoFragmentHandlerMock;
private PermissionManager mPermissionManagerMock;
private WolmoBottomSheetDialogFragment mWolmoBottomSheetDialogFragmentSpy;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

public class WolmoDialogFragmentTest {

private WolmoFragmentHandler<BasePresenter<?>> mWolmoFragmentHandlerMock;
private WolmoFragmentHandler<?, BasePresenter<?>> mWolmoFragmentHandlerMock;
private PermissionManager mPermissionManagerMock;
private WolmoDialogFragment mWolmoDialogFragmentSpy;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingComponent;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.FragmentActivity;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.InOrder;
import org.mockito.internal.matchers.Any;

import ar.com.wolox.wolmo.core.R;
import ar.com.wolox.wolmo.core.presenter.BasePresenter;
Expand All @@ -53,7 +57,7 @@ public class WolmoFragmentHandlerTest {
@Rule
public final ExpectedException exception = ExpectedException.none();

private WolmoFragmentHandler<TestPresenter> mWolmoFragmentHandler;
private WolmoFragmentHandler<?,TestPresenter> mWolmoFragmentHandler;
private WolmoFragment mWolmoFragmentMock;
private TestPresenter mTestPresenter;
private ToastFactory mToastFactoryMock;
Expand Down Expand Up @@ -157,7 +161,7 @@ public void getPresenterShouldReturnThePresenterInstance() {
interface TestView {
}

static class TestFragment extends WolmoFragment<TestPresenter> implements TestView {
static class TestFragment extends WolmoFragment<ViewDataBinding, TestPresenter> implements TestView {
@Override
public int layout() {
return 0;
Expand All @@ -170,5 +174,4 @@ public void init() {

static class TestPresenter extends BasePresenter<TestView> {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.ViewDataBinding;

import org.junit.Before;
import org.junit.Test;

Expand All @@ -40,7 +42,7 @@

public class WolmoFragmentTest {

private WolmoFragmentHandler<BasePresenter<?>> mWolmoFragmentHandlerMock;
private WolmoFragmentHandler<?, BasePresenter<?>> mWolmoFragmentHandlerMock;
private PermissionManager mPermissionManagerMock;
private WolmoFragment mWolmoFragmentSpy;

Expand Down
3 changes: 1 addition & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
android.enableJetifier=true
android.useAndroidX=true
android.enableUnitTestBinaryResources=true
android.useAndroidX=true
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Thu Dec 19 11:05:31 ART 2019
#Mon Dec 21 11:14:06 ART 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip

0 comments on commit f47bb5b

Please sign in to comment.