Skip to content
This repository was archived by the owner on Nov 5, 2023. It is now read-only.

Commit 98a662d

Browse files
author
Mike
committed
[More] Added more classes for utility
1 parent 0e9dcfc commit 98a662d

File tree

11 files changed

+586
-6
lines changed

11 files changed

+586
-6
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ repositories {
2727
}
2828

2929
dependencies {
30-
implementation "com.android.support:appcompat-v7:27.1.1"
31-
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
30+
implementation "com.android.support:appcompat-v7:${appCompat}"
31+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
3232
implementation project(':zenith')
3333
}

build.gradle

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

33
buildscript {
4-
ext.kotlin_version = '1.2.41'
4+
ext {
5+
appCompat = '27.1.1'
6+
kotlinVersion = '1.2.41'
7+
}
58
repositories {
69
jcenter()
710
google()
811
}
912
dependencies {
1013
classpath "com.android.tools.build:gradle:3.1.2"
1114
classpath "com.github.dcendents:android-maven-gradle-plugin:2.0"
12-
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
15+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
1316
// NOTE: Do not place your application dependencies here; they belong
1417
// in the individual module build.gradle files
1518
}

zenith/build.gradle

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ repositories {
2828
}
2929

3030
dependencies {
31-
implementation "com.android.support:appcompat-v7:27.1.1"
32-
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
31+
implementation "com.android.support:appcompat-v7:${appCompat}"
32+
implementation "com.android.support:design:${appCompat}"
33+
implementation "com.android.support:support-v4:${appCompat}"
34+
implementation "com.google.code.gson:gson:2.8.4"
35+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
3336
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package com.mikelau.zenith.adapters
2+
3+
import android.support.v7.widget.RecyclerView
4+
import android.view.ViewGroup
5+
6+
import java.util.ArrayList
7+
8+
/**
9+
* Generic Adapter class for views with custom adapters / models
10+
*
11+
* So instead of extend RecyclerView.Adapter, you can now adjust to
12+
* extending GenericListAdapter<Model> and your model will be easily accessible
13+
**/
14+
abstract class GenericListAdapter<T> : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
15+
16+
var mAddCount = 0
17+
18+
var items = ArrayList<T>()
19+
20+
fun addItems(items: ArrayList<T>) {
21+
this.items.addAll(items)
22+
notifyDataSetChanged()
23+
}
24+
25+
fun addItems(items: Set<T>) {
26+
this.items.addAll(items)
27+
notifyDataSetChanged()
28+
}
29+
30+
fun addItem(item: T) {
31+
this.items.add(item)
32+
notifyDataSetChanged()
33+
}
34+
35+
fun addItem(i: Int, item: T?) {
36+
this.items.add(i, item!!)
37+
notifyDataSetChanged()
38+
}
39+
40+
fun addItemsOnTop(items: ArrayList<T>) {
41+
this.items.addAll(0, items)
42+
notifyDataSetChanged()
43+
}
44+
45+
fun addItemOnTop(item: T) {
46+
this.items.add(0, item)
47+
notifyDataSetChanged()
48+
}
49+
50+
fun modifyItem(t: T, position: Int) {
51+
this.items[position] = t
52+
notifyDataSetChanged()
53+
}
54+
55+
fun refresh(items: ArrayList<T>) {
56+
this.items = items
57+
notifyDataSetChanged()
58+
}
59+
60+
fun refresh() {
61+
notifyDataSetChanged()
62+
}
63+
64+
fun removeItem(index: Int) {
65+
this.items.removeAt(index)
66+
notifyDataSetChanged()
67+
}
68+
69+
fun clear() {
70+
this.items.clear()
71+
notifyDataSetChanged()
72+
}
73+
74+
fun addSection(position: Int) {
75+
addItem(position, null)
76+
}
77+
78+
protected fun fillItems(items: ArrayList<T>) {
79+
this.items = items
80+
}
81+
82+
protected fun fillItems(items: List<T>) {
83+
this.items = items as ArrayList<T>
84+
}
85+
86+
protected abstract fun setOnCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder
87+
88+
protected fun setGetItemViewType(item: T?, position: Int): Int {
89+
return -1
90+
}
91+
92+
protected abstract fun setOnBindViewHolder(holder: RecyclerView.ViewHolder, position: Int, item: T?)
93+
94+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
95+
return setOnCreateViewHolder(parent, viewType)
96+
97+
}
98+
99+
override fun getItemViewType(position: Int): Int {
100+
return try {
101+
val t = items[position]
102+
setGetItemViewType(t, position)
103+
} catch (e: Exception) {
104+
setGetItemViewType(null, position)
105+
}
106+
107+
}
108+
109+
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
110+
try {
111+
val t = items[position]
112+
setOnBindViewHolder(holder, position, t)
113+
} catch (e: IndexOutOfBoundsException) {
114+
setOnBindViewHolder(holder, position, null)
115+
}
116+
117+
}
118+
119+
override fun getItemCount(): Int {
120+
return try {
121+
items.size + mAddCount
122+
} catch (e: Exception) {
123+
0
124+
}
125+
126+
}
127+
128+
protected fun addTotalCount(addCount: Int) {
129+
this.mAddCount = addCount
130+
}
131+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.mikelau.zenith.dialogs
2+
3+
import android.app.DatePickerDialog
4+
import android.content.Context
5+
import android.os.Build
6+
import android.util.AttributeSet
7+
import android.widget.DatePicker
8+
import com.mikelau.zenith.utils.DebugLog
9+
import java.lang.reflect.Field
10+
11+
/**
12+
* Enforce Holo Date Picker Dialog
13+
*/
14+
class FixedHoloDatePickerDialog(context: Context, callBack: DatePickerDialog.OnDateSetListener, year: Int, monthOfYear: Int, dayOfMonth: Int) : DatePickerDialog(context, callBack, year, monthOfYear, dayOfMonth) {
15+
16+
init {
17+
// Force spinners on Android 7.0 only (SDK 24).
18+
// Note: I'm using a naked SDK value of 24 here, because I'm
19+
// targeting SDK 23, and Build.VERSION_CODES.N is not available yet.
20+
// But if you target SDK >= 24, you should have it.
21+
if (Build.VERSION.SDK_INT >= 24) {
22+
try {
23+
val field = this.findField(DatePickerDialog::class.java, DatePicker::class.java, "mDatePicker")
24+
val datePicker = field!!.get(this) as DatePicker
25+
val delegateClass = Class.forName("android.widget.DatePicker\$DatePickerDelegate")
26+
val delegateField = this.findField(DatePicker::class.java, delegateClass, "mDelegate")
27+
28+
val delegate = delegateField!!.get(datePicker)
29+
val spinnerDelegateClass = Class.forName("android.widget.DatePickerSpinnerDelegate")
30+
31+
if (delegate.javaClass != spinnerDelegateClass) {
32+
delegateField.set(datePicker, null)
33+
datePicker.removeAllViews()
34+
35+
val spinnerDelegateConstructor = spinnerDelegateClass.getDeclaredConstructor(
36+
DatePicker::class.java, Context::class.java, AttributeSet::class.java, Int::class.javaPrimitiveType, Int::class.javaPrimitiveType)
37+
spinnerDelegateConstructor.isAccessible = true
38+
39+
val spinnerDelegate = spinnerDelegateConstructor.newInstance(datePicker,
40+
context, null, android.R.attr.datePickerStyle, 0)
41+
delegateField.set(datePicker, spinnerDelegate)
42+
43+
datePicker.init(year, monthOfYear, dayOfMonth, this)
44+
datePicker.calendarViewShown = false
45+
datePicker.spinnersShown = true
46+
}
47+
} catch (e: Exception) {
48+
DebugLog.printStackTrace(e)
49+
}
50+
}
51+
}
52+
53+
/**
54+
* Find Field with expectedName in objectClass. If not found, find first occurrence of
55+
* target fieldClass in objectClass.
56+
*/
57+
private fun findField(objectClass: Class<*>, fieldClass: Class<*>, expectedName: String): Field? {
58+
try {
59+
val field = objectClass.getDeclaredField(expectedName)
60+
field.isAccessible = true
61+
return field
62+
} catch (e: NoSuchFieldException) {
63+
DebugLog.printStackTrace(e)
64+
}
65+
66+
// Search for it if it wasn't found under the expectedName.
67+
for (field in objectClass.declaredFields) {
68+
if (field.type == fieldClass) {
69+
field.isAccessible = true
70+
return field
71+
}
72+
}
73+
74+
return null
75+
}
76+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.mikelau.zenith.models.warp;
2+
3+
import android.os.Parcel;
4+
import android.os.Parcelable;
5+
6+
import com.google.gson.annotations.SerializedName;
7+
8+
public class Pointer<T> implements Parcelable {
9+
10+
private int id;
11+
private String type;
12+
private String className;
13+
14+
@SerializedName("attributes")
15+
private T attribute;
16+
17+
public Pointer(String className, int id) {
18+
this.id = id;
19+
this.type = "Pointer";
20+
this.className = className;
21+
}
22+
23+
public Pointer() {
24+
}
25+
26+
protected Pointer(Parcel in) {
27+
id = in.readInt();
28+
type = in.readString();
29+
className = in.readString();
30+
}
31+
32+
public static final Creator<Pointer> CREATOR = new Creator<Pointer>() {
33+
@Override
34+
public Pointer createFromParcel(Parcel in) {
35+
return new Pointer(in);
36+
}
37+
38+
@Override
39+
public Pointer[] newArray(int size) {
40+
return new Pointer[size];
41+
}
42+
};
43+
44+
public int getId() {
45+
return id;
46+
}
47+
48+
public void setId(int id) {
49+
this.id = id;
50+
}
51+
52+
public String getType() {
53+
return type;
54+
}
55+
56+
public void setType() {
57+
this.type = "Pointer";
58+
}
59+
60+
public String getClassName() {
61+
return className;
62+
}
63+
64+
public void setClassName(String className) {
65+
this.className = className;
66+
}
67+
68+
public T getAttribute() {
69+
return attribute;
70+
}
71+
72+
public void setAttribute(T attribute) {
73+
this.attribute = attribute;
74+
}
75+
76+
@Override
77+
public int describeContents() {
78+
return 0;
79+
}
80+
81+
@Override
82+
public void writeToParcel(Parcel parcel, int i) {
83+
parcel.writeInt(id);
84+
parcel.writeString(type);
85+
parcel.writeString(className);
86+
}
87+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.mikelau.zenith.models.warp
2+
3+
4+
import com.google.gson.annotations.SerializedName
5+
6+
class Status<T> {
7+
8+
var status: Int = 0
9+
var message: String? = null
10+
@SerializedName("result")
11+
var result: T? = null
12+
13+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.mikelau.zenith.utils
2+
3+
import android.support.v7.widget.LinearLayoutManager
4+
import android.support.v7.widget.RecyclerView
5+
6+
/**
7+
* Endless Recycler On Scroll Listener for Scroll Pagination
8+
*/
9+
abstract class EndlessRecyclerOnScrollListener(private val mLinearLayoutManager: LinearLayoutManager) : RecyclerView.OnScrollListener() {
10+
11+
internal var firstVisibleItem: Int = 0
12+
internal var visibleItemCount: Int = 0
13+
internal var totalItemCount: Int = 0
14+
15+
private var previousTotal = 0
16+
private var loading = true
17+
private val visibleThreshold = 5
18+
private var current_page = 1
19+
20+
override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
21+
super.onScrolled(recyclerView, dx, dy)
22+
23+
visibleItemCount = recyclerView!!.childCount
24+
totalItemCount = mLinearLayoutManager.itemCount
25+
firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition()
26+
27+
if (loading) {
28+
if (totalItemCount > previousTotal) {
29+
loading = false
30+
previousTotal = totalItemCount
31+
}
32+
}
33+
if (!loading && totalItemCount - visibleItemCount <= firstVisibleItem + visibleThreshold) {
34+
current_page++
35+
onLoadMore(current_page)
36+
loading = true
37+
}
38+
}
39+
40+
abstract fun onLoadMore(current_page: Int)
41+
}

0 commit comments

Comments
 (0)