Skip to content
Merged
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
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,24 @@ Download
```groovy
compile 'eu.inloop:androidviewmodel:1.3.1'
```

## Android Studio Template
For faster creating new screens, you can use [Android Studio Template](/template/AVM_Inloop)

![Android Studio Template Window](/template/template-preview.png)

### Install template
#### Manually:
Copy the template folder to Android Studio templates folder (`/Applications/Android Studio.app/Contents/plugins/android/lib/templates/others` on Mac)
#### Automatically:
Run the following command to download and install the template automatically (Mac only)
```
curl -o androidviewmodel.zip -Lk https://github.com/inloop/AndroidViewModel/archive/master.zip && unzip androidviewmodel.zip && cp -af AndroidViewModel-master/template/AVM_Inloop/. "/Applications/Android Studio.app/Contents/plugins/android/lib/templates/other/AVM_Inloop" && rm -r AndroidViewModel-master && rm androidviewmodel.zip
```
<b>Don't forget to restart the Android Studio.</b>

### Usage
In the Android Studio right click inside the Projet window and select `File > New > AndroidViewModel Inloop > AVM Fragment`


![Android Studio New Template](/template/create-new-template-preview.png)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions template/AVM_Inloop/globals.xml.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0"?>
<globals>
<global id="generateActivityTitle" type="boolean" value="false" />
<#include "../../activities/common/common_globals.xml.ftl" />
</globals>
48 changes: 48 additions & 0 deletions template/AVM_Inloop/recipe.xml.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0"?>
<recipe>

<#if appCompat && !(hasDependency('eu.inloop:androidviewmodel'))>
<dependency mavenUrl="eu.inloop:androidviewmodel:1.3.1"/>
</#if>

<#if screenType == "Fragment">
<instantiate from="src/app_package/layout.xml.ftl"
to="${escapeXmlAttribute(resOut)}/layout/${layoutName}.xml" />
<instantiate from="src/app_package/Fragment.java.ftl"
to="${escapeXmlAttribute(srcOut)}/${scrPackage}/${screenClass}.java" />
</#if>
<#if screenType == "BindingFragment">
<instantiate from="src/app_package/binding_layout.xml.ftl"
to="${escapeXmlAttribute(resOut)}/layout/${layoutName}.xml" />
<instantiate from="src/app_package/BindingFragment.java.ftl"
to="${escapeXmlAttribute(srcOut)}/${scrPackage}/${screenClass}.java" />
</#if>
<instantiate from="src/app_package/ViewModel.java.ftl"
to="${escapeXmlAttribute(srcOut)}/${vmPackage}/${viewModelClass}.java" />
<#if generateViewInterface>
<instantiate from="src/app_package/ViewInterface.java.ftl"
to="${escapeXmlAttribute(srcOut)}/${vPackage}/I${viewModelClass?replace('ViewModel', 'View')}.java" />
</#if>


<#if scrPackage != "">
<open file="${escapeXmlAttribute(srcOut)}/${scrPackage}/${screenClass}.java" />
<#else>
<open file="${escapeXmlAttribute(srcOut)}/${screenClass}.java" />
</#if>

<#if vmPackage != "">
<open file="${escapeXmlAttribute(srcOut)}/${vmPackage}/${viewModelClass}.java" />
<#else>
<open file="${escapeXmlAttribute(srcOut)}/${viewModelClass}.java" />
</#if>

<#if vPackage != "">
<open file="${escapeXmlAttribute(srcOut)}/${vPackage}/I${viewModelClass?replace('ViewModel', 'View')}.java" />
<#else>
<open file="${escapeXmlAttribute(srcOut)}/I${viewModelClass?replace('ViewModel', 'View')}.java" />
</#if>

<open file="${escapeXmlAttribute(resOut)}/layout/${layoutName}.xml" />

</recipe>
45 changes: 45 additions & 0 deletions template/AVM_Inloop/root/src/app_package/BindingFragment.java.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ${packageName}<#if scrPackage != "">.${scrPackage}</#if>;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
<#if vPackage != "">
import ${packageName}<#if vPackage != "">.${vPackage?replace('/','.')}</#if>.I${viewModelClass?replace('ViewModel', 'View')};
<#else>
import eu.inloop.viewmodel.IView;
</#if>
import eu.inloop.viewmodel.binding.ViewModelBaseBindingFragment;
import eu.inloop.viewmodel.binding.ViewModelBindingConfig;
import ${packageName}.databinding.${underscoreToCamelCase(layoutName)}Binding;
import ${packageName}.R;
import ${packageName}<#if vmPackage != "">.${vmPackage?replace('/','.')}</#if>.${viewModelClass};

