Skip to content

Commit

Permalink
Merge branch 'master' into zhiqiao-checklist_item_map
Browse files Browse the repository at this point in the history
closes #51
  • Loading branch information
aaronj1335 committed Aug 19, 2016
2 parents a47ecdc + 04bbc00 commit 1cf3045
Show file tree
Hide file tree
Showing 28 changed files with 719 additions and 249 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ dependencies {
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})

def supportLibVersion = '24.2.0'
compile "com.android.support:appcompat-v7:${supportLibVersion}"
compile "com.android.support:cardview-v7:${supportLibVersion}"
Expand Down
108 changes: 108 additions & 0 deletions app/src/main/java/com/g11x/checklistapp/AppPreferences.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright © 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.g11x.checklistapp;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.g11x.checklistapp.language.Language;
import com.g11x.checklistapp.language.PreferredLanguageSupport;

/**
* Simplified class to access the app's {@link android.content.SharedPreferences}.
*/

public class AppPreferences {

private static final String PREF_PREFERRED_LANGUAGE = "PREF_PREFERRED_LANGUAGE";

/**
* Returns whether the user has chosen to override the system's default language.
*
* @param context component context used to retrieve shared preferences
*/
public static boolean isLanguageOverriden(@NonNull Context context) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
String value = sp.getString(PREF_PREFERRED_LANGUAGE, null);
return TextUtils.isEmpty(value);
}

/**
* Saves the user's language preference which overrides the system default.
*
* @param context component context used to retrieve shared preferences
* @param language the language identifier or null to unset
*/
public static void setLanguageOverride(@NonNull Context context,
@Nullable Language language) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sp.edit();
if (language == null) {
editor.remove(PREF_PREFERRED_LANGUAGE);
} else {
editor.putString(PREF_PREFERRED_LANGUAGE, language.name());
}
editor.apply();
}

/**
* Returns the language override or null when not set.
*
* @param context component context used to retrieve shared preferences
*/
@Nullable
public static Language getLanguageOverride(@NonNull Context context) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
String value = sp.getString(PREF_PREFERRED_LANGUAGE, null);
if (value == null) {
return null;
}
return Language.valueOf(value);
}

/**
* Custom shared preference listener for the preferred language property.
*/
public static abstract class LanguageChangeListener {
private final SharedPreferences.OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
if (s.equals(PREF_PREFERRED_LANGUAGE)) {
onChanged(sharedPreferences.getString(s, null));
}
}
};

LanguageChangeListener(@NonNull Context context) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
sp.registerOnSharedPreferenceChangeListener(listener);
}

public void unregister(@NonNull Context context) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
sp.unregisterOnSharedPreferenceChangeListener(listener);
}

