Skip to content
Permalink
Browse files

- Implemented dagger (#1)

- 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 d5029ba8c5c7425ae7f4d97e14e8651e327df9a4
@@ -1,4 +1,5 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 22
@@ -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'
}
@@ -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" />
@@ -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
@@ -26,6 +35,9 @@
@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);
@@ -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) {
@@ -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 {
}
@@ -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();
}
@@ -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();
}
@@ -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
@@ -26,7 +37,7 @@
*
* @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
@@ -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);
}
@@ -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;
}
}
@@ -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;
}
}
@@ -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;
}


}
@@ -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;

/**
@@ -17,15 +20,17 @@
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;
}
@@ -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"
@@ -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"
@@ -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

0 comments on commit d5029ba

Please sign in to comment.
You can’t perform that action at this time.