Skip to content

Commit

Permalink
[app] Introduce MainActivity
Browse files Browse the repository at this point in the history
A class responsible for showing a list of use cases
covered in the app.
This commit also adds some UI libraries to be used
in the app.
  • Loading branch information
serso committed Dec 21, 2016
1 parent bb989bf commit 3c57339
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 2 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle
Expand Up @@ -48,6 +48,10 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile "com.android.support:support-v4:${versions.libs.support}"
compile "com.android.support:appcompat-v7:${versions.libs.support}"
compile "com.android.support:recyclerview-v7:${versions.libs.support}"
compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
compile('ch.acra:acra:4.9.0') {
exclude group: 'org.json'
}
Expand Down
14 changes: 12 additions & 2 deletions app/src/main/AndroidManifest.xml
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?><!--
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2014 serso aka se.solovyev
~
~ Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -39,6 +40,15 @@
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
tools:ignore="GoogleAppIndexingWarning" />
android:theme="@style/Theme.AppCompat.Light.DarkActionBar"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
115 changes: 115 additions & 0 deletions app/src/main/java/org/solovyev/android/checkout/app/MainActivity.java
@@ -0,0 +1,115 @@
package org.solovyev.android.checkout.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
* Shows a list of use cases covered in the app.
*/
public class MainActivity extends AppCompatActivity {

@BindView(R.id.recycler)
RecyclerView mRecycler;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);

mRecycler.setLayoutManager(new LinearLayoutManager(this));
mRecycler.setAdapter(new Adapter(this));
}

private enum UseCase {
;

// activity to be started on click
final Class<? extends Activity> activity;
@StringRes
final int title;
@StringRes
final int description;

UseCase(Class<? extends Activity> activity, @StringRes int title, @StringRes int description) {
this.activity = activity;
this.title = title;
this.description = description;
}
}

static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

private final Activity mActivity;
@BindView(R.id.use_case_title)
TextView mTitle;
@BindView(R.id.use_case_description)
TextView mDescription;
@Nullable
private UseCase mUseCase;


ViewHolder(Activity activity, View view) {
super(view);
mActivity = activity;
ButterKnife.bind(this, view);

view.setOnClickListener(this);
}

void onBind(UseCase useCase) {
mUseCase = useCase;
mTitle.setText(useCase.title);
mDescription.setText(useCase.description);
}

@Override
public void onClick(View v) {
if (mUseCase == null) {
return;
}
mActivity.startActivity(new Intent(mActivity, mUseCase.activity));
}
}

private static class Adapter extends RecyclerView.Adapter<ViewHolder> {

private final Activity mActivity;
private final UseCase[] mUseCases = UseCase.values();
private final LayoutInflater mInflater;

private Adapter(Activity activity) {
mActivity = activity;
mInflater = LayoutInflater.from(activity);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View view = mInflater.inflate(R.layout.use_case, parent, false);
return new ViewHolder(mActivity, view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.onBind(mUseCases[position]);
}

@Override
public int getItemCount() {
return mUseCases.length;
}
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/layout/activity_main.xml
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent" />
28 changes: 28 additions & 0 deletions app/src/main/res/layout/use_case.xml
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?selectableItemBackground"
android:gravity="center_vertical"
android:minHeight="?listPreferredItemHeightLarge"
android:orientation="vertical"
android:paddingEnd="?listPreferredItemPaddingRight"
android:paddingLeft="?listPreferredItemPaddingLeft"
android:paddingRight="?listPreferredItemPaddingRight"
android:paddingStart="?listPreferredItemPaddingLeft">

<TextView
android:id="@+id/use_case_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?textAppearanceListItem" />

<TextView
android:id="@+id/use_case_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?textAppearanceListItem"
android:textColor="@android:color/tertiary_text_light"
android:textSize="14sp" />

</LinearLayout>

0 comments on commit 3c57339

Please sign in to comment.