From 49b50ef4ca2d8df66efb8159d346ebac10810675 Mon Sep 17 00:00:00 2001 From: Super12138 <70494801+Super12138@users.noreply.github.com> Date: Sat, 6 Jan 2024 16:43:53 +0800 Subject: [PATCH] Tidy code --- .../kotlin/cn/super12138/todo/utils/Toast.kt | 2 +- .../todo/views/main/MainActivity.kt | 31 ++++++++----------- .../todo/views/todo/ToDoFragment.kt | 3 +- .../main/res/layout-land/activity_main.xml | 3 +- app/src/main/res/layout/activity_about.xml | 1 + app/src/main/res/layout/activity_main.xml | 3 +- 6 files changed, 18 insertions(+), 25 deletions(-) diff --git a/app/src/main/kotlin/cn/super12138/todo/utils/Toast.kt b/app/src/main/kotlin/cn/super12138/todo/utils/Toast.kt index 04d9583..e6a0990 100644 --- a/app/src/main/kotlin/cn/super12138/todo/utils/Toast.kt +++ b/app/src/main/kotlin/cn/super12138/todo/utils/Toast.kt @@ -11,4 +11,4 @@ fun String.showToast( fun Int.showToast(context: Context = ToDoApplication.context, duration: Int = Toast.LENGTH_SHORT) { Toast.makeText(context, this, duration).show() -} +} \ No newline at end of file diff --git a/app/src/main/kotlin/cn/super12138/todo/views/main/MainActivity.kt b/app/src/main/kotlin/cn/super12138/todo/views/main/MainActivity.kt index 83d00c6..1bd5ddd 100644 --- a/app/src/main/kotlin/cn/super12138/todo/views/main/MainActivity.kt +++ b/app/src/main/kotlin/cn/super12138/todo/views/main/MainActivity.kt @@ -2,8 +2,6 @@ package cn.super12138.todo.views.main // 2023.11.18立项 import android.content.Intent import android.os.Bundle -import android.view.Menu -import android.view.MenuItem import androidx.appcompat.app.AppCompatDelegate import cn.super12138.todo.R import cn.super12138.todo.ToDoApplication @@ -21,7 +19,19 @@ class MainActivity : BaseActivity() { binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) - setSupportActionBar(binding.toolbar) + binding.toolbar.setOnMenuItemClickListener { menuItem -> + when (menuItem.itemId) { + R.id.item_settings -> { + val intent = Intent(ToDoApplication.context, SettingsActivity::class.java) + startActivity(intent) + true + } + + else -> false + } + } + + // setSupportActionBar(binding.toolbar) /*val pref = getSharedPreferences("data", Context.MODE_PRIVATE) val isFirstUse = pref.getBoolean("first_use", false) @@ -46,19 +56,4 @@ class MainActivity : BaseActivity() { "2" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO) } } - - override fun onCreateOptionsMenu(menu: Menu?): Boolean { - menuInflater.inflate(R.menu.main, menu) - return true - } - - override fun onOptionsItemSelected(item: MenuItem): Boolean { - when (item.itemId) { - R.id.item_settings -> { - val intent = Intent(ToDoApplication.context, SettingsActivity::class.java) - startActivity(intent) - } - } - return true - } } \ No newline at end of file diff --git a/app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoFragment.kt b/app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoFragment.kt index 593e901..382cf48 100644 --- a/app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoFragment.kt +++ b/app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoFragment.kt @@ -107,7 +107,6 @@ class ToDoFragment : Fragment() { put("context", todoContext) } Repository.insertData(todoData) - // dbHelper.writableDatabase.insert("ToDo", null, todoData) binding.todoList.adapter?.notifyItemInserted(todoList.size + 1) @@ -126,7 +125,6 @@ class ToDoFragment : Fragment() { .setPositiveButton(R.string.ok) { dialog, which -> todoList.clear() Repository.deleteData(true, null) - // dbHelper.writableDatabase.delete("ToDo", null, null) binding.todoList.adapter?.notifyItemRangeRemoved(0, todoList.size + 1) progressViewModel.updateProgress() @@ -163,6 +161,7 @@ class ToDoFragment : Fragment() { binding.addItem.show() } }) + todoViewModel.refreshData.observe(viewLifecycleOwner, Observer { binding.todoList.adapter?.notifyItemInserted(todoList.size + 1) }) diff --git a/app/src/main/res/layout-land/activity_main.xml b/app/src/main/res/layout-land/activity_main.xml index dae02f0..dd268e4 100644 --- a/app/src/main/res/layout-land/activity_main.xml +++ b/app/src/main/res/layout-land/activity_main.xml @@ -17,6 +17,7 @@ android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" + app:menu="@menu/main" app:title="@string/app_name" /> @@ -35,13 +36,11 @@ android:layout_weight="3" android:minHeight="160dp" /> - - \ No newline at end of file diff --git a/app/src/main/res/layout/activity_about.xml b/app/src/main/res/layout/activity_about.xml index a15f87b..e5168c7 100644 --- a/app/src/main/res/layout/activity_about.xml +++ b/app/src/main/res/layout/activity_about.xml @@ -56,6 +56,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" + android:contentDescription="@string/app_name" app:srcCompat="@drawable/ic_launcher" /> @@ -35,13 +36,11 @@ android:layout_weight="2" android:minHeight="160dp" /> - - \ No newline at end of file