Skip to content

Commit 6e7aa9f

Browse files
author
N1ckn1ght
committed
Achievements implementation
#39 implemented, although there will be more achievements upon release small fixes
1 parent d8933dd commit 6e7aa9f

36 files changed

+480
-19
lines changed

.idea/compiler.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/dictionaries/Nick.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,26 @@
66

77
Скриншоты:
88

9-
<img src="https://i.imgur.com/aNFzYAB.jpg">
9+
<img src="https://i.imgur.com/aS7Ak09.jpg">
1010

11-
<img src="https://i.imgur.com/3fNIr4D.jpg">
11+
<img src="https://i.imgur.com/zmYg0kD.jpg">
1212

13-
<img src="https://i.imgur.com/MticDZq.jpg">
13+
<img src="https://i.imgur.com/WZeWxWl.jpg">
1414

15-
<img src="https://i.imgur.com/5pRJy14.jpg">
15+
<img src="https://i.imgur.com/8CHryM8.jpg">
1616

17-
<img src="https://i.imgur.com/vkHZQau.jpg">
17+
<img src="https://i.imgur.com/ZAro2tt.jpg">
1818

19-
<img src="https://i.imgur.com/OZjYpMc.jpg">
19+
<img src="https://i.imgur.com/Iiww8NW.jpg">
2020

21-
<img src="https://i.imgur.com/F40R5m5.jpg">
21+
<img src="https://i.imgur.com/jOsBe1i.jpg">
22+
23+
<img src="https://i.imgur.com/5ZAvdvK.jpg">
24+
25+
<img src="https://i.imgur.com/MQFuTEa.jpg">
26+
27+
<img src="https://i.imgur.com/p4E4FAh.jpg">
28+
29+
Авторские права:
30+
31+
Изображения тайлов взяты с репозитория https://github.com/FluffyStuff/riichi-mahjong-tiles

app/src/main/AndroidManifest.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
android:name=".SettingsActivity"
1313
android:exported="false"
1414
android:label="@string/title_activity_settings"
15-
android:parentActivityName=".MainActivity"
1615
android:launchMode="singleTop"
16+
android:parentActivityName=".MainActivity"
1717
android:screenOrientation="landscape">
1818
<meta-data
1919
android:name="android.support.PARENT_ACTIVITY"
@@ -41,6 +41,10 @@
4141
android:name=".RecordsActivity"
4242
android:launchMode="singleTop"
4343
android:screenOrientation="landscape" />
44+
<activity
45+
android:name=".AchievementsActivity"
46+
android:launchMode="singleTop"
47+
android:screenOrientation="landscape" />
4448
</application>
4549

