Convert SplashActivity to Kotlin

This commit is contained in:
Billy Brawner 2019-08-17 12:11:17 -05:00 committed by William Brawner
parent 91a0e729db
commit c8547601e6
2 changed files with 89 additions and 97 deletions

View file

@ -1,97 +0,0 @@
package com.wbrawner.simplemarkdown.view.activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import com.wbrawner.simplemarkdown.MarkdownApplication;
import com.wbrawner.simplemarkdown.R;
import com.wbrawner.simplemarkdown.presentation.MarkdownPresenter;
import com.wbrawner.simplemarkdown.utility.ErrorHandler;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javax.inject.Inject;
public class SplashActivity extends AppCompatActivity {
@Inject
MarkdownPresenter presenter;
@Inject
ErrorHandler errorHandler;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
((MarkdownApplication) getApplication()).getComponent().inject(this);
if (sharedPreferences.getBoolean(getString(R.string.error_reports_enabled), true)) {
errorHandler.init(this);
}
String darkModeValue = sharedPreferences.getString(
getString(R.string.pref_key_dark_mode),
getString(R.string.pref_value_auto)
);
int darkMode;
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
darkMode = AppCompatDelegate.MODE_NIGHT_AUTO;
} else {
darkMode = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM;
}
if (darkModeValue != null && !darkModeValue.isEmpty()) {
if (darkModeValue.equalsIgnoreCase(getString(R.string.pref_value_light))) {
darkMode = AppCompatDelegate.MODE_NIGHT_NO;
} else if (darkModeValue.equalsIgnoreCase(getString(R.string.pref_value_dark))) {
darkMode = AppCompatDelegate.MODE_NIGHT_YES;
}
}
AppCompatDelegate.setDefaultNightMode(darkMode);
Intent intent = getIntent();
if (intent != null && intent.getData() != null) {
presenter.loadFromUri(getApplicationContext(), intent.getData());
} else {
presenter.setFileName("Untitled.md");
final File autosave = new File(getFilesDir(), "autosave.md");
if (autosave.exists()) {
try {
FileInputStream fileInputStream = new FileInputStream(autosave);
presenter.loadMarkdown(
"Untitled.md",
fileInputStream,
new MarkdownPresenter.FileLoadedListener() {
@Override
public void onSuccess(String markdown) {
autosave.delete();
}
@Override
public void onError() {
autosave.delete();
}
},
true
);
} catch (FileNotFoundException ignored) {
return;
}
}
}
Intent startIntent = new Intent(this, MainActivity.class);
startActivity(startIntent);
finish();
}
}

View file

@ -0,0 +1,89 @@
package com.wbrawner.simplemarkdown.view.activity
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.preference.PreferenceManager
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import com.wbrawner.simplemarkdown.MarkdownApplication
import com.wbrawner.simplemarkdown.R
import com.wbrawner.simplemarkdown.presentation.MarkdownPresenter
import com.wbrawner.simplemarkdown.utility.ErrorHandler
import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import javax.inject.Inject
class SplashActivity : AppCompatActivity() {
@Inject
internal var presenter: MarkdownPresenter? = null
@Inject
internal var errorHandler: ErrorHandler? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
(application as MarkdownApplication).component.inject(this)
if (sharedPreferences.getBoolean(getString(R.string.error_reports_enabled), true)) {
errorHandler!!.init(this)
}
val darkModeValue = sharedPreferences.getString(
getString(R.string.pref_key_dark_mode),
getString(R.string.pref_value_auto)
)
var darkMode: Int
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
darkMode = AppCompatDelegate.MODE_NIGHT_AUTO
} else {
darkMode = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
}
if (darkModeValue != null && !darkModeValue.isEmpty()) {
if (darkModeValue.equals(getString(R.string.pref_value_light), ignoreCase = true)) {
darkMode = AppCompatDelegate.MODE_NIGHT_NO
} else if (darkModeValue.equals(getString(R.string.pref_value_dark), ignoreCase = true)) {
darkMode = AppCompatDelegate.MODE_NIGHT_YES
}
}
AppCompatDelegate.setDefaultNightMode(darkMode)
val intent = intent
if (intent != null && intent.data != null) {
presenter!!.loadFromUri(applicationContext, intent.data)
} else {
presenter!!.fileName = "Untitled.md"
val autosave = File(filesDir, "autosave.md")
if (autosave.exists()) {
try {
val fileInputStream = FileInputStream(autosave)
presenter!!.loadMarkdown(
"Untitled.md",
fileInputStream,
object : MarkdownPresenter.FileLoadedListener {
override fun onSuccess(markdown: String) {
autosave.delete()
}
override fun onError() {
autosave.delete()
}
},
true
)
} catch (ignored: FileNotFoundException) {
return
}
}
}
val startIntent = Intent(this, MainActivity::class.java)
startActivity(startIntent)
finish()
}
}