Skip to content

Commit

Permalink
profile image and name now retrieved from database
Browse files Browse the repository at this point in the history
  • Loading branch information
Neijwiert committed Jan 13, 2017
1 parent faadce3 commit a0be7b4
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 16 deletions.
7 changes: 6 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@ dependencies {
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.firebaseui:firebase-ui-storage:0.6.0'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.google.firebase:firebase-database:9.8.0'
testCompile 'junit:junit:4.12'
compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
compile 'com.android.support:cardview-v7:25.1.0'
compile 'com.google.firebase:firebase-storage:9.8.0'
compile 'com.google.firebase:firebase-auth:9.8.0'
testCompile 'junit:junit:4.12'
}



apply plugin: 'com.google.gms.google-services'
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package="com.example.tmp.trackmyprint">

<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-feature android:name="android.hardware.nfc" android:required="true" />

<application
Expand Down
36 changes: 29 additions & 7 deletions app/src/main/java/com/example/tmp/trackmyprint/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
import android.widget.TextView;

import org.fontys.trackmyprint.database.Database;
import org.fontys.trackmyprint.database.DatabaseException;
import org.fontys.trackmyprint.database.DatabaseListener;
import org.fontys.trackmyprint.database.entities.Employee;
import org.fontys.trackmyprint.database.entities.Order;
import org.fontys.trackmyprint.database.entities.Phase;
import org.fontys.trackmyprint.database.entities.Product;
import org.fontys.trackmyprint.database.entities.ProductPhase;
import org.fontys.trackmyprint.database.entities.User;
import org.fontys.trackmyprint.utils.Phone;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -36,12 +38,11 @@ public class MainActivity extends AppCompatActivity implements DatabaseListener
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btnScan = (ImageButton) findViewById(R.id.btnScan);

currentEmployee = new Employee("1", "Luuk Hermans");

try
{
Database.getInstance().addDatabaseListener(this);
Expand All @@ -54,9 +55,6 @@ protected void onCreate(Bundle savedInstanceState)
ex.printStackTrace();
}

TextView employeeName = (TextView) findViewById(R.id.profile_name);
employeeName.setText(currentEmployee.getName());

ImageView checkIn = (ImageView) findViewById(R.id.check_in_status);

// Deze is temporary om naar de userList te gaan.
Expand All @@ -72,6 +70,8 @@ protected void onDestroy()

