Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit baff5083 authored by korelstar's avatar korelstar Committed by Niedermann IT-Dienstleistungen
Browse files

merge create and edit note activity

parent 32cd544e
Loading
Loading
Loading
Loading
+6 −11
Original line number Diff line number Diff line
@@ -58,9 +58,12 @@
            />

        <activity
            android:name="it.niedermann.owncloud.notes.android.activity.CreateNoteActivity"
            android:label="@string/action_create"
            android:windowSoftInputMode="stateVisible">
            android:name="it.niedermann.owncloud.notes.android.activity.EditNoteActivity"
            android:label="@string/menu_edit"
            android:parentActivityName="it.niedermann.owncloud.notes.android.activity.NotesListViewActivity"
            android:windowSoftInputMode="stateHidden"
            android:launchMode="singleTask"
            >
            <intent-filter android:label="@string/action_create">
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
@@ -68,14 +71,6 @@
            </intent-filter>
        </activity>

        <activity
            android:name="it.niedermann.owncloud.notes.android.activity.EditNoteActivity"
            android:label="@string/menu_edit"
            android:parentActivityName="it.niedermann.owncloud.notes.android.activity.NotesListViewActivity"
            android:windowSoftInputMode="stateHidden"
            android:launchMode="singleTask"
            />

        <activity
            android:name="it.niedermann.owncloud.notes.android.activity.AboutActivity"
            android:label="@string/menu_about"
+0 −134
Original line number Diff line number Diff line
package it.niedermann.owncloud.notes.android.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.yydcdut.rxmarkdown.RxMDEditText;
import com.yydcdut.rxmarkdown.RxMarkdown;
import com.yydcdut.rxmarkdown.syntax.edit.EditFactory;

import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.model.Category;
import it.niedermann.owncloud.notes.persistence.NoteSQLiteOpenHelper;
import it.niedermann.owncloud.notes.persistence.NoteServerSyncHelper;
import it.niedermann.owncloud.notes.util.MarkDownUtil;
import rx.Subscriber;

public class CreateNoteActivity extends AppCompatActivity {

    public static final String PARAM_CATEGORY = "category";
    private final static int server_settings = 2;

    private RxMDEditText editTextField = null;
    private Category categoryPreselection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (!NoteServerSyncHelper.isConfigured(this)) {
            Intent settingsIntent = new Intent(this, SettingsActivity.class);
            startActivityForResult(settingsIntent, server_settings);
        }

        setContentView(R.layout.activity_create);
        editTextField = (RxMDEditText) findViewById(R.id.createContent);

        // Get intent, action and MIME type
        Intent intent = getIntent();
        String action = intent.getAction();
        String type = intent.getType();

        if(intent.hasExtra(PARAM_CATEGORY)) {
            categoryPreselection = (Category) intent.getSerializableExtra(PARAM_CATEGORY);
        }

        if (Intent.ACTION_SEND.equals(action) && type != null) {
            if ("text/plain".equals(type)) {
                editTextField.setText(intent.getStringExtra(Intent.EXTRA_TEXT));
            }
        }

        RxMarkdown.live(editTextField)
                .config(MarkDownUtil.getMarkDownConfiguration(getApplicationContext()))
                .factory(EditFactory.create())
                .intoObservable()
                .subscribe(new Subscriber<CharSequence>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(CharSequence charSequence) {
                        editTextField.setText(charSequence, TextView.BufferType.SPANNABLE);
                    }
                });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_menu_create, menu);
        return true;
    }

    /**
     * Main-Menu
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.action_create_save:
                saveAndClose(false);
                return true;
            case R.id.action_create_cancel:
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onBackPressed() {
        saveAndClose(true);
    }

    /**
     * Saves as new note and closes the Activity.
     * @param implicit If <code>true</code>, the note is only saved if non-empty.
     */
    private void saveAndClose(boolean implicit) {
        editTextField.setEnabled(false);
        String content = editTextField.getText().toString();
        if(!implicit || !content.isEmpty()) {
            NoteSQLiteOpenHelper db = NoteSQLiteOpenHelper.getInstance(this);
            Intent data = new Intent(this, NotesListViewActivity.class);
            String category = categoryPreselection==null ? null : categoryPreselection.category;
            boolean favorite = categoryPreselection!=null && categoryPreselection.favorite!=null ? categoryPreselection.favorite : false;
            data.putExtra(NotesListViewActivity.CREATED_NOTE, db.getNote(db.addNoteAndSync(content, category, favorite)));
            setResult(RESULT_OK, data);
        }
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == server_settings) {
            if (resultCode != RESULT_OK) {
                // User has not setup the server config and no note can be created
                finish();
            }
        }
    }
}
 No newline at end of file
