Skip to content

Commit

Permalink
- Implemented dagger (#1)
Browse files Browse the repository at this point in the history
- Introduced new application architecture to support wiring of DI and
  better code structures
  • Loading branch information
jonburney committed Feb 17, 2016
1 parent 55eee1c commit d5029ba
Show file tree
Hide file tree
Showing 15 changed files with 198 additions and 10 deletions.
8 changes: 8 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 22
Expand Down Expand Up @@ -28,7 +29,14 @@ android {
}
}

def dagger_version = "2.0.1"

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

compile "com.google.dagger:dagger:$dagger_version"
kapt "com.google.dagger:dagger-compiler:$dagger_version"
provided "org.glassfish:javax.annotation:10.0-b28"
testCompile 'junit:junit:4.12'
}
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
<uses-permission android:name="android.permission.WAKE_LOCK" />

<application
android:name=".MainApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".Activities.HomeActivity"
android:name=".Activities.MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,22 @@
import android.widget.ListView;
import java.util.ArrayList;

import javax.inject.Inject;

import jonburney.version7.kingsgatemediaplayer.Components.IApplicationComponent;
import jonburney.version7.kingsgatemediaplayer.DataProviders.EndisRssProvider;
import jonburney.version7.kingsgatemediaplayer.DataProviders.IVideoListDataProvider;
import jonburney.version7.kingsgatemediaplayer.MainApp;
import jonburney.version7.kingsgatemediaplayer.R;
import jonburney.version7.kingsgatemediaplayer.Services.VideoUpdater;

