From ab7211e85c5402550445831c0d9d739683abc34f Mon Sep 17 00:00:00 2001 From: kubasz Date: Wed, 28 Aug 2019 16:24:46 +0200 Subject: [PATCH] Fix navbar detection, again. Add text input to bottom sheet. Add Drawer customization, drawer profiles, drawer badges, update Sample. Add text styles. Completed BottomSheet & Drawer implementations. --- app/build.gradle | 2 +- app/src/main/AndroidManifest.xml | 9 +- .../szczodrzynski/navigation/MainActivity.kt | 188 +++++- app/src/main/res/layout/sample_nav_view.xml | 286 +++++++- app/src/main/res/values/styles.xml | 2 - navlib/build.gradle | 1 + .../utils/SwipeRefreshLayoutNoIndicator.java | 208 ++++++ .../utils/SwipeRefreshLayoutNoTouch.java | 1 - .../szczodrzynski/navlib/BezelGifImageView.kt | 279 ++++++++ .../szczodrzynski/navlib/DrawerExtensions.kt | 68 ++ .../pl/szczodrzynski/navlib/ImageHolder.kt | 74 +++ .../pl/szczodrzynski/navlib/NavBottomBar.kt | 10 +- .../java/pl/szczodrzynski/navlib/NavView.kt | 117 ++-- .../szczodrzynski/navlib/NavigationLoader.kt | 5 + .../pl/szczodrzynski/navlib/SystemBarsUtil.kt | 28 +- .../java/pl/szczodrzynski/navlib/Utils.kt | 44 +- .../navlib/bottomsheet/BottomSheetAdapter.kt | 12 +- .../navlib/bottomsheet/NavBottomSheet.kt | 134 +++- ...imaryItem.kt => BottomSheetPrimaryItem.kt} | 20 +- ...torItem.kt => BottomSheetSeparatorItem.kt} | 2 +- .../bottomsheet/items/EditTextFilledItem.kt | 84 --- .../listeners/OnItemInputListener.kt | 7 - .../navlib/drawer/IDrawerProfile.kt | 6 + .../navlib/drawer/IUnreadCounter.kt | 6 + .../szczodrzynski/navlib/drawer/NavDrawer.kt | 616 ++++++++++++++++++ .../navlib/drawer/items/DrawerPrimaryItem.kt | 11 + .../color/mtrl_filled_background_color.xml | 21 + .../color/text_input_layout_background.xml | 22 + .../src/main/res/drawable/shadow_bottom.xml | 8 + navlib/src/main/res/drawable/shadow_right.xml | 8 + .../src/main/res/layout/nav_bottom_sheet.xml | 73 +-- .../layout/nav_bs_item_edittext_filled.xml | 21 - navlib/src/main/res/layout/nav_view.xml | 224 ++++--- navlib/src/main/res/values/attrs.xml | 3 +- navlib/src/main/res/values/styles.xml | 79 ++- 35 files changed, 2262 insertions(+), 417 deletions(-) create mode 100644 navlib/src/main/java/pl/szczodrzynski/edziennik/utils/SwipeRefreshLayoutNoIndicator.java rename {app => navlib}/src/main/java/pl/szczodrzynski/edziennik/utils/SwipeRefreshLayoutNoTouch.java (97%) create mode 100644 navlib/src/main/java/pl/szczodrzynski/navlib/BezelGifImageView.kt create mode 100644 navlib/src/main/java/pl/szczodrzynski/navlib/DrawerExtensions.kt create mode 100644 navlib/src/main/java/pl/szczodrzynski/navlib/ImageHolder.kt create mode 100644 navlib/src/main/java/pl/szczodrzynski/navlib/NavigationLoader.kt rename navlib/src/main/java/pl/szczodrzynski/navlib/bottomsheet/items/{PrimaryItem.kt => BottomSheetPrimaryItem.kt} (82%) rename navlib/src/main/java/pl/szczodrzynski/navlib/bottomsheet/items/{SeparatorItem.kt => BottomSheetSeparatorItem.kt} (84%) delete mode 100644 navlib/src/main/java/pl/szczodrzynski/navlib/bottomsheet/items/EditTextFilledItem.kt delete mode 100644 navlib/src/main/java/pl/szczodrzynski/navlib/bottomsheet/listeners/OnItemInputListener.kt create mode 100644 navlib/src/main/java/pl/szczodrzynski/navlib/drawer/IDrawerProfile.kt create mode 100644 navlib/src/main/java/pl/szczodrzynski/navlib/drawer/IUnreadCounter.kt create mode 100644 navlib/src/main/java/pl/szczodrzynski/navlib/drawer/NavDrawer.kt create mode 100644 navlib/src/main/java/pl/szczodrzynski/navlib/drawer/items/DrawerPrimaryItem.kt create mode 100644 navlib/src/main/res/color/mtrl_filled_background_color.xml create mode 100644 navlib/src/main/res/color/text_input_layout_background.xml create mode 100644 navlib/src/main/res/drawable/shadow_bottom.xml create mode 100644 navlib/src/main/res/drawable/shadow_right.xml delete mode 100644 navlib/src/main/res/layout/nav_bs_item_edittext_filled.xml diff --git a/app/build.gradle b/app/build.gradle index edc7230..254c30f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -35,7 +35,7 @@ dependencies { implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'androidx.appcompat:appcompat:1.0.2' implementation "androidx.legacy:legacy-support-v4:1.0.0" - //implementation "com.mikepenz:materialdrawer:7.0.0-rc05" + implementation "com.mikepenz:materialdrawer:7.0.0-rc05" //implementation "com.mikepenz:crossfader:1.6.0" // do not update implementation "com.mikepenz:iconics-core:${iconics}" // do not update. >3.1.0 Breaks jelly bean implementation "com.mikepenz:iconics-views:${iconics}" // do not update diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e5d5260..9cb4ef6 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,6 +3,8 @@ package="pl.szczodrzynski.navigation"> + + - + android:theme="@style/AppTheme.Light"> + diff --git a/app/src/main/java/pl/szczodrzynski/navigation/MainActivity.kt b/app/src/main/java/pl/szczodrzynski/navigation/MainActivity.kt index 2872733..ba17a2e 100644 --- a/app/src/main/java/pl/szczodrzynski/navigation/MainActivity.kt +++ b/app/src/main/java/pl/szczodrzynski/navigation/MainActivity.kt @@ -8,7 +8,16 @@ import android.view.Gravity import android.view.View import android.widget.Toast import androidx.appcompat.app.AppCompatActivity +import com.mikepenz.iconics.IconicsColor +import com.mikepenz.iconics.IconicsDrawable +import com.mikepenz.iconics.IconicsSize import com.mikepenz.iconics.typeface.library.community.material.CommunityMaterial +import com.mikepenz.materialdrawer.Drawer +import com.mikepenz.materialdrawer.holder.StringHolder +import com.mikepenz.materialdrawer.model.PrimaryDrawerItem +import com.mikepenz.materialdrawer.model.ProfileDrawerItem +import com.mikepenz.materialdrawer.model.ProfileSettingDrawerItem +import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem import kotlinx.android.synthetic.main.sample_nav_view.* import pl.szczodrzynski.navlib.SystemBarsUtil import pl.szczodrzynski.navlib.SystemBarsUtil.Companion.COLOR_DO_NOT_CHANGE @@ -18,8 +27,10 @@ import pl.szczodrzynski.navlib.bottomsheet.NavBottomSheet import pl.szczodrzynski.navlib.bottomsheet.NavBottomSheet.Companion.SORT_MODE_ASCENDING import pl.szczodrzynski.navlib.bottomsheet.NavBottomSheet.Companion.SORT_MODE_DESCENDING import pl.szczodrzynski.navlib.bottomsheet.NavBottomSheet.Companion.TOGGLE_GROUP_SORTING_ORDER -import pl.szczodrzynski.navlib.bottomsheet.items.PrimaryItem -import pl.szczodrzynski.navlib.bottomsheet.items.SeparatorItem +import pl.szczodrzynski.navlib.bottomsheet.items.BottomSheetPrimaryItem +import pl.szczodrzynski.navlib.bottomsheet.items.BottomSheetSeparatorItem +import pl.szczodrzynski.navlib.drawer.IDrawerProfile +import pl.szczodrzynski.navlib.drawer.items.DrawerPrimaryItem import pl.szczodrzynski.navlib.getColorFromAttr @@ -38,6 +49,8 @@ class MainActivity : AppCompatActivity() { Log.d("MainActivity", "Dark theme $darkTheme") setTheme(if (darkTheme == true) R.style.AppTheme else R.style.AppTheme_Light) + Log.d("NavLib", "ACTIVITY created") + setContentView(R.layout.sample_nav_view) appFullscreen.isChecked = getSharedPreferences("prefs", Context.MODE_PRIVATE).getBoolean("appFullscreen", true) @@ -65,18 +78,21 @@ class MainActivity : AppCompatActivity() { else -> R.id.gradientDoNotChange }) - button.setOnClickListener { + themeButton.setOnClickListener { // use commit instead of apply because of recreating the activity darkTheme = (darkTheme == false) getSharedPreferences("prefs", Context.MODE_PRIVATE).edit().putBoolean("darkTheme", darkTheme == true).commit() recreate() } + commitButton.setOnClickListener { + navView.systemBarsUtil?.commit() + } //navView.init(this) // init the drawer before SystemBarsUtil - navView.addDrawer(activity = this) + navView.drawer.init(this) SystemBarsUtil(this).apply { paddingByKeyboard = navView @@ -206,26 +222,150 @@ class MainActivity : AppCompatActivity() { navView.bottomBar.fabExtended = false + navView.drawer.apply { + addUnreadCounterType(type = 10, drawerItem = 1) + addUnreadCounterType(type = 20, drawerItem = 2) + addUnreadCounterType(type = 30, drawerItem = 60) + addUnreadCounterType(type = 40, drawerItem = 62) + + appendItems( + DrawerPrimaryItem() + .withAppTitle("Navigation") + .withName("Home") + .withSelected(true) + .withIdentifier(1) + .withBadgeStyle(badgeStyle) + .withIcon(CommunityMaterial.Icon.cmd_google_home), + + DrawerPrimaryItem() + .withIdentifier(2) + .withName("Settings") + .withBadgeStyle(badgeStyle) + .withIcon(CommunityMaterial.Icon2.cmd_settings), + + DrawerPrimaryItem().withName("iOS") + .withIdentifier(60) + .withBadgeStyle(badgeStyle) + .withIcon(CommunityMaterial.Icon.cmd_apple), + + DrawerPrimaryItem().withName("School bell") + .withDescription("why not?") + .withIdentifier(61) + .withBadgeStyle(badgeStyle) + .withIcon(CommunityMaterial.Icon.cmd_alarm_bell), + + DrawerPrimaryItem().withName("Lock screen") + .withIdentifier(62) + .withBadgeStyle(badgeStyle) + .withIcon(CommunityMaterial.Icon.cmd_fingerprint), + + DrawerPrimaryItem().withName("HDR enable/disable") + .withTag(0) + .withIdentifier(63) + .withBadgeStyle(badgeStyle) + .withSelectable(false) + .withIcon(CommunityMaterial.Icon2.cmd_hdr), + + DrawerPrimaryItem().withName("AdBlockPlus") + .withDescription("Because we all hate ads") + .withIdentifier(64) + .withBadgeStyle(badgeStyle) + .withIcon(CommunityMaterial.Icon.cmd_adchoices), + + DrawerPrimaryItem().withName("Wonderful browsing experience and this is a long string") + .withIdentifier(65) + .withBadgeStyle(badgeStyle) + .withIcon(CommunityMaterial.Icon2.cmd_internet_explorer) + ) + + setUnreadCount(2, 20, 30) // phil swift has 30 unreads on "Settings item" + setUnreadCount(4, 40, 1000) // mark has 99+ unreads on "Lock screen item" + + setAccountHeaderBackground("/sdcard/ban.gif") + + appendProfiles( + IDrawerProfile(1, "Think Pad", "think with a pad", "/sdcard/thinkpad.gif"), + IDrawerProfile(2, "Phil Swift", "I sawed this boat in half!!!", "/sdcard/phil.jpg"), + IDrawerProfile(3, "The meme bay", "Visit my amazing website", "/sdcard/loader.gif"), + IDrawerProfile(4, "Mark Zuckerberg", "", null), + IDrawerProfile(5, "I love GDPR", "spotify:user:popjustice:playlist:5Pe51v0sHLybSEkX0m0JRf", "/sdcard/tenor2.gif"), + IDrawerProfile(6, "Gandalf", "http://sax.hol.es/", "/sdcard/facepalm.gif") + ) + + addProfileSettings( + ProfileSettingDrawerItem() + .withName("Add Account") + .withDescription("Add new GitHub Account") + .withIcon( + IconicsDrawable(context, CommunityMaterial.Icon2.cmd_plus) + .actionBar() + .padding(IconicsSize.dp(5)) + .color(IconicsColor.colorRes(pl.szczodrzynski.navlib.R.color.material_drawer_dark_primary_text)) + ) + .withOnDrawerItemClickListener(object : Drawer.OnDrawerItemClickListener { + override fun onItemClick(view: View?, position: Int, drawerItem: IDrawerItem<*>): Boolean { + Toast.makeText(context, "Add account", Toast.LENGTH_SHORT).show() + return true + } + }), + ProfileSettingDrawerItem() + .withName("Manage Account") + .withIcon(CommunityMaterial.Icon2.cmd_settings) + ) + + drawerItemSelectedListener = { id, position, drawerItem -> + if (id == 1 || id == 2) { + getItemById(id) { + if (it is DrawerPrimaryItem) { + if (it.tag !is Int) { + it.tag = 0 + } + it.tag = (it.tag as Int) + 1 + // TODO 2019-08-27 allow string to be passed as name + it.name = StringHolder("Home ${it.tag as Int}") + // do not set item.badge unless you're not using Unread Counters + // because this *may* disappear/be overridden on profile change + // (if UnreadCounterList have at least one counter with matching + // drawer item ID) + // See with "Settings" when it.badge AND UnreadCounter is present. + // + // just do not do this. + it.badge = StringHolder("${it.tag as Int * 10}") + } + } + } + if (id == 63) { + getItemById(id) { + if (it is DrawerPrimaryItem) { + it.tag = if (it.tag as Int == 1) 0 else 1 + it.withIcon(if (it.tag as Int == 1) CommunityMaterial.Icon2.cmd_hdr_off else CommunityMaterial.Icon2.cmd_hdr) + } + } + } + // you cannot select apple + id != 60 + } + } navView.bottomSheet.apply { - this += PrimaryItem(true) + this += BottomSheetPrimaryItem(true) .withId(1) .withTitle("Compose") .withIcon(CommunityMaterial.Icon2.cmd_pencil) .withOnClickListener(View.OnClickListener { Toast.makeText(this@MainActivity, "Compose message", Toast.LENGTH_SHORT).show() }) - this += SeparatorItem(false) - this += PrimaryItem(false) + this += BottomSheetSeparatorItem(false) + this += BottomSheetPrimaryItem(false) .withId(3) .withTitle("Synchronise") .withIcon(CommunityMaterial.Icon2.cmd_sync) .withOnClickListener(View.OnClickListener { Toast.makeText(this@MainActivity, "Synchronising...", Toast.LENGTH_SHORT).show() }) - this += PrimaryItem(false) + this += BottomSheetPrimaryItem(false) .withId(4) .withTitle("Help") .withIcon(CommunityMaterial.Icon2.cmd_help) @@ -233,6 +373,7 @@ class MainActivity : AppCompatActivity() { Toast.makeText(this@MainActivity, "Want some help?", Toast.LENGTH_SHORT).show() }) + toggleGroupEnabled = true toggleGroupTitle = "Sort by" toggleGroupRemoveItems() toggleGroupSelectionMode = TOGGLE_GROUP_SORTING_ORDER @@ -240,16 +381,31 @@ class MainActivity : AppCompatActivity() { toggleGroupAddItem(1, "By subject", null, SORT_MODE_ASCENDING) toggleGroupAddItem(2, "By sender", null, SORT_MODE_ASCENDING) toggleGroupAddItem(3, "By android", null, SORT_MODE_ASCENDING) - toggleGroupSortingOrderListener = object : NavBottomSheet.OnToggleGroupSortingListener { - override fun onSortingOrder(id: Int, sortMode: Int) { - Toast.makeText( - this@MainActivity, - "Sort mode $id ${if (sortMode == SORT_MODE_ASCENDING) "ascending" else "descending"}", - Toast.LENGTH_SHORT - ).show() - } + toggleGroupSortingOrderListener = { id, sortMode -> + Toast.makeText( + this@MainActivity, + "Sort mode $id ${if (sortMode == SORT_MODE_ASCENDING) "ascending" else "descending"}", + Toast.LENGTH_SHORT + ).show() } toggleGroupCheck(1) + + textInputEnabled = true + textInputHint = "Search" + textInputHelperText = "0 messages found" + textInputIcon = CommunityMaterial.Icon2.cmd_magnify + textInputChangedListener = object : NavBottomSheet.OnTextInputChangedListener { + override fun onTextChanged(s: String, start: Int, before: Int, count: Int) { + navView.toolbar.subtitle = s + textInputError = if (s.length > 10) "Too many messages" else null + textInputHelperText = "${s.length} messages found" + } + } } } + + override fun onBackPressed() { + if (!navView.onBackPressed()) + super.onBackPressed() + } } diff --git a/app/src/main/res/layout/sample_nav_view.xml b/app/src/main/res/layout/sample_nav_view.xml index 6e4f0c7..b73bacc 100644 --- a/app/src/main/res/layout/sample_nav_view.xml +++ b/app/src/main/res/layout/sample_nav_view.xml @@ -8,6 +8,9 @@ android:id="@+id/navView" android:layout_width="match_parent" android:layout_height="match_parent"> + + android:orientation="vertical" + tools:ignore="HardcodedText">