O'zbekiston respublikasi axborot texnologiyalari va kommunikatsiyalarini rivojlantirish vazirligi



Yüklə 1,03 Mb.
Pdf görüntüsü
səhifə19/20
tarix26.09.2023
ölçüsü1,03 Mb.
#148792
1   ...   12   13   14   15   16   17   18   19   20
Mobil ilovalar kurs ishi

MainActivity.java fayli: 
public class MainActivity extends AppCompatActivity implements 
BaseSliderView.OnSliderClickListener, ViewPagerEx.OnPageChangeListener{ 
List list; 
RecyclerView recyclerview; 
List menuList = new ArrayList<>(); 
RecyclerAdapterHome adapter; 
CardView infoBtnCardView, searchBtnCardView; 
SliderLayout slider; 
SharedPref sharedpref; 


NestedScrollView nestedScrollView; 
ProgressBar progressbar; 
LinearLayout linearlayoutHome, linearlayoutProgressbar; 
Button refreshBtn; 
TextView textviewContactPhone, textviewAddress; 
DrawerLayout drawerLayout; 
RecyclerView exRecyclerview; 
ExpandableAdapter exAdapter; 
TextView textviewQrcode, textviewSettings, textviewShare, textviewAbout; 
JsonPlaceHolderApi jsonPlaceHolderApi; 
private static final int REQUEST_CODE_QR_SCAN = 101; 
RecyclerView newsRecyclerview; 
RecyclerAdapterHomeNews adapterHomeNews; 
ImageView logotipImageView; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
final SharedPref sharedPref; 
super.onCreate(savedInstanceState); 
sharedPref = new SharedPref(this); 
LanguageHelper.setAppLocale(sharedPref.getLang(), this); 
setContentView(R.layout.activity_main); 
exRecyclerview = 
(RecyclerView)findViewById(R.id.expandablerecyclerview); 
logotipImageView = (ImageView)findViewById(R.id.imageview_logo); 
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout); 
FirebaseMessaging.getInstance().subscribeToTopic("all"); 