/**
* Home activity - The main activity when first starting the appilication
*/
public class HomeActivity extends Activity {
public class MainActivity extends Activity {

@Inject
EndisRssProvider videoListDataProvider;

/**
* Executed when the activity is created
Expand All @@ -26,6 +35,9 @@ public class HomeActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

this.getApplicationComponent().inject(this);

setContentView(R.layout.activity_home);

ListView videoList = (ListView) findViewById(R.id.videoList);
Expand All @@ -34,9 +46,12 @@ protected void onCreate(Bundle savedInstanceState) {
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listItems);
videoList.setAdapter(adapter);

new VideoUpdater(this).execute();
new VideoUpdater(this, (IVideoListDataProvider)this.videoListDataProvider).execute();
}

protected IApplicationComponent getApplicationComponent() {
return ((MainApp)getApplication()).getApplicationComponent();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package jonburney.version7.kingsgatemediaplayer.Attributes;

import java.lang.annotation.Retention;

import javax.inject.Scope;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* Created by jburney on 16/02/2016.
*/
@Scope
@Retention(RUNTIME)
public @interface PerActivity {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package jonburney.version7.kingsgatemediaplayer.Components;

import android.app.Activity;

import dagger.Component;
import jonburney.version7.kingsgatemediaplayer.Attributes.PerActivity;
import jonburney.version7.kingsgatemediaplayer.Modules.ActivityModule;

/**
* Created by jburney on 16/02/2016.
*/
@PerActivity
@Component (dependencies = IApplicationComponent.class, modules = ActivityModule.class)
public interface IActivityComponent {
Activity activity();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package jonburney.version7.kingsgatemediaplayer.Components;

import android.content.Context;

import javax.inject.Singleton;

import dagger.Component;
import dagger.Provides;
import jonburney.version7.kingsgatemediaplayer.Activities.MainActivity;
import jonburney.version7.kingsgatemediaplayer.Modules.ApplicationModule;

/**
* Created by jburney on 16/02/2016.
*/
@Singleton
@Component(modules = ApplicationModule.class)
public interface IApplicationComponent {
void inject(MainActivity mainActivity);
Context context();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,26 @@
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import javax.inject.Inject;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import dagger.Module;
import dagger.Provides;

/**
* Created by jburney on 06/02/2016.
*/
public class EndisRssProvider {
@Module
public class EndisRssProvider implements IVideoListDataProvider {

@Inject
public EndisRssProvider() {

}

/**
* Update the video list from the remote source
Expand All @@ -26,7 +37,7 @@ public class EndisRssProvider {
*
* @return an ArrayList of entry titles as strings
*/
public ArrayList<String> UpdateVideoList(String rssFeedUrl) {
@Provides public ArrayList<String> FetchVideoList(String rssFeedUrl) {

try {
// @// TODO: 11/02/2016 Wrap this in a HttpClient abstraction for better testing
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package jonburney.version7.kingsgatemediaplayer.DataProviders;

import java.util.ArrayList;

import dagger.Module;
import dagger.Provides;

/**
* Created by jburney on 16/02/2016.
*/
public interface IVideoListDataProvider {
public ArrayList<String> FetchVideoList(String rssFeedUrl);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package jonburney.version7.kingsgatemediaplayer;

import android.app.Application;

import jonburney.version7.kingsgatemediaplayer.Components.DaggerIApplicationComponent;
import jonburney.version7.kingsgatemediaplayer.Components.IApplicationComponent;
import jonburney.version7.kingsgatemediaplayer.Modules.ApplicationModule;

/**
* Created by jburney on 16/02/2016.
*/
public class MainApp extends Application {

private IApplicationComponent applicationComponent;

@Override
public void onCreate() {
super.onCreate();
this.applicationComponent = DaggerIApplicationComponent.builder()
.applicationModule(new ApplicationModule(this))
.build();

}



public IApplicationComponent getApplicationComponent() {
return this.applicationComponent;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package jonburney.version7.kingsgatemediaplayer.Modules;

import android.app.Activity;

import dagger.Module;
import dagger.Provides;
import jonburney.version7.kingsgatemediaplayer.Attributes.PerActivity;

/**
* Created by jburney on 16/02/2016.
*/
@Module
public class ActivityModule {
private final Activity activity;

public ActivityModule(Activity activity) {
this.activity = activity;
}

@Provides @PerActivity Activity activity() {
return this.activity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package jonburney.version7.kingsgatemediaplayer.Modules;

import android.content.Context;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import jonburney.version7.kingsgatemediaplayer.MainApp;

/**
* Created by jburney on 16/02/2016.
*/
@Module
public class ApplicationModule {

private final MainApp application;

public ApplicationModule(MainApp application) {
this.application = application;
}

@Provides @Singleton Context provideApplicationContext() {
return this.application;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import android.widget.ListView;
import java.util.ArrayList;

import javax.inject.Inject;

import jonburney.version7.kingsgatemediaplayer.DataProviders.EndisRssProvider;
import jonburney.version7.kingsgatemediaplayer.DataProviders.IVideoListDataProvider;
import jonburney.version7.kingsgatemediaplayer.R;

/**
Expand All @@ -17,15 +20,17 @@ public class VideoUpdater extends AsyncTask<String, Integer, ArrayList<String>>
private String VideoListUrl = "http://www.kingsgateuk.com/Media/rss.xml";

private Activity HomeActivity;
private IVideoListDataProvider videoListProvider;

public VideoUpdater(Activity targetActivity) {
@Inject
public VideoUpdater(Activity targetActivity, IVideoListDataProvider videoListProvider) {
HomeActivity = targetActivity;
this.videoListProvider = videoListProvider;
}

protected ArrayList<String> doInBackground(String... urls) {

EndisRssProvider videoProvider = new EndisRssProvider();
ArrayList<String> videoList = videoProvider.UpdateVideoList(VideoListUrl);
ArrayList<String> videoList = videoListProvider.FetchVideoList(VideoListUrl);

return videoList;
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/background"
tools:context=".Activities.HomeActivity">
tools:context=".Activities.MainActivity">

<LinearLayout
android:orientation="horizontal"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/menu/menu_home.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".Activities.HomeActivity">
tools:context=".Activities.MainActivity">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
Expand Down
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext {
kotlin_version = '1.0.0-beta-1038'
}

repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.android.tools.build:gradle:1.5.0'

// NOTE: Do not place your application dependencies here; they belong
Expand Down

0 comments on commit d5029ba

Please sign in to comment.