diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/AlarmHelper.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/AlarmHelper.kt index a949b07..ab9b253 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/AlarmHelper.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/AlarmHelper.kt @@ -3,6 +3,7 @@ package com.tommasoberlose.anotherwidget.helpers import android.app.AlarmManager import android.content.Context import android.text.format.DateFormat +import java.text.SimpleDateFormat import java.util.* object AlarmHelper { @@ -11,9 +12,12 @@ object AlarmHelper { return if ( alarm != null && alarm.triggerTime - Calendar.getInstance().timeInMillis > 2 * 60 * 1000 - && alarm.triggerTime - Calendar.getInstance().timeInMillis < 24 * 60 * 60 * 1000 + && alarm.triggerTime - Calendar.getInstance().timeInMillis < 12 * 60 * 60 * 1000 ) { - DateFormat.getTimeFormat(context).format(Date(alarm.triggerTime)) + "%s %s".format( + SimpleDateFormat("EEE", Locale.getDefault()).format(alarm.triggerTime), + DateFormat.getTimeFormat(context).format(Date(alarm.triggerTime)) + ) } else { "" } diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/CalendarHelper.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/CalendarHelper.kt index f0628cf..ff0d1e1 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/CalendarHelper.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/CalendarHelper.kt @@ -109,7 +109,6 @@ object CalendarHelper { } }) eventList.reverse() - Log.d("ciao", "list: $eventList") eventRepository.saveEvents( eventList ) diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/activities/ChooseApplicationActivity.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/activities/ChooseApplicationActivity.kt index c1233b3..21debe9 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/activities/ChooseApplicationActivity.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/activities/ChooseApplicationActivity.kt @@ -30,7 +30,6 @@ class ChooseApplicationActivity : AppCompatActivity() { private lateinit var adapter: SlimAdapter private lateinit var viewModel: ChooseApplicationViewModel - private val pm by lazy { packageManager } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -63,11 +62,11 @@ class ChooseApplicationActivity : AppCompatActivity() { } .register(R.layout.application_info_layout) { item, injector -> injector - .text(R.id.text, item.loadLabel(pm)) + .text(R.id.text, item.loadLabel(viewModel.pm)) .with(R.id.icon) { Glide .with(this) - .load(item.loadIcon(pm)) + .load(item.loadIcon(viewModel.pm)) .centerCrop() .into(it) } @@ -88,49 +87,47 @@ class ChooseApplicationActivity : AppCompatActivity() { private fun subscribeUi(binding: ActivityChooseApplicationBinding, viewModel: ChooseApplicationViewModel) { binding.viewModel = viewModel + viewModel.appList.observe(this, Observer { - adapter.updateData(listOf("Default") + it) + updateList(list = it) loader.visibility = View.INVISIBLE }) viewModel.searchInput.observe(this, Observer { search -> - loader.visibility = View.VISIBLE - filterJob?.cancel() - filterJob = lifecycleScope.launch(Dispatchers.IO) { + updateList(search = search) + }) + } + + private fun updateList(list: List? = viewModel.appList.value, search: String? = viewModel.searchInput.value) { + loader.visibility = View.VISIBLE + filterJob?.cancel() + filterJob = lifecycleScope.launch(Dispatchers.IO) { + if (list != null && list.isNotEmpty()) { delay(200) - val list = if (search == null || search == "") { - viewModel.appList.value!! + val filteredList: List = if (search == null || search == "") { + list } else { - viewModel.appList.value!!.filter { - it.loadLabel(pm).contains(search, true) + list.filter { + it.loadLabel(viewModel.pm).contains(search, true) } } withContext(Dispatchers.Main) { - adapter.updateData(listOf("Default") + list) + adapter.updateData(listOf("Default") + filteredList) loader.visibility = View.INVISIBLE } - } - }) - -// viewModel.filterSettingsApp.observe(this, Observer { -// action_filter.alpha = if (it) 1f else 0.5f -// }) + } } private fun setupListener() { action_back.setOnClickListener { onBackPressed() } - -// action_filter.setOnClickListener { -// viewModel.toggleFilter() -// } } private fun saveApp(app: ResolveInfo) { val resultIntent = Intent() - resultIntent.putExtra(Constants.RESULT_APP_NAME, app.loadLabel(pm)) + resultIntent.putExtra(Constants.RESULT_APP_NAME, app.loadLabel(viewModel.pm)) resultIntent.putExtra(Constants.RESULT_APP_PACKAGE, app.activityInfo.packageName) setResult(Activity.RESULT_OK, resultIntent) finish() diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/viewmodels/ChooseApplicationViewModel.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/viewmodels/ChooseApplicationViewModel.kt index d2c02d9..572bcb5 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/viewmodels/ChooseApplicationViewModel.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/viewmodels/ChooseApplicationViewModel.kt @@ -3,20 +3,33 @@ package com.tommasoberlose.anotherwidget.ui.viewmodels import android.app.Application import android.content.Intent import android.content.pm.ApplicationInfo +import android.content.pm.PackageManager import android.content.pm.ResolveInfo -import androidx.lifecycle.AndroidViewModel -import androidx.lifecycle.LiveData -import androidx.lifecycle.MutableLiveData -import androidx.lifecycle.liveData +import android.util.Log +import androidx.lifecycle.* +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext class ChooseApplicationViewModel(application: Application) : AndroidViewModel(application) { - val appList: LiveData> = liveData { - val mainIntent = Intent(Intent.ACTION_MAIN, null).apply { - addCategory(Intent.CATEGORY_LAUNCHER) - } - val app = application.packageManager.queryIntentActivities( mainIntent, 0) - emit(app) - } + val pm: PackageManager by lazy { application.packageManager } + val appList: MutableLiveData> = MutableLiveData() val searchInput: MutableLiveData = MutableLiveData("") + + init { + viewModelScope.launch(Dispatchers.IO) { + val mainIntent = Intent(Intent.ACTION_MAIN, null).apply { + addCategory(Intent.CATEGORY_LAUNCHER) + } + + val app = application.packageManager.queryIntentActivities(mainIntent, 0) + val sortedApp = app.sortedWith(Comparator { app1: ResolveInfo, app2: ResolveInfo -> + app1.loadLabel(pm).toString().compareTo(app2.loadLabel(pm).toString()) + }) + withContext(Dispatchers.Main) { + appList.postValue(sortedApp) + } + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/widgets/MainWidget.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/widgets/MainWidget.kt index 413e88f..e273c75 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/widgets/MainWidget.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/widgets/MainWidget.kt @@ -336,7 +336,7 @@ class MainWidget : AppWidgetProvider() { // Text Size listOf>( v.empty_date to Preferences.textMainSize, - v.divider1 to Preferences.textSecondSize, + v.divider1 to Preferences.textMainSize, v.temp to Preferences.textMainSize, v.next_event to Preferences.textMainSize, v.next_event_difference_time to Preferences.textMainSize, diff --git a/app/src/main/res/layout/the_widget_sans.xml b/app/src/main/res/layout/the_widget_sans.xml index 6540012..9016047 100644 --- a/app/src/main/res/layout/the_widget_sans.xml +++ b/app/src/main/res/layout/the_widget_sans.xml @@ -3,7 +3,8 @@ android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" - android:padding="8dp" + android:paddingLeft="8dp" + android:paddingRight="8dp" android:id="@+id/main_layout"> 8 9