+63 −14
Original line number Diff line number Diff line
@@ -11,16 +11,21 @@ import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import java.util.Calendar;

import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.android.fragment.BaseNoteFragment;
import it.niedermann.owncloud.notes.android.fragment.NoteEditFragment;
import it.niedermann.owncloud.notes.android.fragment.NotePreviewFragment;
import it.niedermann.owncloud.notes.model.Category;
import it.niedermann.owncloud.notes.model.CloudNote;
import it.niedermann.owncloud.notes.model.DBNote;
import it.niedermann.owncloud.notes.util.NoteUtil;

public class EditNoteActivity extends AppCompatActivity implements BaseNoteFragment.NoteFragmentListener {

    public static final String PARAM_NOTE_ID = "noteId";
    public static final String PARAM_CATEGORY = "category";

    private BaseNoteFragment fragment;

@@ -29,7 +34,7 @@ public class EditNoteActivity extends AppCompatActivity implements BaseNoteFragm
        super.onCreate(savedInstanceState);

        if (savedInstanceState == null) {
            createFragmentByPreference();
            launchNoteFragment();
        } else {
            fragment = (BaseNoteFragment) getFragmentManager().findFragmentById(android.R.id.content);
        }
@@ -48,16 +53,32 @@ public class EditNoteActivity extends AppCompatActivity implements BaseNoteFragm
            getFragmentManager().beginTransaction().detach(fragment).commit();
            fragment = null;
        }
        createFragmentByPreference();
        launchNoteFragment();
    }

    private long getNoteId() {
        return getIntent().getLongExtra(PARAM_NOTE_ID, 0);
    }

    private void createFragmentByPreference() {
    /**
     * Starts the note fragment for an existing note or a new note.
     * The actual behavior is triggered by the activity's intent.
     */
    private void launchNoteFragment() {
        long noteId = getNoteId();
        if(noteId>0) {
            launchExistingNote(noteId);
        } else {
            launchNewNote();
        }
    }

    /**
     * Starts a {@link NoteEditFragment} or {@link NotePreviewFragment} for an existing note.
     * The type of fragment (view-mode) is chosen based on the user preferences.
     * @param noteId ID of the existing note.
     */
    private void launchExistingNote(long noteId) {
        final String prefKeyNoteMode = getString(R.string.pref_key_note_mode);
        final String prefKeyLastMode = getString(R.string.pref_key_last_note_mode);
        final String prefValueEdit = getString(R.string.pref_value_mode_edit);
@@ -67,19 +88,21 @@ public class EditNoteActivity extends AppCompatActivity implements BaseNoteFragm
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        String mode = preferences.getString(prefKeyNoteMode, prefValueEdit);
        String lastMode = preferences.getString(prefKeyLastMode, prefValueEdit);
        boolean editMode = true;
        if (prefValuePreview.equals(mode) || (prefValueLast.equals(mode) && prefValuePreview.equals(lastMode))) {
            createFragment(noteId, false);
        /* TODO enhancement: store last mode in note
           for cross device functionality per note mode should be stored on the server.
        } else if(prefValueLast.equals(mode) && prefValuePreview.equals(note.getMode())) {
            createPreviewFragment(note);
         */
        } else {
            createFragment(noteId, true);
            editMode = false;
        }
        launchExistingNote(noteId, editMode);
    }

    private void createFragment(long noteId, boolean edit) {
    /**
     * Starts a {@link NoteEditFragment} or {@link NotePreviewFragment} for an existing note.
     * @param noteId ID of the existing note.
     * @param edit View-mode of the fragment:
     *            <code>true</code> for {@link NoteEditFragment},
     *            <code>false</code> for {@link NotePreviewFragment}.
     */
    private void launchExistingNote(long noteId, boolean edit) {
        // save state of the fragment in order to resume with the same note and originalNote
        Fragment.SavedState savedState = null;
        if(fragment != null) {
@@ -96,6 +119,31 @@ public class EditNoteActivity extends AppCompatActivity implements BaseNoteFragm
        getFragmentManager().beginTransaction().replace(android.R.id.content, fragment).commit();
    }

    /**
     * Starts the {@link NoteEditFragment} with a new note.
     * Content ("share" functionality), category and favorite attribute can be preset.
     */
    private void launchNewNote() {
        Intent intent = getIntent();

        String category = null;
        boolean favorite = false;
        if(intent.hasExtra(PARAM_CATEGORY)) {
            Category categoryPreselection = (Category) intent.getSerializableExtra(PARAM_CATEGORY);
            category = categoryPreselection.category;
            favorite = categoryPreselection.favorite!=null ? categoryPreselection.favorite : false;
        }

        String content = "";
        if (Intent.ACTION_SEND.equals(intent.getAction()) && "text/plain".equals(intent.getType())) {
            content = intent.getStringExtra(Intent.EXTRA_TEXT);
        }

        CloudNote newNote = new CloudNote(0, Calendar.getInstance(), NoteUtil.generateNonEmptyNoteTitle(content, this), content, favorite, category, null);
        fragment = NoteEditFragment.newInstanceWithNewNote(newNote);
        getFragmentManager().beginTransaction().replace(android.R.id.content, fragment).commit();
    }

    @Override
    public void onBackPressed() {
        fragment.onPrepareClose();
@@ -117,11 +165,11 @@ public class EditNoteActivity extends AppCompatActivity implements BaseNoteFragm
                return true;
            case R.id.menu_preview:
                fragment.onPrepareClose();
                createFragment(getNoteId(), false);
                launchExistingNote(getNoteId(), false);
                return true;
            case R.id.menu_edit:
                fragment.onPrepareClose();
                createFragment(getNoteId(), true);
                launchExistingNote(getNoteId(), true);
                return true;
            default:
                return super.onOptionsItemSelected(item);
@@ -143,6 +191,7 @@ public class EditNoteActivity extends AppCompatActivity implements BaseNoteFragm
        } else {
            preferences.edit().putString(prefKeyLastMode, getString(R.string.pref_value_mode_preview)).apply();
        }
        fragment.onFinalClose();
        finish();
    }

+3 −3
Original line number Diff line number Diff line
@@ -182,8 +182,8 @@ public class NotesListViewActivity extends AppCompatActivity implements ItemAdap
        findViewById(R.id.fab_create).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent createIntent = new Intent(getApplicationContext(), CreateNoteActivity.class);
                createIntent.putExtra(CreateNoteActivity.PARAM_CATEGORY, navigationSelection);
                Intent createIntent = new Intent(getApplicationContext(), EditNoteActivity.class);
                createIntent.putExtra(EditNoteActivity.PARAM_CATEGORY, navigationSelection);
                startActivityForResult(createIntent, create_note_cmd);
            }
        });
@@ -362,7 +362,7 @@ public class NotesListViewActivity extends AppCompatActivity implements ItemAdap
            // add notification
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
            PendingIntent newNoteIntent = PendingIntent.getActivity(this, 0,
                    new Intent(this, CreateNoteActivity.class)
                    new Intent(this, EditNoteActivity.class)
                            .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP),
                    0);
            builder.setSmallIcon(R.drawable.ic_action_new)
+2 −2
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@ import android.content.Intent;
import android.widget.RemoteViews;

import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.android.activity.CreateNoteActivity;
import it.niedermann.owncloud.notes.android.activity.EditNoteActivity;

/**
 * Implementation of App Widget functionality.
@@ -20,7 +20,7 @@ public class CreateNoteWidget extends AppWidgetProvider {

        // Construct the RemoteViews object
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_create_note);
        Intent intent = new Intent(context, CreateNoteActivity.class);
        Intent intent = new Intent(context, EditNoteActivity.class);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setOnClickPendingIntent(R.id.widget_create_note, pendingIntent);
Loading