<#if vPackage != "">
public class ${screenClass}
extends ViewModelBase${screenType}<I${viewModelClass?replace('ViewModel', 'View')}, ${viewModelClass}, ${underscoreToCamelCase(layoutName)}Binding>
implements I${viewModelClass?replace('ViewModel', 'View')} {
<#else>
public class ${screenClass} extends ViewModelBase${screenType}<IView, ${viewModelClass}, ${underscoreToCamelCase(layoutName)}Binding> {
</#if>


public static ${screenClass} newInstance() {
final Bundle bundle = new Bundle();
// set arguments
final ${screenClass} fragment = new ${screenClass}();
fragment.setArguments(bundle);
return fragment;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setModelView(this);
}

@Override
public ViewModelBindingConfig getViewModelBindingConfig() {
return new ViewModelBindingConfig(R.layout.${layoutName}, getActivity());
}

}
45 changes: 45 additions & 0 deletions template/AVM_Inloop/root/src/app_package/Fragment.java.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ${packageName}<#if scrPackage != "">.${scrPackage}</#if>;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
<#if vPackage != "">
import ${packageName}<#if vPackage != "">.${vPackage?replace('/','.')}</#if>.I${viewModelClass?replace('ViewModel', 'View')};
<#else>
import eu.inloop.viewmodel.IView;
</#if>
import eu.inloop.viewmodel.base.ViewModelBaseFragment;
import ${packageName}.R;
import ${packageName}<#if vmPackage != "">.${vmPackage?replace('/','.')}</#if>.${viewModelClass};

<#if vPackage != "">
public class ${screenClass}
extends ViewModelBase${screenType}<I${viewModelClass?replace('ViewModel', 'View')}, ${viewModelClass}>
implements I${viewModelClass?replace('ViewModel', 'View')} {
<#else>
public class ${screenClass} extends ViewModelBase${screenType}<IView, ${viewModelClass}> {
</#if>

public static ${screenClass} newInstance() {
final Bundle bundle = new Bundle();
// set arguments
final ${screenClass} fragment = new ${screenClass}();
fragment.setArguments(bundle);
return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.${layoutName}, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

setModelView(this);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

package ${packageName}<#if vmPackage != "">.${vPackage?replace('/','.')}</#if>;

import eu.inloop.viewmodel.IView;

public interface I${viewModelClass?replace('ViewModel', 'View')} extends IView {

}
24 changes: 24 additions & 0 deletions template/AVM_Inloop/root/src/app_package/ViewModel.java.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

package ${packageName}<#if vmPackage != "">.${vmPackage}</#if>;

import android.os.Bundle;
import android.support.annotation.Nullable;
import eu.inloop.viewmodel.AbstractViewModel;
<#if vPackage != "">
import ${packageName}<#if vPackage != "">.${vPackage?replace('/','.')}</#if>.I${viewModelClass?replace('ViewModel', 'View')};
<#else>
import eu.inloop.viewmodel.IView;
</#if>

<#if vPackage != "">
public class ${viewModelClass} extends AbstractViewModel<I${viewModelClass?replace('ViewModel', 'View')}> {
<#else>
public class ${viewModelClass} extends AbstractViewModel<IView> {
</#if>

@Override
public void onCreate(@Nullable Bundle arguments, @Nullable Bundle savedInstanceState) {
super.onCreate(arguments, savedInstanceState);

}
}
19 changes: 19 additions & 0 deletions template/AVM_Inloop/root/src/app_package/binding_layout.xml.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="<#if scrPackage != "">.${scrPackage?replace('/','.')}</#if>.${screenClass}">

<data>
<variable
name="viewModel"
type="${packageName}.<#if vmPackage != "">${vmPackage?replace('/','.')}.</#if>${viewModelClass}"/>
</data>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

</FrameLayout>

</layout>
9 changes: 9 additions & 0 deletions template/AVM_Inloop/root/src/app_package/layout.xml.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="<#if scrPackage != "">.${scrPackage?replace('/','.')}</#if>.${screenClass}"
android:layout_width="match_parent"
android:layout_height="match_parent">

</FrameLayout>
90 changes: 90 additions & 0 deletions template/AVM_Inloop/template.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<template format="4"
revision="1"
name="AVM Fragment"
minApi="15"
minBuildApi="15"
description="Creates a new AVM Fragment, ViweModel and Layout">

<category value="AndroidViewModel Inloop"/>
<formfactor value="Mobile" />

<parameter
id="screenType"
name="Screen Type"
type="enum"
default="Fragment"
help="Either an Fragment or Fragment with data binding">

<option id="Fragment">Fragment</option>
<option id="BindingFragment">Fragment with data binding</option>
</parameter>

<parameter
id="screenClass"
name="Screen Name"
type="string"
suggest="Main${screenType?replace('Binding', '')}"
constraints="class|unique|nonempty"
default="MainFragment"
help="The name of the screen class to create" />

<parameter
id="layoutName"
name="Layout Name"
type="string"
constraints="layout|unique|nonempty"
suggest="${screenType?lower_case?replace('binding', '')}_${classToResource(screenClass)}"
default="fragment_main"
help="The name of the layout to create for the screen" />

<parameter
id="viewModelClass"
name="ViewModel class name"
type="string"
constraints="class|unique|nonempty"
suggest="${screenClass?replace('_', '')?replace('Fragment', '')}ViewModel"
default="MainViewModel"
help="The name of the ViewModel class to create" />

<parameter
id="generateViewInterface"
name="Generate view interface"
type="boolean"
default="true"
help="Generate child interface of IView with screen name." />

<parameter
id="packageName"
name="Package name"
type="string"
constraints="package"
default="com.company.myapp" />

<parameter
id="vmPackage"
name="View model package name"
type="string"
default="viewmodel" />

<parameter
id="vPackage"
name="View interface package name"
type="string"
default="viewmodel/view" />

<parameter
id="scrPackage"
name="Screen package name"
type="string"
default="fragment" />

<!-- 128x128 thumbnails relative to template.xml -->
<thumbs>
<!-- default thumbnail is required -->
<thumb>avm_inloop_template_preview.png</thumb>
</thumbs>

<globals file="globals.xml.ftl" />
<execute file="recipe.xml.ftl" />

</template>
Binary file added template/create-new-template-preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added template/template-preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.