Skip to content
This repository has been archived by the owner on Sep 10, 2019. It is now read-only.

Commit

Permalink
Adds GridExampleActivity for grid tutorial and NYTimesData integration
Browse files Browse the repository at this point in the history
  • Loading branch information
thorbenprimke committed Nov 6, 2015
1 parent 49a7a34 commit 93c8b9d
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 10 deletions.
20 changes: 18 additions & 2 deletions example/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,30 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
}

dexOptions {
preDexLibraries = false
}
}

dependencies {
testCompile 'junit:junit:4.12'
compile 'com.android.support:support-v4:22.2.1'
compile 'com.android.support:appcompat-v7:22.2.1'

compile 'com.android.support:recyclerview-v7:22.0.+'

compile 'com.github.bumptech.glide:glide:3.6.1'

// The library reference
compile project(':library')
// compile 'com.github.thorbenprimke:realm-recyclerview:0.9.1'
// compile 'com.github.thorbenprimke:realm-recyclerview:0.9.2'

// The date project reference
// compile project(':data')
compile 'com.github.thorbenprimke:realm-nytimes-data:0.9.0'
}
8 changes: 7 additions & 1 deletion example/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="co.moonmonkeylabs.realmrecyclerview.example" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand All @@ -24,6 +27,9 @@
android:name=".MainActivity2"
android:label="@string/app_name" >
</activity>
</application>
<activity
android:name=".GridExampleActivity"
android:label="@string/app_name" >
</activity> </application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package co.moonmonkeylabs.realmrecyclerview.example;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import co.moonmonkeylabs.realmnytimesdata.NYTimesDataLoader;
import co.moonmonkeylabs.realmnytimesdata.NYTimesModule;
import co.moonmonkeylabs.realmnytimesdata.model.NYTimesMultimedium;
import co.moonmonkeylabs.realmnytimesdata.model.NYTimesStory;
import co.moonmonkeylabs.realmrecyclerview.RealmRecyclerView;
import io.realm.Realm;
import io.realm.RealmBasedRecyclerViewAdapter;
import io.realm.RealmConfiguration;
import io.realm.RealmList;
import io.realm.RealmResults;
import io.realm.RealmViewHolder;

public class GridExampleActivity extends AppCompatActivity {

private RealmRecyclerView realmRecyclerView;
private NYTimesStoryRecyclerViewAdapter nyTimesStoryAdapter;
private Realm realm;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main_grid_layout);
realmRecyclerView = (RealmRecyclerView) findViewById(R.id.realm_recycler_view);

setTitle(getResources().getString(
R.string.activity_layout_name,
getIntent().getStringExtra("Type")));

resetRealm();

Realm.setDefaultConfiguration(getRealmConfig());
realm = Realm.getDefaultInstance();
RealmResults<NYTimesStory> nyTimesStories =
realm.where(NYTimesStory.class).findAllSorted("sortTimeStamp", false);
nyTimesStoryAdapter = new NYTimesStoryRecyclerViewAdapter(this, nyTimesStories, true, true);
realmRecyclerView.setAdapter(nyTimesStoryAdapter);

final NYTimesDataLoader nyTimesDataLoader = new NYTimesDataLoader();
nyTimesDataLoader.loadData("home", realm, "b9989f55a0c330b0bdfea069af08b163:15:73381676");
}

@Override
protected void onDestroy() {
super.onDestroy();
realm.close();
realm = null;
}

public class NYTimesStoryRecyclerViewAdapter extends RealmBasedRecyclerViewAdapter<NYTimesStory,
NYTimesStoryRecyclerViewAdapter.ViewHolder> {

public NYTimesStoryRecyclerViewAdapter(
Context context,
RealmResults<NYTimesStory> realmResults,
boolean automaticUpdate,
boolean animateIdType) {
super(context, realmResults, automaticUpdate, animateIdType);
}

public class ViewHolder extends RealmViewHolder {

public TextView title;
public TextView publishedDate;
public ImageView image;
public TextView storyAbstract;

public ViewHolder(LinearLayout container) {
super(container);
this.title = (TextView) container.findViewById(R.id.title);
this.publishedDate = (TextView) container.findViewById(R.id.date);
this.image = (ImageView) container.findViewById(R.id.image);
this.storyAbstract = (TextView) container.findViewById(R.id.story_abstract);
}
}

@Override
public ViewHolder onCreateRealmViewHolder(ViewGroup viewGroup, int viewType) {
View v = inflater.inflate(R.layout.grid_item_view, viewGroup, false);
ViewHolder vh = new ViewHolder((LinearLayout) v);
return vh;
}

@Override
public void onBindRealmViewHolder(ViewHolder viewHolder, int position) {
final NYTimesStory nyTimesStory = realmResults.get(position);
viewHolder.title.setText(nyTimesStory.getTitle());
viewHolder.publishedDate.setText(nyTimesStory.getPublishedDate());
final RealmList<NYTimesMultimedium> multimedia = nyTimesStory.getMultimedia();
if (multimedia != null && !multimedia.isEmpty()) {
Glide.with(GridExampleActivity.this).load(
multimedia.get(0).getUrl()).into(viewHolder.image);
}
viewHolder.storyAbstract.setText(nyTimesStory.getStoryAbstract());
}
}

private RealmConfiguration getRealmConfig() {
return new RealmConfiguration
.Builder(this)
.setModules(Realm.getDefaultModule(), new NYTimesModule())
.build();
}

private void resetRealm() {
Realm.deleteRealm(getRealmConfig());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public void onClick(View v) {
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(LayoutSelectorActivity.this, MainActivity.class);
Intent intent =
new Intent(LayoutSelectorActivity.this, GridExampleActivity.class);
intent.putExtra("Type", "Grid");
startActivity(intent);
}
Expand Down
7 changes: 3 additions & 4 deletions example/app/src/main/res/layout/activity_main_grid_layout.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<RelativeLayout
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
Expand All @@ -8,9 +8,8 @@
android:id="@+id/realm_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:rrvIsRefreshable="true"
app:rrvEmptyLayoutId="@layout/empty_view"
app:rrvIsRefreshable="false"
app:rrvLayoutType="Grid"
app:rrvGridLayoutSpanCount="2"
/>
</RelativeLayout>
</FrameLayout>
45 changes: 45 additions & 0 deletions example/app/src/main/res/layout/grid_item_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent">

<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="5dp"
android:layout_gravity="center_horizontal"/>

<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:singleLine="true"
android:ellipsize="end"
android:textStyle="bold"/>

<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_gravity="center_horizontal"
android:singleLine="true"
android:ellipsize="end"/>

<TextView
android:id="@+id/story_abstract"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:gravity="center_horizontal"
android:maxLines="5"
android:ellipsize="end"/>
</LinearLayout>
5 changes: 4 additions & 1 deletion example/settings.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
include ':app'

include 'library'
project(':library').projectDir = new File(settingsDir, '../library')
project(':library').projectDir = new File(settingsDir, '../library')

include 'data'
project(':data').projectDir = new File(settingsDir, '../../realm-nytimes-data/library')
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dependencies {
compile fileTree(dir: 'libs', include: ['diffutils-1.2.1.jar'])

compile 'com.android.support:recyclerview-v7:22.0.+'
compile 'io.realm:realm-android:0.82.+'
compile 'io.realm:realm-android:0.84.1'

compile 'com.github.TonicArtos:SuperSLiM:ed0ba4b4d2'
}

0 comments on commit 93c8b9d

Please sign in to comment.