Skip to content
Snippets Groups Projects
Commit 0ef6ca6a authored by ect1u17's avatar ect1u17
Browse files

Started working on the group pages - very buggy still

parent 75bbc3ce
No related branches found
No related tags found
No related merge requests found
package com.yearthreeproject.xbframework;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import org.bson.Document;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import io.realm.mongodb.App;
import io.realm.mongodb.mongo.MongoCollection;
import io.realm.mongodb.mongo.iterable.MongoCursor;
import static com.google.android.gms.tasks.Tasks.await;
public class GroupsActivity extends AppCompatActivity {
private static final String TAG = "GroupsActivity";
private App realmApp;
private Menu infoMenu;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
realmApp = ((MongoRealmApp)this.getApplication()).getApp();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_groups);
Toolbar toolbar = findViewById(R.id.toolbar);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
toolbar.setNestedScrollingEnabled(false);
......@@ -24,7 +54,78 @@ public class GroupsActivity extends AppCompatActivity {
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
LinearLayout groupLayout = findViewById(R.id.GroupActivityLayout);
new Thread(()->{
try {
MongoCollection<Document> groupCollection = realmApp.currentUser().getMongoClient("mongodb-atlas").getDatabase("V1").getCollection("Groups");
MongoCursor<Document> found = await(groupCollection.find(new Document("users", new Document("$elemMatch", new Document("$eq", realmApp.currentUser().getId())))).iterator());
Log.d(TAG, "onCreate: " + found);
while (found.hasNext()) {
Document group = found.next();
Button groupButton = new Button(this);
groupButton.setText(group.getString("name"));
groupButton.setOnClickListener(v ->{
Log.d(TAG, "onCreate: " + group.toString());
Intent groupExperiment = new Intent(this, SurveyResponseActivity.class);
groupExperiment.putExtra("group", group.toJson());
startActivity(groupExperiment);
});
groupLayout.post(() -> groupLayout.addView(groupButton));
}
} catch(InterruptedException | ExecutionException e){
e.printStackTrace();
}
}).start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.groups_menu, menu);
infoMenu = menu;
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.add_group)
{
final String[] m_Text = {""};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Enter the group id:");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
input.setFilters(new InputFilter[] {new InputFilter.LengthFilter(6)});
builder.setView(input);
builder.setPositiveButton("OK", (dialog, which) -> {
new Thread(() -> {
try {
m_Text[0] = input.getText().toString();
Log.d(TAG, "onOptionsItemSelected: " + m_Text[0]);
// attach the user to the group - if it exists, otherwise tell the user that the group does not exist:
MongoCollection<Document> groupCollection = realmApp.currentUser().getMongoClient("mongodb-atlas").getDatabase("V1").getCollection("Groups");
MongoCollection<Document> userCollection = realmApp.currentUser().getMongoClient("mongodb-atlas").getDatabase("V1").getCollection("Users");
// check if the code does not match one that the user is already in then apply the filter
Document foundDocument = await(groupCollection.findOneAndUpdate(new Document("identifier", m_Text[0]), new Document("$addToSet", new Document("users", realmApp.currentUser().getId()))));
if(foundDocument != null) {
Log.d(TAG, "onOptionsItemSelected: found document " + foundDocument);
Document userUpdate = await(userCollection.findOneAndUpdate(new Document("_user_id", realmApp.currentUser().getId()), new Document("$addToSet", new Document("groups", foundDocument.getObjectId("_id")))));
Log.d(TAG, "onOptionsItemSelected: updated user" + userUpdate);
} else {
// this means the group does not exist in the database
Log.d(TAG, "onOptionsItemSelected: no document found (do a popup for this result)");
}
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
}).start();
});
builder.setNegativeButton("Cancel", (dialog, which) -> dialog.cancel());
builder.show();
}
return super.onOptionsItemSelected(item);
}
}
\ No newline at end of file
......@@ -52,7 +52,7 @@ public class MainActivity extends AppCompatActivity {
private MongoCollection<Document> userCollection;
private MongoCollection<Document> experimentsCollection;
Menu infoMenu;
private Menu infoMenu;
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
......@@ -134,11 +134,9 @@ public class MainActivity extends AppCompatActivity {
ProjectMacros.saveFile(MainActivity.this, "localUserData.json", user.toString());
// DEBUG INTENT FOR QUICK ACCESS TO ANOTHER INTENT
/*// DEBUG INTENT FOR QUICK ACCESS TO ANOTHER INTENT
Intent open_quicker_DEBUG = new Intent(this, MetronomeActivity.class);
startActivity(open_quicker_DEBUG);
startActivity(open_quicker_DEBUG);*/
if(user.optJSONObject("current_experiment").optBoolean("locked", false)) checkReleaseFromExperiment();
else checkNotifications();
......@@ -156,6 +154,11 @@ public class MainActivity extends AppCompatActivity {
homeLayout.removeView(surveyButton);
}
Button metronome = new Button(this);
metronome.setText("Metronome");
homeLayout.addView(metronome);
metronome.setOnClickListener(v -> startActivity(new Intent(this, MetronomeActivity.class)));
Button logoutButton = findViewById(R.id.HomeLogoutButton);
logoutButton.setOnClickListener(v -> realmApp.currentUser().logOutAsync(logoutTask -> {
SharedPreferences.Editor savedSettingsEditor = savedSettings.edit();
......@@ -171,6 +174,9 @@ public class MainActivity extends AppCompatActivity {
Button progressButton = findViewById(R.id.HomeProgressButton);
progressButton.setOnClickListener(v -> launchProgressPage());
Button groupsButton = findViewById(R.id.HomeGroupsButton);
groupsButton.setOnClickListener(v -> startActivity(new Intent(this, GroupsActivity.class)));
experimentsButton.setOnClickListener(v -> launchBoxPage());
surveyButton.setOnClickListener(this::surveyButtonEvent);
......
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GroupsActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:minHeight="56dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_groups" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/GroupActivityLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:layout_marginTop="32dp"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".GroupsActivity"
tools:showIn="@layout/activity_groups">
</LinearLayout>
......@@ -39,6 +39,15 @@
android:layout_weight="1"
android:text="@string/MainProgress" />
<Button
android:id="@+id/HomeGroupsButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:backgroundTint="@color/colorPrimaryAccent"
android:text="@string/MainGroups"
android:textColor="@color/colorPrimary" />
<Button
android:id="@+id/HomeAboutButton"
android:layout_width="match_parent"
......
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/add_group"
android:icon="@android:drawable/ic_menu_add"
android:title="Add group"
app:showAsAction="ifRoom" />
</menu>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_group_info"
android:icon="@drawable/ic_baseline_info_24"
android:title="GroupSurveyInfo"
app:showAsAction="ifRoom" />
</menu>
\ No newline at end of file
......@@ -180,5 +180,6 @@
<string name="startButtonLabel">Start</string>
<string name="stopButtonLabel">Stop</string>
<string name="two">Two</string>
<string name="MainGroups">Groups</string>
<!-- Metronome strings End -->
</resources>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment