Fixed file opening splash screen

This commit is contained in:
William Brawner 2017-08-24 16:12:29 -05:00
parent 1ea93a2cdf
commit fb0bf7ab5f
7 changed files with 67 additions and 31 deletions

View file

@ -20,8 +20,6 @@
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity>
<activity android:name=".view.activity.MainActivity">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.EDIT" /> <action android:name="android.intent.action.EDIT" />
<action android:name="android.intent.action.VIEW" /> <action android:name="android.intent.action.VIEW" />
@ -39,6 +37,7 @@
<data android:host="*" /> <data android:host="*" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".view.activity.MainActivity"></activity>
<provider <provider
android:name="android.support.v4.content.FileProvider" android:name="android.support.v4.content.FileProvider"

View file

@ -1,6 +1,7 @@
package com.wbrawner.simplemarkdown; package com.wbrawner.simplemarkdown;
import com.wbrawner.simplemarkdown.view.activity.MainActivity; import com.wbrawner.simplemarkdown.view.activity.MainActivity;
import com.wbrawner.simplemarkdown.view.activity.SplashActivity;
import com.wbrawner.simplemarkdown.view.fragment.EditFragment; import com.wbrawner.simplemarkdown.view.fragment.EditFragment;
import com.wbrawner.simplemarkdown.view.fragment.PreviewFragment; import com.wbrawner.simplemarkdown.view.fragment.PreviewFragment;
@ -17,6 +18,7 @@ import dagger.Component;
public interface AppComponent { public interface AppComponent {
void inject(MarkdownApplication application); void inject(MarkdownApplication application);
void inject(MainActivity activity); void inject(MainActivity activity);
void inject(SplashActivity activity);
void inject(EditFragment fragment); void inject(EditFragment fragment);
void inject(PreviewFragment fragment); void inject(PreviewFragment fragment);
} }

View file

@ -1,5 +1,8 @@
package com.wbrawner.simplemarkdown.presentation; package com.wbrawner.simplemarkdown.presentation;
import android.content.Context;
import android.net.Uri;
import com.wbrawner.simplemarkdown.view.MarkdownEditView; import com.wbrawner.simplemarkdown.view.MarkdownEditView;
import com.wbrawner.simplemarkdown.view.MarkdownPreviewView; import com.wbrawner.simplemarkdown.view.MarkdownPreviewView;
@ -12,6 +15,7 @@ import java.io.InputStream;
public interface MarkdownPresenter extends LifecyclePresenter { public interface MarkdownPresenter extends LifecyclePresenter {
void loadMarkdown(String filePath); void loadMarkdown(String filePath);
void loadMarkdown(InputStream in); void loadMarkdown(InputStream in);
void loadFromUri(Context context, Uri fileUri);
void loadTempMarkdown(InputStream in, OnTempFileLoadedListener listener); void loadTempMarkdown(InputStream in, OnTempFileLoadedListener listener);
void setEditView(MarkdownEditView editView); void setEditView(MarkdownEditView editView);
void setPreviewView(MarkdownPreviewView previewView); void setPreviewView(MarkdownPreviewView previewView);

View file

@ -1,6 +1,13 @@
package com.wbrawner.simplemarkdown.presentation; package com.wbrawner.simplemarkdown.presentation;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.OpenableColumns;
import android.widget.Toast;
import com.commonsware.cwac.anddown.AndDown; import com.commonsware.cwac.anddown.AndDown;
import com.wbrawner.simplemarkdown.R;
import com.wbrawner.simplemarkdown.model.MarkdownFile; import com.wbrawner.simplemarkdown.model.MarkdownFile;
import com.wbrawner.simplemarkdown.view.MarkdownEditView; import com.wbrawner.simplemarkdown.view.MarkdownEditView;
import com.wbrawner.simplemarkdown.view.MarkdownPreviewView; import com.wbrawner.simplemarkdown.view.MarkdownPreviewView;
@ -147,4 +154,24 @@ public class MarkdownPresenterImpl implements MarkdownPresenter {
public void setMarkdown(String markdown) { public void setMarkdown(String markdown) {
file.setContent(markdown); file.setContent(markdown);
} }
@Override
public void loadFromUri(Context context, Uri fileUri) {
try {
InputStream in =
context.getContentResolver().openInputStream(fileUri);
Cursor retCur = context.getContentResolver()
.query(fileUri, null, null, null, null);
if (retCur != null) {
int nameIndex = retCur
.getColumnIndex(OpenableColumns.DISPLAY_NAME);
retCur.moveToFirst();
setFileName(retCur.getString(nameIndex));
}
loadMarkdown(in);
} catch (Exception e) {
Toast.makeText(context, R.string.file_load_error, Toast.LENGTH_SHORT)
.show();
}
}
} }

View file

@ -58,17 +58,12 @@ public class MainActivity extends AppCompatActivity
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
((MarkdownApplication) getApplication()).getComponent().inject(this); ((MarkdownApplication) getApplication()).getComponent().inject(this);
setTitle(presenter.getFileName());
ButterKnife.bind(this); ButterKnife.bind(this);
pager.setAdapter( pager.setAdapter(
new EditPagerAdapter(getSupportFragmentManager(), MainActivity.this) new EditPagerAdapter(getSupportFragmentManager(), MainActivity.this)
); );
pager.setPageMargin(1); pager.setPageMargin(1);
pager.setPageMarginDrawable(R.color.colorAccent); pager.setPageMarginDrawable(R.color.colorAccent);
Intent intent = getIntent();
if (intent != null && intent.getData() != null) {
loadFromUri(intent.getData());
}
if (getResources().getConfiguration().orientation if (getResources().getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE) { == Configuration.ORIENTATION_LANDSCAPE) {
tabLayout.setVisibility(View.GONE); tabLayout.setVisibility(View.GONE);
@ -199,7 +194,7 @@ public class MainActivity extends AppCompatActivity
builder.show(); builder.show();
} }
private String getDocsPath() { public String getDocsPath() {
return Environment.getExternalStorageDirectory() + "/" + return Environment.getExternalStorageDirectory() + "/" +
Environment.DIRECTORY_DOCUMENTS + "/"; Environment.DIRECTORY_DOCUMENTS + "/";
} }
@ -237,32 +232,12 @@ public class MainActivity extends AppCompatActivity
switch (requestCode) { switch (requestCode) {
case OPEN_FILE_REQUEST: case OPEN_FILE_REQUEST:
if (resultCode == RESULT_OK) { if (resultCode == RESULT_OK) {
loadFromUri(data.getData()); presenter.loadFromUri(MainActivity.this, data.getData());
} }
} }
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
} }
private void loadFromUri(Uri fileUri) {
try {
InputStream in =
getContentResolver().openInputStream(fileUri);
Cursor retCur = getContentResolver()
.query(fileUri, null, null, null, null);
if (retCur != null) {
int nameIndex = retCur
.getColumnIndex(OpenableColumns.DISPLAY_NAME);
retCur.moveToFirst();
presenter.setFileName(retCur.getString(nameIndex));
}
presenter.loadMarkdown(in);
} catch (Exception e) {
Toast.makeText(MainActivity.this, R.string.file_load_error, Toast.LENGTH_SHORT)
.show();
}
}
private void requestOpen() { private void requestOpen() {
Intent openIntent = new Intent(Intent.ACTION_GET_CONTENT); Intent openIntent = new Intent(Intent.ACTION_GET_CONTENT);
openIntent.setType("text/*"); openIntent.setType("text/*");
@ -281,5 +256,9 @@ public class MainActivity extends AppCompatActivity
} }
} }
@Override
protected void onResume() {
super.onResume();
setTitle(presenter.getFileName());
}
} }