4650
</manifest>
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.example.riichit
2+
3+
import android.os.Bundle
4+
import android.text.Layout
5+
import android.text.Spannable
6+
import android.text.SpannableString
7+
import android.text.style.AlignmentSpan
8+
import android.util.DisplayMetrics
9+
import android.view.LayoutInflater
10+
import android.widget.Toast
11+
import androidx.appcompat.app.AppCompatActivity
12+
import androidx.preference.PreferenceManager
13+
import androidx.recyclerview.widget.GridLayoutManager
14+
import androidx.recyclerview.widget.RecyclerView
15+
import com.example.riichit.LocaleHelper.setLocale
16+
import com.example.riichit.Ruleset.achievements
17+
import java.lang.Integer.min
18+
19+
20+
class AchievementsActivity : AppCompatActivity() {
21+
private val context = this
22+
private var toast: Toast? = null
23+
private var achievementsList = MutableList(achievements.size) { 0 }
24+
25+
override fun onCreate(savedInstanceState: Bundle?) {
26+
setLocale(context)
27+
super.onCreate(savedInstanceState)
28+
setContentView(R.layout.activity_achievements)
29+
supportActionBar?.hide()
30+
31+
val displayMetrics = DisplayMetrics()
32+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
33+
val display = context.display
34+
@Suppress("DEPRECATION")
35+
display?.getMetrics(displayMetrics)
36+
} else {
37+
@Suppress("DEPRECATION")
38+
val display = context.windowManager.defaultDisplay
39+
@Suppress("DEPRECATION")
40+
display.getMetrics(displayMetrics)
41+
}
42+
val padding = (displayMetrics.widthPixels * 0.016).toInt()
43+
val ivHeight = min(
44+
(displayMetrics.widthPixels * 1.248 / 3).toInt(),
45+
((displayMetrics.heightPixels - 4 * padding) / 2)
46+
)
47+
val ivWidth = ivHeight * 4 / 3
48+
49+
val rvGallery = findViewById<RecyclerView>(R.id.rvGallery)
50+
rvGallery.layoutManager = GridLayoutManager(context, 3, RecyclerView.VERTICAL, false)
51+
rvGallery.addItemDecoration(GridSpacingItemDecoration(3, padding, true, 0, false))
52+
val galleryAdapter = GalleryAdapter(
53+
LayoutInflater.from(context),
54+
this::onClick,
55+
ivWidth,
56+
ivHeight,
57+
padding
58+
)
59+
60+
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
61+
for ((i, achievement) in achievements.withIndex()) {
62+
if (sharedPreferences.getBoolean(achievement, false)) {
63+
achievementsList[i] = i + 1
64+
}
65+
}
66+
67+
galleryAdapter.submitList(achievementsList)
68+
rvGallery.adapter = galleryAdapter
69+
}
70+
71+
override fun onPause() {
72+
toast?.cancel()
73+
super.onPause()
74+
}
75+
76+
private fun onClick(id: Int) {
77+
showLongToast(
78+
context.getString(
79+
context.resources.getIdentifier(
80+
"achievement_$id",
81+
"string",
82+
context.packageName
83+
)
84+
)
85+
)
86+
}
87+
88+
private fun showLongToast(text: String) {
89+
toast?.cancel()
90+
91+
val centeredText: Spannable = SpannableString(text)
92+
centeredText.setSpan(
93+
AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER),
94+
0, text.length - 1,
95+
Spannable.SPAN_INCLUSIVE_INCLUSIVE
96+
)
97+
98+
toast = Toast.makeText(
99+
baseContext,
100+
centeredText,
101+
Toast.LENGTH_LONG
102+
)
103+
toast?.show()
104+
}
105+
}