try
{
Database.getInstance().removeDatabaseListener(this);

Database.deInitializeInstance();
}
catch(Exception ex)
Expand All @@ -88,13 +88,20 @@ public static MainActivity getInstance()
public void setCurrentPhase(Phase p)
{
this.currentEmployee.setPhaseId(p == null ? null : p.getId());
// update in database

try
{
Database.getInstance().update(this.currentEmployee);
}
catch(DatabaseException e)
{
e.printStackTrace();
}

ImageView status = (ImageView) findViewById(R.id.check_in_status);
TextView lblScan = (TextView) findViewById(R.id.lblScan);

if(p.getId() == "100")
if(p == null)
{
status.setImageResource(R.drawable.checkin_status);
lblScan.setText("Please check in to a sector");
Expand Down Expand Up @@ -127,7 +134,22 @@ public Phase getCurrentPhase()
@Override
public void onEmployeesInitialized(Map<String, Employee> employees)
{
this.currentEmployee = employees.get(Phone.getIMEI(getApplicationContext()));

TextView employeeName = (TextView) findViewById(R.id.profile_name);
employeeName.setText(this.currentEmployee.getName());

ImageView employeeImage = (ImageView) findViewById(R.id.profile_image);

try
{
Database.getInstance().downloadImage(Employee.class, this.currentEmployee.getId(),
getApplicationContext(), employeeImage);
}
catch(DatabaseException ex)
{
ex.printStackTrace();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void onClick(View v) {
MainActivity.getInstance().setCurrentPhase(pp);
checkIn.setImageResource(R.drawable.icon_user_vink);
} else {
MainActivity.getInstance().setCurrentPhase(new Phase("100", ""));
MainActivity.getInstance().setCurrentPhase(null);
checkIn.setImageResource(R.drawable.checkinbtn);
}
}
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/org/fontys/trackmyprint/database/Database.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.fontys.trackmyprint.database;

import android.content.Context;
import android.widget.ImageView;

import org.fontys.trackmyprint.database.entities.Employee;
import org.fontys.trackmyprint.database.entities.Entity;
import org.fontys.trackmyprint.database.entities.Order;
import org.fontys.trackmyprint.database.entities.Phase;
import org.fontys.trackmyprint.database.entities.Product;
Expand Down Expand Up @@ -325,6 +329,16 @@ public void removeDatabaseListener(DatabaseListener databaseListener)
this.databaseImpl.removeDatabaseListener(databaseListener);
}

@Override
public <T extends Entity> void downloadImage(Class<T> tClass, String id, Context context,
ImageView imageView)
throws
IllegalArgumentException,
DatabaseException
{
this.databaseImpl.downloadImage(tClass, id, context, imageView);
}

public static void initializeInstance()
throws
IllegalStateException,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.fontys.trackmyprint.database;

import android.content.Context;
import android.widget.ImageView;

import org.fontys.trackmyprint.database.entities.Employee;
import org.fontys.trackmyprint.database.entities.Entity;
import org.fontys.trackmyprint.database.entities.Order;
import org.fontys.trackmyprint.database.entities.Phase;
import org.fontys.trackmyprint.database.entities.Product;
Expand Down Expand Up @@ -162,4 +166,9 @@ void update(User user)
void addDatabaseListener(DatabaseListener databaseListener);

void removeDatabaseListener(DatabaseListener databaseListener);

<T extends Entity> void downloadImage(Class<T> tClass, String id, Context context, ImageView imageView)
throws
IllegalArgumentException,
DatabaseException;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package org.fontys.trackmyprint.database;

import android.content.Context;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.firebase.ui.storage.images.FirebaseImageLoader;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;

import org.fontys.trackmyprint.database.entities.Employee;
import org.fontys.trackmyprint.database.entities.Entity;
Expand Down Expand Up @@ -33,6 +40,8 @@
*/
public final class FirebaseDatabaseImpl implements DatabaseImpl
{
private static final String IMAGE_EXTENSION = ".png";

private final class EntityInitializer<T extends Entity> implements ValueEventListener
{
private final Class<T> tClass;
Expand Down Expand Up @@ -67,7 +76,9 @@ public void onDataChange(DataSnapshot dataSnapshot)

for(DatabaseListener databaseListener : this.databaseListeners)
{
this.databaseInitializedTrigger.trigger(databaseListener, Collections.unmodifiableMap(this.dataMap));
this.databaseInitializedTrigger.trigger(databaseListener,
Collections.unmodifiableMap(
this.dataMap));
}
}
finally
Expand Down Expand Up @@ -285,6 +296,7 @@ public void triggerChanged(DatabaseListener databaseListener, User entity)
private boolean initialized;
private volatile boolean deInitialized;
private FirebaseDatabase fBase;
private FirebaseStorage fStorage;
private DatabaseReference employeeRef;
private DatabaseReference orderRef;
private DatabaseReference phaseRef;
Expand Down Expand Up @@ -312,6 +324,7 @@ public FirebaseDatabaseImpl()
this.initialized = false;
this.deInitialized = false;
this.fBase = null;
this.fStorage = null;
this.employeeRef = null;
this.orderRef = null;
this.phaseRef = null;
Expand Down Expand Up @@ -351,6 +364,7 @@ public void initialize()
try
{
this.fBase = FirebaseDatabase.getInstance();
this.fStorage = FirebaseStorage.getInstance();
DatabaseReference rootRef = this.fBase.getReference();

this.employeeRef = rootRef.child(Employee.class.getSimpleName());
Expand Down Expand Up @@ -563,7 +577,8 @@ public Order createOrder(User user, Order.OrderStatus orderStatus, Calendar orde
}

Order newOrder = new Order(newOrderRef.getKey(), user.getId(), orderStatus,
orderDate == null ? null : this.simpleDateFormat.format(orderDate.getTime()), productIds);
orderDate == null ? null : this.simpleDateFormat.format(
orderDate.getTime()), productIds);

newOrderRef.setValue(newOrder);

Expand Down Expand Up @@ -713,11 +728,10 @@ public ProductPhase createProductPhase(Calendar startDate, Calendar endDate,

ProductPhase newProductPhase = new ProductPhase(newProductPhaseRef.getKey(),
startDate ==
null ? null : this.simpleDateFormat.format(startDate.getTime()),
endDate ==
null ? null : this.simpleDateFormat.format(endDate.getTime()),
productPhaseStatus, employee.getId(),
phase.getId());
null ? null : this.simpleDateFormat.format(
startDate.getTime()), endDate ==
null ? null : this.simpleDateFormat.format(
endDate.getTime()), productPhaseStatus, employee.getId(), phase.getId());

newProductPhaseRef.setValue(newProductPhase);

Expand Down Expand Up @@ -1247,6 +1261,36 @@ public void removeDatabaseListener(DatabaseListener databaseListener)
}
}

@Override
public <T extends Entity> void downloadImage(Class<T> tClass, String id, Context context,
ImageView imageView)
throws
IllegalArgumentException,
DatabaseException
{
Throw.ifNull(IllegalArgumentException.class, tClass, "tClass");
Throw.ifNull(IllegalArgumentException.class, id, "id");
Throw.ifNull(IllegalArgumentException.class, context, "context");
Throw.ifNull(IllegalArgumentException.class, imageView, "imageView");

try
{
StorageReference rootRef = this.fStorage.getReference();
StorageReference entityRef = rootRef.child(tClass.getSimpleName());

StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(id).append(IMAGE_EXTENSION);

StorageReference imageRef = entityRef.child(stringBuilder.toString());

Glide.with(context).using(new FirebaseImageLoader()).load(imageRef).into(imageView);
}
catch(Exception ex)
{
throw new DatabaseException(ex);
}
}

private <T extends Entity> void throwIfExists(Map<String, T> dataMap, String key,
String message)
throws
Expand Down
26 changes: 26 additions & 0 deletions app/src/main/java/org/fontys/trackmyprint/utils/Phone.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.fontys.trackmyprint.utils;

import android.content.Context;
import android.telephony.TelephonyManager;

/**
* Created by guido on 13-Jan-17.
*/
public final class Phone
{
private Phone()
{

}

public static String getIMEI(Context context)
throws
IllegalArgumentException
{
Throw.ifNull(IllegalArgumentException.class, context, "context");

TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);

return telephonyManager.getDeviceId();
}
}
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
Expand All @@ -15,6 +16,7 @@ buildscript {
allprojects {
repositories {
jcenter()
mavenCentral()
}
}

Expand Down

0 comments on commit a0be7b4

Please sign in to comment.