Skip to content
This repository has been archived by the owner on Aug 13, 2021. It is now read-only.

Commit

Permalink
Merge pull request #108 from schul-cloud/98-integrate-demo-mode
Browse files Browse the repository at this point in the history
98 integrate demo mode
  • Loading branch information
Niklas Kiefer authored Dec 18, 2017
2 parents c89448b + 318fe31 commit 7d83f85
Show file tree
Hide file tree
Showing 31 changed files with 347 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package org.schulcloud.mobile.test.common;

import org.schulcloud.mobile.data.model.CurrentUser;
import org.schulcloud.mobile.data.model.Device;
import org.schulcloud.mobile.data.model.Event;
import org.schulcloud.mobile.data.model.Homework;
import org.schulcloud.mobile.data.model.RealmString;
import org.schulcloud.mobile.data.model.User;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import io.realm.RealmList;

/**
* Factory class that makes instances of data models with random field values.
* The aim of this class is to help setting up test fixtures.
Expand All @@ -27,6 +31,18 @@ public static User makeUser(String uniqueSuffix) {
u.setFirstName(uniqueSuffix);
return u;
}
public static CurrentUser makeCurrentUser(String uniqueSuffix,
boolean permissionHomeworkCreate) {
CurrentUser u = new CurrentUser();
u.set_id(randomUuid());
u.setLastName(uniqueSuffix);
u.setEmail(uniqueSuffix);
u.setFirstName(uniqueSuffix);
if (permissionHomeworkCreate)
u.setPermissions(
new RealmList<>(new RealmString(CurrentUser.PERMISSION_HOMEWORK_CREATE)));
return u;
}

public static List<User> makeListUsers(int number) {
List <User> users = new ArrayList<>();
Expand Down
54 changes: 31 additions & 23 deletions app/src/main/java/org/schulcloud/mobile/data/DataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import okhttp3.ResponseBody;
import retrofit2.Response;
import rx.Observable;
import rx.Single;
import rx.functions.Func1;

@Singleton
Expand Down Expand Up @@ -87,16 +88,13 @@ public String getAccessToken() {

public Observable<CurrentUser> signIn(String username, String password) {
return mRestService.signIn(new Credentials(username, password))
.concatMap(new Func1<AccessToken, Observable<CurrentUser>>() {
@Override
public Observable<CurrentUser> call(AccessToken accessToken) {
// save current user data
String jwt = mPreferencesHelper.saveAccessToken(accessToken);
String currentUser = JWTUtil.decodeToCurrentUser(jwt);
mPreferencesHelper.saveCurrentUserId(currentUser);
.concatMap(accessToken -> {
// save current user data
String jwt = mPreferencesHelper.saveAccessToken(accessToken);
String currentUser = JWTUtil.decodeToCurrentUser(jwt);
mPreferencesHelper.saveCurrentUserId(currentUser);

return syncCurrentUser(currentUser);
}
return syncCurrentUser(currentUser);
});
}
public void signOut() {
Expand All @@ -105,24 +103,32 @@ public void signOut() {
}

public Observable<CurrentUser> syncCurrentUser(String userId) {
return mRestService.getUser(getAccessToken(), userId).concatMap(new Func1<CurrentUser, Observable<CurrentUser>>() {
@Override
public Observable<CurrentUser> call(CurrentUser currentUser) {
mPreferencesHelper.saveCurrentUsername(currentUser.displayName);
mPreferencesHelper.saveCurrentSchoolId(currentUser.schoolId);
return mDatabaseHelper.setCurrentUser(currentUser);
}
}).doOnError(Throwable::printStackTrace);
return mRestService.getUser(getAccessToken(), userId).concatMap(
new Func1<CurrentUser, Observable<CurrentUser>>() {
@Override
public Observable<CurrentUser> call(CurrentUser currentUser) {
mPreferencesHelper.saveCurrentUsername(currentUser.displayName);
mPreferencesHelper.saveCurrentSchoolId(currentUser.schoolId);
return mDatabaseHelper.setCurrentUser(currentUser);
}
}).doOnError(Throwable::printStackTrace);
}

public Observable<CurrentUser> getCurrentUser() {
return mDatabaseHelper.getCurrentUser().distinct();
public Single<CurrentUser> getCurrentUser() {
return mDatabaseHelper.getCurrentUser();
}

public String getCurrentUserId() {
return mPreferencesHelper.getCurrentUserId();
}

public void setInDemoMode(boolean isInDemoMode) {
mPreferencesHelper.saveIsInDemoMode(isInDemoMode);
}
public Single<Boolean> isInDemoMode() {
return Single.just(mPreferencesHelper.isInDemoMode());
}


/**** FileStorage ****/