NavigationView navigationView = 
(NavigationView)findViewById(R.id.nav_view); 
if(navigationView != null){ 
setupDrawerContent(navigationView); 

//DrawerLayout swipe length 
try{ 
Field mDragger = drawerLayout.getClass().getDeclaredField( 
"mLeftDragger");//mRightDragger or mLeftDragger based on 
Drawer Gravity 
mDragger.setAccessible(true); 
ViewDragHelper draggerObj = (ViewDragHelper) mDragger 
.get(drawerLayout); 
Field mEdgeSize = draggerObj.getClass().getDeclaredField( 
"mEdgeSize"); 
mEdgeSize.setAccessible(true); 
int edge = mEdgeSize.getInt(draggerObj); 
mEdgeSize.setInt(draggerObj, edge * 6); 
}catch (Exception e){ 
e.printStackTrace(); 

//nav menu custom textview items 
textviewQrcode = (TextView)findViewById(R.id.textview_qrcode); 
textviewSettings = (TextView)findViewById(R.id.textview_settings); 
textviewShare = (TextView)findViewById(R.id.textview_share); 
textviewAbout = (TextView)findViewById(R.id.textview_about); 
textviewQrcode.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View v) { 
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) 



startActivity(new Intent(MainActivity.this, 
ScannerActivity.class)); 
drawerLayout.closeDrawer(GravityCompat.START); 
}else{ 
Intent intent = new Intent(MainActivity.this, 
QrCodeActivity.class); 
startActivityForResult(intent, REQUEST_CODE_QR_SCAN); 
drawerLayout.closeDrawer(GravityCompat.START); 


}); 
textviewSettings.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View v) { 
startActivity(new Intent(MainActivity.this, 
SettingsActivity.class)); 
drawerLayout.closeDrawer(GravityCompat.START); 

}); 
textviewShare.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View v) { 
Intent shareIntent = new Intent(); 
shareIntent.setAction(Intent.ACTION_SEND); 
shareIntent.setType("text/plain"); 
shareIntent.putExtra(Intent.EXTRA_TEXT, 
"https://play.google.com/store/apps/details?id=" + getPackageName() + 
" BIZLER"); 
startActivity(shareIntent.createChooser(shareIntent, 
"Share")); 
drawerLayout.closeDrawer(GravityCompat.START); 



}); 
textviewAbout.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View v) { 
startActivity(new Intent(MainActivity.this, 
AboutActivity.class)); 
drawerLayout.closeDrawer(GravityCompat.START); 

}); 
newsRecyclerview = 
(RecyclerView)findViewById(R.id.recyclerview_home_news); 
list = new ArrayList<>(); 
sharedpref = new SharedPref(this); 
progressbar = (ProgressBar)findViewById(R.id.progress_home); 
progressbar.setVisibility(View.VISIBLE); 
linearlayoutProgressbar = 
(LinearLayout)findViewById(R.id.linearlayout_home_progressbar); 
nestedScrollView = 
(NestedScrollView)findViewById(R.id.nested_scroll); 
linearlayoutHome = 
(LinearLayout)findViewById(R.id.linearlayout_home); 
refreshBtn = (Button)findViewById(R.id.refresh_button_home); 
//Here used FONTAWESOME 
textviewContactPhone = 
(TextView)findViewById(R.id.textview_contactphone); 
textviewAddress = (TextView)findViewById(R.id.textview_address); 
slider = (SliderLayout)findViewById(R.id.slider); 
infoBtnCardView = (CardView)findViewById(R.id.cardview_info); 
searchBtnCardView = (CardView)findViewById(R.id.cardview_search); 
infoBtnCardView.setOnClickListener(new View.OnClickListener(){ 
@Override 
public void onClick(View v) { 
startActivity(new Intent(MainActivity.this, 
SearchActivity.class)); 



}); 
searchBtnCardView.setOnClickListener(new View.OnClickListener(){ 
@Override 
public void onClick(View v) { 
//startActivity(new Intent(MainActivity.this, 
SearchActivity.class)); 
drawerLayout.openDrawer(GravityCompat.START); 

}); 
recyclerview = (RecyclerView)findViewById(R.id.recyclerview_home); 
recyclerview.setHasFixedSize(true); 
if(isNetworkConnected()){ 
linearlayoutHome.setVisibility(View.GONE); 
loadData(); 
}else{ 
progressbar.setVisibility(View.INVISIBLE); 
linearlayoutProgressbar.setVisibility(View.INVISIBLE); 
linearlayoutHome.setVisibility(View.VISIBLE); 
nestedScrollView.setVisibility(View.INVISIBLE); 

refreshBtn.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View v) { 
if(isNetworkConnected()){ 
loadData(); 
linearlayoutHome.setVisibility(View.GONE); 
}else{ 
progressbar.setVisibility(View.INVISIBLE); 
linearlayoutProgressbar.setVisibility(View.INVISIBLE); 


linearlayoutHome.setVisibility(View.VISIBLE); 
nestedScrollView.setVisibility(View.INVISIBLE); 


}); 

