Skip to content

Instantly share code, notes, and snippets.

@pavelpoley
Created May 1, 2019 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pavelpoley/49a1470fdb776491545256b680620cd6 to your computer and use it in GitHub Desktop.
Save pavelpoley/49a1470fdb776491545256b680620cd6 to your computer and use it in GitHub Desktop.
Realm PermissionManager issue
/*
* Copyright 2018 Realm Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.realm.todo;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import io.realm.ObjectServerError;
import io.realm.PermissionManager;
import io.realm.Realm;
import io.realm.RealmResults;
import io.realm.SyncUser;
import io.realm.permissions.Permission;
/*
* For simplicity the fab button will call pm.getPermissions()
*
* To reproduce:
*
* 1. Open the app and click fab button (getPermissions will called first time)
* 2. Disable wifi and mobile data
* 3. Enable wifi or mobile data
* 4. Click on fab button again (error will thrown)
*
* */
public class ProjectsActivity extends AppCompatActivity {
private Realm realm;
private RecyclerView recyclerView;
private TextView statusView;
private PermissionManager pm;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_items);
setSupportActionBar(findViewById(R.id.toolbar));
recyclerView = findViewById(R.id.recycler_view);
statusView = findViewById(R.id.status);
pm = SyncUser.current().getPermissionManager();
findViewById(R.id.fab).setOnClickListener(view -> {
pm.getPermissions(new PermissionManager.PermissionsCallback() {
@Override
public void onSuccess(RealmResults<Permission> permissions) {
}
@Override
public void onError(ObjectServerError error) {
Log.e("getPermissions error", "onError: "+error.getErrorMessage() );
showMessage(error.getErrorMessage());
}
});
});
// Using the current SyncUser identity, we create a subscription for projects created by that user.
setStatus("Loading...");
}
private boolean isValidProjectName(String projectName) {
return projectName.matches("[a-z0-9]*");
}
private void setStatus(String str) {
statusView.setText(str);
}
private void showMessage(String msg) {
Toast.makeText(ProjectsActivity.this, msg, Toast.LENGTH_LONG).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
pm.close();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_items, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_logout) {
SyncUser syncUser = SyncUser.current();
if (syncUser != null) {
syncUser.logOut();
Intent intent = new Intent(this, WelcomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
return true;
}
return super.onOptionsItemSelected(item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment