Skip to content

Commit

Permalink
Added example project
Browse files Browse the repository at this point in the history
  • Loading branch information
ossiv committed Jan 15, 2016
1 parent c286d82 commit 81646b6
Show file tree
Hide file tree
Showing 20 changed files with 483 additions and 0 deletions.
45 changes: 45 additions & 0 deletions redux.NET-thunk.AndroidExample/ActionCreators.cs
@@ -0,0 +1,45 @@
using System;
using Redux;
using System.Net;
using System.Threading.Tasks;
using System.Reactive.Linq;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Immutable;
using System.Threading;

namespace Taiste.Redux.AndroidExample
{
public static class ActionCreators
{
public static IAction SearchUsers (string query)
{
return new ThunkAction<State> ((dispatcher, getState) => {
var request = WebRequest.Create (new Uri (String.Format ("https://api.github.com/search/users?q={0}", query))) as HttpWebRequest;
request.UserAgent = "redux.NET thunk example";
var obs = Observable.FromAsync (() => Task.Factory.FromAsync<WebResponse> (request.BeginGetResponse, request.EndGetResponse, request));
obs.Subscribe (
(next) => {
var responseData = new StreamReader (next.GetResponseStream ()).ReadToEnd ();
var results = ImmutableList<string>.Empty;
JObject res = JObject.Parse (responseData);
var items = (JArray)res ["items"];
foreach (var item in items) {
results = results.Add (item ["login"].Value<string> ());
}
dispatcher (new SetResultsAction () { Results = results });
},
(error) => System.Diagnostics.Debug.WriteLine (error.Message),
CancellationToken.None);
});
}
}
}

11 changes: 11 additions & 0 deletions redux.NET-thunk.AndroidExample/Actions.cs
@@ -0,0 +1,11 @@
using System;
using Redux;
using System.Collections.Immutable;

namespace Taiste.Redux.AndroidExample
{
public class SetResultsAction : IAction {
public ImmutableList<string> Results;
}
}

19 changes: 19 additions & 0 deletions redux.NET-thunk.AndroidExample/Assets/AboutAssets.txt
@@ -0,0 +1,19 @@
Any raw assets you want to be deployed with your application can be placed in
this directory (and child directories) and given a Build Action of "AndroidAsset".

These files will be deployed with your package and will be accessible using Android's
AssetManager, like this:

public class ReadAsset : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);

InputStream input = Assets.Open ("my_asset.txt");
}
}

Additionally, some Android functions will automatically load asset files:

Typeface tf = Typeface.CreateFromAsset (Context.Assets, "fonts/samplefont.ttf");
58 changes: 58 additions & 0 deletions redux.NET-thunk.AndroidExample/MainActivity.cs
@@ -0,0 +1,58 @@
using Android.App;
using Android.Widget;
using Android.OS;
using Redux;
using System.Collections.Immutable;
using System.Reactive.Linq;
using Android.Text;
using System;
using System.Collections.Generic;

namespace Taiste.Redux.AndroidExample
{
[Activity (Label = "Redux.NET-thunk Android Example", MainLauncher = true, Icon = "@mipmap/icon")]
public class MainActivity : Activity
{
Store<State> store;
ListView list;

public MainActivity ()
{
store = new Store<State> (new State (ImmutableList<string>.Empty), Reducers.ReduceState, Middleware.ThunkMiddleware);
}

protected override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);

SetContentView (Resource.Layout.main);

list = FindViewById<ListView> (Android.Resource.Id.List);

ArrayAdapter<string> adapter = new ArrayAdapter<string> (this, Android.Resource.Layout.SimpleListItem1, new List<string> ());
list.Adapter = adapter;

EditText edit = FindViewById<EditText> (Android.Resource.Id.Edit);

Observable.FromEventPattern<TextChangedEventArgs> (edit, "TextChanged")
.Throttle (TimeSpan.FromMilliseconds (500))
.Subscribe (e => store.Dispatch (ActionCreators.SearchUsers (edit.Text.Trim ())));

store.Select (s => s.Results)
.DistinctUntilChanged ()
.Subscribe (UpdateAdapter);
}

private void UpdateAdapter (ImmutableList<string> items)
{
RunOnUiThread (() => {
var adapter = list.Adapter as ArrayAdapter<string>;
adapter.Clear ();
adapter.AddAll (items);
adapter.NotifyDataSetChanged ();
});
}
}
}


5 changes: 5 additions & 0 deletions redux.NET-thunk.AndroidExample/Properties/AndroidManifest.xml
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="fi.taiste.redux.thunk_androidexample">
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="23" />
<application android:allowBackup="true" android:icon="@mipmap/icon" android:label="@string/app_name"></application>
</manifest>
28 changes: 28 additions & 0 deletions redux.NET-thunk.AndroidExample/Properties/AssemblyInfo.cs
@@ -0,0 +1,28 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using Android.App;

