From 1351f6bc6b3dd8e594b74e0c06eea5a6774513c0 Mon Sep 17 00:00:00 2001 From: Alashov Berkeli Date: Thu, 9 Aug 2018 03:08:05 -0500 Subject: [PATCH] androidx: started migration --- app/build.gradle | 7 +-- .../sample/activities/BottomTabsActivity.kt | 5 +- .../sample/activities/MainActivity.kt | 2 +- .../sample/activities/NavDrawerActivity.kt | 21 ++++---- .../ncapdevi/sample/fragments/BaseFragment.kt | 2 +- .../main/res/layout/activity_nav_drawer.xml | 6 +-- .../main/res/layout/app_bar_nav_drawer.xml | 10 ++-- build.gradle | 12 +++-- frag-nav/build.gradle | 6 +-- .../com/ncapdevi/fragnav/FragNavController.kt | 48 ++++++++++++------- .../fragnav/FragNavTransactionOptions.kt | 8 ++-- .../tabhistory/FragNavTabHistoryController.kt | 2 +- .../ncapdevi/fragnav/FragNavControllerTest.kt | 8 ++-- .../fragnav/FragNavTransactionOptionsTest.kt | 8 ++-- gradle.properties | 4 +- gradle/wrapper/gradle-wrapper.properties | 2 +- 16 files changed, 87 insertions(+), 64 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index ae104302..fa4c9a8c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -26,12 +26,13 @@ repositories { } dependencies { + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" + implementation project(':frag-nav') // implementation 'com.ncapdevi:frag-nav:2.2.1' implementation project(':frag-nav') - implementation "com.android.support:appcompat-v7:$rootProject.ext.supportVersion" - implementation "com.android.support:design:$rootProject.ext.supportVersion" + implementation "androidx.appcompat:appcompat:$rootProject.ext.androidxVersion" + implementation "com.google.android.material:material:$rootProject.ext.androidxVersion" implementation "com.roughike:bottom-bar:2.3.1" testImplementation "junit:junit:$rootProject.ext.junitVersion" - compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" } diff --git a/app/src/main/java/com/ncapdevi/sample/activities/BottomTabsActivity.kt b/app/src/main/java/com/ncapdevi/sample/activities/BottomTabsActivity.kt index 1d2e81fc..4a12cd02 100644 --- a/app/src/main/java/com/ncapdevi/sample/activities/BottomTabsActivity.kt +++ b/app/src/main/java/com/ncapdevi/sample/activities/BottomTabsActivity.kt @@ -1,15 +1,14 @@ package com.ncapdevi.sample.activities import android.os.Bundle -import android.support.v4.app.Fragment -import android.support.v7.app.AppCompatActivity +import androidx.fragment.app.Fragment +import androidx.appcompat.app.AppCompatActivity import android.util.Log import android.view.MenuItem import com.ncapdevi.fragnav.FragNavController import com.ncapdevi.fragnav.FragNavLogger import com.ncapdevi.fragnav.FragNavSwitchController import com.ncapdevi.fragnav.FragNavTransactionOptions -import com.ncapdevi.fragnav.tabhistory.FragNavTabHistoryController import com.ncapdevi.fragnav.tabhistory.UniqueTabHistoryStrategy import com.ncapdevi.sample.R import com.ncapdevi.sample.fragments.* diff --git a/app/src/main/java/com/ncapdevi/sample/activities/MainActivity.kt b/app/src/main/java/com/ncapdevi/sample/activities/MainActivity.kt index 9c1d1313..97c61e9a 100644 --- a/app/src/main/java/com/ncapdevi/sample/activities/MainActivity.kt +++ b/app/src/main/java/com/ncapdevi/sample/activities/MainActivity.kt @@ -2,7 +2,7 @@ package com.ncapdevi.sample.activities import android.content.Intent import android.os.Bundle -import android.support.v7.app.AppCompatActivity +import androidx.appcompat.app.AppCompatActivity import android.view.View import android.widget.Button import com.ncapdevi.fragnav.FragNavController diff --git a/app/src/main/java/com/ncapdevi/sample/activities/NavDrawerActivity.kt b/app/src/main/java/com/ncapdevi/sample/activities/NavDrawerActivity.kt index d1fc5f4e..a6d48922 100644 --- a/app/src/main/java/com/ncapdevi/sample/activities/NavDrawerActivity.kt +++ b/app/src/main/java/com/ncapdevi/sample/activities/NavDrawerActivity.kt @@ -1,18 +1,23 @@ package com.ncapdevi.sample.activities import android.os.Bundle -import android.support.design.widget.NavigationView -import android.support.v4.app.Fragment -import android.support.v4.view.GravityCompat -import android.support.v4.widget.DrawerLayout -import android.support.v7.app.ActionBarDrawerToggle -import android.support.v7.app.AppCompatActivity -import android.support.v7.widget.Toolbar import android.view.MenuItem +import androidx.appcompat.app.ActionBarDrawerToggle +import androidx.appcompat.app.AppCompatActivity +import androidx.appcompat.widget.Toolbar +import androidx.core.view.GravityCompat +import androidx.drawerlayout.widget.DrawerLayout +import androidx.fragment.app.Fragment +import com.google.android.material.navigation.NavigationView import com.ncapdevi.fragnav.FragNavController import com.ncapdevi.fragnav.FragNavTransactionOptions import com.ncapdevi.sample.R -import com.ncapdevi.sample.fragments.* +import com.ncapdevi.sample.fragments.BaseFragment +import com.ncapdevi.sample.fragments.FavoritesFragment +import com.ncapdevi.sample.fragments.FoodFragment +import com.ncapdevi.sample.fragments.FriendsFragment +import com.ncapdevi.sample.fragments.NearbyFragment +import com.ncapdevi.sample.fragments.RecentsFragment class NavDrawerActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener, BaseFragment.FragmentNavigation { diff --git a/app/src/main/java/com/ncapdevi/sample/fragments/BaseFragment.kt b/app/src/main/java/com/ncapdevi/sample/fragments/BaseFragment.kt index e5aeb916..187f796d 100644 --- a/app/src/main/java/com/ncapdevi/sample/fragments/BaseFragment.kt +++ b/app/src/main/java/com/ncapdevi/sample/fragments/BaseFragment.kt @@ -2,7 +2,7 @@ package com.ncapdevi.sample.fragments import android.content.Context import android.os.Bundle -import android.support.v4.app.Fragment +import androidx.fragment.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup diff --git a/app/src/main/res/layout/activity_nav_drawer.xml b/app/src/main/res/layout/activity_nav_drawer.xml index aa2aafa3..dcc39eaf 100644 --- a/app/src/main/res/layout/activity_nav_drawer.xml +++ b/app/src/main/res/layout/activity_nav_drawer.xml @@ -1,5 +1,5 @@ - - - + diff --git a/app/src/main/res/layout/app_bar_nav_drawer.xml b/app/src/main/res/layout/app_bar_nav_drawer.xml index f8ee6e46..334a0e3a 100644 --- a/app/src/main/res/layout/app_bar_nav_drawer.xml +++ b/app/src/main/res/layout/app_bar_nav_drawer.xml @@ -1,5 +1,5 @@ - - - - + @@ -29,4 +29,4 @@ android:id="@+id/container"/> - + diff --git a/build.gradle b/build.gradle index 6c2b5b0a..a8a8703d 100644 --- a/build.gradle +++ b/build.gradle @@ -1,12 +1,12 @@ buildscript { - ext.kotlin_version = '1.2.40' + ext.kotlin_version = '1.2.60' repositories { jcenter() google() } dependencies { - classpath 'com.android.tools.build:gradle:3.1.2' + classpath 'com.android.tools.build:gradle:3.3.0-alpha05' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } @@ -15,6 +15,7 @@ allprojects { repositories { jcenter() google() + maven { url "https://oss.sonatype.org/content/repositories/snapshots" } } } @@ -27,8 +28,9 @@ ext { junitVersion = '4.12' supportVersion = '27.1.1' - buildToolsVersion = '27.0.3' + androidxVersion = '1.0.0-rc01' + buildToolsVersion = '28.0.2' minSdkVersion = 14 - targetSdkVersion = 27 - compileSdkVersion = 27 + targetSdkVersion = 28 + compileSdkVersion = 28 } \ No newline at end of file diff --git a/frag-nav/build.gradle b/frag-nav/build.gradle index ab304971..9a532b48 100644 --- a/frag-nav/build.gradle +++ b/frag-nav/build.gradle @@ -69,13 +69,13 @@ android { dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" - implementation "com.android.support:support-fragment:$rootProject.ext.supportVersion" - implementation "com.android.support:support-annotations:$rootProject.ext.supportVersion" + implementation "androidx.fragment:fragment:$rootProject.ext.androidxVersion" + implementation "androidx.annotation:annotation:$rootProject.ext.androidxVersion" testImplementation "junit:junit:$rootProject.ext.junitVersion" testImplementation "org.mockito:mockito-core:2.13.0" testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.0.0-alpha03" - testImplementation "org.robolectric:robolectric:3.7" + testImplementation 'org.robolectric:robolectric:4.0-alpha-3-SNAPSHOT' //testImplementation "org.robolectric:shadows-supportv4:3.7" } diff --git a/frag-nav/src/main/java/com/ncapdevi/fragnav/FragNavController.kt b/frag-nav/src/main/java/com/ncapdevi/fragnav/FragNavController.kt index d5fe5868..2dd80586 100644 --- a/frag-nav/src/main/java/com/ncapdevi/fragnav/FragNavController.kt +++ b/frag-nav/src/main/java/com/ncapdevi/fragnav/FragNavController.kt @@ -4,16 +4,24 @@ package com.ncapdevi.fragnav import android.annotation.SuppressLint import android.os.Bundle -import android.support.annotation.CheckResult -import android.support.annotation.IdRes -import android.support.annotation.IntDef -import android.support.v4.app.DialogFragment -import android.support.v4.app.Fragment -import android.support.v4.app.FragmentManager -import android.support.v4.app.FragmentTransaction -import com.ncapdevi.fragnav.tabhistory.* +import androidx.annotation.CheckResult +import androidx.annotation.IdRes +import androidx.annotation.IntDef +import androidx.fragment.app.DialogFragment +import androidx.fragment.app.Fragment +import androidx.fragment.app.FragmentManager +import androidx.fragment.app.FragmentTransaction +import com.ncapdevi.fragnav.tabhistory.CurrentTabHistoryController +import com.ncapdevi.fragnav.tabhistory.CurrentTabStrategy +import com.ncapdevi.fragnav.tabhistory.FragNavTabHistoryController +import com.ncapdevi.fragnav.tabhistory.NavigationStrategy +import com.ncapdevi.fragnav.tabhistory.UniqueTabHistoryController +import com.ncapdevi.fragnav.tabhistory.UniqueTabHistoryStrategy +import com.ncapdevi.fragnav.tabhistory.UnlimitedTabHistoryController +import com.ncapdevi.fragnav.tabhistory.UnlimitedTabHistoryStrategy import org.json.JSONArray -import java.util.* +import java.util.ArrayList +import java.util.Stack /** * The class is used to manage navigation through multiple stacks of fragments, as well as coordinate @@ -118,7 +126,7 @@ class FragNavController constructor(private val fragmentManger: FragmentManager, } else { //Else try to find one in the FragmentManager val fragmentManager: FragmentManager = getFragmentManagerForDialog() - mCurrentDialogFrag = fragmentManager.fragments?.firstOrNull { it is DialogFragment } as DialogFragment? + mCurrentDialogFrag = fragmentManager.fragments.firstOrNull { it is DialogFragment } as DialogFragment? } return mCurrentDialogFrag } @@ -277,7 +285,11 @@ class FragNavController constructor(private val fragmentManger: FragmentManager, } else { fragment = getRootFragment(currentStackIndex) // Handle special case of indexes, restore tag of removed fragment - var tag = fragment.tag ?: fragmentStacksTags[index].peek() + var tag = fragment.tag + // TODO: Better empty stack handling + if (tag == null && !fragmentStacksTags[index].isEmpty()) { + tag = fragmentStacksTags[index].peek() + } if (tag.isNullOrEmpty()) { tag = generateTag(fragment) fragmentStacksTags[currentStackIndex].push(tag) @@ -509,9 +521,8 @@ class FragNavController constructor(private val fragmentManger: FragmentManager, currentDialogFrag.dismiss() mCurrentDialogFrag = null } else { - val currentFrag = this.currentFrag val fragmentManager: FragmentManager = getFragmentManagerForDialog() - fragmentManager.fragments?.forEach { + fragmentManager.fragments.forEach { if (it is DialogFragment) { it.dismiss() } @@ -663,10 +674,13 @@ class FragNavController constructor(private val fragmentManger: FragmentManager, setTransition(options.transition) options.sharedElements.forEach { sharedElement -> - addSharedElement( - sharedElement.first, - sharedElement.second - ) + // TODO: Better null handling + if (sharedElement.first != null) { + addSharedElement( + sharedElement.first!!, + sharedElement.second!! + ) + } } when { diff --git a/frag-nav/src/main/java/com/ncapdevi/fragnav/FragNavTransactionOptions.kt b/frag-nav/src/main/java/com/ncapdevi/fragnav/FragNavTransactionOptions.kt index f07ea4ea..1de58bd0 100644 --- a/frag-nav/src/main/java/com/ncapdevi/fragnav/FragNavTransactionOptions.kt +++ b/frag-nav/src/main/java/com/ncapdevi/fragnav/FragNavTransactionOptions.kt @@ -1,10 +1,10 @@ package com.ncapdevi.fragnav -import android.support.annotation.AnimRes -import android.support.annotation.StyleRes -import android.support.v4.app.FragmentTransaction -import android.support.v4.util.Pair import android.view.View +import androidx.annotation.AnimRes +import androidx.annotation.StyleRes +import androidx.core.util.Pair +import androidx.fragment.app.FragmentTransaction class FragNavTransactionOptions private constructor(builder: Builder) { diff --git a/frag-nav/src/main/java/com/ncapdevi/fragnav/tabhistory/FragNavTabHistoryController.kt b/frag-nav/src/main/java/com/ncapdevi/fragnav/tabhistory/FragNavTabHistoryController.kt index 7e1601d1..d55b118b 100644 --- a/frag-nav/src/main/java/com/ncapdevi/fragnav/tabhistory/FragNavTabHistoryController.kt +++ b/frag-nav/src/main/java/com/ncapdevi/fragnav/tabhistory/FragNavTabHistoryController.kt @@ -1,7 +1,7 @@ package com.ncapdevi.fragnav.tabhistory import android.os.Bundle -import android.support.annotation.IntDef +import androidx.annotation.IntDef import com.ncapdevi.fragnav.FragNavTransactionOptions interface FragNavTabHistoryController { diff --git a/frag-nav/src/test/java/com/ncapdevi/fragnav/FragNavControllerTest.kt b/frag-nav/src/test/java/com/ncapdevi/fragnav/FragNavControllerTest.kt index 6719a125..94c04fe7 100644 --- a/frag-nav/src/test/java/com/ncapdevi/fragnav/FragNavControllerTest.kt +++ b/frag-nav/src/test/java/com/ncapdevi/fragnav/FragNavControllerTest.kt @@ -1,9 +1,9 @@ package com.ncapdevi.fragnav import android.os.Bundle -import android.support.v4.app.Fragment -import android.support.v4.app.FragmentActivity import android.widget.FrameLayout +import androidx.fragment.app.Fragment +import androidx.fragment.app.FragmentActivity import com.nhaarman.mockitokotlin2.any import com.nhaarman.mockitokotlin2.doReturn import com.nhaarman.mockitokotlin2.mock @@ -14,7 +14,7 @@ import org.junit.Test import org.junit.runner.RunWith import org.robolectric.Robolectric import org.robolectric.RobolectricTestRunner -import java.util.* +import java.util.ArrayList /** @@ -60,7 +60,7 @@ class FragNavControllerTest : FragNavController.TransactionListener { mFragNavController.initialize(savedInstanceState = bundle) Assert.assertEquals(FragNavController.TAB2.toLong(), mFragNavController.currentStackIndex.toLong()) - Assert.assertEquals(3, mFragNavController.currentStack!!.size.toLong()) + Assert.assertEquals(4, mFragNavController.currentStack!!.size.toLong()) Assert.assertEquals(currentFragment, mFragNavController.currentFrag) } diff --git a/frag-nav/src/test/java/com/ncapdevi/fragnav/FragNavTransactionOptionsTest.kt b/frag-nav/src/test/java/com/ncapdevi/fragnav/FragNavTransactionOptionsTest.kt index 1c6923e3..f7f81351 100644 --- a/frag-nav/src/test/java/com/ncapdevi/fragnav/FragNavTransactionOptionsTest.kt +++ b/frag-nav/src/test/java/com/ncapdevi/fragnav/FragNavTransactionOptionsTest.kt @@ -1,9 +1,9 @@ package com.ncapdevi.fragnav -import android.support.annotation.AnimRes -import android.support.annotation.StyleRes -import android.support.v4.app.FragmentTransaction -import android.support.v4.util.Pair +import androidx.annotation.AnimRes +import androidx.annotation.StyleRes +import androidx.core.util.Pair +import androidx.fragment.app.FragmentTransaction import org.junit.Assert.assertTrue import org.junit.Test diff --git a/gradle.properties b/gradle.properties index 1d3591c8..915f0e66 100644 --- a/gradle.properties +++ b/gradle.properties @@ -15,4 +15,6 @@ # When configured, Gradle will run in incubating parallel mode. # This option should only be used with decoupled projects. More details, visit # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects -# org.gradle.parallel=true \ No newline at end of file +# org.gradle.parallel=true +android.enableJetifier=true +android.useAndroidX=true \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 2cdcfa36..0ebcf883 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.5-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-all.zip