Skip to content

Commit

Permalink
solved GitHub issue 65
Browse files Browse the repository at this point in the history
  • Loading branch information
konradrenner committed Sep 10, 2015
1 parent de6adf8 commit f19f366
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 5 deletions.
29 changes: 27 additions & 2 deletions app/src/main/java/org/kore/kolabnotes/android/DetailActivity.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
package org.kore.kolabnotes.android;

import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.Toast;

import org.kore.kolabnotes.android.content.AccountIdentifier;
import org.kore.kolabnotes.android.fragment.ChooseAccountDialogFragment;
import org.kore.kolabnotes.android.fragment.DetailFragment;
import org.kore.kolabnotes.android.fragment.OnAccountChooseListener;
import org.kore.kolabnotes.android.fragment.OnFragmentCallback;

public class DetailActivity extends AppCompatActivity implements OnFragmentCallback {
public class DetailActivity extends AppCompatActivity implements OnFragmentCallback, OnAccountChooseListener {

private DetailFragment detailFragment;
private Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar = (Toolbar) findViewById(R.id.toolbar);
if(toolbar != null){
toolbar.setTitle("");
}
Expand All @@ -30,6 +36,25 @@ protected void onCreate(Bundle savedInstanceState) {

detailFragment.setStartNotebook(notebook);
detailFragment.setStartUid(uid);

String action = startIntent.getAction();
if (Intent.ACTION_SEND.equals(action)) {
showAccountChooseDialog();
}
}

private void showAccountChooseDialog() {
FragmentManager fm = getFragmentManager();
ChooseAccountDialogFragment chooseAccountDialog = new ChooseAccountDialogFragment();
chooseAccountDialog.show(fm, "fragment_choose_account");
}

@Override
public void onAccountElected(String name,AccountIdentifier accountIdentifier){
detailFragment.resetSpinner();
if(toolbar != null){
toolbar.setTitle(name);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package org.kore.kolabnotes.android.fragment;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import org.kore.kolabnotes.android.R;
import org.kore.kolabnotes.android.Utils;
import org.kore.kolabnotes.android.content.AccountIdentifier;
import org.kore.kolabnotes.android.content.ActiveAccount;
import org.kore.kolabnotes.android.content.ActiveAccountRepository;
import org.kore.kolabnotes.android.security.AuthenticatorActivity;

import java.util.Arrays;

/**
* Created by koni on 10.09.15.
*/
public class ChooseAccountDialogFragment extends DialogFragment implements View.OnClickListener{

private ActiveAccountRepository activeAccountRepository;

private Spinner accountSpinner;
private Button selectButton;
private Button cancelButton;
private AccountManager mAccountManager;
private String localAccountName;

public ChooseAccountDialogFragment() {
// Empty constructor required for DialogFragment
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_choose_account, container);

activeAccountRepository = new ActiveAccountRepository(getActivity());

accountSpinner = (Spinner) view.findViewById(R.id.spinner_account);
selectButton = (Button) view.findViewById(R.id.select_button);
cancelButton = (Button) view.findViewById(R.id.cancel_button);
selectButton.setOnClickListener(this);
cancelButton.setOnClickListener(this);
localAccountName = getResources().getString(R.string.drawer_account_local);
getDialog().setTitle(R.string.account_choose_title);

mAccountManager = AccountManager.get(getActivity());

initAccountSpinner();
return view;
}

@Override
public void onClick(View view) {
if(view.getId() == R.id.select_button) {
OnAccountChooseListener listener = (OnAccountChooseListener) getActivity();

String name = accountSpinner.getSelectedItem().toString();

final AccountIdentifier selectedAccount = Utils.getAccountIdentifierWithName(getActivity(), name);

activeAccountRepository.switchAccount(selectedAccount.getAccount(), selectedAccount.getRootFolder());

listener.onAccountElected(name, selectedAccount);
}
this.dismiss();
}

void initAccountSpinner(){
Account[] accounts = mAccountManager.getAccountsByType(AuthenticatorActivity.ARG_ACCOUNT_TYPE);

String[] accountNames = new String[accounts.length+1];

accountNames[0] = localAccountName;
int selection = 0;

final ActiveAccount activeAccount = activeAccountRepository.getActiveAccount();

for(int i=0; i< accounts.length;i++){
accountNames[i+1] = mAccountManager.getUserData(accounts[i],AuthenticatorActivity.KEY_ACCOUNT_NAME);

String folder = mAccountManager.getUserData(accounts[i],AuthenticatorActivity.KEY_ROOT_FOLDER);
String email = mAccountManager.getUserData(accounts[i],AuthenticatorActivity.KEY_EMAIL);

if(activeAccount.getAccount().equals(email) && activeAccount.getRootFolder().equals(folder)){
selection = i+1;
}
}

Arrays.sort(accountNames, 1, accountNames.length);

ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getActivity(),R.layout.widget_config_spinner_item,accountNames);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
accountSpinner.setAdapter(adapter);
accountSpinner.setSelection(selection);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ void setNotebook(ActiveAccount activeAccount,String uid, boolean setGivenNoteboo
}
}
}

void initEditor(){
editor.setOnTextChangeListener(new RichEditor.OnTextChangeListener() {
@Override
Expand Down Expand Up @@ -1021,10 +1021,17 @@ public void onClick(DialogInterface dialog, int which) {
}
}

void initSpinner(){
public void resetSpinner(){
Spinner spinner = initSpinner();
spinner.setSelection(0);
}

Spinner initSpinner(){
Spinner spinner = (Spinner) activity.findViewById(R.id.spinner_notebook);

List<Notebook> notebooks = notebookRepository.getAll( activeAccountRepository.getActiveAccount().getAccount(), activeAccountRepository.getActiveAccount().getRootFolder());
final ActiveAccount activeAccount = activeAccountRepository.getActiveAccount();

List<Notebook> notebooks = notebookRepository.getAll(activeAccount.getAccount(), activeAccount.getRootFolder());

String[] notebookArr = new String[notebooks.size()];

Expand All @@ -1037,6 +1044,8 @@ void initSpinner(){
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(activity,R.layout.notebook_spinner_item,notebookArr);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

return spinner;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.kore.kolabnotes.android.fragment;

import org.kore.kolabnotes.android.content.AccountIdentifier;

/**
* Created by koni on 10.09.15.
*/
public interface OnAccountChooseListener {
void onAccountElected(String name, AccountIdentifier accountIdentifier);
}
43 changes: 43 additions & 0 deletions app/src/main/res/layout/fragment_choose_account.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/choose_account_fragment"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:orientation="vertical"
android:background="@color/background_material_dark">

<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner_account"
android:spinnerMode="dropdown"
android:layout_marginTop="4dp"
android:layout_margin="2dp" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="wrap_content">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cancel"
android:id="@+id/cancel_button"
android:layout_margin="2dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/select_button"
android:id="@+id/select_button"
android:layout_margin="2dp" />

</LinearLayout>

</RelativeLayout>
</LinearLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,6 @@
<string name="radio_asc">Aufsteigend</string>
<string name="title_dialog_sorting">Ändere Sortierung</string>
<string name="color">Farbe</string>
<string name="account_choose_title">Kontoauswahl</string>
<string name="select_button">Übernehmen</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,6 @@
<string name="title_share">Поделиться</string>
<string name="widget_allnotes">Все заметки аккаунта</string>
<string name="yes">Да</string>
<string name="account_choose_title">Выберите учетную запись</string>
<string name="select_button">выбрать</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,6 @@
<string name="radio_asc">Ascending</string>
<string name="title_dialog_sorting">Change sorting</string>
<string name="color">Color</string>
<string name="account_choose_title">Choose an account</string>
<string name="select_button">Select</string>
</resources>

0 comments on commit f19f366

Please sign in to comment.