first commit
This commit is contained in:
107
lib/services/http_services.dart
Normal file
107
lib/services/http_services.dart
Normal file
@ -0,0 +1,107 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
import 'package:stacked_services/stacked_services.dart';
|
||||
|
||||
import '../app/app.locator.dart';
|
||||
import '../app/app.logger.dart';
|
||||
|
||||
class MyHttpServices {
|
||||
final _log = getLogger('MyHttpServices');
|
||||
final _snackbarService = locator<SnackbarService>();
|
||||
final _options = BaseOptions(
|
||||
baseUrl: dotenv.env['api_url']!,
|
||||
connectTimeout: const Duration(seconds: 120),
|
||||
receiveTimeout: const Duration(seconds: 120),
|
||||
);
|
||||
|
||||
late Dio _dio;
|
||||
|
||||
MyHttpServices() {
|
||||
_dio = Dio(_options);
|
||||
}
|
||||
|
||||
Future<Response> get(String path, {bool stat = true}) async {
|
||||
try {
|
||||
return await _dio.get(path);
|
||||
} on DioException catch (e) {
|
||||
_log.e(e.response!.statusCode!);
|
||||
String response = e.response!.statusCode! < 500
|
||||
? e.response!.data['message'].toString()
|
||||
: e.response!.statusMessage.toString();
|
||||
_log.e('ini errornya: $response');
|
||||
if (stat) {
|
||||
_snackbarService.showSnackbar(
|
||||
message: response,
|
||||
title: 'Error',
|
||||
duration: const Duration(milliseconds: 1000),
|
||||
);
|
||||
}
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Future<Response> postWithFormData(String path, FormData formData,
|
||||
{bool stat = true}) async {
|
||||
_log.wtf('path: $path');
|
||||
try {
|
||||
return await _dio.post(path, data: formData);
|
||||
} on DioException catch (e) {
|
||||
_log.e(e.response!.statusCode!);
|
||||
String response = e.response!.statusCode! < 500
|
||||
? e.response!.data['message'].toString()
|
||||
: e.response!.statusMessage.toString();
|
||||
_log.e('ini errornya: $response');
|
||||
if (stat) {
|
||||
_snackbarService.showSnackbar(
|
||||
message: response,
|
||||
title: 'Error',
|
||||
duration: const Duration(milliseconds: 1000),
|
||||
);
|
||||
}
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
// putWithFormData
|
||||
Future<Response> putWithFormData(String path, FormData formData,
|
||||
{bool stat = true}) async {
|
||||
try {
|
||||
return await _dio.put(path, data: formData);
|
||||
} on DioException catch (e) {
|
||||
_log.e(e.response!.statusCode!);
|
||||
String response = e.response!.statusCode! < 500
|
||||
? e.response!.data['message'].toString()
|
||||
: e.response!.statusMessage.toString();
|
||||
_log.e('ini errornya: $response');
|
||||
if (stat) {
|
||||
_snackbarService.showSnackbar(
|
||||
message: response,
|
||||
title: 'Error',
|
||||
duration: const Duration(milliseconds: 1000),
|
||||
);
|
||||
}
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
// // delete
|
||||
// Future<Response> delete(String path, FormData data) async {
|
||||
// try {
|
||||
// // log.i('path: $path');
|
||||
// return await _dio.delete(
|
||||
// path,
|
||||
// data: data,
|
||||
// // encoding: Encoding.getByName('utf-8'),
|
||||
// options: Options(
|
||||
// headers: {
|
||||
// 'Content-Type': 'application/x-www-form-urlencoded',
|
||||
// },
|
||||
// ),
|
||||
// );
|
||||
// } on DioError catch (e) {
|
||||
// log.e(e.message);
|
||||
// log.e(e.response);
|
||||
// rethrow;
|
||||
// }
|
||||
// }
|
||||
}
|
39
lib/services/my_easyloading.dart
Normal file
39
lib/services/my_easyloading.dart
Normal file
@ -0,0 +1,39 @@
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
|
||||
class MyEasyLoading {
|
||||
showLoading() {
|
||||
EasyLoading.show(
|
||||
status: 'loading...',
|
||||
maskType: EasyLoadingMaskType.black,
|
||||
dismissOnTap: false,
|
||||
);
|
||||
}
|
||||
|
||||
dismiss() {
|
||||
EasyLoading.dismiss();
|
||||
}
|
||||
|
||||
customLoading(String message) {
|
||||
EasyLoading.show(
|
||||
status: message,
|
||||
maskType: EasyLoadingMaskType.black,
|
||||
dismissOnTap: false,
|
||||
);
|
||||
}
|
||||
|
||||
showSuccess(String message) {
|
||||
EasyLoading.showSuccess(message);
|
||||
}
|
||||
|
||||
showError(String message) {
|
||||
EasyLoading.showError(message);
|
||||
}
|
||||
|
||||
showInfo(String message) {
|
||||
EasyLoading.showInfo(message);
|
||||
}
|
||||
|
||||
showProgress(double progress, String status) {
|
||||
EasyLoading.showProgress(progress, status: status);
|
||||
}
|
||||
}
|
42
lib/services/my_notification.dart
Normal file
42
lib/services/my_notification.dart
Normal file
@ -0,0 +1,42 @@
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
|
||||
class MyNotification {
|
||||
static Future initialize(
|
||||
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin) async {
|
||||
var androidInitialize =
|
||||
const AndroidInitializationSettings('mipmap/ic_launcher');
|
||||
var iOSInitialize = const DarwinInitializationSettings();
|
||||
var initializeSettings =
|
||||
InitializationSettings(android: androidInitialize, iOS: iOSInitialize);
|
||||
await flutterLocalNotificationsPlugin.initialize(initializeSettings);
|
||||
}
|
||||
|
||||
Future showNotification(
|
||||
{var id = 0,
|
||||
var title,
|
||||
var body,
|
||||
var payload,
|
||||
required FlutterLocalNotificationsPlugin
|
||||
flutterLocalNotificationsPlugin}) async {
|
||||
AndroidNotificationDetails androidPlatformChannelSpecifics =
|
||||
const AndroidNotificationDetails(
|
||||
'07eff3c8-e3d7-4386-b8a1-e6588cd9fbb5', // channelId
|
||||
'channel_name',
|
||||
sound: RawResourceAndroidNotificationSound('notification_fuck'),
|
||||
importance: Importance.max,
|
||||
priority: Priority.high,
|
||||
);
|
||||
|
||||
var iOSPlatformChannelSpecifics = const DarwinNotificationDetails();
|
||||
var platformChannelSpecifics = NotificationDetails(
|
||||
android: androidPlatformChannelSpecifics,
|
||||
iOS: iOSPlatformChannelSpecifics);
|
||||
await flutterLocalNotificationsPlugin.show(
|
||||
id,
|
||||
title,
|
||||
body,
|
||||
platformChannelSpecifics,
|
||||
payload: payload,
|
||||
);
|
||||
}
|
||||
}
|
25
lib/services/my_preferences.dart
Normal file
25
lib/services/my_preferences.dart
Normal file
@ -0,0 +1,25 @@
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class MySharedPrefs {
|
||||
final Future<SharedPreferences> prefs = SharedPreferences.getInstance();
|
||||
|
||||
Future<String?> getString(String key) async {
|
||||
final SharedPreferences prefs = await this.prefs;
|
||||
return prefs.getString(key);
|
||||
}
|
||||
|
||||
Future<bool> setString(String key, String value) async {
|
||||
final SharedPreferences prefs = await this.prefs;
|
||||
return prefs.setString(key, value);
|
||||
}
|
||||
|
||||
Future<bool> removeString(String key) async {
|
||||
final SharedPreferences prefs = await this.prefs;
|
||||
return prefs.remove(key);
|
||||
}
|
||||
|
||||
Future<bool> clear() async {
|
||||
final SharedPreferences prefs = await this.prefs;
|
||||
return prefs.clear();
|
||||
}
|
||||
}
|
54
lib/services/my_socket_io_client.dart
Normal file
54
lib/services/my_socket_io_client.dart
Normal file
@ -0,0 +1,54 @@
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
import 'package:socket_io_client/socket_io_client.dart';
|
||||
|
||||
import '../app/app.logger.dart';
|
||||
|
||||
class MySocketIoClient {
|
||||
final log = getLogger('MySocketIoClient');
|
||||
final String _url = dotenv.env['url']!;
|
||||
double waterHeight = 0;
|
||||
int warningLevel = 0;
|
||||
int dangerLevel = 0;
|
||||
String status = '...';
|
||||
static final MySocketIoClient _instance = MySocketIoClient._internal();
|
||||
factory MySocketIoClient() => _instance;
|
||||
MySocketIoClient._internal();
|
||||
|
||||
int notif = 0;
|
||||
|
||||
late Socket _socket;
|
||||
Socket get socket => _socket;
|
||||
|
||||
Future<void> init() async {
|
||||
try {
|
||||
_socket = io(_url, <String, dynamic>{
|
||||
'transports': ['websocket'],
|
||||
'autoConnect': false,
|
||||
});
|
||||
_socket.connect();
|
||||
log.i('socket connected');
|
||||
} catch (e) {
|
||||
log.e('error : $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> emit(String event, dynamic data) async {
|
||||
_socket.emit(event, data);
|
||||
}
|
||||
|
||||
Future<void> on(String event, Function(dynamic) callback) async {
|
||||
_socket.on(event, callback);
|
||||
}
|
||||
|
||||
Future<void> off(String event) async {
|
||||
_socket.off(event);
|
||||
}
|
||||
|
||||
Future<void> disconnect() async {
|
||||
_socket.disconnect();
|
||||
}
|
||||
|
||||
Future<void> connect() async {
|
||||
_socket.connect();
|
||||
}
|
||||
}
|
150
lib/services/other_function.dart
Normal file
150
lib/services/other_function.dart
Normal file
@ -0,0 +1,150 @@
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:ont_app2/model/ont_data_model.dart';
|
||||
import 'package:ont_app2/model/ont_model.dart';
|
||||
|
||||
class OtherFunction {
|
||||
List<OntDataModel> listDataOnt = [];
|
||||
OntModel? ontModel;
|
||||
|
||||
int umur(String tanggalLahir) {
|
||||
// change tanggalLahir to DateTime
|
||||
DateTime date = DateTime.parse(tanggalLahir);
|
||||
// get current date
|
||||
DateTime now = DateTime.now();
|
||||
// get difference in year
|
||||
int year = now.year - date.year;
|
||||
return year;
|
||||
}
|
||||
|
||||
String commaFormat(int number) {
|
||||
final formatter = NumberFormat('#,###');
|
||||
return formatter.format(number);
|
||||
}
|
||||
|
||||
String changeMonth(String month) {
|
||||
switch (month) {
|
||||
case 'Januari':
|
||||
return '01';
|
||||
case 'Februari':
|
||||
return '02';
|
||||
case 'Maret':
|
||||
return '03';
|
||||
case 'April':
|
||||
return '04';
|
||||
case 'Mei':
|
||||
return '05';
|
||||
case 'Juni':
|
||||
return '06';
|
||||
case 'Juli':
|
||||
return '07';
|
||||
case 'Agustus':
|
||||
return '08';
|
||||
case 'September':
|
||||
return '09';
|
||||
case 'Oktober':
|
||||
return '10';
|
||||
case 'November':
|
||||
return '11';
|
||||
case 'Desember':
|
||||
return '12';
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
String changeMonthYear(String s) {
|
||||
// get the last 2 digits
|
||||
String month = s.substring(s.length - 2);
|
||||
// get the first 4 digits
|
||||
String year = s.substring(0, 4);
|
||||
// return the month and year
|
||||
switch (month) {
|
||||
case '01':
|
||||
return 'Januari $year';
|
||||
case '02':
|
||||
return 'Februari $year';
|
||||
case '03':
|
||||
return 'Maret $year';
|
||||
case '04':
|
||||
return 'April $year';
|
||||
case '05':
|
||||
return 'Mei $year';
|
||||
case '06':
|
||||
return 'Juni $year';
|
||||
case '07':
|
||||
return 'Juli $year';
|
||||
case '08':
|
||||
return 'Agustus $year';
|
||||
case '09':
|
||||
return 'September $year';
|
||||
case '10':
|
||||
return 'Oktober $year';
|
||||
case '11':
|
||||
return 'November $year';
|
||||
case '12':
|
||||
return 'Desember $year';
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
String getDayOfWeek(String date) {
|
||||
DateTime dateTime = DateTime.parse(date);
|
||||
List<String> daysOfWeek = [
|
||||
'Senin',
|
||||
'Selasa',
|
||||
'Rabu',
|
||||
'Kamis',
|
||||
'Jumat',
|
||||
'Sabtu',
|
||||
'Minggu'
|
||||
];
|
||||
return daysOfWeek[dateTime.weekday - 1];
|
||||
}
|
||||
|
||||
String formatDateString(String dateString) {
|
||||
// Remove the "T" and replace it with " | "
|
||||
String formattedString = dateString.replaceAll('T', '\n');
|
||||
|
||||
// Remove the ".000Z"
|
||||
formattedString = formattedString.replaceAll('.000Z', '');
|
||||
|
||||
// Parse the input string to DateTime object
|
||||
DateTime dateTime = DateTime.parse(dateString);
|
||||
|
||||
// Get the day of the week in Indonesian
|
||||
String dayOfWeek = DateFormat.EEEE('id_ID').format(dateTime);
|
||||
|
||||
// Add the day of the week to the formatted string
|
||||
formattedString = '$formattedString\n$dayOfWeek';
|
||||
|
||||
return formattedString;
|
||||
}
|
||||
|
||||
String formatDateString2(String dateString) {
|
||||
DateTime dateTime = DateTime.parse(dateString);
|
||||
|
||||
// Adjust for the timezone if needed (this example assumes UTC)
|
||||
dateTime = dateTime.toLocal();
|
||||
|
||||
// Format the DateTime object to match your database format
|
||||
String dbDate = DateFormat('yyyy-MM-dd HH:mm:ss').format(dateTime);
|
||||
|
||||
return dbDate;
|
||||
}
|
||||
|
||||
String timeNameRemover(String time) {
|
||||
List<String> parts = time.split(' ');
|
||||
String timePart = parts[0];
|
||||
|
||||
// Split the time part into hours, minutes, and seconds
|
||||
List<String> timeComponents = timePart.split(':');
|
||||
String hours = timeComponents[0];
|
||||
String minutes = timeComponents[1];
|
||||
|
||||
// Create the new time string without seconds and with a period instead of a colon
|
||||
String newTimeStr = '$hours.$minutes';
|
||||
|
||||
return newTimeStr;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user