Skip to content

Commit a2a3260

Browse files
committed
Added multi type map support
1 parent 9378c7d commit a2a3260

4 files changed

Lines changed: 400 additions & 329 deletions

File tree

core/src/main/java/com/omega_r/libs/omegaintentbuilder/OmegaIntentBuilder.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ open class OmegaIntentBuilder(private val context: Context) {
143143
/**
144144
* @return MapIntentBuilder for creating intent to open Map application
145145
*/
146-
fun map(type: MapTypes): MapIntentBuilder {
147-
return MapIntentBuilder(context, type)
146+
fun map(vararg types: MapTypes): MapIntentBuilder {
147+
return MapIntentBuilder(context, *types)
148148
}
149149

150150
/**

core/src/main/java/com/omega_r/libs/omegaintentbuilder/builders/MapIntentBuilder.kt

Lines changed: 178 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -15,126 +15,200 @@ import android.content.Intent
1515
import android.net.Uri
1616
import com.omega_r.libs.omegaintentbuilder.OmegaIntentBuilder
1717
import com.omega_r.libs.omegaintentbuilder.handlers.ContextIntentHandler
18+
import com.omega_r.libs.omegaintentbuilder.handlers.FailCallback
1819
import com.omega_r.libs.omegaintentbuilder.types.MapTypes
1920
import com.omega_r.libs.omegaintentbuilder.types.MapTypes.*
2021

2122
/**
2223
* MapIntentBuilder is a helper for open Maps applications
2324
*/
24-
class MapIntentBuilder(val context: Context, val type: MapTypes) : BaseActivityBuilder(context) {
25-
26-
private var latitude: Double? = null
27-
private var longitude: Double? = null
28-
private var address: String? = null
29-
30-
/**
31-
* Set a latitude.
32-
*
33-
* @param latitude Double
34-
* @return This MapIntentBuilder for method chaining
35-
*/
36-
fun latitude(latitude: Double): MapIntentBuilder {
37-
this.latitude = latitude
38-
return this
39-
}
40-
41-
/**
42-
* Set a longitude.
43-
*
44-
* @param longitude Double
45-
* @return This MapIntentBuilder for method chaining
46-
*/
47-
fun longitude(longitude: Double): MapIntentBuilder {
48-
this.longitude = longitude
49-
return this
50-
}
51-
52-
/**
53-
* Set latitude and longitude.
54-
*
55-
* @param latitude Double
56-
* @param longitude Double
57-
* @return This MapIntentBuilder for method chaining
58-
*/
59-
fun latitudeLongitude(latitude: Double, longitude: Double): MapIntentBuilder {
60-
this.latitude = latitude
61-
this.longitude = longitude
62-
return this
63-
}
64-
65-
/**
66-
* Set a searching address.
67-
*
68-
* @param address String
69-
* @return This MapIntentBuilder for method chaining
70-
*/
71-
fun address(address: String): MapIntentBuilder {
72-
this.address = address
73-
return this
74-
}
75-
76-
override fun createIntent(): Intent {
77-
val uri: Uri = getFormattedUri()
78-
val intent = Intent(Intent.ACTION_VIEW, uri)
79-
80-
when (type) {
81-
GOOGLE_MAP -> intent.setPackage(GOOGLE_MAP.packageName)
82-
YANDEX_MAP -> intent.setPackage(YANDEX_MAP.packageName)
83-
KAKAO_MAP -> intent.setPackage(KAKAO_MAP.packageName)
84-
NAVER_MAP -> intent.setPackage(NAVER_MAP.packageName)
25+
class MapIntentBuilder(val context: Context, private vararg var types: MapTypes) : BaseActivityBuilder(context) {
26+
27+
private var latitude: Double? = null
28+
private var longitude: Double? = null
29+
private var address: String? = null
30+
private var failtype: MapTypes? = null
31+
32+
init {
33+
if (types.isEmpty()) types = MapTypes.values()
34+
}
35+
36+
/**
37+
* Set a latitude.
38+
*
39+
* @param latitude Double
40+
* @return This MapIntentBuilder for method chaining
41+
*/
42+
fun latitude(latitude: Double): MapIntentBuilder {
43+
this.latitude = latitude
44+
return this
45+
}
46+
47+
/**
48+
* Set a longitude.
49+
*
50+
* @param longitude Double
51+
* @return This MapIntentBuilder for method chaining
52+
*/
53+
fun longitude(longitude: Double): MapIntentBuilder {
54+
this.longitude = longitude
55+
return this
56+
}
57+
58+
/**
59+
* Set latitude and longitude.
60+
*
61+
* @param latitude Double
62+
* @param longitude Double
63+
* @return This MapIntentBuilder for method chaining
64+
*/
65+
fun latitudeLongitude(latitude: Double, longitude: Double): MapIntentBuilder {
66+
this.latitude = latitude
67+
this.longitude = longitude
68+
return this
69+
}
70+
71+
/**
72+
* Set a searching address.
73+
*
74+
* @param address String
75+
* @return This MapIntentBuilder for method chaining
76+
*/
77+
fun address(address: String): MapIntentBuilder {
78+
this.address = address
79+
return this
80+
}
81+
82+
fun openPlayStoreIfFail(type: MapTypes): MapIntentBuilder {
83+
failtype = type
84+
return this
85+
}
86+
87+
override fun createIntent(): Intent {
88+
val uri: Uri = getFormattedUri(0)
89+
val intent = Intent(Intent.ACTION_VIEW, uri)
90+
91+
when (types[0]) {
92+
GOOGLE_MAP -> intent.setPackage(GOOGLE_MAP.packageName)
93+
YANDEX_MAP -> intent.setPackage(YANDEX_MAP.packageName)
94+
KAKAO_MAP -> intent.setPackage(KAKAO_MAP.packageName)
95+
NAVER_MAP -> intent.setPackage(NAVER_MAP.packageName)
96+
}
97+
98+
return intent
99+
}
100+
101+
private fun getFormattedUri(index: Int): Uri {
102+
val sb = StringBuilder()
103+
when (types[index]) {
104+
GOOGLE_MAP -> {
105+
sb.append("geo:")
106+
if (latitude != null && longitude != null) {
107+
sb.append(latitude, ",", longitude)
108+
}
109+
if (address != null) {
110+
sb.append("?q=", Uri.encode(address))
111+
}
112+
}
113+
YANDEX_MAP -> {
114+
sb.append("yandexmaps://", YANDEX_MAP.packageName, "/?pt=")
115+
if (latitude != null && longitude != null) {
116+
sb.append(longitude, ",", latitude)
117+
}
118+
if (address != null) {
119+
sb.append("&text=", address)
120+
}
121+
}
122+
KAKAO_MAP -> {
123+
sb.append("daummaps://look?p=")
124+
if (latitude != null && longitude != null) {
125+
sb.append(latitude, ",", longitude)
126+
}
127+
}
128+
NAVER_MAP -> {
129+
sb.append("geo:")
130+
if (latitude != null && longitude != null) {
131+
sb.append(latitude, ",", longitude)
132+
}
133+
if (address != null) {
134+
sb.append("?q=", Uri.encode(address))
135+
}
136+
}
137+
}
138+
return Uri.parse(sb.toString())
139+
}
140+
141+
override fun createIntentHandler(): ContextIntentHandler {
142+
val failIntentHandlers = createFailIntentHandler()
143+
if (failIntentHandlers.isEmpty()) {
144+
return super.createIntentHandler()
145+
} else {
146+
return WrapperIntentHandler(context, createIntent(), failIntentHandlers.last(), failIntentHandlers.first())
147+
}
148+
85149
}
86150

87-
return intent
88-
}
151+
private fun createFailIntentHandler(): List<ContextIntentHandler> {
152+
val result = ArrayList<ContextIntentHandler>(types.size)
89153

90-
private fun getFormattedUri(): Uri {
91-
val sb = StringBuilder()
92-
when (type) {
93-
GOOGLE_MAP -> {
94-
sb.append("geo:")
95-
if (latitude != null && longitude != null) {
96-
sb.append(latitude, ",", longitude)
154+
var prevIntentHandler : ContextIntentHandler? = null
155+
156+
for (index in 1 until types.size) {
157+
val intentHandler = OmegaIntentBuilder.from(context)
158+
.map(types[index])
159+
.createIntentHandler()
160+
result += intentHandler
161+
prevIntentHandler?.failIntentHandler(intentHandler)
162+
prevIntentHandler = intentHandler
97163
}
98-
if (address != null) {
99-
sb.append("?q=", Uri.encode(address))
164+
165+
if (failtype != null) {
166+
val lastHandler = OmegaIntentBuilder.from(context)
167+
.playStore()
168+
.packageName(failtype!!.packageName)
169+
.createIntentHandler()
170+
result += lastHandler
171+
prevIntentHandler?.failIntentHandler(lastHandler)
172+
100173
}
101-
}
102-
YANDEX_MAP -> {
103-
sb.append("yandexmaps://", YANDEX_MAP.packageName, "/?pt=")
104-
if (latitude != null && longitude != null) {
105-
sb.append(longitude, ",", latitude)
174+
175+
return result
176+
}
177+
178+
private class WrapperIntentHandler(context: Context, createdIntent: Intent,
179+
private val handler: ContextIntentHandler,
180+
failIntentHandler: ContextIntentHandler?
181+
): ContextIntentHandler(context, createdIntent) {
182+
183+
init {
184+
super.failIntentHandler(failIntentHandler)
106185
}
107-
if (address != null) {
108-
sb.append("&text=", address)
186+
187+
override fun failToast(message: String): ContextIntentHandler {
188+
handler.failToast(message)
189+
return this
109190
}
110-
}
111-
KAKAO_MAP -> {
112-
sb.append("daummaps://look?p=")
113-
if (latitude != null && longitude != null) {
114-
sb.append(latitude, ",", longitude)
191+
192+
override fun failToast(message: Int): ContextIntentHandler {
193+
handler.failToast(message)
194+
return this
115195
}
116-
}
117-
NAVER_MAP -> {
118-
sb.append("geo:")
119-
if (latitude != null && longitude != null) {
120-
sb.append(latitude, ",", longitude)
196+
197+
override fun failIntent(failIntent: Intent): ContextIntentHandler {
198+
handler.failIntent(failIntent)
199+
return this
121200
}
122-
if (address != null) {
123-
sb.append("?q=", Uri.encode(address))
201+
202+
override fun failCallback(failCallback: FailCallback): ContextIntentHandler {
203+
handler.failCallback(failCallback)
204+
return this
205+
}
206+
207+
override fun failIntentHandler(failIntentHandler: ContextIntentHandler?): ContextIntentHandler {
208+
handler.failIntentHandler(failIntentHandler)
209+
return this
124210
}
125-
}
211+
126212
}
127-
return Uri.parse(sb.toString())
128-
}
129-
130-
override fun createIntentHandler(): ContextIntentHandler {
131-
return super.createIntentHandler()
132-
.failIntentHandler(
133-
OmegaIntentBuilder.from(context)
134-
.playStore()
135-
.packageName(type.packageName)
136-
.createIntentHandler()
137-
)
138-
}
139213

140214
}

0 commit comments

Comments
 (0)