View file

@ -1,18 +1,40 @@
package com.wbrawner.simplemarkdown.view.activity; package com.wbrawner.simplemarkdown.view.activity;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.provider.OpenableColumns;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import com.wbrawner.simplemarkdown.MarkdownApplication;
import com.wbrawner.simplemarkdown.R;
import com.wbrawner.simplemarkdown.presentation.MarkdownPresenter;
import java.io.InputStream;
import javax.inject.Inject;
/** /**
* Created by billy on 8/23/17. * Created by billy on 8/23/17.
*/ */
public class SplashActivity extends AppCompatActivity { public class SplashActivity extends AppCompatActivity {
@Inject
MarkdownPresenter presenter;
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
((MarkdownApplication) getApplication()).getComponent().inject(this);
Intent intent = getIntent();
if (intent != null && intent.getData() != null) {
presenter.loadFromUri(getApplicationContext(), intent.getData());
}
startActivity(new Intent(this, MainActivity.class)); startActivity(new Intent(this, MainActivity.class));
finish(); finish();
} }

View file

@ -14,6 +14,7 @@ import com.wbrawner.simplemarkdown.R;
import com.wbrawner.simplemarkdown.model.MarkdownFile; import com.wbrawner.simplemarkdown.model.MarkdownFile;
import com.wbrawner.simplemarkdown.presentation.MarkdownPresenter; import com.wbrawner.simplemarkdown.presentation.MarkdownPresenter;
import com.wbrawner.simplemarkdown.view.MarkdownEditView; import com.wbrawner.simplemarkdown.view.MarkdownEditView;
import com.wbrawner.simplemarkdown.view.activity.MainActivity;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -80,7 +81,9 @@ public class EditFragment extends Fragment implements MarkdownEditView {
@Override @Override
public void showFileSavedMessage() { public void showFileSavedMessage() {
Toast.makeText(getActivity(), R.string.file_saved, Toast.LENGTH_SHORT).show(); String location = ((MainActivity) getActivity()).getDocsPath() + presenter.getFileName();
String message = getString(R.string.file_saved, location);
Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
} }
@Override @Override