Expand All @@ -138,7 +144,8 @@ public Observable<File> call(FilesResponse filesResponse) {

// set fullPath for every file
for (File file : filesResponse.files) {
file.fullPath = file.key.substring(0, file.key.lastIndexOf(java.io.File.separator));
file.fullPath = file.key.substring(0,
file.key.lastIndexOf(java.io.File.separator));
files.add(file);
}

Expand Down Expand Up @@ -172,7 +179,7 @@ public Observable<Directory> call(FilesResponse filesResponse) {
mDatabaseHelper.clearTable(Directory.class);

List<Directory> improvedDirs = new ArrayList<Directory>();
for(Directory d : filesResponse.directories) {
for (Directory d : filesResponse.directories) {
d.path = getCurrentStorageContext();
improvedDirs.add(d);
}
Expand Down Expand Up @@ -206,7 +213,7 @@ public Observable<ResponseBody> downloadFile(String url) {
}

public Observable<ResponseBody> uploadFile(java.io.File file, SignedUrlResponse signedUrlResponse) {
RequestBody requestBody = RequestBody.create(MediaType.parse("file/*"), file);
RequestBody requestBody = RequestBody.create(MediaType.parse("file/*"), file);
return mRestService.uploadFile(
signedUrlResponse.url,
signedUrlResponse.header.getContentType(),
Expand All @@ -224,7 +231,8 @@ public Observable<ResponseBody> deleteFile(String path) {
public String getCurrentStorageContext() {
String storageContext = mPreferencesHelper.getCurrentStorageContext();
// personal files are default
return storageContext.equals("null") ? "users/" + this.getCurrentUserId() + "/" : storageContext + "/";
return storageContext.equals(
"null") ? "users/" + this.getCurrentUserId() + "/" : storageContext + "/";
}

public void setCurrentStorageContext(String newStorageContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import io.realm.Realm;
import io.realm.RealmResults;
import rx.Observable;
import rx.Single;
import timber.log.Timber;

@Singleton
Expand Down Expand Up @@ -133,9 +134,9 @@ public Observable<CurrentUser> setCurrentUser(final CurrentUser currentUser) {
});
}

public Observable<CurrentUser> getCurrentUser() {
public Single<CurrentUser> getCurrentUser() {
final Realm realm = mRealmProvider.get();
return Observable.just(realm.where(CurrentUser.class).findFirst());
return Single.just(realm.where(CurrentUser.class).findFirst());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class PreferencesHelper {
public static final String PREFERENCE_STORAGE_CONTEXT = "storageContext";
public static final String PREFERENCE_CALENDAR_SYNC_ENABLED = "calendarSyncEnabled";
public static final String PREFERENCE_CALENDAR_SYNC_NAME = "calendarSyncName";
public static final String PREFERENCE_IS_IN_DEMO_MODE = "IS_IN_DEMO_MODE";

private final ObscuredSharedPreferences mPref;

Expand Down Expand Up @@ -122,6 +123,12 @@ public String saveCalendarSyncName(String calendarName) {
return null;
}

public boolean saveIsInDemoMode(boolean isInDemoMode) {
SharedPreferences.Editor editor = mPref.edit();
editor.putBoolean(PREFERENCE_IS_IN_DEMO_MODE, isInDemoMode);
return editor.commit();
}

// ##### Getter

public String getAccessToken() {
Expand Down Expand Up @@ -156,6 +163,10 @@ public String getCalendarSyncName() {
return mPref.getString(PREFERENCE_CALENDAR_SYNC_NAME, "null");
}

public boolean isInDemoMode() {
return mPref.getBoolean(PREFERENCE_IS_IN_DEMO_MODE, false);
}

// ##### Clearing

public void clear() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ public class CurrentUser implements RealmModel {
public static final String PERMISSION_LESSONS_VIEW = "LESSONS_VIEW";
public static final String PERMISSION_TOOL_NEW_VIEW = "TOOL_NEW_VIEW";
public static final String PERMISSION_COURSE_EDIT = "COURSE_EDIT";
public static final String PERMISSION_HOMEWORK_CREATE = "HOMEWORK_CREATE";
public static final String PERMISSION_FILE_CREATE = "FILE_CREATE";
public static final String PERMISSION_FILE_MOVE = "FILE_MOVE";
public static final String PERMISSION_FILE_DELETE = "FILE_DELETE";
public static final String PERMISSION_FOLDER_CREATE = "FOLDER_CREATE";
public static final String PERMISSION_FOLDER_DELETE = "FOLDER_DELETE";
public static final String PERMISSION_TEACHER_CREATE = "TEACHER_CREATE";
public static final String PERMISSION_STUDENT_CREATE = "STUDENT_CREATE";
public static final String PERMISSION_BASE_VIEW = "BASE_VIEW";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.schulcloud.mobile.R;
import org.schulcloud.mobile.data.DataManager;
import org.schulcloud.mobile.data.model.Directory;
import org.schulcloud.mobile.util.ViewUtil;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -24,11 +25,14 @@

public class DirectoriesAdapter
extends RecyclerView.Adapter<DirectoriesAdapter.DirectoriesViewHolder> {

@Inject
FilePresenter mFilesPresenter;
@Inject
DataManager dataManager;
DataManager mDataManager;

private List<Directory> mDirectories;
private boolean mCanDeleteDirectories = false;

@Inject
public DirectoriesAdapter() {
Expand All @@ -38,6 +42,10 @@ public DirectoriesAdapter() {
public void setDirectories(List<Directory> directories) {
mDirectories = directories;
}
public void setCanDeleteDirectories(boolean canDeleteDirectories) {
mCanDeleteDirectories = canDeleteDirectories;
notifyItemRangeChanged(0, mDirectories.size());
}

@Override
public DirectoriesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Expand All @@ -63,6 +71,8 @@ public void onBindViewHolder(DirectoriesViewHolder holder, int position) {

holder.cardView.setOnClickListener(v -> mFilesPresenter.goIntoDirectory(finalPath));
// TODO: refactor it when we also support course/class files

ViewUtil.setVisibility(holder.deleteDirectory, mCanDeleteDirectories);
holder.deleteDirectory.setOnClickListener(
v -> mFilesPresenter.startDirectoryDeleting(finalPath, directory.name));
}
Expand Down
18 changes: 14 additions & 4 deletions app/src/main/java/org/schulcloud/mobile/ui/files/FileFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
Expand Down Expand Up @@ -102,7 +99,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_files, container, false);
ButterKnife.bind(this, view);
setTitle(R.string.files_title);
Expand Down Expand Up @@ -181,6 +178,19 @@ public void showDirectories(List<Directory> directories) {
mDirectoriesAdapter.notifyDataSetChanged();
}

@Override
public void showCanCreateFile(boolean canCreateFile) {
ViewUtil.setVisibility(fileUploadButton, canCreateFile);
}
@Override
public void showCanDeleteFiles(boolean canDeleteFiles) {
mFilesAdapter.setCanDeleteFiles(canDeleteFiles);
}
@Override
public void showCanDeleteDirectories(boolean canDeleteDirectories) {
mDirectoriesAdapter.setCanDeleteDirectories(canDeleteDirectories);
}

@Override
public void showError() {
DialogFactory.createGenericErrorDialog(getContext(), getString(R.string.files_fetch_error))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@


public interface FileMvpView extends MvpView {

void showFiles(List<File> files);

void showDirectories(List<Directory> directories);

void showCanCreateFile(boolean canCreateFile);

void showCanDeleteFiles(boolean canDeleteFiles);

void showCanDeleteDirectories(boolean canDeleteDirectories);


void showError();

void showLoadingFileFromServerError();
Expand All @@ -39,4 +47,5 @@ public interface FileMvpView extends MvpView {
void showDirectoryDeleteSuccess();

void showFileDeleteError();

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.ipaulpro.afilechooser.utils.FileUtils;

import org.schulcloud.mobile.data.DataManager;
import org.schulcloud.mobile.data.model.CurrentUser;
import org.schulcloud.mobile.data.model.File;
import org.schulcloud.mobile.data.model.requestBodies.SignedUrlRequest;
import org.schulcloud.mobile.data.model.responseBodies.SignedUrlResponse;
Expand Down Expand Up @@ -37,6 +38,16 @@ public FilePresenter(DataManager dataManager) {
@Override
public void attachView(FileMvpView mvpView) {
super.attachView(mvpView);
mDataManager.getCurrentUser()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(currentUser -> {
getMvpView().showCanCreateFile(
currentUser.hasPermission(CurrentUser.PERMISSION_FILE_CREATE));
getMvpView().showCanDeleteFiles(
currentUser.hasPermission(CurrentUser.PERMISSION_FILE_DELETE));
getMvpView().showCanDeleteDirectories(
currentUser.hasPermission(CurrentUser.PERMISSION_FOLDER_DELETE));
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import org.schulcloud.mobile.R;
import org.schulcloud.mobile.data.model.File;
import org.schulcloud.mobile.util.ViewUtil;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -24,7 +25,9 @@ public class FilesAdapter extends RecyclerView.Adapter<FilesAdapter.FilesViewHol

@Inject
FilePresenter mFilesPresenter;

private List<File> mFiles;
private boolean mCanDeleteFiles;

@Inject
public FilesAdapter() {
Expand All @@ -34,6 +37,10 @@ public FilesAdapter() {
public void setFiles(List<File> files) {
mFiles = files;
}
public void setCanDeleteFiles(boolean canDeleteFiles) {
mCanDeleteFiles = canDeleteFiles;
notifyItemRangeChanged(0, mFiles.size());
}

@Override
public FilesAdapter.FilesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Expand All @@ -48,6 +55,8 @@ public void onBindViewHolder(FilesAdapter.FilesViewHolder holder, int position)
holder.nameTextView.setText(file.name);
holder.cardView.setOnClickListener(v -> mFilesPresenter.startDownloading(file, false));
holder.downloadIcon.setOnClickListener(v -> mFilesPresenter.startDownloading(file, true));

ViewUtil.setVisibility(holder.deleteIcon, mCanDeleteFiles);
holder.deleteIcon.setOnClickListener(
v -> mFilesPresenter.startFileDeleting(file.key, file.name));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ public void showHomework(List<Homework> homework) {
mHomeworkAdapter.notifyDataSetChanged();
}
@Override
public void showCanCreateHomework(boolean canCreateHomework) {
ViewUtil.setVisibility(fabAddHomework, canCreateHomework);
}
@Override
public void showHomeworkEmpty() {
mHomeworkAdapter.setHomework(Collections.emptyList());
mHomeworkAdapter.notifyDataSetChanged();
Expand Down
Loading

0 comments on commit 7d83f85

Please sign in to comment.