From 3f229dfa9b901d2018574324c162693456ddfa03 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 22 Jan 2021 11:34:28 +0100 Subject: [PATCH] MainActivity -> jetpack --- app/build.gradle | 8 +-- .../nightscout/androidaps/MainActivity.kt | 59 ++++++++++--------- 2 files changed, 35 insertions(+), 32 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index ce1b0af2aa..24b7d45ed2 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -87,7 +87,7 @@ def gitAvailable = { -> } -def allCommited = { -> +def allCommitted = { -> StringBuilder stringBuilder = new StringBuilder() try { def stdout = new ByteArrayOutputStream() @@ -125,7 +125,7 @@ android { buildConfigField "String", "BUILDVERSION", '"' + generateGitBuild() + '-' + generateDate() + '"' buildConfigField "String", "REMOTE", '"' + generateGitRemote() + '"' buildConfigField "String", "HEAD", '"' + generateGitBuild() + '"' - buildConfigField "String", "COMMITED", '"' + allCommited() + '"' + buildConfigField "String", "COMMITTED", '"' + allCommitted() + '"' testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" // if you change minSdkVersion to less than 11, you need to change executeTask for wear @@ -315,12 +315,12 @@ tasks.whenTaskAdded { task -> printf('--------------\n') printf('isMaster: %s\n', isMaster().toString()) printf('gitAvailable: %s\n', gitAvailable().toString()) -printf('allCommited: %s\n', allCommited().toString()) +printf('allCommitted: %s\n', allCommitted().toString()) printf('--------------\n') if (isMaster() && !gitAvailable()) { throw new GradleException('GIT system is not available. On Windows try to run Android Studio as an Administrator. Check if GIT is installed and Studio have permissions to use it') } -if (isMaster() && !allCommited()) { +if (isMaster() && !allCommitted()) { throw new GradleException('There are uncommitted changes. Clone sources again as described in wiki and do not allow gradle update') } diff --git a/app/src/main/java/info/nightscout/androidaps/MainActivity.kt b/app/src/main/java/info/nightscout/androidaps/MainActivity.kt index c03c218866..8466a0c2f1 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainActivity.kt +++ b/app/src/main/java/info/nightscout/androidaps/MainActivity.kt @@ -32,6 +32,7 @@ import info.nightscout.androidaps.activities.PreferencesActivity import info.nightscout.androidaps.activities.ProfileHelperActivity import info.nightscout.androidaps.activities.SingleFragmentActivity import info.nightscout.androidaps.activities.StatsActivity +import info.nightscout.androidaps.databinding.ActivityMainBinding import info.nightscout.androidaps.events.EventAppExit import info.nightscout.androidaps.events.EventPreferenceChange import info.nightscout.androidaps.events.EventRebuildTabs @@ -61,7 +62,6 @@ import info.nightscout.androidaps.utils.tabs.TabPageAdapter import info.nightscout.androidaps.utils.ui.UIRunnable import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.CompositeDisposable -import kotlinx.android.synthetic.main.activity_main.* import java.util.* import javax.inject.Inject import kotlin.system.exitProcess @@ -91,28 +91,31 @@ class MainActivity : NoSplashAppCompatActivity() { private var pluginPreferencesMenuItem: MenuItem? = null private var menu: Menu? = null + private lateinit var binding: ActivityMainBinding + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) Iconify.with(FontAwesomeModule()) LocaleHelper.update(applicationContext) - setContentView(R.layout.activity_main) - setSupportActionBar(toolbar) + binding = ActivityMainBinding.inflate(layoutInflater) + setContentView(binding.root) + setSupportActionBar(binding.toolbar) supportActionBar?.setDisplayShowTitleEnabled(false) supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setHomeButtonEnabled(true) - actionBarDrawerToggle = ActionBarDrawerToggle(this, main_drawer_layout, R.string.open_navigation, R.string.close_navigation).also { - main_drawer_layout.addDrawerListener(it) + actionBarDrawerToggle = ActionBarDrawerToggle(this, binding.mainDrawerLayout, R.string.open_navigation, R.string.close_navigation).also { + binding.mainDrawerLayout.addDrawerListener(it) it.syncState() } // initialize screen wake lock processPreferenceChange(EventPreferenceChange(resourceHelper.gs(R.string.key_keep_screen_on))) - main_pager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() { + binding.mainPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() { override fun onPageScrollStateChanged(state: Int) {} override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {} override fun onPageSelected(position: Int) { setPluginPreferenceMenuName() - checkPluginPreferences(main_pager) + checkPluginPreferences(binding.mainPager) } }) @@ -183,8 +186,8 @@ class MainActivity : NoSplashAppCompatActivity() { private fun setupViews() { // Menu val pageAdapter = TabPageAdapter(this) - main_navigation_view.setNavigationItemSelectedListener { true } - val menu = main_navigation_view.menu.also { it.clear() } + binding.mainNavigationView.setNavigationItemSelectedListener { true } + val menu = binding.mainNavigationView.menu.also { it.clear() } for (p in activePlugin.getPluginsList()) { pageAdapter.registerNewFragment(p) if (p.isEnabled() && p.hasFragment() && !p.isFragmentVisible() && !p.pluginDescription.neverVisible) { @@ -199,33 +202,33 @@ class MainActivity : NoSplashAppCompatActivity() { val intent = Intent(this, SingleFragmentActivity::class.java) intent.putExtra("plugin", activePlugin.getPluginsList().indexOf(p)) startActivity(intent) - main_drawer_layout.closeDrawers() + binding.mainDrawerLayout.closeDrawers() true } } } - main_pager.adapter = pageAdapter - main_pager.offscreenPageLimit = 8 // This may cause more memory consumption - checkPluginPreferences(main_pager) + binding.mainPager.adapter = pageAdapter + binding.mainPager.offscreenPageLimit = 8 // This may cause more memory consumption + checkPluginPreferences(binding.mainPager) // Tabs if (sp.getBoolean(R.string.key_short_tabtitles, false)) { - tabs_normal.visibility = View.GONE - tabs_compact.visibility = View.VISIBLE - toolbar.layoutParams = LinearLayout.LayoutParams(Toolbar.LayoutParams.MATCH_PARENT, resources.getDimension(R.dimen.compact_height).toInt()) - TabLayoutMediator(tabs_compact, main_pager) { tab, position -> - tab.text = (main_pager.adapter as TabPageAdapter).getPluginAt(position).nameShort + binding.tabsNormal.visibility = View.GONE + binding.tabsCompact.visibility = View.VISIBLE + binding.toolbar.layoutParams = LinearLayout.LayoutParams(Toolbar.LayoutParams.MATCH_PARENT, resources.getDimension(R.dimen.compact_height).toInt()) + TabLayoutMediator(binding.tabsCompact, binding.mainPager) { tab, position -> + tab.text = (binding.mainPager.adapter as TabPageAdapter).getPluginAt(position).nameShort }.attach() } else { - tabs_normal.visibility = View.VISIBLE - tabs_compact.visibility = View.GONE + binding.tabsNormal.visibility = View.VISIBLE + binding.tabsCompact.visibility = View.GONE val typedValue = TypedValue() if (theme.resolveAttribute(R.attr.actionBarSize, typedValue, true)) { - toolbar.layoutParams = LinearLayout.LayoutParams(Toolbar.LayoutParams.MATCH_PARENT, + binding.toolbar.layoutParams = LinearLayout.LayoutParams(Toolbar.LayoutParams.MATCH_PARENT, TypedValue.complexToDimensionPixelSize(typedValue.data, resources.displayMetrics)) } - TabLayoutMediator(tabs_normal, main_pager) { tab, position -> - tab.text = (main_pager.adapter as TabPageAdapter).getPluginAt(position).name + TabLayoutMediator(binding.tabsNormal, binding.mainPager) { tab, position -> + tab.text = (binding.mainPager.adapter as TabPageAdapter).getPluginAt(position).name }.attach() } } @@ -247,8 +250,8 @@ class MainActivity : NoSplashAppCompatActivity() { } private fun setPluginPreferenceMenuName() { - if (main_pager.currentItem >= 0) { - val plugin = (main_pager.adapter as TabPageAdapter).getPluginAt(main_pager.currentItem) + if (binding.mainPager.currentItem >= 0) { + val plugin = (binding.mainPager.adapter as TabPageAdapter).getPluginAt(binding.mainPager.currentItem) this.menu?.findItem(R.id.nav_plugin_preferences)?.title = resourceHelper.gs(R.string.nav_preferences_plugin, plugin.name) } } @@ -258,7 +261,7 @@ class MainActivity : NoSplashAppCompatActivity() { menuInflater.inflate(R.menu.menu_main, menu) pluginPreferencesMenuItem = menu.findItem(R.id.nav_plugin_preferences) setPluginPreferenceMenuName() - checkPluginPreferences(main_pager) + checkPluginPreferences(binding.mainPager) return true } @@ -316,7 +319,7 @@ class MainActivity : NoSplashAppCompatActivity() { } R.id.nav_plugin_preferences -> { - val plugin = (main_pager.adapter as TabPageAdapter).getPluginAt(main_pager.currentItem) + val plugin = (binding.mainPager.adapter as TabPageAdapter).getPluginAt(binding.mainPager.currentItem) protectionCheck.queryProtection(this, ProtectionCheck.Protection.PREFERENCES, { val i = Intent(this, PreferencesActivity::class.java) i.putExtra("id", plugin.preferencesId) @@ -375,7 +378,7 @@ class MainActivity : NoSplashAppCompatActivity() { FirebaseCrashlytics.getInstance().setCustomKey("HEAD", BuildConfig.HEAD) FirebaseCrashlytics.getInstance().setCustomKey("Version", BuildConfig.VERSION) FirebaseCrashlytics.getInstance().setCustomKey("Remote", remote) - FirebaseCrashlytics.getInstance().setCustomKey("Commited", BuildConfig.COMMITED) + FirebaseCrashlytics.getInstance().setCustomKey("Committed", BuildConfig.COMMITTED) FirebaseCrashlytics.getInstance().setCustomKey("Hash", hashes[0]) }