org.radonix.moted.service.PreviewService.java Source code

Java tutorial

Introduction

Here is the source code for org.radonix.moted.service.PreviewService.java

Source

/*
 * Copyright (C) 2013, Radmon Acaranen.
 *
 * 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 org.radonix.moted.service;

import android.util.Log;

import com.google.common.base.Strings;

import org.markdown4j.Markdown4jProcessor;
import org.radonix.moted.core.concurrent.Task;
import org.radonix.moted.criteria.ResultViewer;
import org.radonix.moted.criteria.SourceEditor;

/**
 * Created by Radmon, 2014/12/31 0031.
 */
public class PreviewService extends Service {

    private static final String TAG = "PreviewService";

    private Markdown4jProcessor processor;

    PreviewService() {
        processor = new Markdown4jProcessor();
    }

    public void preview(SourceEditor editor, final ResultViewer viewer) {
        final String source = editor.getSource();
        executeTask(new Task<String>() {
            @Override
            public String atWorking() throws Exception {
                String result = processor.process(source);
                return Strings.nullToEmpty(result);
            }

            @Override
            public void onSuccess(String result) {
                viewer.viewResult(result);
            }

            @Override
            public void onFailure(Exception e) {
                Log.e(TAG, "Cannot process the markdown source.", e);
            }
        });
    }

}