Skip to content

Commit

Permalink
move new editor logic to a fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
maxme committed Feb 4, 2015
1 parent 4a2f1ba commit 714eeb1
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 18 deletions.
3 changes: 2 additions & 1 deletion editor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ android {
}

dependencies {
compile 'com.android.support:appcompat-v7:21.0.+'
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:support-v4:21.0.3'
}

signing {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.wordpress.android.editor;

import android.annotation.SuppressLint;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.webkit.ConsoleMessage;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class EditorActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import android.annotation.SuppressLint;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ConsoleMessage;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
Expand All @@ -17,20 +20,67 @@
import java.io.InputStream;
import java.io.InputStreamReader;

// TODO: use AppLog instead of Log
public class EditorActivity extends ActionBarActivity {
WebView mWebView;
public class EditorFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private WebView mWebView;

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment EditorFragment.
*/
// TODO: Rename and change types and number of parameters
public static EditorFragment newInstance(String param1, String param2) {
EditorFragment fragment = new EditorFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}

public EditorFragment() {
}

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editor);
mWebView = (WebView) findViewById(R.id.webview);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_editor, container, false);
mWebView = (WebView) view.findViewById(R.id.webview);
initWebView();
return view;
}

@Override
public void onDetach() {
super.onDetach();
}

// TODO: use AppLog instead of Log
@SuppressLint("SetJavaScriptEnabled")
private void initWebView() {
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDefaultTextEncodingName("utf-8");
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.e("WordPress-Editor", description);
Expand Down Expand Up @@ -58,7 +108,10 @@ public void onConsoleMessage(String message, int lineNumber, String sourceId) {
}

private String getStringFromAsset(String filename) throws IOException {
AssetManager assetManager = getAssets();
if (!isAdded()) {
return null;
}
AssetManager assetManager = getActivity().getAssets();
InputStream in = assetManager.open(filename);
InputStreamReader is = new InputStreamReader(in);
StringBuilder sb = new StringBuilder();
Expand Down
7 changes: 4 additions & 3 deletions editor/src/main/res/layout/activity_editor.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
android:layout_height="match_parent"
tools:context=".EditorActivity">

<WebView
<fragment
android:id="@+id/postEditor"
android:name="org.wordpress.android.editor.EditorFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webview"/>
android:layout_height="match_parent"/>

</RelativeLayout>
12 changes: 12 additions & 0 deletions editor/src/main/res/layout/fragment_editor.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="org.wordpress.android.editor.EditorFragment">

<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webview"/>

</FrameLayout>
3 changes: 3 additions & 0 deletions editor/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<resources>
<string name="app_name">Editor</string>

<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
</resources>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.wordpress.android.editorexample;
package org.wordpress.android.editor.example;

import android.app.Application;
import android.test.ApplicationTestCase;
Expand Down
5 changes: 4 additions & 1 deletion example/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.wordpress.android.editorexample" >
package="org.wordpress.android.editor.example" >

<application
android:allowBackup="true"
Expand All @@ -11,6 +11,9 @@
android:name=".ExampleActivity"
android:label="@string/title_activity_example" >
</activity>
<activity
android:name="org.wordpress.android.editor.EditorActivity" >
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.wordpress.android.editorexample;
package org.wordpress.android.editor.example;

import android.content.Intent;
import android.os.Bundle;
Expand All @@ -8,6 +8,7 @@
import android.widget.Button;

import org.wordpress.android.editor.EditorActivity;
import org.wordpress.android.editor.example.R;

public class ExampleActivity extends ActionBarActivity {
@Override
Expand Down
2 changes: 1 addition & 1 deletion example/src/main/res/layout/activity_example.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="org.wordpress.android.editorexample.ExampleActivity">
tools:context="org.wordpress.android.editor.example.ExampleActivity">

<Button
android:layout_width="wrap_content"
Expand Down

0 comments on commit 714eeb1

Please sign in to comment.