// Information about this assembly is defined by the following attributes.
// Change them to the values specific to your project.

[assembly: AssemblyTitle ("redux.NET-thunk.AndroidExample")]
[assembly: AssemblyDescription ("")]
[assembly: AssemblyConfiguration ("")]
[assembly: AssemblyCompany ("Taiste")]
[assembly: AssemblyProduct ("")]
[assembly: AssemblyCopyright ("")]
[assembly: AssemblyTrademark ("")]
[assembly: AssemblyCulture ("")]

// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.

[assembly: AssemblyVersion ("1.0.0")]

// The following attributes are used to specify the signing key for the assembly,
// if desired. See the Mono documentation for more information about signing.

//[assembly: AssemblyDelaySign(false)]
//[assembly: AssemblyKeyFile("")]

19 changes: 19 additions & 0 deletions redux.NET-thunk.AndroidExample/Reducers.cs
@@ -0,0 +1,19 @@
using System;
using Redux;

namespace Taiste.Redux.AndroidExample
{
public static class Reducers
{
public static State ReduceState(State previous, IAction action)
{
var resultAction = action as SetResultsAction;
if (resultAction != null)
{
return new State (resultAction.Results);
}
return previous;
}
}
}

44 changes: 44 additions & 0 deletions redux.NET-thunk.AndroidExample/Resources/AboutResources.txt
@@ -0,0 +1,44 @@
Images, layout descriptions, binary blobs and string dictionaries can be included
in your application as resource files. Various Android APIs are designed to
operate on the resource IDs instead of dealing with images, strings or binary blobs
directly.

For example, a sample Android app that contains a user interface layout (main.axml),
an internationalization string table (strings.xml) and some icons (drawable-XXX/icon.png)
would keep its resources in the "Resources" directory of the application:

Resources/
drawable/
icon.png

layout/
main.axml

values/
strings.xml

In order to get the build system to recognize Android resources, set the build action to
"AndroidResource". The native Android APIs do not operate directly with filenames, but
instead operate on resource IDs. When you compile an Android application that uses resources,
the build system will package the resources for distribution and generate a class called "R"
(this is an Android convention) that contains the tokens for each one of the resources
included. For example, for the above Resources layout, this is what the R class would expose:

public class R {
public class drawable {
public const int icon = 0x123;
}

public class layout {
public const int main = 0x456;
}

public class strings {
public const int first_string = 0xabc;
public const int second_string = 0xbcd;
}
}

You would then use R.drawable.icon to reference the drawable/icon.png file, or R.layout.main
to reference the layout/main.axml file, or R.strings.first_string to reference the first
string in the dictionary file values/strings.xml.
96 changes: 96 additions & 0 deletions redux.NET-thunk.AndroidExample/Resources/Resource.designer.cs

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

16 changes: 16 additions & 0 deletions redux.NET-thunk.AndroidExample/Resources/layout/main.xml
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@android:id/edit" />
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/list" />
</LinearLayout>
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions redux.NET-thunk.AndroidExample/Resources/values/Strings.xml
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string
name="hello">Hello World, Click Me!</string>
<string
name="app_name">redux.NET-thunk.AndroidExample</string>
</resources>
15 changes: 15 additions & 0 deletions redux.NET-thunk.AndroidExample/State.cs
@@ -0,0 +1,15 @@
using System;
using System.Collections.Immutable;

namespace Taiste.Redux.AndroidExample
{
public class State
{
public ImmutableList<string> Results { get; }
public State (ImmutableList<string> results)
{
Results = results;
}
}
}

11 changes: 11 additions & 0 deletions redux.NET-thunk.AndroidExample/packages.config
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="8.0.2" targetFramework="MonoAndroid44" />
<package id="Redux.NET" version="0.3.0" targetFramework="MonoAndroid44" />
<package id="Rx-Core" version="2.2.5" targetFramework="MonoAndroid44" />
<package id="Rx-Interfaces" version="2.2.5" targetFramework="MonoAndroid44" />
<package id="Rx-Linq" version="2.2.5" targetFramework="MonoAndroid44" />
<package id="Rx-Main" version="2.2.5" targetFramework="MonoAndroid44" />
<package id="Rx-PlatformServices" version="2.2.5" targetFramework="MonoAndroid44" />
<package id="System.Collections.Immutable" version="1.1.37" targetFramework="MonoAndroid44" />
</packages>

0 comments on commit 81646b6

Please sign in to comment.