private void setupDrawerContent(NavigationView navigationView) { 
navigationView.setNavigationItemSelectedListener(new 
NavigationView.OnNavigationItemSelectedListener() { 
@Override 
public boolean onNavigationItemSelected(@NonNull MenuItem 
menuItem) { 
menuItem.setChecked(true); 
drawerLayout.closeDrawers(); 
return true; 

}); 

private void loadData(){ 
Retrofit retrofit = new Retrofit.Builder() 
.baseUrl("http://bizler.uz/") 
.addConverterFactory(GsonConverterFactory.create()) 
.build(); 
jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class); 
String lang = "kr"; 
if(sharedpref.getLang().equals("kr")) 
lang = "kr"; 
else if(sharedpref.getLang().equals("uz")) 
lang = "uz"; 
else if(sharedpref.getLang().equals("ru")) 
lang = "ru"; 


Call> call = jsonPlaceHolderApi.getBanners(lang); 
Call> callCategories = 
jsonPlaceHolderApi.getCategories(lang); 
Call callAbout = jsonPlaceHolderApi.getAbout(); 
call.enqueue(new Callback>() { 
@Override 
public void onResponse(Call> call, 
Response> response) { 
progressbar.setVisibility(View.VISIBLE); 
linearlayoutProgressbar.setVisibility(View.VISIBLE); 
nestedScrollView.setVisibility(View.INVISIBLE); 
if(!response.isSuccessful()){ 
progressbar.setVisibility(View.INVISIBLE); 
linearlayoutProgressbar.setVisibility(View.INVISIBLE); 
Toast.makeText(MainActivity.this, ""+response.code(), 
Toast.LENGTH_SHORT).show(); 
return; 

list = response.body(); 
final RequestOptions requestOptions = new RequestOptions(); 
requestOptions.centerCrop(); 
for (int i = 0; i < list.size(); i++) { 
TextSliderView sliderView = new 
TextSliderView(MainActivity.this); 
// if you want show image only / without description text 
use DefaultSliderView instea 
// initialize SliderLayout 
try{ 
sliderView 
.image("http://bizler.uz"+list.get(i).getImages()[0]) 
//.description(list.get(i).getTitle()) 
.setRequestOption(requestOptions) 
.setProgressBarVisible(true) 


.setOnSliderClickListener(MainActivity.this); 
}catch (Exception e){ 
e.printStackTrace(); 

//add your extra information 
sliderView.bundle(new Bundle()); 
sliderView.getBundle().putInt("extra", 
list.get(i).getId()); 
slider.addSlider(sliderView); 

slider.setPresetTransformer(SliderLayout.Transformer.ZoomOutSlide); 
slider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom); 
slider.setCustomAnimation(new DescriptionAnimation()); 
slider.setDuration(3000); 
slider.addOnPageChangeListener(MainActivity.this); 
// Toast.makeText(MainActivity.this, ""+list.size(), 
Toast.LENGTH_SHORT).show(); 
// Toast.makeText(MainActivity.this, 
""+response.body().get(0).getImages()[0], Toast.LENGTH_SHORT).show(); 
// // dialog.dismiss(); 
progressbar.setVisibility(View.INVISIBLE); 
linearlayoutProgressbar.setVisibility(View.INVISIBLE); 
nestedScrollView.setVisibility(View.VISIBLE); 

@Override 
public void onFailure(Call> call, Throwable t) { 
progressbar.setVisibility(View.INVISIBLE); 
linearlayoutProgressbar.setVisibility(View.INVISIBLE); 

}); 
final GridLayoutManager gridLayoutManager = new 
GridLayoutManager(this, 3); 


callCategories.enqueue(new Callback>() { 
@Override 
public void onResponse(Call> call, 
Response> response) { 
progressbar.setVisibility(View.VISIBLE); 
linearlayoutProgressbar.setVisibility(View.VISIBLE); 
nestedScrollView.setVisibility(View.INVISIBLE); 
if(!response.isSuccessful()){ 
progressbar.setVisibility(View.INVISIBLE); 
linearlayoutProgressbar.setVisibility(View.INVISIBLE); 
Toast.makeText(MainActivity.this, ""+response.code(), 
Toast.LENGTH_SHORT).show(); 
return; 

// Log.d("FOR EXAMPLE", response.body().get(2).getIcon()); 
menuList = response.body(); 
final int totalSize = menuList.size(); 
gridLayoutManager.setSpanSizeLookup(new 
GridLayoutManager.SpanSizeLookup() { 
@Override 
public int getSpanSize(int i) { 
int span; 
span = totalSize % 3; 
if(totalSize < 3){ 
return 2; 
}else if(span == 0 || (i <= ((totalSize - 1) - 
span))){ 
return 1; 
}else if(span == 1){ 
return 2; 
}else{ 


return 1; 


}); 
adapter = new RecyclerAdapterHome(MainActivity.this, 
menuList); 
recyclerview.setLayoutManager(gridLayoutManager); 
recyclerview.setAdapter(adapter); 
progressbar.setVisibility(View.INVISIBLE); 
linearlayoutProgressbar.setVisibility(View.INVISIBLE); 
nestedScrollView.setVisibility(View.VISIBLE); 

@Override 
public void onFailure(Call> call, Throwable 
t) { 
progressbar.setVisibility(View.INVISIBLE); 
linearlayoutProgressbar.setVisibility(View.INVISIBLE); 
Toast.makeText(MainActivity.this, "Error connecting to 
server", Toast.LENGTH_SHORT).show(); 

}); 
callAbout.enqueue(new Callback() { 
@Override 
public void onResponse(Call call, 
Response response) { 
progressbar.setVisibility(View.VISIBLE); 
linearlayoutProgressbar.setVisibility(View.VISIBLE); 
nestedScrollView.setVisibility(View.INVISIBLE); 
if(!response.isSuccessful()){ 
Toast.makeText(MainActivity.this, ""+response.message() + 
" " + response.code(), Toast.LENGTH_SHORT).show(); 



AboutModel model = response.body(); 
textviewAddress.setText(model.getAddress()); 
textviewContactPhone.setText(model.getContactPhone()); 
try{ 
Glide.with(MainActivity.this) 
.load("http://bizler.uz/" + model.getLogo()) 
.apply(new 
RequestOptions().placeholder(R.drawable.birinchi).error(R.drawable.birinchi)) 
.into(logotipImageView); 
}catch (Exception e){ 
e.printStackTrace(); 

progressbar.setVisibility(View.INVISIBLE); 
linearlayoutProgressbar.setVisibility(View.INVISIBLE); 
nestedScrollView.setVisibility(View.VISIBLE); 

@Override 
public void onFailure(Call call, Throwable t) { 

}); 
final GridLayoutManager glm = new GridLayoutManager(this, 2); 
newsRecyclerview.setHasFixedSize(true); 
Call> callNewsCat = 
jsonPlaceHolderApi.getNewsCategory(lang); 
callNewsCat.enqueue(new Callback>() { 
@Override 


public void onResponse(Call> call, 
Response> response) { 
List templist = new ArrayList<>(); 
//Log.d("EXAMP ", response.body().toString()); 
templist.add(new News(getString(R.string.news), 
response.body(), R.drawable.ic_newspaper)); 
exAdapter = new ExpandableAdapter(templist, 
MainActivity.this, drawerLayout); 
exRecyclerview.setLayoutManager(new 
LinearLayoutManager(MainActivity.this)); 
exRecyclerview.setAdapter(exAdapter); 

@Override 
public void onFailure(Call> call, Throwable t) { 

}); 
Call> callhomenews = 
jsonPlaceHolderApi.getNewsCategoryHome(lang); 
callhomenews.enqueue(new Callback>() { 
@Override 
public void onResponse(Call> call, 
Response> response) { 
List listSubnews = new ArrayList<>(); 
listSubnews = response.body(); 
final int totalSize = listSubnews.size(); 
glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() 

@Override 
public int getSpanSize(int i) { 


int span; 
span = totalSize % 2; 
if(totalSize < 3){ 
return 2; 
}else if(span == 0 || (i <= ((totalSize - 1) - 
span))){ 
return 1; 
}else if(span == 1){ 
return 2; 
}else{ 
return 1; 


}); 
newsRecyclerview.setLayoutManager(glm); 
adapterHomeNews = new 
RecyclerAdapterHomeNews(MainActivity.this, listSubnews); 
newsRecyclerview.setAdapter(adapterHomeNews); 

@Override 
public void onFailure(Call> call, Throwable 
t) { 

}); 

@Override 
protected void onActivityResult(int requestCode, int resultCode, 
@Nullable Intent data) { 
if(resultCode != Activity.RESULT_OK) 

if(data == null) 
return; 


String result = 
data.getStringExtra("com.blikoon.qrcodescanner.error_decoding_image"); 
if(result != null) 

AlertDialog alertDialog = new 
AlertDialog.Builder(MainActivity.this).create(); 
alertDialog.setTitle("Scan error"); 
alertDialog.setMessage("QR Code could not be scanned"); 
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new 
DialogInterface.OnClickListener() { 
@Override 
public void onClick(DialogInterface dialog, int which) { 
dialog.dismiss(); 

}); 
alertDialog.show(); 

return; 

if(requestCode == REQUEST_CODE_QR_SCAN) 

if(data == null) 
return; 
final String result = 
data.getStringExtra("com.blikoon.qrcodescanner.got_qr_scan_relult"); 
AlertDialog alertDialog = new 
AlertDialog.Builder(MainActivity.this).create(); 
alertDialog.setTitle("Scan result"); 
alertDialog.setMessage(result); 
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new 
DialogInterface.OnClickListener() { 
@Override 


public void onClick(DialogInterface dialog, int which) { 
dialog.dismiss(); 

}); 
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, 
getString(R.string.openwithbrowser), new DialogInterface.OnClickListener() { 
@Override 
public void onClick(DialogInterface dialog, int which) { 
String url = result; 
try{ 
if(!url.startsWith("https://") && 
!url.startsWith("http://")){ 
String query = URLEncoder.encode(url, "utf-8"); 
url = "http://www.google.com/search?q=" + query; 
Intent browserIntent = new 
Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
startActivity(browserIntent); 
}else{ 
Intent browserIntent = new 
Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
startActivity(browserIntent); 

}catch (Exception e){ 
e.printStackTrace(); 


}); 
alertDialog.show(); 


@Override 


public void onBackPressed() { 
if(drawerLayout.isDrawerOpen(GravityCompat.START)){ 
drawerLayout.closeDrawer(GravityCompat.START); 
}else{ 
moveTaskToBack(true); 
MainActivity.this.finish(); 
} } 
@Override 
public void onSliderClick(BaseSliderView slider) { 
int val = (int) slider.getBundle().get("extra"); 
Intent intent = new Intent(this, ProductViewActivity.class); 
intent.putExtra("id", val); 
// CommonModel.CATEGORY_SELECTED = list.get(val-1).getTitle(); 
startActivity(intent); 

@Override 
public void onPageScrolled(int position, float positionOffset, int 
positionOffsetPixels) { 

@Override 
public void onPageSelected(int position) { 

@Override 
public void onPageScrollStateChanged(int state) { 

@Override 
protected void onStop() { 
//slider.stopAutoCycle(); 
super.onStop(); 



@Override 
protected void onStart() { 
slider.startAutoCycle(); 
super.onStart(); 

public boolean isNetworkConnected(){ 
ConnectivityManager cm = 
(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 
return cm.getActiveNetworkInfo() != null; 

public int getPixelFromDips(float pixels){ 
final float scale = getResources().getDisplayMetrics().density; 
return (int)(pixels * scale + 0.5f); 

//ExpandableRecyclerView resources 
public List makeGenres() { 
return Arrays.asList(makeRockGenre()); 

public News makeRockGenre() { 
return new News(getString(R.string.news), makeRockArtists(), 
R.drawable.ic_newspaper); 

public List makeRockArtists() { 
Subnews queen = new Subnews(9, "Shou biznes"); 
Subnews styx = new Subnews(7, "Texnologiya"); 
Subnews reoSpeedwagon = new Subnews(39,"Dunyo"); 
Subnews boston = new Subnews(47, "Mahalliy"); 
return Arrays.asList(queen, styx, reoSpeedwagon, boston); 




Yuqoridagi kod va rasmlar (Screenshotlar) dasturning ishlanishidan namuna 
sifatida keltirildi. Ilovada foydalanilgan barcha kodlar va resurslarning ko’pligini 
inobatga olgan holda, to’liq kod va fayllarni ko’rish uchun loyihaning arxiv 
faylidan foydalanish tavsiya qilinadi. 

Yüklə 1,03 Mb.

Dostları ilə paylaş:
1   ...   12   13   14   15   16   17   18   19   20




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin