mirror of
https://github.com/SimpleMobileTools/Simple-Notes.git
synced 2025-02-13 18:00:45 +01:00
convert utils to kotlin
This commit is contained in:
parent
0444d3f0ad
commit
8227d5cc56
@ -28,7 +28,7 @@ public class MyWidgetProvider extends AppWidgetProvider {
|
|||||||
final int newTextColor = mPrefs.getInt(Constants.WIDGET_TEXT_COLOR, Color.WHITE);
|
final int newTextColor = mPrefs.getInt(Constants.WIDGET_TEXT_COLOR, Color.WHITE);
|
||||||
mRemoteViews.setInt(R.id.notes_view, "setBackgroundColor", newBgColor);
|
mRemoteViews.setInt(R.id.notes_view, "setBackgroundColor", newBgColor);
|
||||||
mRemoteViews.setInt(R.id.notes_view, "setTextColor", newTextColor);
|
mRemoteViews.setInt(R.id.notes_view, "setTextColor", newTextColor);
|
||||||
mRemoteViews.setFloat(R.id.notes_view, "setTextSize", Utils.getTextSize(context) / context.getResources().getDisplayMetrics().density);
|
mRemoteViews.setFloat(R.id.notes_view, "setTextSize", Utils.INSTANCE.getTextSize(context) / context.getResources().getDisplayMetrics().density);
|
||||||
|
|
||||||
for (int widgetId : appWidgetIds) {
|
for (int widgetId : appWidgetIds) {
|
||||||
updateWidget(appWidgetManager, widgetId, mRemoteViews);
|
updateWidget(appWidgetManager, widgetId, mRemoteViews);
|
||||||
|
@ -1,42 +0,0 @@
|
|||||||
package com.simplemobiletools.notes;
|
|
||||||
|
|
||||||
import android.appwidget.AppWidgetManager;
|
|
||||||
import android.content.ComponentName;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.content.res.Resources;
|
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
public class Utils {
|
|
||||||
public static void showToast(Context context, int resId) {
|
|
||||||
Toast.makeText(context, context.getResources().getString(resId), Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static float getTextSize(Context context) {
|
|
||||||
final int fontSize = Config.Companion.newInstance(context).getFontSize();
|
|
||||||
final Resources res = context.getResources();
|
|
||||||
float textSize = res.getDimension(R.dimen.medium_text_size);
|
|
||||||
switch (fontSize) {
|
|
||||||
case Constants.FONT_SIZE_SMALL:
|
|
||||||
textSize = res.getDimension(R.dimen.small_text_size);
|
|
||||||
break;
|
|
||||||
case Constants.FONT_SIZE_LARGE:
|
|
||||||
textSize = res.getDimension(R.dimen.large_text_size);
|
|
||||||
break;
|
|
||||||
case Constants.FONT_SIZE_EXTRA_LARGE:
|
|
||||||
textSize = res.getDimension(R.dimen.extra_large_text_size);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return textSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void updateWidget(Context context) {
|
|
||||||
final AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
|
|
||||||
int[] ids = widgetManager.getAppWidgetIds(new ComponentName(context, MyWidgetProvider.class));
|
|
||||||
|
|
||||||
final Intent intent = new Intent(context, MyWidgetProvider.class);
|
|
||||||
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
|
|
||||||
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
|
|
||||||
context.sendBroadcast(intent);
|
|
||||||
}
|
|
||||||
}
|
|
@ -52,7 +52,7 @@ public class MainActivity extends SimpleActivity implements OpenNoteDialog.OpenN
|
|||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
invalidateOptionsMenu();
|
invalidateOptionsMenu();
|
||||||
mNotesView.setTextSize(TypedValue.COMPLEX_UNIT_PX, Utils.getTextSize(getApplicationContext()));
|
mNotesView.setTextSize(TypedValue.COMPLEX_UNIT_PX, Utils.INSTANCE.getTextSize(getApplicationContext()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -124,7 +124,7 @@ public class MainActivity extends SimpleActivity implements OpenNoteDialog.OpenN
|
|||||||
|
|
||||||
mCurrNoteLabel.setVisibility(mNotes.size() <= 1 ? View.GONE : View.VISIBLE);
|
mCurrNoteLabel.setVisibility(mNotes.size() <= 1 ? View.GONE : View.VISIBLE);
|
||||||
mCurrNoteTitle.setVisibility(mNotes.size() <= 1 ? View.GONE : View.VISIBLE);
|
mCurrNoteTitle.setVisibility(mNotes.size() <= 1 ? View.GONE : View.VISIBLE);
|
||||||
Utils.updateWidget(getApplicationContext());
|
Utils.INSTANCE.updateWidget(getApplicationContext());
|
||||||
}
|
}
|
||||||
|
|
||||||
@OnClick(R.id.notes_fab)
|
@OnClick(R.id.notes_fab)
|
||||||
@ -146,9 +146,9 @@ public class MainActivity extends SimpleActivity implements OpenNoteDialog.OpenN
|
|||||||
final EditText titleET = (EditText) newNoteView.findViewById(R.id.note_name);
|
final EditText titleET = (EditText) newNoteView.findViewById(R.id.note_name);
|
||||||
final String title = titleET.getText().toString().trim();
|
final String title = titleET.getText().toString().trim();
|
||||||
if (title.isEmpty()) {
|
if (title.isEmpty()) {
|
||||||
Utils.showToast(getApplicationContext(), R.string.no_title);
|
Utils.INSTANCE.showToast(getApplicationContext(), R.string.no_title);
|
||||||
} else if (mDb.doesTitleExist(title)) {
|
} else if (mDb.doesTitleExist(title)) {
|
||||||
Utils.showToast(getApplicationContext(), R.string.title_taken);
|
Utils.INSTANCE.showToast(getApplicationContext(), R.string.title_taken);
|
||||||
} else {
|
} else {
|
||||||
saveText();
|
saveText();
|
||||||
final Note newNote = new Note(0, title, "");
|
final Note newNote = new Note(0, title, "");
|
||||||
@ -200,19 +200,19 @@ public class MainActivity extends SimpleActivity implements OpenNoteDialog.OpenN
|
|||||||
final String newText = getCurrentNote();
|
final String newText = getCurrentNote();
|
||||||
final String oldText = mCurrentNote.getValue();
|
final String oldText = mCurrentNote.getValue();
|
||||||
if (!newText.equals(oldText)) {
|
if (!newText.equals(oldText)) {
|
||||||
Utils.showToast(getApplicationContext(), R.string.note_saved);
|
Utils.INSTANCE.showToast(getApplicationContext(), R.string.note_saved);
|
||||||
mCurrentNote.setValue(newText);
|
mCurrentNote.setValue(newText);
|
||||||
mDb.updateNote(mCurrentNote);
|
mDb.updateNote(mCurrentNote);
|
||||||
}
|
}
|
||||||
|
|
||||||
hideKeyboard();
|
hideKeyboard();
|
||||||
Utils.updateWidget(getApplicationContext());
|
Utils.INSTANCE.updateWidget(getApplicationContext());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void shareText() {
|
private void shareText() {
|
||||||
final String text = getCurrentNote();
|
final String text = getCurrentNote();
|
||||||
if (text.isEmpty()) {
|
if (text.isEmpty()) {
|
||||||
Utils.showToast(getApplicationContext(), R.string.cannot_share_empty_text);
|
Utils.INSTANCE.showToast(getApplicationContext(), R.string.cannot_share_empty_text);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,7 +47,7 @@ public class SettingsActivity extends SimpleActivity {
|
|||||||
@OnItemSelected(R.id.settings_font_size)
|
@OnItemSelected(R.id.settings_font_size)
|
||||||
public void handleFontSize() {
|
public void handleFontSize() {
|
||||||
mConfig.setFontSize(mFontSizeSpinner.getSelectedItemPosition());
|
mConfig.setFontSize(mFontSizeSpinner.getSelectedItemPosition());
|
||||||
Utils.updateWidget(getApplicationContext());
|
Utils.INSTANCE.updateWidget(getApplicationContext());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void restartActivity() {
|
private void restartActivity() {
|
||||||
|
@ -57,7 +57,7 @@ public class WidgetConfigureActivity extends AppCompatActivity {
|
|||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
mNotesView.setTextSize(TypedValue.COMPLEX_UNIT_PX, Utils.getTextSize(getApplicationContext()));
|
mNotesView.setTextSize(TypedValue.COMPLEX_UNIT_PX, Utils.INSTANCE.getTextSize(getApplicationContext()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initVariables() {
|
private void initVariables() {
|
||||||
|
35
app/src/main/kotlin/com/simplemobiletools/notes/Utils.kt
Normal file
35
app/src/main/kotlin/com/simplemobiletools/notes/Utils.kt
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package com.simplemobiletools.notes
|
||||||
|
|
||||||
|
import android.appwidget.AppWidgetManager
|
||||||
|
import android.content.ComponentName
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.widget.Toast
|
||||||
|
|
||||||
|
object Utils {
|
||||||
|
fun showToast(context: Context, resId: Int) {
|
||||||
|
Toast.makeText(context, context.resources.getString(resId), Toast.LENGTH_SHORT).show()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getTextSize(context: Context): Float {
|
||||||
|
val fontSize = Config.newInstance(context).fontSize
|
||||||
|
val res = context.resources
|
||||||
|
var textSize = res.getDimension(R.dimen.medium_text_size)
|
||||||
|
when (fontSize) {
|
||||||
|
Constants.FONT_SIZE_SMALL -> textSize = res.getDimension(R.dimen.small_text_size)
|
||||||
|
Constants.FONT_SIZE_LARGE -> textSize = res.getDimension(R.dimen.large_text_size)
|
||||||
|
Constants.FONT_SIZE_EXTRA_LARGE -> textSize = res.getDimension(R.dimen.extra_large_text_size)
|
||||||
|
}
|
||||||
|
return textSize
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateWidget(context: Context) {
|
||||||
|
val widgetManager = AppWidgetManager.getInstance(context)
|
||||||
|
val ids = widgetManager.getAppWidgetIds(ComponentName(context, MyWidgetProvider::class.java))
|
||||||
|
|
||||||
|
val intent = Intent(context, MyWidgetProvider::class.java)
|
||||||
|
intent.action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
|
||||||
|
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids)
|
||||||
|
context.sendBroadcast(intent)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user