app/src/main/java/com/example/riichit/Drawables.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,21 @@ object Drawables {
6161
"en" to R.drawable.en,
6262
"ru" to R.drawable.ru
6363
)
64+
65+
val cards = arrayOf(
66+
R.drawable.card_locked,
67+
R.drawable.card_mentanpin,
68+
R.drawable.card_chantaiayo,
69+
R.drawable.card_iipeikou,
70+
R.drawable.card_tsumo_only,
71+
R.drawable.card_chiitoitsu,
72+
R.drawable.card_sanshoku_doujun,
73+
R.drawable.card_ittsu,
74+
R.drawable.card_ippatsu_haitei,
75+
R.drawable.card_sanshoku_doukou,
76+
R.drawable.card_streak_22,
77+
R.drawable.card_dora_13,
78+
R.drawable.card_hand_of_god,
79+
R.drawable.card_kokushi_musou
80+
)
6481
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.example.riichit
2+
3+
import android.view.LayoutInflater
4+
import android.view.View
5+
import android.view.ViewGroup
6+
import androidx.recyclerview.widget.DiffUtil
7+
import androidx.recyclerview.widget.ListAdapter
8+
import com.example.riichit.Drawables.cards
9+
import com.example.riichit.Utility.logicIncrement
10+
import com.example.riichit.Utility.setMargin
11+
import com.example.riichit.Utility.toInt
12+
13+
class GalleryAdapter(
14+
private val inflater: LayoutInflater,
15+
private val onClick: (id: Int) -> Unit,
16+
private val width: Int,
17+
private val height: Int,
18+
private val padding: Int
19+
) : ListAdapter<Int, GalleryViewHolder>(DIFF_CALLBACK) {
20+
21+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GalleryViewHolder {
22+
val row: View = inflater.inflate(R.layout.image, parent, false)
23+
return GalleryViewHolder(row, onClick, 0)
24+
}
25+
26+
override fun onBindViewHolder(holder: GalleryViewHolder, position: Int) {
27+
val x = getItem(position)
28+
holder.iv.layoutParams.width = width
29+
holder.iv.layoutParams.height = height
30+
holder.iv.setMargin(0, 0, 0, 0)
31+
// the structure of strings in strings.xml will be like this:
32+
// [0] - debug message
33+
// [1, size + 1] - locked achievements
34+
// [1 + logicIncrement, size + 1 + logicIncrement] - unlocked achievements
35+
// where size = achievements.size, which also equals cards.size - 1
36+
// holder.bindTo(cards[x], position + 1 + logicIncrement * (x > 0).toInt())
37+
holder.bindTo(cards[x], position + 1)
38+
}
39+
40+
companion object {
41+
private val DIFF_CALLBACK: DiffUtil.ItemCallback<Int> =
42+
object : DiffUtil.ItemCallback<Int>() {
43+
override fun areItemsTheSame(oldImage: Int, newImage: Int): Boolean {
44+
return oldImage == newImage
45+
}
46+
47+
override fun areContentsTheSame(oldImage: Int, newImage: Int): Boolean {
48+
return areItemsTheSame(oldImage, newImage)
49+
}
50+
}
51+
}
52+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.example.riichit
2+
3+
import android.view.View
4+
import android.widget.ImageView
5+
import androidx.recyclerview.widget.RecyclerView
6+
7+
class GalleryViewHolder(
8+
itemView: View,
9+
onClick: (id: Int) -> Unit,
10+
private var id: Int
11+
) : RecyclerView.ViewHolder(itemView) {
12+
val iv: ImageView = itemView.findViewById(R.id.image)
13+
14+
init {
15+
itemView.setOnClickListener {
16+
onClick(id)
17+
}
18+
}
19+
20+
fun bindTo(drawable: Int, id_: Int) {
21+
iv.setImageResource(drawable)
22+
id = id_
23+
}
24+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.example.riichit
2+
3+
import android.graphics.Rect
4+
import android.view.View
5+
import androidx.recyclerview.widget.RecyclerView
6+
7+
// source: https://gist.github.com/liangzhitao/e57df3c3232ee446d464
8+
class GridSpacingItemDecoration(
9+
private val spanCount: Int,
10+
private val spacing: Int,
11+
private val includeEdge: Boolean,
12+
private val headerNum: Int,
13+
private val isReverse: Boolean
14+
) : RecyclerView.ItemDecoration() {
15+
16+
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
17+
val position = parent.getChildAdapterPosition(view) - headerNum
18+
19+
if (position >= 0) {
20+
val column = position % spanCount
21+
22+
if (includeEdge) {
23+
outRect.left = spacing - column * spacing / spanCount
24+
outRect.right = (column + 1) * spacing / spanCount
25+
26+
if (position < spanCount) {
27+
if (isReverse)
28+
outRect.bottom = spacing else
29+
outRect.top = spacing
30+
}
31+
32+
if (isReverse)
33+
outRect.top = spacing else
34+
outRect.bottom = spacing
35+
} else {
36+
outRect.left = column * spacing / spanCount
37+
outRect.right = spacing - (column + 1) * spacing / spanCount
38+
39+
if (position >= spanCount) {
40+
if (isReverse)
41+
outRect.bottom = spacing else
42+
outRect.top = spacing
43+
}
44+
}
45+
} else {
46+
outRect.left = 0
47+
outRect.right = 0
48+
outRect.top = 0
49+
outRect.bottom = 0
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)