Skip to content

Commit

Permalink
v0.1-alpha2 hotfix
Browse files Browse the repository at this point in the history
Fixed a critical bug on v0.1-alpha1> (projects don't get detected as a sketchcollab project after being uploaded) and some bugfixes (#27)
  • Loading branch information
iyxan23 authored Feb 12, 2021
2 parents dd7fb6d + 1535a2a commit 7199414
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 18 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId "com.iyxan23.sketch.collab"
minSdkVersion 22
targetSdkVersion 30
versionCode 1
versionName "0.1-alpha"
versionCode 2
versionName "0.1-alpha1"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
android:name=".online.UploadActivity"
android:theme="@style/Theme.SketchCollab.NoActionBar" />
<activity
android:name=".SketchwareProjectsListActivity"
android:name=".pman.SketchwareProjectsListActivity"
android:theme="@style/Theme.SketchCollab.NoActionBar" />
<activity
android:name=".LoginActivity"
Expand Down
42 changes: 34 additions & 8 deletions app/src/main/java/com/iyxan23/sketch/collab/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.iyxan23.sketch.collab.models.SketchwareProject;
import com.iyxan23.sketch.collab.models.SketchwareProjectChanges;
import com.iyxan23.sketch.collab.online.BrowseActivity;
import com.iyxan23.sketch.collab.pman.SketchwareProjectsListActivity;

import org.json.JSONException;

Expand Down Expand Up @@ -223,7 +224,32 @@ private void initialize() {
boolean is_project_public = project.isSketchCollabProjectPublic();
String author = project.getSketchCollabAuthorUid();

if (!author.equals(auth.getUid())) {
// Fetch the project data
Task<DocumentSnapshot> task_proj =
database.collection(is_project_public ? "projects" : "userdata/" + author + "/projects").document(project_key)
.get(Source.SERVER); // Don't get the cache :/

// Wait for the task to finish, i don't want to query a lot of tasks in a short amount of time
DocumentSnapshot project_data = Tasks.await(task_proj);

// Check if task is successful or not
if (!task_proj.isSuccessful()) {
assert task_proj.getException() != null; // Exception shouldn't be null if the task is not successful

Toast.makeText(MainActivity.this, "Error: " + task_proj.getException().getMessage(), Toast.LENGTH_LONG).show();
return;
}

assert project_data != null; // This shouldn't be null

ArrayList<String> members = (ArrayList<String>) project_data.get("members");

// Fallback to an empty arraylist if members is null / doesn't exist
// (To avoid NPE)
members = members == null ? new ArrayList<>() : members;

// Check if user is an author / a member of this project
if (!author.equals(auth.getUid()) && !members.contains(auth.getUid())) {
// Hmm, the user "stole" another user's project
// Let's skip this one :e_sweat_smile:
// =========================================================================
Expand All @@ -241,7 +267,7 @@ private void initialize() {
.get(Source.SERVER); // Don't get the cache :/

// Wait for the task to finish, i don't want to query a lot of tasks in a short amount of time
QuerySnapshot snapshot = Tasks.await(task);
QuerySnapshot commit_snapshot = Tasks.await(task);

// Check if task is successful or not
if (!task.isSuccessful()) {
Expand All @@ -251,15 +277,15 @@ private void initialize() {
return;
}

assert snapshot != null; // snapshot shouldn't be null
assert commit_snapshot != null; // snapshot shouldn't be null

Log.d("MainActivity", "documents: " + snapshot.getDocuments());
Log.d("MainActivity", "documents: " + commit_snapshot.getDocuments());
Log.d("MainActivity", "project_key: " + project_key);

// Check if the project doesn't exists in the database.
if (snapshot.getDocuments().size() == 0) continue;
if (commit_snapshot.getDocuments().size() == 0) continue;

DocumentSnapshot commit_info = snapshot.getDocuments().get(0);
DocumentSnapshot commit_info = commit_snapshot.getDocuments().get(0);

if (!project_commit.equals(commit_info.getId())) {
// Hmm, looks like this man's project has an older commit, tell him to update his project
Expand All @@ -280,13 +306,13 @@ private void initialize() {
// Alright looks like he's got some local updates with the same head commit

// Fetch the project
Task<QuerySnapshot> project_data =
Task<QuerySnapshot> project_snapshot =
database.collection(is_project_public ? "projects" : "userdata/" + author + "/projects").document(project_key).collection("snapshot")
.get(Source.SERVER); // Don't get the cache :/

// Wait for the task to finish, i don't want to query a lot of tasks in a short amount of time,
// it can cause some performance issues
QuerySnapshot project_data_snapshot = Tasks.await(project_data);
QuerySnapshot project_data_snapshot = Tasks.await(project_snapshot);
SketchwareProject head_project = querySnapshotToSketchwareProject(project_data_snapshot);

// Add this to the changed sketchcollab sketchware projects arraylist
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.google.android.gms.tasks.Tasks;
import com.google.firebase.Timestamp;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ protected void onCreate(Bundle savedInstanceState) {
members = getIntent().getParcelableArrayListExtra("members");
members_before.addAll(members);

update_members_text();

TextInputEditText description_edit = findViewById(R.id.description_edit_project_text);
description_edit.setText(description_before);
}
Expand Down Expand Up @@ -127,7 +129,7 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.

if (requestCode == MEMBER_USER_PICK_REQ_CODE) {
if (resultCode == Activity.RESULT_OK) {
members.addAll( Objects.requireNonNull(data) .getParcelableArrayListExtra("selected_users"));
members = Objects.requireNonNull(data) .getParcelableArrayListExtra("selected_users");

update_members_text();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,29 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
upload_batch
.commit()
.addOnSuccessListener(result -> {
// Add a custom key to the sketchware project named "sk-colab-key": "(pushkey)" and "sk-collab-owner": "(uid)"
try {
swProj.mysc_project = Util.encrypt(
new JSONObject(
Util.decrypt(swProj.mysc_project)
)
.put("sk-collab-key", projectRefDoc.getId()) // The document id (location of the project)
.put("sk-collab-owner", auth.getUid()) // The owner of the project (used to access private projects)
.put("sk-collab-latest-commit", "initial") // The latest commit id of this project
.put("sk-collab-project-visibility", isPrivate.isChecked() ? "private" : "public") // The project visibility. Don't worry, it's used to determine where is the project located in the database
.toString()
.getBytes()
);
swProj.applyChanges();
} catch (JSONException | IOException e) {
// This shouldn't happen
e.printStackTrace();
Toast.makeText(UploadActivity.this, "An error occured while applying the key: " + e.getMessage(), Toast.LENGTH_LONG).show();

// Stop
return;
}

Toast.makeText(UploadActivity.this, "Project Uploaded", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
finish();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
Expand Down Expand Up @@ -39,6 +40,8 @@

public class ViewOnlineProjectActivity extends AppCompatActivity {

private static final String TAG = "ViewOnlineProjectActivi";

private ActivityViewOnlineProjectBinding binding;

private String description_;
Expand Down Expand Up @@ -134,6 +137,8 @@ protected void onCreate(Bundle savedInstanceState) {
})
*/
.addOnSuccessListener(result -> {
Log.d(TAG, "onCreate: Fetch success");

DocumentSnapshot project_data = tmp[0];
DocumentSnapshot uploader_userdata = tmp[1];
DocumentSnapshot latest_commit = result.getDocuments().get(0);
Expand Down Expand Up @@ -168,15 +173,15 @@ protected void onCreate(Bundle savedInstanceState) {
commit_start_id.setText(first_commit_id);
description_textview.setText(description);

Log.d(TAG, "onCreate: Fetching members");
fetchMembers(members_);

// Hide the progressbar
findViewById(R.id.progress_project).setVisibility(View.GONE);
})
.addOnFailureListener(e -> Toast.makeText(this, "Error while fetching: " + e.getMessage(), Toast.LENGTH_LONG).show());
}

private void fetchMembers(List<String> members_) {
Log.d(TAG, "fetchMembers: members_: " + members_);

// Check if members_ is null bruh
if (members_ == null) {
// kekw we're outta here
Expand All @@ -191,6 +196,8 @@ private void fetchMembers(List<String> members_) {
if (cached_names.containsKey(uid)) {
username = cached_names.get(uid);

Log.d(TAG, "fetchMembers: Username already cached: " + uid + "; value: " + username);

} else {
// Fetch it's username
Task<DocumentSnapshot> userdata_fetch = userdata.document(uid).get();
Expand All @@ -208,6 +215,8 @@ private void fetchMembers(List<String> members_) {

username = user.getString("name");

Log.d(TAG, "fetchMembers: Username fetched, uid: " + uid + ", name: " + username);

cached_names.put(uid, username);

} catch (ExecutionException | InterruptedException e) {
Expand All @@ -218,8 +227,26 @@ private void fetchMembers(List<String> members_) {
}
}

Log.d(TAG, "fetchMembers: Add member: " + username + " (" + uid + ")");
members.add(new Userdata(username, uid));
}

// Update the textview
runOnUiThread(() -> {
TextView members_list = findViewById(R.id.members_list);
members_list.setText("");

boolean is_start = false;

for (Userdata member_userdata: members) {
members_list.append((is_start ? ", " : "") + member_userdata.getName());

is_start = true;
}

// Hide the progressbar
findViewById(R.id.progress_project).setVisibility(View.GONE);
});
}).start();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.iyxan23.sketch.collab;
package com.iyxan23.sketch.collab.pman;

import android.os.Bundle;
import android.view.View;
Expand All @@ -8,6 +8,8 @@
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.iyxan23.sketch.collab.R;
import com.iyxan23.sketch.collab.Util;
import com.iyxan23.sketch.collab.adapters.SketchwareProjectAdapter;
import com.iyxan23.sketch.collab.models.SketchwareProject;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackground"
tools:context=".SketchwareProjectsListActivity">
tools:context=".pman.SketchwareProjectsListActivity">

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/app_bar_proj_list"
Expand Down

0 comments on commit 7199414

Please sign in to comment.