Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Commit

Permalink
Mood with email and somes changes and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony committed Jul 6, 2016
1 parent ff4d78c commit 57dc946
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 17 deletions.
8 changes: 8 additions & 0 deletions app/src/main/java/ch/xero88/goodmood/Data/Position.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ public class Position {
private Double latitude;
private Double longitude;

public Position() {
}

public Position(Double latitude, Double longitude) {
this.latitude = latitude;
this.longitude = longitude;
}

public Double getLatitude() {
return latitude;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@
*/
public class MoodMarkerRendered extends DefaultClusterRenderer<Mood> {

private Context context;

public MoodMarkerRendered(Context context, GoogleMap map,
ClusterManager<Mood> clusterManager) {
super(context, map, clusterManager);
this.context = context;
}

@Override
protected void onBeforeClusterItemRendered(Mood item, MarkerOptions markerOptions) {
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_goodmood));
markerOptions.title(item.getId());
markerOptions.title(item.getDisplayName() + " " + context.getString(R.string.has_a_good_mood_here));
super.onBeforeClusterItemRendered(item, markerOptions);
}
}
45 changes: 42 additions & 3 deletions app/src/main/java/ch/xero88/goodmood/Mood/FirebaseRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.support.annotation.NonNull;
import android.util.Log;

import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
Expand Down Expand Up @@ -31,7 +32,7 @@ public FirebaseRepository() {
@Override
public void getMoods(@NonNull final LoadMoodsCallback callback) {

ValueEventListener listener = new ValueEventListener() {
final ValueEventListener listener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<Mood> moods = new ArrayList<>();
Expand All @@ -40,6 +41,7 @@ public void onDataChange(DataSnapshot dataSnapshot) {
moods.add(mood);
}
callback.onMoodLoaded(moods);

}

@Override
Expand All @@ -53,13 +55,50 @@ public void onCancelled(DatabaseError databaseError) {

}


@Override
public void getChangedMoods(@NonNull final ChangedMoodsCallback callback) {

final ChildEventListener listener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Mood mood = dataSnapshot.getValue(Mood.class);
callback.onMoodAdded(mood);
}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
// TODO: 07/07/2016
}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
// TODO: 07/07/2016
}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
// TODO: 07/07/2016
}

@Override
public void onCancelled(DatabaseError databaseError) {
// TODO: 07/07/2016
}

};
mRef.addChildEventListener(listener);

}


@Override
public void getMood(@NonNull String noteId, @NonNull GetMoodCallback callback) {
// TODO: 05/07/2016
}

@Override
public void saveMood(@NonNull Mood note) {
// TODO: 05/07/2016
public void saveMood(@NonNull Mood mood) {
mRef.child(mood.getId()).setValue(mood);
}
}
21 changes: 21 additions & 0 deletions app/src/main/java/ch/xero88/goodmood/Mood/Mood.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import com.google.android.gms.maps.model.LatLng;
import com.google.firebase.database.Exclude;
import com.google.maps.android.clustering.ClusterItem;

import ch.xero88.goodmood.Data.Position;
Expand All @@ -13,8 +14,11 @@ public class Mood implements ClusterItem {

private String id;
private String name;
@Exclude
private LatLng latLng;
private Position position;
private String email;
private String displayName;

public String getId() {
return id;
Expand All @@ -37,6 +41,7 @@ public LatLng getPosition() {
return latLng;
}

@Exclude
public Position getMoodPosition() {
return position;
}
Expand All @@ -45,4 +50,20 @@ public void setPosition(Position position) {
this.position = position;
this.latLng = new LatLng(position.getLatitude(), position.getLongitude());
}

public void setEmail(String email) {
this.email = email;
}

public String getEmail() {
return email;
}

public void setDisplayName(String displayName) {
this.displayName = displayName;
}

public String getDisplayName() {
return displayName;
}
}
12 changes: 12 additions & 0 deletions app/src/main/java/ch/xero88/goodmood/Mood/MoodRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import android.support.annotation.NonNull;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;

import java.util.List;

/**
Expand All @@ -15,12 +18,21 @@ interface LoadMoodsCallback {
void onMoodFailed();
}

interface ChangedMoodsCallback{
void onMoodAdded(Mood mood);
void onMoodChanged(Mood mood);
void onMoodRemoved(Mood moods);
void onMoodMoved(Mood mood);
void onCancelled();
}

interface GetMoodCallback {

void onMoodLoaded(Mood mood);
}

void getMoods(@NonNull LoadMoodsCallback callback);
void getChangedMoods(@NonNull ChangedMoodsCallback callback);
void getMood(@NonNull String noteId, @NonNull GetMoodCallback callback);
void saveMood(@NonNull Mood note);
}
19 changes: 19 additions & 0 deletions app/src/main/java/ch/xero88/goodmood/Utils/StringUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ch.xero88.goodmood.Utils;

import java.security.SecureRandom;

/**
* Created by Xero88 on 06/07/2016.
*/
public class StringUtil {

static final String AB = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
static SecureRandom rnd = new SecureRandom();

public static String randomString( int len ){
StringBuilder sb = new StringBuilder( len );
for( int i = 0; i < len; i++ )
sb.append( AB.charAt( rnd.nextInt(AB.length()) ) );
return sb.toString();
}
}
12 changes: 4 additions & 8 deletions app/src/main/java/ch/xero88/goodmood/View/LoginActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.facebook.AccessToken;
Expand All @@ -18,18 +15,13 @@
import com.facebook.appevents.AppEventsLogger;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FacebookAuthProvider;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.maps.android.clustering.ClusterManager;

import ch.xero88.goodmood.Mood.Mood;
import ch.xero88.goodmood.Mood.MoodRepository;
import ch.xero88.goodmood.R;

/**
Expand Down Expand Up @@ -61,6 +53,10 @@ public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (user != null) {
// User is signed in
Log.e(TAG, "onAuthStateChanged:signed_in:" + user.getUid());

Intent intent = new Intent(LoginActivity.this, MapsActivity.class);
startActivity(intent);

} else {
// User is signed out
Log.e(TAG, "onAuthStateChanged:signed_out");
Expand Down
64 changes: 59 additions & 5 deletions app/src/main/java/ch/xero88/goodmood/View/MapsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
import android.support.v4.content.ContextCompat;
import android.util.Log;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.maps.android.clustering.ClusterManager;

import java.util.List;
Expand All @@ -25,6 +24,7 @@
import ch.xero88.goodmood.Mood.MoodRepositories;
import ch.xero88.goodmood.Mood.MoodRepository;
import ch.xero88.goodmood.R;
import ch.xero88.goodmood.Utils.StringUtil;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

Expand All @@ -46,6 +46,22 @@ protected void onCreate(Bundle savedInstanceState) {
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

// TODO: 06/07/2016 delete
Mood mood = new Mood();
mood.setId(StringUtil.randomString(20));
Position position = new Position(46.233122, 7.360626);
mood.setPosition(position);

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
String email = user.getEmail();
mood.setEmail(email);
mood.setDisplayName(user.getDisplayName());
}

MoodRepository repository = MoodRepositories.getFirebaseRepoInstance();
repository.saveMood(mood);
}

@Override
Expand All @@ -59,18 +75,50 @@ public void onMapReady(GoogleMap googleMap) {
// Set up cluster
setUpClusterer();

// get moods
repository = MoodRepositories.getFirebaseRepoInstance();
// get moods
/*
repository.getMoods(new MoodRepository.LoadMoodsCallback() {
@Override
public void onMoodLoaded(List<Mood> moods) {
placeMoodsOnMap(moods);
Log.e(TAG, "Place moods on map (must be called once)");
}
@Override
public void onMoodFailed() {
Log.e(TAG, "Moods failed");
}
});*/

// get new moods if any
repository.getChangedMoods(new MoodRepository.ChangedMoodsCallback() {

@Override
public void onMoodAdded(Mood mood) {
placeMoodOnMap(mood);
Log.e(TAG, "Place a mood");
}

@Override
public void onMoodChanged(Mood mood) {

}

@Override
public void onMoodRemoved(Mood moods) {

}

@Override
public void onMoodMoved(Mood mood) {

}

@Override
public void onCancelled() {

}
});
}

Expand All @@ -89,10 +137,16 @@ private void setUpClusterer() {
mMap.setOnMarkerClickListener(mClusterManager);
}

private void placeMoodOnMap(Mood mood) {
mClusterManager.addItem(mood);
mClusterManager.cluster();
}

private void placeMoodsOnMap(List<Mood> moods) {

// place on map moods
for (Mood mood: moods) {
mClusterManager.addItem(mood);
placeMoodOnMap(mood);
}
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<resources>
<string name="app_name">Good Mood</string>
<string name="facebook_app_id">1732205433701723</string>
<string name="has_a_good_mood_here">has a good mood here !</string>
</resources>

0 comments on commit 57dc946

Please sign in to comment.