public abstract void onChanged(String newValue);
}

}
21 changes: 19 additions & 2 deletions app/src/main/java/com/g11x/checklistapp/ChecklistActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.g11x.checklistapp.data.ChecklistItem;
import com.g11x.checklistapp.data.Database;
import com.g11x.checklistapp.language.Language;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class ChecklistActivity extends NavigationActivity {

private FirebaseRecyclerAdapter<ChecklistItem, ChecklistItemHolder> checklistAdapter;
private AppPreferences.LanguageChangeListener languageChangeListener;
private Language language;

@Override
protected int getNavDrawerItemIndex() {
Expand All @@ -46,6 +49,15 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checklist);

language = AppPreferences.getLanguageOverride(this);
languageChangeListener = new AppPreferences.LanguageChangeListener(this) {

@Override
public void onChanged(String newValue) {
ChecklistActivity.this.onLanguageChange(newValue);
}
};

DatabaseReference databaseRef = FirebaseDatabase.getInstance().getReference()
.child("checklists")
.child("basic");
Expand All @@ -66,8 +78,8 @@ public void onBindViewHolder(ChecklistItemHolder viewHolder, int position) {
@Override
protected void populateViewHolder(
final ChecklistItemHolder itemHolder, ChecklistItem model, final int position) {
itemHolder.setText(model.getName());
itemHolder.markDone(model.isDone(getContentResolver()));
itemHolder.setText(model.getName(language));
itemHolder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Expand Down Expand Up @@ -96,14 +108,19 @@ protected void onStart() {
}
}

private void onLanguageChange(String newValue) {
language = Language.valueOf(newValue);
}

@Override
protected void onDestroy() {
super.onDestroy();
checklistAdapter.cleanup();
languageChangeListener.unregister(this);
}

public static class ChecklistItemHolder extends RecyclerView.ViewHolder {
View view;
final View view;

public ChecklistItemHolder(View itemView) {
super(itemView);
Expand Down
34 changes: 26 additions & 8 deletions app/src/main/java/com/g11x/checklistapp/ChecklistItemActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import android.widget.ToggleButton;

import com.g11x.checklistapp.data.ChecklistItem;
import com.g11x.checklistapp.language.Language;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
Expand All @@ -39,13 +40,25 @@ public class ChecklistItemActivity extends AppCompatActivity {
private ChecklistItem checklistItem;
private ToggleButton isDone;
private DatabaseReference databaseRef;
private Language language;
private AppPreferences.LanguageChangeListener languageChangeListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
setContentView(R.layout.activity_checklist_item);

databaseRef = FirebaseDatabase.getInstance().getReferenceFromUrl(
language = AppPreferences.getLanguageOverride(this);
languageChangeListener = new AppPreferences.LanguageChangeListener(this) {

@Override
public void onChanged(String newValue) {
ChecklistItemActivity.this.onLanguageChange(newValue);
}
};

DatabaseReference databaseRef = FirebaseDatabase.getInstance().getReferenceFromUrl(
this.getIntent().getStringExtra("databaseRefUrl"));

databaseRef.addValueEventListener(new ValueEventListener() {
Expand All @@ -63,12 +76,22 @@ public void onCancelled(DatabaseError databaseError) {

}

private void onLanguageChange(String newValue) {
language = Language.valueOf(newValue);
}

@Override
protected void onDestroy() {
super.onDestroy();
languageChangeListener.unregister(this);
}

private void createUI() {
TextView name = (TextView) findViewById(R.id.name);
name.setText(checklistItem.getName());
name.setText(checklistItem.getName(language));

TextView description = (TextView) findViewById(R.id.description);
description.setText(checklistItem.getDescription());
description.setText(checklistItem.getDescription(language));

Button getDirections = (Button) findViewById(R.id.directions);
getDirections.setOnClickListener(new View.OnClickListener() {
Expand All @@ -88,11 +111,6 @@ public void onClick(View view) {
});
}

@Override
protected void onStart() {
super.onStart();
}

private void onClickIsDone() {
ContentResolver contentResolver = getContentResolver();
checklistItem.setDone(contentResolver, !checklistItem.isDone(contentResolver));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.g11x.checklistapp.data.Database;

import org.w3c.dom.Text;

import java.util.ArrayList;

/**
Expand Down Expand Up @@ -74,6 +77,8 @@ public void onClick(View view) {
}
});

final TextView emptyListInfo = (TextView) findViewById(R.id.important_information_empty);

Intent intent = getIntent();

if (intent.getExtras() != null && intent.getExtras().get("title") != null) {
Expand All @@ -93,6 +98,8 @@ public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
refreshUi(cursor.getCount());

if (adapter == null) {
if (cursor != null) {
adapter = new Adapter(cursor);
Expand All @@ -107,6 +114,17 @@ public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
public void onLoaderReset(Loader<Cursor> loader) {
adapter.swapCursor(null);
}

private void refreshUi(int itemCount) {
if (itemCount > 0) {
recyclerView.setVisibility(View.VISIBLE);
emptyListInfo.setVisibility(View.GONE);
} else {
emptyListInfo.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
}

}
});
}

Expand All @@ -131,8 +149,8 @@ private static class Adapter extends RecyclerViewCursorAdapter<Adapter.ViewHolde
static class ViewHolder extends RecyclerView.ViewHolder {
private final TextView textView;

ViewHolder(LinearLayout layout) {
super(layout);
ViewHolder(CardView cardView) {
super(cardView);
textView = (TextView) itemView.findViewById(R.id.data);
}
}
Expand All @@ -143,9 +161,9 @@ static class ViewHolder extends RecyclerView.ViewHolder {

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LinearLayout layout = (LinearLayout) LayoutInflater.from(parent.getContext())
View layout = LayoutInflater.from(parent.getContext())
.inflate(R.layout.view_important_information_item, parent, false);
return new ViewHolder(layout);
return new ViewHolder((CardView)layout);
}

@Override
Expand Down
19 changes: 11 additions & 8 deletions app/src/main/java/com/g11x/checklistapp/LanguageActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.g11x.checklistapp.language.Language;

/**
* Displays a language selection screen to allow the user to override the system defaults.
* Displays a language selection screen to allow the user to override the system default language.
*/
public class LanguageActivity extends NavigationActivity {
private static final String LOG_TAG = "LanguageActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -47,11 +51,7 @@ protected int getNavDrawerItemIndex() {
}

static class LanguageSelectionAdapter extends RecyclerView.Adapter<LanguageSelectionAdapter.ViewHolder> {
private String[] dataSet;

LanguageSelectionAdapter() {
dataSet = new String[] { "Device default (English)", "English", "Espanol", "Russian" };
}
private Language[] dataSet = Language.values();

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Expand All @@ -62,7 +62,8 @@ public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.textView.setText(dataSet[position]);
holder.language = dataSet[position];
holder.textView.setText(dataSet[position].getNativeDescription());
}

@Override
Expand All @@ -72,6 +73,7 @@ public int getItemCount() {

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView textView;
private Language language;
ViewHolder(final CardView view) {
super(view);
textView = (TextView)view.findViewById(R.id.checkbox);
Expand All @@ -80,7 +82,8 @@ class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener

@Override
public void onClick(View view) {

Log.w(LOG_TAG, "Setting language to: " + language);
AppPreferences.setLanguageOverride(view.getContext(), language);
}
}
}
Expand Down
Loading

0 comments on commit 1cf3045

Please sign in to comment.