From e9560662d8f9d67715461bb01e51156b573ece2d Mon Sep 17 00:00:00 2001 From: Ilya Eremin Date: Sun, 25 Nov 2018 16:33:12 +0300 Subject: [PATCH 01/26] migration done --- .idea/codeStyleSettings.xml | 252 ------------------ app/build.gradle | 14 +- .../main/java/com/flatstack/android/App.java | 12 - .../main/java/com/flatstack/android/App.kt | 11 + .../com/flatstack/android/SecondActivity.java | 13 - .../com/flatstack/android/SecondActivity.kt | 12 + .../android/main_screen/MainActivity.java | 31 --- .../android/main_screen/MainActivity.kt | 24 ++ .../android/main_screen/MainFragment.java | 42 --- .../android/main_screen/MainFragment.kt | 41 +++ .../android/main_screen/TestDialog.java | 54 ---- .../android/main_screen/TestDialog.kt | 54 ++++ .../com/flatstack/android/utils/Intents.java | 25 -- .../com/flatstack/android/utils/Intents.kt | 19 ++ .../com/flatstack/android/utils/Keyboard.java | 40 --- .../com/flatstack/android/utils/Keyboard.kt | 39 +++ .../com/flatstack/android/utils/Lists.java | 92 ------- .../java/com/flatstack/android/utils/Lists.kt | 82 ++++++ .../utils/recycler_view/BaseAdapter.java | 60 ----- .../utils/recycler_view/BaseAdapter.kt | 37 +++ .../utils/recycler_view/BaseHolder.java | 16 -- .../android/utils/recycler_view/BaseHolder.kt | 8 + .../recycler_view/OnItemClickListener.java | 11 - .../recycler_view/OnItemClickListener.kt | 5 + .../android/utils/storage/IStorage.java | 40 --- .../android/utils/storage/IStorage.kt | 32 +++ .../android/utils/storage/Storage.java | 83 ------ .../android/utils/storage/Storage.kt | 66 +++++ .../android/utils/ui/BaseActivity.java | 74 ----- .../android/utils/ui/BaseActivity.kt | 61 +++++ .../android/utils/ui/BaseDialogFragment.java | 84 ------ .../android/utils/ui/BaseDialogFragment.kt | 73 +++++ .../android/utils/ui/BaseFragment.java | 40 --- .../android/utils/ui/BaseFragment.kt | 41 +++ .../android/utils/ui/ScreenConfig.kt | 11 + .../flatstack/android/utils/ui/UiInfo.java | 63 ----- app/src/main/res/layout/fragment_main.xml | 6 +- .../flatstack/android/utils/ListsTest.java | 49 ---- .../com/flatstack/android/utils/ListsTest.kt | 33 +++ .../android/utils/storage/RuntimeStorage.java | 71 ----- .../android/utils/storage/RuntimeStorage.kt | 66 +++++ build.gradle | 4 +- deps.gradle | 25 +- gradle/wrapper/gradle-wrapper.properties | 4 +- test-deps.gradle | 20 -- 45 files changed, 752 insertions(+), 1188 deletions(-) delete mode 100644 .idea/codeStyleSettings.xml delete mode 100644 app/src/main/java/com/flatstack/android/App.java create mode 100644 app/src/main/java/com/flatstack/android/App.kt delete mode 100644 app/src/main/java/com/flatstack/android/SecondActivity.java create mode 100644 app/src/main/java/com/flatstack/android/SecondActivity.kt delete mode 100644 app/src/main/java/com/flatstack/android/main_screen/MainActivity.java create mode 100644 app/src/main/java/com/flatstack/android/main_screen/MainActivity.kt delete mode 100644 app/src/main/java/com/flatstack/android/main_screen/MainFragment.java create mode 100644 app/src/main/java/com/flatstack/android/main_screen/MainFragment.kt delete mode 100644 app/src/main/java/com/flatstack/android/main_screen/TestDialog.java create mode 100644 app/src/main/java/com/flatstack/android/main_screen/TestDialog.kt delete mode 100644 app/src/main/java/com/flatstack/android/utils/Intents.java create mode 100644 app/src/main/java/com/flatstack/android/utils/Intents.kt delete mode 100644 app/src/main/java/com/flatstack/android/utils/Keyboard.java create mode 100644 app/src/main/java/com/flatstack/android/utils/Keyboard.kt delete mode 100644 app/src/main/java/com/flatstack/android/utils/Lists.java create mode 100644 app/src/main/java/com/flatstack/android/utils/Lists.kt delete mode 100644 app/src/main/java/com/flatstack/android/utils/recycler_view/BaseAdapter.java create mode 100644 app/src/main/java/com/flatstack/android/utils/recycler_view/BaseAdapter.kt delete mode 100644 app/src/main/java/com/flatstack/android/utils/recycler_view/BaseHolder.java create mode 100644 app/src/main/java/com/flatstack/android/utils/recycler_view/BaseHolder.kt delete mode 100644 app/src/main/java/com/flatstack/android/utils/recycler_view/OnItemClickListener.java create mode 100644 app/src/main/java/com/flatstack/android/utils/recycler_view/OnItemClickListener.kt delete mode 100644 app/src/main/java/com/flatstack/android/utils/storage/IStorage.java create mode 100644 app/src/main/java/com/flatstack/android/utils/storage/IStorage.kt delete mode 100644 app/src/main/java/com/flatstack/android/utils/storage/Storage.java create mode 100644 app/src/main/java/com/flatstack/android/utils/storage/Storage.kt delete mode 100644 app/src/main/java/com/flatstack/android/utils/ui/BaseActivity.java create mode 100644 app/src/main/java/com/flatstack/android/utils/ui/BaseActivity.kt delete mode 100644 app/src/main/java/com/flatstack/android/utils/ui/BaseDialogFragment.java create mode 100644 app/src/main/java/com/flatstack/android/utils/ui/BaseDialogFragment.kt delete mode 100644 app/src/main/java/com/flatstack/android/utils/ui/BaseFragment.java create mode 100644 app/src/main/java/com/flatstack/android/utils/ui/BaseFragment.kt create mode 100644 app/src/main/java/com/flatstack/android/utils/ui/ScreenConfig.kt delete mode 100644 app/src/main/java/com/flatstack/android/utils/ui/UiInfo.java delete mode 100644 app/src/test/java/com/flatstack/android/utils/ListsTest.java create mode 100644 app/src/test/java/com/flatstack/android/utils/ListsTest.kt delete mode 100644 app/src/test/java/com/flatstack/android/utils/storage/RuntimeStorage.java create mode 100644 app/src/test/java/com/flatstack/android/utils/storage/RuntimeStorage.kt delete mode 100644 test-deps.gradle diff --git a/.idea/codeStyleSettings.xml b/.idea/codeStyleSettings.xml deleted file mode 100644 index a9d0c52..0000000 --- a/.idea/codeStyleSettings.xml +++ /dev/null @@ -1,252 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 627d6aa..7916afd 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,13 +1,13 @@ apply plugin: 'com.android.application' +apply plugin: 'kotlin-android' +apply plugin: 'kotlin-kapt' apply from: 'checkstyle/checkstyle.gradle' apply from: './jacoco.gradle' apply from: '../deps.gradle' ext { - APPLICATION_ID = "com.flatstack.android" isCI = "true".equals(System.getenv("CI")) - commitMessage = 'git log -1 --pretty=%B'.execute().text.trim() // STORE_PASSWORD = System.getenv("PROJECT_NAME_STORE_PASSWORD") // KEY_ALIAS = System.getenv("PROJECT_NAME_KEY_ALIAS") // KEY_PASSWORD = System.getenv("PROJECT_NAME_KEY_PASSWORD") @@ -21,7 +21,7 @@ android { minSdkVersion versions.MIN_SDK_VERSION targetSdkVersion versions.TARGET_SDK_VERSION - applicationId APPLICATION_ID + applicationId 'com.flatstack.android' versionCode 1 versionName '0.9.1' testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner' @@ -112,9 +112,8 @@ repositories { mavenCentral() } -apply from: '../test-deps.gradle' - dependencies { + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$versions.kotlin" implementation supportLibs implementation rxJavaLibs // implementation retrofitLibs @@ -123,7 +122,10 @@ dependencies { compileOnly 'org.jetbrains:annotations:13.0' implementation 'com.google.code.gson:gson:2.4' implementation 'com.github.bumptech.glide:glide:3.7.0' - implementation 'com.jakewharton:butterknife:7.0.0' + + implementation "com.jakewharton:butterknife:$versions.butterKnife" + kapt "com.jakewharton:butterknife-compiler:$versions.butterKnife" + testImplementation unitTestLibs androidTestImplementation androidTestsLibs } diff --git a/app/src/main/java/com/flatstack/android/App.java b/app/src/main/java/com/flatstack/android/App.java deleted file mode 100644 index e56e149..0000000 --- a/app/src/main/java/com/flatstack/android/App.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.flatstack.android; - -import android.app.Application; - -public class App extends Application { - - @Override - public void onCreate() { - super.onCreate(); - // your super code here - } -} \ No newline at end of file diff --git a/app/src/main/java/com/flatstack/android/App.kt b/app/src/main/java/com/flatstack/android/App.kt new file mode 100644 index 0000000..e994a33 --- /dev/null +++ b/app/src/main/java/com/flatstack/android/App.kt @@ -0,0 +1,11 @@ +package com.flatstack.android + +import android.app.Application + +class App : Application() { + + override fun onCreate() { + super.onCreate() + // your super code here + } +} \ No newline at end of file diff --git a/app/src/main/java/com/flatstack/android/SecondActivity.java b/app/src/main/java/com/flatstack/android/SecondActivity.java deleted file mode 100644 index fa00f1f..0000000 --- a/app/src/main/java/com/flatstack/android/SecondActivity.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.flatstack.android; - -import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; - -public class SecondActivity extends AppCompatActivity { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_second); - } -} diff --git a/app/src/main/java/com/flatstack/android/SecondActivity.kt b/app/src/main/java/com/flatstack/android/SecondActivity.kt new file mode 100644 index 0000000..0d0c10b --- /dev/null +++ b/app/src/main/java/com/flatstack/android/SecondActivity.kt @@ -0,0 +1,12 @@ +package com.flatstack.android + +import android.os.Bundle +import android.support.v7.app.AppCompatActivity + +class SecondActivity : AppCompatActivity() { + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_second) + } +} diff --git a/app/src/main/java/com/flatstack/android/main_screen/MainActivity.java b/app/src/main/java/com/flatstack/android/main_screen/MainActivity.java deleted file mode 100644 index a320840..0000000 --- a/app/src/main/java/com/flatstack/android/main_screen/MainActivity.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.flatstack.android.main_screen; - -import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; - -import com.flatstack.android.utils.Intents; -import com.flatstack.android.utils.ui.BaseActivity; -import com.flatstack.android.R; -import com.flatstack.android.utils.ui.UiInfo; - -public class MainActivity extends BaseActivity { - - @NonNull @Override public UiInfo getUiInfo() { - return new UiInfo(R.layout.activity_main) - .setTitleRes(R.string.app_name) - .enableBackButton(); - } - - @Override protected void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - if (Intents.isActivityExpandedFromLauncherIcon(this)) { - finish(); - } else if (savedInstanceState == null) { - getSupportFragmentManager().beginTransaction() - .replace(R.id.content, new MainFragment()) - .commit(); - } - } - -} \ No newline at end of file diff --git a/app/src/main/java/com/flatstack/android/main_screen/MainActivity.kt b/app/src/main/java/com/flatstack/android/main_screen/MainActivity.kt new file mode 100644 index 0000000..cd714cf --- /dev/null +++ b/app/src/main/java/com/flatstack/android/main_screen/MainActivity.kt @@ -0,0 +1,24 @@ +package com.flatstack.android.main_screen + +import android.os.Bundle + +import com.flatstack.android.utils.Intents +import com.flatstack.android.utils.ui.BaseActivity +import com.flatstack.android.R +import com.flatstack.android.utils.ui.ScreenConfig + +class MainActivity : BaseActivity() { + override val screenConfig: ScreenConfig + get() = ScreenConfig(R.layout.activity_main, titleRes = R.string.app_name, enableBackButton = true) + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + if (Intents.isActivityExpandedFromLauncherIcon(this)) { + finish() + } else if (savedInstanceState == null) { + supportFragmentManager.beginTransaction() + .replace(R.id.content, MainFragment()) + .commit() + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/flatstack/android/main_screen/MainFragment.java b/app/src/main/java/com/flatstack/android/main_screen/MainFragment.java deleted file mode 100644 index 4255c52..0000000 --- a/app/src/main/java/com/flatstack/android/main_screen/MainFragment.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.flatstack.android.main_screen; - -import android.content.Intent; -import android.os.Bundle; -import android.view.View; -import android.widget.Button; -import android.widget.ImageView; - -import com.bumptech.glide.Glide; -import com.flatstack.android.R; -import com.flatstack.android.SecondActivity; -import com.flatstack.android.utils.ui.BaseFragment; - -import butterknife.Bind; - -public class MainFragment extends BaseFragment { - - @Bind(R.id.image) ImageView image; - @Bind(R.id.button) Button button; - - @Override public int getLayoutRes() { - return R.layout.fragment_main; - } - - @Override public void onViewCreated(View view, Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - - image.setOnClickListener(listener -> - TestDialog.show("Example Hello", "Ublyudok, mat' tvoyu, a nu idi syuda, govno" + - " sobachye, reshil ko mne lezt'? Ti, zasranec vonyuchiy, mat' " + - "tvoyu, a?", - getFragmentManager()) - ); - - button.setOnClickListener(v -> startActivity(new Intent(getActivity(), - SecondActivity.class))); - - Glide.with(this) - .load("https://pbs.twimg.com/profile_images/502109671600033792/QOAC0YGo.png") - .into(image); - } -} diff --git a/app/src/main/java/com/flatstack/android/main_screen/MainFragment.kt b/app/src/main/java/com/flatstack/android/main_screen/MainFragment.kt new file mode 100644 index 0000000..ece376c --- /dev/null +++ b/app/src/main/java/com/flatstack/android/main_screen/MainFragment.kt @@ -0,0 +1,41 @@ +package com.flatstack.android.main_screen + +import android.content.Intent +import android.os.Bundle +import android.view.View +import android.widget.Button +import android.widget.ImageView +import butterknife.BindView + +import com.bumptech.glide.Glide +import com.flatstack.android.R +import com.flatstack.android.SecondActivity +import com.flatstack.android.utils.ui.BaseFragment + +class MainFragment : BaseFragment() { + override val layoutRes: Int + get() = R.layout.fragment_main + + @BindView(R.id.ivImage) internal lateinit var uiImage: ImageView + @BindView(R.id.btnButton) internal lateinit var uiButton: Button + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + uiImage.setOnClickListener { + fragmentManager?.let { + TestDialog.show("Example Hello", "Ublyudok, mat' tvoyu, a nu idi syuda, govno" + + " sobachye, reshil ko mne lezt'? Ti, zasranec vonyuchiy, mat' " + + "tvoyu, a?", it) + } + } + + uiButton.setOnClickListener { + startActivity(Intent(activity, SecondActivity::class.java)) + } + + Glide.with(this) + .load("https://pbs.twimg.com/profile_images/502109671600033792/QOAC0YGo.png") + .into(uiImage) + } +} diff --git a/app/src/main/java/com/flatstack/android/main_screen/TestDialog.java b/app/src/main/java/com/flatstack/android/main_screen/TestDialog.java deleted file mode 100644 index 421d99b..0000000 --- a/app/src/main/java/com/flatstack/android/main_screen/TestDialog.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.flatstack.android.main_screen; - -import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v4.app.FragmentManager; -import android.view.View; -import android.widget.TextView; - -import com.flatstack.android.R; -import com.flatstack.android.utils.ui.BaseDialogFragment; - -import butterknife.Bind; - -public class TestDialog extends BaseDialogFragment { - - private static final String KEY_TITLE = "dialogTitle"; - private static final String KEY_MESSAGE = "dialogMessage"; - - @Bind(R.id.dialog_title) TextView uiTitle; - @Bind(R.id.dialog_message) TextView uiMessage; - - private String title; - private String message; - - @Override public int getLayoutRes() { - return R.layout.dialog_test; - } - - public static void show(@Nullable String title, - @Nullable String message, - @NonNull FragmentManager fm) { - - TestDialog dialog = new TestDialog(); - - Bundle args = new Bundle(); - args.putString(KEY_TITLE, title != null ? title : ""); - args.putString(KEY_MESSAGE, message != null ? message : ""); - dialog.setArguments(args); - - dialog.show(fm, TestDialog.class.getName()); - } - - @Override protected void parseArguments(@NonNull Bundle args) { - title = getArguments().getString(KEY_TITLE); - message = getArguments().getString(KEY_MESSAGE); - } - - @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - uiTitle.setText(title != null ? title : ""); - uiMessage.setText(message != null ? message : ""); - } -} diff --git a/app/src/main/java/com/flatstack/android/main_screen/TestDialog.kt b/app/src/main/java/com/flatstack/android/main_screen/TestDialog.kt new file mode 100644 index 0000000..d590dfb --- /dev/null +++ b/app/src/main/java/com/flatstack/android/main_screen/TestDialog.kt @@ -0,0 +1,54 @@ +package com.flatstack.android.main_screen + +import android.os.Bundle +import android.support.v4.app.FragmentManager +import android.view.View +import android.widget.TextView +import butterknife.BindView + +import com.flatstack.android.R +import com.flatstack.android.utils.ui.BaseDialogFragment + +class TestDialog : BaseDialogFragment() { + @BindView(R.id.dialog_title) internal lateinit var uiTitle: TextView + @BindView(R.id.dialog_message) internal lateinit var uiMessage: TextView + + private var title: String? = null + private var message: String? = null + + override val layoutRes: Int + get() = R.layout.dialog_test + + override fun parseArguments(args: Bundle) { + arguments?.let { + title = it.getString(KEY_TITLE) + message = it.getString(KEY_MESSAGE) + } + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + uiTitle.text = title + uiMessage.text = message + } + + companion object { + + private const val KEY_TITLE = "dialogTitle" + private const val KEY_MESSAGE = "dialogMessage" + + fun show(title: String?, + message: String?, + fm: FragmentManager) { + + val dialog = TestDialog() + + val args = Bundle() + args.putString(KEY_TITLE, title ?: "") + args.putString(KEY_MESSAGE, message ?: "") + dialog.arguments = args + + dialog.show(fm, TestDialog::class.java.name) + } + } +} diff --git a/app/src/main/java/com/flatstack/android/utils/Intents.java b/app/src/main/java/com/flatstack/android/utils/Intents.java deleted file mode 100644 index 80bee0b..0000000 --- a/app/src/main/java/com/flatstack/android/utils/Intents.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.flatstack.android.utils; - -import android.app.Activity; -import android.content.Intent; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; - -/** - * Created by almaziskhakov on 22/11/2017. - */ - -public class Intents { - - public static boolean isActivityExpandedFromLauncherIcon(@NonNull Activity activity) { - return !activity.isTaskRoot() && isActivityStartedFromLauncherIcon(activity.getIntent()); - } - - public static boolean isActivityStartedFromLauncherIcon(@Nullable Intent intent) { - return intent != null && - intent.hasCategory(Intent.CATEGORY_LAUNCHER) && - intent.getAction() != null && - intent.getAction().equals(Intent.ACTION_MAIN); - } - -} \ No newline at end of file diff --git a/app/src/main/java/com/flatstack/android/utils/Intents.kt b/app/src/main/java/com/flatstack/android/utils/Intents.kt new file mode 100644 index 0000000..6620ff3 --- /dev/null +++ b/app/src/main/java/com/flatstack/android/utils/Intents.kt @@ -0,0 +1,19 @@ +package com.flatstack.android.utils + +import android.app.Activity +import android.content.Intent + +object Intents { + + fun isActivityExpandedFromLauncherIcon(activity: Activity): Boolean { + return !activity.isTaskRoot && isActivityStartedFromLauncherIcon(activity.intent) + } + + private fun isActivityStartedFromLauncherIcon(intent: Intent?): Boolean { + return intent != null && + intent.hasCategory(Intent.CATEGORY_LAUNCHER) && + intent.action != null && + intent.action == Intent.ACTION_MAIN + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/flatstack/android/utils/Keyboard.java b/app/src/main/java/com/flatstack/android/utils/Keyboard.java deleted file mode 100644 index 295b3e0..0000000 --- a/app/src/main/java/com/flatstack/android/utils/Keyboard.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.flatstack.android.utils; - -import android.app.Activity; -import android.content.Context; -import android.support.annotation.NonNull; -import android.view.View; -import android.view.inputmethod.InputMethodManager; - -public class Keyboard { - - public static void hide(@NonNull Activity activity) { - InputMethodManager inputManager = (InputMethodManager) activity - .getSystemService(Context.INPUT_METHOD_SERVICE); - View v = activity.getCurrentFocus(); - if (v != null) { - inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0); - } - } - - public static void hide(@NonNull View view) { - InputMethodManager imm = (InputMethodManager) view.getContext() - .getSystemService(Context.INPUT_METHOD_SERVICE); - if (!imm.isActive()) { - return; - } - imm.hideSoftInputFromWindow(view.getWindowToken(), 0); - } - - public static void show(@NonNull Context context) { - InputMethodManager imm = (InputMethodManager) context - .getSystemService(Context.INPUT_METHOD_SERVICE); - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - } - - public static void show(@NonNull View view) { - InputMethodManager inputManager = (InputMethodManager) view.getContext() - .getSystemService(Context.INPUT_METHOD_SERVICE); - inputManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT); - } -} diff --git a/app/src/main/java/com/flatstack/android/utils/Keyboard.kt b/app/src/main/java/com/flatstack/android/utils/Keyboard.kt new file mode 100644 index 0000000..a4b422f --- /dev/null +++ b/app/src/main/java/com/flatstack/android/utils/Keyboard.kt @@ -0,0 +1,39 @@ +package com.flatstack.android.utils + +import android.app.Activity +import android.content.Context +import android.view.View +import android.view.inputmethod.InputMethodManager + +object Keyboard { + + fun hide(activity: Activity) { + val inputManager = activity + .getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager + val v = activity.currentFocus + if (v != null) { + inputManager.hideSoftInputFromWindow(v.windowToken, 0) + } + } + + fun hide(view: View) { + val imm = view.context + .getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager + if (!imm.isActive) { + return + } + imm.hideSoftInputFromWindow(view.windowToken, 0) + } + + fun show(context: Context) { + val imm = context + .getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager + imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0) + } + + fun show(view: View) { + val inputManager = view.context + .getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager + inputManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT) + } +} diff --git a/app/src/main/java/com/flatstack/android/utils/Lists.java b/app/src/main/java/com/flatstack/android/utils/Lists.java deleted file mode 100644 index 7c128e9..0000000 --- a/app/src/main/java/com/flatstack/android/utils/Lists.java +++ /dev/null @@ -1,92 +0,0 @@ -package com.flatstack.android.utils; - -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v4.util.Pair; - -import org.jetbrains.annotations.Contract; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import rx.functions.Action1; -import rx.functions.Func1; - -/** - * Created by adel on 6/7/14 - */ -public class Lists { - - @Contract("null -> true") - public static boolean isEmpty(@Nullable List list) { - return list == null || list.isEmpty(); - } - - /** - * @return list of T which contains values which filter.call(item) returns true - */ - @Contract(pure = true) - @NonNull - public static List filterBy(@NonNull List list, - @NonNull Func1 filter) { - List filteredList = new ArrayList<>(); - for (T item : list) { - if (filter.call(item)) { - filteredList.add(item); - } - } - return filteredList; - } - - @Nullable - @Contract(value = "null, null -> null; !null, _ -> !null; _, !null -> !null", pure = true) - public static List merge(@Nullable List list1, @Nullable List list2) { - if (list1 == null && list2 == null) return null; - List finalList = new ArrayList(); - if (list1 != null) { - finalList.addAll(list1); - } - if (list2 != null) { - finalList.addAll(list2); - } - return finalList; - } - - @Nullable - @Contract(value = "null -> null; !null -> !null", pure = false) - public static List uniq(@Nullable List list) { - if (list != null) { - Set set = new HashSet<>(list); - list.clear(); - list.addAll(set); - } - return list; - } - - public static void forEach(@NonNull List list, @NonNull Action1 action) { - for (T item : list) { - action.call(item); - } - } - - /** - * @return Pair of two lists. First list contains values which satisfy groupBy - * i.e. condition.call(item) returns true. Second list contains rest of items from #list. - */ - @Contract(pure = true) - public static Pair, List> groupBy(@NonNull List list, - @NonNull Func1 condition) { - List list1 = new ArrayList<>(); - List list2 = new ArrayList<>(); - for (T item : list) { - if (condition.call(item)) { - list1.add(item); - } else { - list2.add(item); - } - } - return new Pair<>(list1, list2); - } -} diff --git a/app/src/main/java/com/flatstack/android/utils/Lists.kt b/app/src/main/java/com/flatstack/android/utils/Lists.kt new file mode 100644 index 0000000..787b473 --- /dev/null +++ b/app/src/main/java/com/flatstack/android/utils/Lists.kt @@ -0,0 +1,82 @@ +package com.flatstack.android.utils + +import android.support.v4.util.Pair + +import org.jetbrains.annotations.Contract + +import java.util.ArrayList +import java.util.HashSet + +import rx.functions.Action1 +import rx.functions.Func1 + +object Lists { + + @Contract("null -> true") + fun isEmpty(list: List<*>?): Boolean { + return list == null || list.isEmpty() + } + + /** + * @return list of T which contains values which filter.call(item) returns true + */ + @Contract(pure = true) + fun filterBy(list: List, + filter: Func1): List { + val filteredList = ArrayList() + for (item in list) { + if (filter.call(item)) { + filteredList.add(item) + } + } + return filteredList + } + + @Contract(value = "null, null -> null; !null, _ -> !null; _, !null -> !null", pure = true) + fun merge(list1: List?, list2: List?): List? { + if (list1 == null && list2 == null) return null + val finalList = ArrayList() + if (list1 != null) { + finalList.addAll(list1) + } + if (list2 != null) { + finalList.addAll(list2) + } + return finalList + } + + @Contract(value = "null -> null; !null -> !null", pure = false) + fun uniq(list: MutableList?): List? { + if (list != null) { + val set = HashSet(list) + list.clear() + list.addAll(set) + } + return list + } + + fun forEach(list: List, action: Action1) { + for (item in list) { + action.call(item) + } + } + + /** + * @return Pair of two lists. First list contains values which satisfy groupBy + * i.e. condition.call(item) returns true. Second list contains rest of items from #list. + */ + @Contract(pure = true) + fun groupBy(list: List, + condition: Func1): Pair, List> { + val list1 = ArrayList() + val list2 = ArrayList() + for (item in list) { + if (condition.call(item)) { + list1.add(item) + } else { + list2.add(item) + } + } + return Pair(list1, list2) + } +} diff --git a/app/src/main/java/com/flatstack/android/utils/recycler_view/BaseAdapter.java b/app/src/main/java/com/flatstack/android/utils/recycler_view/BaseAdapter.java deleted file mode 100644 index 22b78d7..0000000 --- a/app/src/main/java/com/flatstack/android/utils/recycler_view/BaseAdapter.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.flatstack.android.utils.recycler_view; - -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v7.widget.RecyclerView; -import android.view.ViewGroup; - -import java.util.List; - -import rx.functions.Func1; - -/** - * Created by ereminilya on 14/12/16. - */ -public class BaseAdapter> extends RecyclerView.Adapter { - - @NonNull private final Func1 func0; - @NonNull private final List items; - - @Nullable private OnItemClickListener onItemClickListener; - - public BaseAdapter(@NonNull List items, @NonNull Func1 func0) { - this(items, func0, null); - } - - public BaseAdapter(@NonNull List items, @NonNull Func1 func0, - @Nullable OnItemClickListener onItemClickListener) { - this.items = items; - this.func0 = func0; - this.onItemClickListener = onItemClickListener; - } - - @Override public VH onCreateViewHolder(ViewGroup viewGroup, int holderType) { - VH holder = func0.call(viewGroup); - holder.itemView.setOnClickListener(v -> { - int position = holder.getAdapterPosition(); - if (position != RecyclerView.NO_POSITION && onItemClickListener != null) { - onItemClickListener.onItemClick(getData().get(position)); - } - }); - return holder; - } - - @Override public void onBindViewHolder(VH vh, int position) { - vh.bind(items.get(position)); - } - - @Override public int getItemCount() { - return items.size(); - } - - public List getData() { - return items; - } - - public void add(T item) { - this.items.add(item); - notifyItemInserted(items.size() - 1); - } -} \ No newline at end of file diff --git a/app/src/main/java/com/flatstack/android/utils/recycler_view/BaseAdapter.kt b/app/src/main/java/com/flatstack/android/utils/recycler_view/BaseAdapter.kt new file mode 100644 index 0000000..b0ae008 --- /dev/null +++ b/app/src/main/java/com/flatstack/android/utils/recycler_view/BaseAdapter.kt @@ -0,0 +1,37 @@ +package com.flatstack.android.utils.recycler_view + +import android.support.v7.widget.RecyclerView +import android.view.ViewGroup + +import rx.functions.Func1 + +class BaseAdapter> @JvmOverloads constructor(private val items: MutableList, private val func0: Func1, + private val onItemClickListener: OnItemClickListener? = null) : RecyclerView.Adapter() { + + val data: List + get() = items + + override fun onCreateViewHolder(viewGroup: ViewGroup, holderType: Int): VH { + val holder = func0.call(viewGroup) + holder.itemView.setOnClickListener { + val position = holder.adapterPosition + if (position != RecyclerView.NO_POSITION && onItemClickListener != null) { + onItemClickListener.onItemClick(data[position]) + } + } + return holder + } + + override fun onBindViewHolder(vh: VH, position: Int) { + vh.bind(items[position]) + } + + override fun getItemCount(): Int { + return items.size + } + + fun add(item: T) { + this.items.add(item) + notifyItemInserted(items.size - 1) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/flatstack/android/utils/recycler_view/BaseHolder.java b/app/src/main/java/com/flatstack/android/utils/recycler_view/BaseHolder.java deleted file mode 100644 index bf1a477..0000000 --- a/app/src/main/java/com/flatstack/android/utils/recycler_view/BaseHolder.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.flatstack.android.utils.recycler_view; - -import android.support.v7.widget.RecyclerView; -import android.view.View; - -/** - * Created by ereminilya on 14/12/16. - */ -public abstract class BaseHolder extends RecyclerView.ViewHolder { - - public BaseHolder(View itemView) { - super(itemView); - } - - protected abstract void bind(T item); -} diff --git a/app/src/main/java/com/flatstack/android/utils/recycler_view/BaseHolder.kt b/app/src/main/java/com/flatstack/android/utils/recycler_view/BaseHolder.kt new file mode 100644 index 0000000..7d82bd5 --- /dev/null +++ b/app/src/main/java/com/flatstack/android/utils/recycler_view/BaseHolder.kt @@ -0,0 +1,8 @@ +package com.flatstack.android.utils.recycler_view + +import android.support.v7.widget.RecyclerView +import android.view.View + +abstract class BaseHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { + internal abstract fun bind(item: T) +} diff --git a/app/src/main/java/com/flatstack/android/utils/recycler_view/OnItemClickListener.java b/app/src/main/java/com/flatstack/android/utils/recycler_view/OnItemClickListener.java deleted file mode 100644 index 79a8305..0000000 --- a/app/src/main/java/com/flatstack/android/utils/recycler_view/OnItemClickListener.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.flatstack.android.utils.recycler_view; - -import android.support.annotation.NonNull; - -/** - * Created by ereminilya on 14/12/16. - */ -public interface OnItemClickListener { - - void onItemClick(@NonNull T item); -} diff --git a/app/src/main/java/com/flatstack/android/utils/recycler_view/OnItemClickListener.kt b/app/src/main/java/com/flatstack/android/utils/recycler_view/OnItemClickListener.kt new file mode 100644 index 0000000..c499f47 --- /dev/null +++ b/app/src/main/java/com/flatstack/android/utils/recycler_view/OnItemClickListener.kt @@ -0,0 +1,5 @@ +package com.flatstack.android.utils.recycler_view + +interface OnItemClickListener { + fun onItemClick(item: T) +} diff --git a/app/src/main/java/com/flatstack/android/utils/storage/IStorage.java b/app/src/main/java/com/flatstack/android/utils/storage/IStorage.java deleted file mode 100644 index aa0bfcb..0000000 --- a/app/src/main/java/com/flatstack/android/utils/storage/IStorage.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.flatstack.android.utils.storage; - -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; - -import java.lang.reflect.Type; -import java.util.List; - -/** - * Created by ereminilya on 8/1/17. - */ - -public interface IStorage { - - @Nullable T get(@NonNull String key, @NonNull Type type); - - void put(@NonNull String key, @NonNull T item); - - void putString(@NonNull final String key, @NonNull String str); - - @Nullable String getString(@NonNull String key); - - void putLong(@NonNull String key, long number); - - long getLong(@NonNull String key, long defaultValue); - - void putInt(@NonNull String key, int number); - - int getInt(@NonNull String key, int defaultValue); - - void putBoolean(@NonNull String key, boolean value); - - boolean getBoolean(@NonNull String key, boolean defaultValue); - - void remove(@NonNull String key); - - void putCollection(@NonNull String key, @NonNull List items); - - @Nullable List getCollection(@NonNull String key, @NonNull Type type); -} diff --git a/app/src/main/java/com/flatstack/android/utils/storage/IStorage.kt b/app/src/main/java/com/flatstack/android/utils/storage/IStorage.kt new file mode 100644 index 0000000..b12a698 --- /dev/null +++ b/app/src/main/java/com/flatstack/android/utils/storage/IStorage.kt @@ -0,0 +1,32 @@ +package com.flatstack.android.utils.storage + +import java.lang.reflect.Type + +interface IStorage { + + operator fun get(key: String, type: Type): T? + + fun put(key: String, items: Any) + + fun putString(key: String, str: String) + + fun getString(key: String): String? + + fun putLong(key: String, number: Long) + + fun getLong(key: String, defaultValue: Long): Long + + fun putInt(key: String, number: Int) + + fun getInt(key: String, defaultValue: Int): Int + + fun putBoolean(key: String, value: Boolean) + + fun getBoolean(key: String, defaultValue: Boolean): Boolean + + fun remove(key: String) + + fun putCollection(key: String, items: List) + + fun getCollection(key: String, type: Type): List? +} diff --git a/app/src/main/java/com/flatstack/android/utils/storage/Storage.java b/app/src/main/java/com/flatstack/android/utils/storage/Storage.java deleted file mode 100644 index b395c3f..0000000 --- a/app/src/main/java/com/flatstack/android/utils/storage/Storage.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.flatstack.android.utils.storage; - -import android.content.SharedPreferences; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; - -import com.google.gson.Gson; - -import java.lang.reflect.Type; -import java.util.List; - -/** - * Created by ereminilya on 8/1/17. - */ - -public class Storage implements IStorage { - - private final SharedPreferences sp; - private final Gson gson; - - public Storage(SharedPreferences sp, Gson gson) { - this.sp = sp; - this.gson = gson; - } - - @Override - @Nullable public T get(@NonNull String key, @NonNull Type type) { - String json = sp.getString(key, ""); - if ("".equals(json)) return null; - else { - return gson.fromJson(json, type); - } - } - - @Override public void put(@NonNull String key, @NonNull Object items) { - sp.edit().putString(key, gson.toJson(items)).apply(); - } - - @Override public void putString(@NonNull String key, @NonNull String str) { - sp.edit().putString(key, str).apply(); - } - - @Nullable @Override public String getString(@NonNull String key) { - return sp.getString(key, null); - } - - @Override public void putLong(@NonNull String key, long number) { - sp.edit().putLong(key, number).apply(); - } - - @Override public long getLong(@NonNull String key, long defaultValue) { - return sp.getLong(key, defaultValue); - } - - @Override public void putInt(@NonNull String key, int number) { - sp.edit().putInt(key, number).apply(); - } - - @Override public int getInt(@NonNull String key, int defaultValue) { - return sp.getInt(key, defaultValue); - } - - @Override public void putBoolean(@NonNull String key, boolean value) { - sp.edit().putBoolean(key, value).apply(); - } - - @Override public boolean getBoolean(@NonNull String key, boolean defaultValue) { - return sp.getBoolean(key, defaultValue); - } - - @Override public void remove(@NonNull String key) { - sp.edit().remove(key).apply(); - } - - public void putCollection(@NonNull String key, @NonNull List items) { - put(key, items); - } - - @Override public List getCollection(@NonNull String key, @NonNull Type type) { - return get(key, type); - } - -} \ No newline at end of file diff --git a/app/src/main/java/com/flatstack/android/utils/storage/Storage.kt b/app/src/main/java/com/flatstack/android/utils/storage/Storage.kt new file mode 100644 index 0000000..b20b2be --- /dev/null +++ b/app/src/main/java/com/flatstack/android/utils/storage/Storage.kt @@ -0,0 +1,66 @@ +package com.flatstack.android.utils.storage + +import android.content.SharedPreferences + +import com.google.gson.Gson + +import java.lang.reflect.Type + +class Storage(private val sp: SharedPreferences, private val gson: Gson) : IStorage { + override fun get(key: String, type: Type): T? { + val json = sp.getString(key, "") + return if ("" == json) + null + else { + gson.fromJson(json, type) + } + } + + override fun put(key: String, items: Any) { + sp.edit().putString(key, gson.toJson(items)).apply() + } + + override fun putString(key: String, str: String) { + sp.edit().putString(key, str).apply() + } + + override fun getString(key: String): String? { + return sp.getString(key, null) + } + + override fun putLong(key: String, number: Long) { + sp.edit().putLong(key, number).apply() + } + + override fun getLong(key: String, defaultValue: Long): Long { + return sp.getLong(key, defaultValue) + } + + override fun putInt(key: String, number: Int) { + sp.edit().putInt(key, number).apply() + } + + override fun getInt(key: String, defaultValue: Int): Int { + return sp.getInt(key, defaultValue) + } + + override fun putBoolean(key: String, value: Boolean) { + sp.edit().putBoolean(key, value).apply() + } + + override fun getBoolean(key: String, defaultValue: Boolean): Boolean { + return sp.getBoolean(key, defaultValue) + } + + override fun remove(key: String) { + sp.edit().remove(key).apply() + } + + override fun putCollection(key: String, items: List) { + put(key, items) + } + + override fun getCollection(key: String, type: Type): List? { + return get>(key, type) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/flatstack/android/utils/ui/BaseActivity.java b/app/src/main/java/com/flatstack/android/utils/ui/BaseActivity.java deleted file mode 100644 index 984fb42..0000000 --- a/app/src/main/java/com/flatstack/android/utils/ui/BaseActivity.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.flatstack.android.utils.ui; - -import android.content.Context; -import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.Toolbar; -import android.view.Menu; -import android.view.MenuInflater; -import android.view.MenuItem; -import android.view.View; - -import com.flatstack.android.R; -import com.flatstack.android.utils.Keyboard; - -import butterknife.Bind; -import butterknife.ButterKnife; - -public abstract class BaseActivity extends AppCompatActivity { - - @Bind(R.id.toolbar) View toolbar; - - @NonNull public abstract UiInfo getUiInfo(); - - @Override protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(getUiInfo().getLayoutRes()); - ButterKnife.bind(this); - if (getIntent() != null && getIntent().getExtras() != null) { - parseArguments(getIntent().getExtras()); - } - if (toolbar != null) { - setSupportActionBar((Toolbar) toolbar); - if (getUiInfo().getTitleRes() != 0) { - setTitle(getUiInfo().getTitleRes()); - } else if (getUiInfo().getTitle() != null) { - setTitle(getUiInfo().getTitle()); - } - if (getUiInfo().isHasBackButton()) { - getSupportActionBar().setDisplayHomeAsUpEnabled(true); - } - } - } - - @Override public void setContentView(int layoutResID) { - super.setContentView(layoutResID); - ButterKnife.bind(this); - } - - protected void parseArguments(@NonNull Bundle extras) { - } - - @Override public boolean onCreateOptionsMenu(Menu menu) { - if (getUiInfo().getMenuRes() != 0) { - MenuInflater inflater = getMenuInflater(); - inflater.inflate(getUiInfo().getMenuRes(), menu); - return true; - } - return super.onCreateOptionsMenu(menu); - } - - @Override public boolean onOptionsItemSelected(MenuItem item) { - if (item.getItemId() == android.R.id.home && getUiInfo().isHasBackButton()) { - Keyboard.hide(this); - finish(); - return true; - } - return super.onOptionsItemSelected(item); - } - - public Context context() { - return this; - } -} diff --git a/app/src/main/java/com/flatstack/android/utils/ui/BaseActivity.kt b/app/src/main/java/com/flatstack/android/utils/ui/BaseActivity.kt new file mode 100644 index 0000000..4e9ad92 --- /dev/null +++ b/app/src/main/java/com/flatstack/android/utils/ui/BaseActivity.kt @@ -0,0 +1,61 @@ +package com.flatstack.android.utils.ui + +import android.os.Bundle +import android.support.v7.app.AppCompatActivity +import android.support.v7.widget.Toolbar +import android.view.Menu +import android.view.MenuItem +import android.view.View + +import com.flatstack.android.R +import com.flatstack.android.utils.Keyboard + +import butterknife.ButterKnife + +abstract class BaseActivity : AppCompatActivity() { + + abstract val screenConfig: ScreenConfig + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(screenConfig.layoutRes) + ButterKnife.bind(this) + intent?.extras?.let { parseArguments(it) } + findViewById(R.id.toolbar)?.let { + setSupportActionBar(it) + if (screenConfig.titleRes != 0) { + setTitle(screenConfig.titleRes) + } else if (screenConfig.title != null) { + title = screenConfig.title + } + if (screenConfig.enableBackButton) { + supportActionBar?.setDisplayHomeAsUpEnabled(true) + } + } + } + + override fun setContentView(layoutResID: Int) { + super.setContentView(layoutResID) + ButterKnife.bind(this) + } + + internal fun parseArguments(extras: Bundle) {} + + override fun onCreateOptionsMenu(menu: Menu): Boolean { + if (screenConfig.menuRes != 0) { + val inflater = menuInflater + inflater.inflate(screenConfig.menuRes, menu) + return true + } + return super.onCreateOptionsMenu(menu) + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + if (item.itemId == android.R.id.home && screenConfig.enableBackButton) { + Keyboard.hide(this) + finish() + return true + } + return super.onOptionsItemSelected(item) + } +} diff --git a/app/src/main/java/com/flatstack/android/utils/ui/BaseDialogFragment.java b/app/src/main/java/com/flatstack/android/utils/ui/BaseDialogFragment.java deleted file mode 100644 index d09d698..0000000 --- a/app/src/main/java/com/flatstack/android/utils/ui/BaseDialogFragment.java +++ /dev/null @@ -1,84 +0,0 @@ -package com.flatstack.android.utils.ui; - -import android.app.Dialog; -import android.content.Context; -import android.os.Bundle; -import android.support.annotation.LayoutRes; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v4.app.DialogFragment; -import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentActivity; -import android.support.v4.app.FragmentTransaction; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.view.Window; - -import butterknife.ButterKnife; - -public abstract class BaseDialogFragment extends DialogFragment { - - @LayoutRes public abstract int getLayoutRes(); - - protected static T show(@NonNull T dialogFragment, - @NonNull FragmentActivity activity) { - FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction(); - Fragment prev = activity.getSupportFragmentManager() - .findFragmentByTag(dialogFragment.getClass().getName()); - if (prev != null) { - ft.remove(prev); - DialogFragment df = (DialogFragment) prev; - df.dismissAllowingStateLoss(); - } - ft.addToBackStack(null); - dialogFragment.show(ft, dialogFragment.getClass().getName()); - return dialogFragment; - } - - @Override public void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - if (getArguments() != null) { - parseArguments(getArguments()); - } - if (savedInstanceState != null) { - restoreState(savedInstanceState); - } - } - - protected void restoreState(@NonNull Bundle savedState) { - } - - protected void parseArguments(@NonNull Bundle args) { - throw new IllegalStateException("should be overridden"); - } - - @NonNull @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - Dialog dialog = super.onCreateDialog(savedInstanceState); - dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE); - return dialog; - } - - @Override - public View onCreateView(LayoutInflater inflater, - @Nullable ViewGroup container, - @Nullable Bundle savedInstanceState) { - View v = inflater.inflate(getLayoutRes(), container, false); - ButterKnife.bind(this, v); - return v; - } - - @Override public void onDestroyView() { - ButterKnife.unbind(this); - super.onDestroyView(); - } - - public Context context() { - return getActivity(); - } - - public FragmentActivity activity() { - return getActivity(); - } -} diff --git a/app/src/main/java/com/flatstack/android/utils/ui/BaseDialogFragment.kt b/app/src/main/java/com/flatstack/android/utils/ui/BaseDialogFragment.kt new file mode 100644 index 0000000..930de14 --- /dev/null +++ b/app/src/main/java/com/flatstack/android/utils/ui/BaseDialogFragment.kt @@ -0,0 +1,73 @@ +package com.flatstack.android.utils.ui + +import android.app.Dialog +import android.os.Bundle +import android.support.v4.app.DialogFragment +import android.support.v4.app.FragmentActivity +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.view.Window + +import butterknife.ButterKnife +import butterknife.Unbinder + +abstract class BaseDialogFragment : DialogFragment() { + + private var butterKnifeUnbinder: Unbinder? = null + + internal abstract val layoutRes: Int + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + if (arguments != null) { + parseArguments(arguments!!) + } + if (savedInstanceState != null) { + restoreState(savedInstanceState) + } + } + + protected fun restoreState(savedState: Bundle) {} + + protected open fun parseArguments(args: Bundle) { + throw IllegalStateException("should be overridden") + } + + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + val dialog = super.onCreateDialog(savedInstanceState) + dialog.window!!.requestFeature(Window.FEATURE_NO_TITLE) + return dialog + } + + override fun onCreateView(inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle?): View? { + val v = inflater.inflate(layoutRes, container, false) + butterKnifeUnbinder = ButterKnife.bind(this, v) + return v + } + + override fun onDestroyView() { + butterKnifeUnbinder!!.unbind() + super.onDestroyView() + } + + companion object { + + protected fun show(dialogFragment: T, + activity: FragmentActivity): T { + val ft = activity.supportFragmentManager.beginTransaction() + val prev = activity.supportFragmentManager + .findFragmentByTag(dialogFragment.javaClass.name) + if (prev != null) { + ft.remove(prev) + val df = prev as DialogFragment + df.dismissAllowingStateLoss() + } + ft.addToBackStack(null) + dialogFragment.show(ft, dialogFragment.javaClass.name) + return dialogFragment + } + } +} diff --git a/app/src/main/java/com/flatstack/android/utils/ui/BaseFragment.java b/app/src/main/java/com/flatstack/android/utils/ui/BaseFragment.java deleted file mode 100644 index 2395d3a..0000000 --- a/app/src/main/java/com/flatstack/android/utils/ui/BaseFragment.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.flatstack.android.utils.ui; - -import android.content.Context; -import android.os.Bundle; -import android.support.annotation.LayoutRes; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v4.app.Fragment; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; - -import butterknife.ButterKnife; - -public abstract class BaseFragment extends Fragment { - - @LayoutRes public abstract int getLayoutRes(); - - @Override public void onCreate(@Nullable Bundle savedState) { - super.onCreate(savedState); - if (savedState != null) { - restoreState(savedState); - } - } - - public void restoreState(@NonNull Bundle savedState) { - } - - @Nullable @Override public View onCreateView(LayoutInflater inflater, - @Nullable ViewGroup container, - @Nullable Bundle savedInstanceState) { - View rootView = inflater.inflate(getLayoutRes(), container, false); - ButterKnife.bind(this, rootView); - return rootView; - } - - protected Context context() { - return getContext(); - } -} diff --git a/app/src/main/java/com/flatstack/android/utils/ui/BaseFragment.kt b/app/src/main/java/com/flatstack/android/utils/ui/BaseFragment.kt new file mode 100644 index 0000000..dded898 --- /dev/null +++ b/app/src/main/java/com/flatstack/android/utils/ui/BaseFragment.kt @@ -0,0 +1,41 @@ +package com.flatstack.android.utils.ui + +import android.os.Bundle +import android.support.annotation.LayoutRes +import android.support.v4.app.Fragment +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup + +import butterknife.ButterKnife +import butterknife.Unbinder + +abstract class BaseFragment : Fragment() { + + private var butterKnifeUnbinder: Unbinder? = null + + @get:LayoutRes abstract val layoutRes: Int + + override fun onCreate(savedState: Bundle?) { + super.onCreate(savedState) + if (savedState != null) { + restoreState(savedState) + } + } + + fun restoreState(savedState: Bundle) {} + + override fun onCreateView(inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle?): View? { + val rootView = inflater.inflate(layoutRes, container, false) + butterKnifeUnbinder = ButterKnife.bind(this, rootView) + return rootView + } + + override fun onDestroyView() { + butterKnifeUnbinder!!.unbind() + super.onDestroyView() + } + +} diff --git a/app/src/main/java/com/flatstack/android/utils/ui/ScreenConfig.kt b/app/src/main/java/com/flatstack/android/utils/ui/ScreenConfig.kt new file mode 100644 index 0000000..5514b4d --- /dev/null +++ b/app/src/main/java/com/flatstack/android/utils/ui/ScreenConfig.kt @@ -0,0 +1,11 @@ +package com.flatstack.android.utils.ui + +import android.support.annotation.LayoutRes +import android.support.annotation.MenuRes +import android.support.annotation.StringRes + +class ScreenConfig(@LayoutRes val layoutRes: Int, + val titleRes: Int = 0, + val title: String? = null, + val enableBackButton: Boolean = false, + @MenuRes val menuRes: Int = 0) \ No newline at end of file diff --git a/app/src/main/java/com/flatstack/android/utils/ui/UiInfo.java b/app/src/main/java/com/flatstack/android/utils/ui/UiInfo.java deleted file mode 100644 index 26e2574..0000000 --- a/app/src/main/java/com/flatstack/android/utils/ui/UiInfo.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.flatstack.android.utils.ui; - -import android.support.annotation.LayoutRes; -import android.support.annotation.MenuRes; -import android.support.annotation.NonNull; -import android.support.annotation.StringRes; - -/** - * Created by almaziskhakov on 20/12/2016. - */ - -public class UiInfo { - - @LayoutRes private final int layoutRes; - @StringRes private int titleRes; - @MenuRes private int menuRes; - private String titleStr; - private boolean hasBackButton; - - public UiInfo(@LayoutRes int layoutRes) { - this.layoutRes = layoutRes; - } - - public int getMenuRes() { - return menuRes; - } - - public UiInfo setMenuRes(int menuRes) { - this.menuRes = menuRes; - return this; - } - - public int getLayoutRes() { - return layoutRes; - } - - public int getTitleRes() { - return titleRes; - } - - public UiInfo setTitleRes(int titleRes) { - this.titleRes = titleRes; - return this; - } - - public UiInfo setTitle(@NonNull String title) { - this.titleStr = title; - return this; - } - - public String getTitle() { - return titleStr; - } - - public boolean isHasBackButton() { - return hasBackButton; - } - - public UiInfo enableBackButton() { - this.hasBackButton = true; - return this; - } -} diff --git a/app/src/main/res/layout/fragment_main.xml b/app/src/main/res/layout/fragment_main.xml index f743885..4ab1dc3 100644 --- a/app/src/main/res/layout/fragment_main.xml +++ b/app/src/main/res/layout/fragment_main.xml @@ -9,7 +9,7 @@ >